directx direct3d 的第八章的内容详细注释说明.docx
《directx direct3d 的第八章的内容详细注释说明.docx》由会员分享,可在线阅读,更多相关《directx direct3d 的第八章的内容详细注释说明.docx(27页珍藏版)》请在冰豆网上搜索。
directxdirect3d的第八章的内容详细注释说明
Checker
brick0
Ice
d3dUtility.cpp
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//File:
d3dUtility.cpp
//
//Author:
FrankLuna(C)AllRightsReserved
//
//System:
AMDAthlon1800+XP,512DDR,Geforce3,WindowsXP,MSVC++7.0
//
//Desc:
Providesutilityfunctionsforsimplifyingcommontasks.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include"d3dUtility.h"
boold3d:
:
InitD3D(
HINSTANCEhInstance,
intwidth,intheight,
boolwindowed,
D3DDEVTYPEdeviceType,
IDirect3DDevice9**device)
{
//
//Createthemainapplicationwindow.
//
WNDCLASSwc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=(WNDPROC)d3d:
:
WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(0,IDI_APPLICATION);
wc.hCursor=LoadCursor(0,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=0;
wc.lpszClassName="Direct3D9App";
if(!
RegisterClass(&wc))
{
:
:
MessageBox(0,"RegisterClass()-FAILED",0,0);
returnfalse;
}
HWNDhwnd=0;
hwnd=:
:
CreateWindow("Direct3D9App","Direct3D9App",
WS_EX_TOPMOST,
0,0,width,height,
0/*parenthwnd*/,0/*menu*/,hInstance,0/*extra*/);
if(!
hwnd)
{
:
:
MessageBox(0,"CreateWindow()-FAILED",0,0);
returnfalse;
}
:
:
ShowWindow(hwnd,SW_SHOW);
:
:
UpdateWindow(hwnd);
//
//InitD3D:
//
HRESULThr=0;
//Step1:
CreatetheIDirect3D9object.
IDirect3D9*d3d9=0;
d3d9=Direct3DCreate9(D3D_SDK_VERSION);
if(!
d3d9)
{
:
:
MessageBox(0,"Direct3DCreate9()-FAILED",0,0);
returnfalse;
}
//Step2:
Checkforhardwarevp.
D3DCAPS9caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,deviceType,&caps);
intvp=0;
if(caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//Step3:
FillouttheD3DPRESENT_PARAMETERSstructure.
D3DPRESENT_PARAMETERSd3dpp;
d3dpp.BackBufferWidth=width;
d3dpp.BackBufferHeight=height;
d3dpp.BackBufferFormat=D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount=1;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality=0;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow=hwnd;
d3dpp.Windowed=windowed;
d3dpp.EnableAutoDepthStencil=true;
d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;
d3dpp.Flags=0;
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
//Step4:
Createthedevice.
hr=d3d9->CreateDevice(
D3DADAPTER_DEFAULT,//primaryadapter
deviceType,//devicetype
hwnd,//windowassociatedwithdevice
vp,//vertexprocessing
&d3dpp,//presentparameters
device);//returncreateddevice
if(FAILED(hr))
{
//tryagainusinga16-bitdepthbuffer
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
hr=d3d9->CreateDevice(
D3DADAPTER_DEFAULT,
deviceType,
hwnd,
vp,
&d3dpp,
device);
if(FAILED(hr))
{
d3d9->Release();//donewithd3d9object
:
:
MessageBox(0,"CreateDevice()-FAILED",0,0);
returnfalse;
}
}
d3d9->Release();//donewithd3d9object
returntrue;
}
intd3d:
:
EnterMsgLoop(bool(*ptr_display)(floattimeDelta))
{
MSGmsg;
:
:
ZeroMemory(&msg,sizeof(MSG));
staticfloatlastTime=(float)timeGetTime();
while(msg.message!
=WM_QUIT)
{
if(:
:
PeekMessage(&msg,0,0,0,PM_REMOVE))
{
:
:
TranslateMessage(&msg);
:
:
DispatchMessage(&msg);
}
else
{
floatcurrTime=(float)timeGetTime();
floattimeDelta=(currTime-lastTime)*0.001f;
ptr_display(timeDelta);
lastTime=currTime;
}
}
returnmsg.wParam;
}
D3DLIGHT9d3d:
:
InitDirectionalLight(D3DXVECTOR3*direction,D3DXCOLOR*color)
{
D3DLIGHT9light;
:
:
ZeroMemory(&light,sizeof(light));
light.Type=D3DLIGHT_DIRECTIONAL;
light.Ambient=*color*0.4f;
light.Diffuse=*color;
light.Specular=*color*0.6f;
light.Direction=*direction;
returnlight;
}
D3DLIGHT9d3d:
:
InitPointLight(D3DXVECTOR3*position,D3DXCOLOR*color)
{
D3DLIGHT9light;
:
:
ZeroMemory(&light,sizeof(light));
light.Type=D3DLIGHT_POINT;
light.Ambient=*color*0.4f;
light.Diffuse=*color;
light.Specular=*color*0.6f;
light.Position=*position;
light.Range=10000.0f;
light.Falloff=1.0f;
light.Attenuation0=1.0f;
light.Attenuation1=0.0f;
light.Attenuation2=0.0f;
returnlight;
}
D3DLIGHT9d3d:
:
InitSpotLight(D3DXVECTOR3*position,D3DXVECTOR3*direction,D3DXCOLOR*color)
{
D3DLIGHT9light;
:
:
ZeroMemory(&light,sizeof(light));
light.Type=D3DLIGHT_SPOT;
light.Ambient=*color*0.4f;
light.Diffuse=*color;
light.Specular=*color*0.6f;
light.Position=*position;
light.Direction=*direction;
light.Range=1000.0f;
light.Falloff=1.0f;
light.Attenuation0=1.0f;
light.Attenuation1=0.0f;
light.Attenuation2=0.0f;
light.Theta=0.5f;
light.Phi=0.7f;
returnlight;
}
D3DMATERIAL9d3d:
:
InitMtrl(D3DXCOLORa,D3DXCOLORd,D3DXCOLORs,D3DXCOLORe,floatp)
{
D3DMATERIAL9mtrl;
mtrl.Ambient=a;
mtrl.Diffuse=d;
mtrl.Specular=s;
mtrl.Emissive=e;
mtrl.Power=p;
returnmtrl;
}
d3dUtility.h
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//File:
d3dUtility.h
//
//Author:
FrankLuna(C)AllRightsReserved
//
//System:
AMDAthlon1800+XP,512DDR,Geforce3,WindowsXP,MSVC++7.0
//
//Desc:
Providesutilityfunctionsforsimplifyingcommontasks.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef__d3dUtilityH__
#define__d3dUtilityH__
#include
#include
#include
namespaced3d
{
//
//Windows/Direct3DInitialization
//
boolInitD3D(
HINSTANCEhInstance,//[in]Applicationinstance.
intwidth,intheight,//[in]Backbufferdimensions.
boolwindowed,//[in]Windowed(true)orfullscreen(false).
D3DDEVTYPEdeviceType,//[in]HALorREF
IDirect3DDevice9**device);//[out]Thecreateddevice.
intEnterMsgLoop(
bool(*ptr_display)(floattimeDelta));
LRESULTCALLBACKWndProc(
HWNDhwnd,
UINTmsg,
WPARAMwParam,
LPARAMlParam);
//
//Cleanup
//
templatevoidRelease(Tt)
{
if(t)
{
t->Release();
t=0;
}
}
templatevoidDelete(Tt)
{
if(t)
{
deletet;
t=0;
}
}
//
//Colors
//
constD3DXCOLORWHITE(D3DCOLOR_XRGB(255,255,255));
constD3DXCOLORBLACK(D3DCOLOR_XRGB(0,0,0));
constD3DXCOLORRED(D3DCOLOR_XRGB(255,0,0));
constD3DXCOLORGREEN(D3DCOLOR_XRGB(0,255,0));
constD3DXCOLORBLUE(D3DCOLOR_XRGB(0,0,255));
constD3DXCOLORYELLOW(D3DCOLOR_XRGB(255,255,0));
constD3DXCOLORCYAN(D3DCOLOR_XRGB(0,255,255));
constD3DXCOLORMAGENTA(D3DCOLOR_XRGB(255,0,255));
//
//Lights
//
D3DLIGHT9InitDirectionalLight(D3DXVECTOR3*direction,D3DXCOLOR*color);
D3DLIGHT9InitPointLight(D3DXVECTOR3*position,D3DXCOLOR*color);
D3DLIGHT9InitSpotLight(D3DXVECTOR3*position,D3DXVECTOR3*direction,D3DXCOLOR*color);
//
//Materials
//
D3DMATERIAL9InitMtrl(D3DXCOLORa,D3DXCOLORd,D3DXCOLORs,D3DXCOLORe,floatp);
constD3DMATERIAL9WHITE_MTRL=InitMtrl(WHITE,WHITE,WHITE,BLACK,2.0f);
constD3DMATERIAL9RED_MTRL=InitMtrl(RED,RED,RED,BLACK,2.0f);
constD3DMATERIAL9GREEN_MTRL=InitMtrl(GREEN,GREEN,GREEN,BLACK,2.0f);
constD3DMATERIAL9BLUE_MTRL=InitMtrl(BLUE,BLUE,BLUE,BLACK,2.0f);
constD3DMATERIAL9YELLOW_MTRL=InitMtrl(YELLOW,YELLOW,YELLOW,BLACK,2.0f);
//
//BoundingObjects
//
structBoundingBox
{
BoundingBox();
boolisPointInside(D3DXVECTOR3&p);
D3DXVECTOR3_min;
D3DXVECTOR3_max;
};
structBoundingSphere
{
BoundingSphere();
D3DXVECTOR3_center;
float_radius;
};
//
//Constants
//
constfloatINFINITY=std:
:
numeric_limits:
:
infinity();
constfloatEPSILON=0.001f;
//
//Randomness
//
//Desc:
Returnrandomfloatin[lowBound,highBound]interval.
floatGetRandomFloat(floatlowBound,floathighBound);
//Desc:
Returnsarandomvectorintheboundsspecifiedbyminandmax.
voidGetRandomVector(
D3DXVECTOR3*out,
D3DXVECTOR3*min,
D3DXVECTOR3*max);
//
//Conversion
//
DWORDFtoDw(floatf);
}
#endif//__d3dUtilityH__
Stencilmirrorshadow.cpp
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//File:
stencilmirrorshadow.cpp
//
//Author:
FrankLuna(C)AllRightsReserved
//
//System:
AMDAthlon1800+XP,512DDR,Geforce3,WindowsXP,MSVC++7.0
//
//Desc:
Demonstratesmirrorsandshadowswithstencils.Usethearrowkeys
//andthe'A'and'S'keytonavigatethesceneandtranslatetheteapot.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include"d3dUtility.h"
//
//Globals
//
IDirect3DDevice9*Device=0;
constintWidth=640;
constintHeight=480;
IDirect3DVertexBuffer9*VB=0;
IDirect3DTexture9*FloorTex=0;
IDirect3DTexture9*WallTex=0;
IDirect3DTexture9*MirrorTex=0;
D3DMATERIAL9FloorMtrl=d3d:
:
WHITE_MTRL;
D3DMATERIAL9WallMtrl=d3d:
:
WHITE_MTRL;
D3DMATERIAL9MirrorMtrl=d3d:
:
WHITE_MTRL;
ID3DXMesh*Teapot=0;
D3DXVECTOR3TeapotPosition(0.0f,3.0f,-7.5f);
D3DMATERIAL9TeapotMtrl=d3d:
:
YELLOW_MTRL;
voidRenderScene();
voidRenderMirror();
voidRenderShadow();
//
//ClassesandStructures
//
structVertex
{
Vertex(){}
Vertex(floatx,floaty,floatz,
floatnx,floatny,float