D3D中的 模板缓冲区
自己感觉自开始学习 D3D以来,D3D中的模板缓冲区技术 最难理解得了,
到现在自己还不敢说自己对模板缓冲区很理解,他的工作机制,以及如何创
建模板缓冲区,还不是很了解。自己从加载纹理文件,到设置模板缓冲区,
费了好久才把它调试通过。
首先来看看工程结构:
运行效果为:
(注意只有在镜子前面的才会在镜子中成像)
前两个文件:
d3dUtility.h该头文件主要是添加主要的头文件和库文件
- #include "d3dx9.h"
- #include "d3d9.h"
- #include <tchar.h>
- #include <iostream>
- #include <vector>
- #include<Windows.h>
- #pragma comment(lib,"d3dx9.lib")
- #pragma comment(lib,"d3d9.lib")
- #pragma comment(lib,"winmm.lib")
- namespace d3d
- {
- bool InitD3D(
- HINSTANCE hInstance, // [in] Application instance.
- int width, int height, // [in] Backbuffer dimensions.
- bool windowed, // [in] Windowed (true)or full screen (false).
- D3DDEVTYPE deviceType, // [in] HAL or REF
- IDirect3DDevice9** device);// [out]The created device.
- int EnterMsgLoop(
- bool (*ptr_display)(float timeDelta));
- LRESULT CALLBACK WndProc(
- HWND hwnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam);
- template<class T> void Release(T t)
- {
- if( t )
- {
- t->Release();
- t = 0;
- }
- }
- template<class T> void Delete(T t)
- {
- if( t )
- {
- delete t;
- t = 0;
- }
- }
- const D3DXCOLOR WHITE( D3DCOLOR_XRGB(255, 255, 255) );
- const D3DXCOLOR BLACK( D3DCOLOR_XRGB( 0, 0, 0) );
- const D3DXCOLOR RED( D3DCOLOR_XRGB(255, 0, 0) );
- const D3DXCOLOR GREEN( D3DCOLOR_XRGB( 0, 255, 0) );
- const D3DXCOLOR BLUE( D3DCOLOR_XRGB( 0, 0, 255) );
- const D3DXCOLOR YELLOW( D3DCOLOR_XRGB(255, 255, 0) );
- const D3DXCOLOR CYAN( D3DCOLOR_XRGB( 0, 255, 255) );
- const D3DXCOLOR MAGENTA( D3DCOLOR_XRGB(255, 0, 255) );
- //
- // Lights
- //
- D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color);
- D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color);
- D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color);
- //
- // Materials
- //
- D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);
- const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
- const D3DMATERIAL9 RED_MTRL = InitMtrl(RED, RED, RED, BLACK, 2.0f);
- const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
- const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
- const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
- }
d3dutility.cpp
- #include "d3dutility.h"
- bool d3d::InitD3D(HINSTANCE hInstance,
- int width,
- int height,
- bool windowed,
- D3DDEVTYPE deviceType,
- IDirect3DDevice9 ** device)
- {
- //创建应用程序主窗口
- WNDCLASS wc;
- 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 = _T("Direct3D9App");
- if (!RegisterClass(&wc))
- {
- ::MessageBox(0,_T("RegisterClass() ---error"),0,0);
- return false;
- }
- HWND hwnd = 0;
- hwnd = ::CreateWindow(_T("Direct3D9App"), _T("Direct3D9App"),
- WS_EX_TOPMOST,
- 0, 0, width, height,
- 0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/);
- if (!hwnd)
- {
- ::MessageBox(0,_T("CreateWindow()--error"),0,0);
- return false;
- }
- ::ShowWindow(hwnd,SW_SHOW);
- ::UpdateWindow(hwnd);
- //初始化 D3D
- HRESULT hr = 0;
- //创建IDirect3D9 对象
- IDirect3D9 *d3d9 = 0;
- d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
- if (!d3d9)
- {
- ::MessageBox(0,_T("error"),0,0);
- return false;
- }
- // 检测硬件支持
- D3DCAPS9 caps ;
- d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,deviceType,&caps);
- int vp= 0;
- if (caps.DeviceType & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
- {
- vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- }
- else
- {
- vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- }
- // 填充D3DPRESENT_PARAMETERS struct
- D3DPRESENT_PARAMETERS d3dpp;
- 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;
- //创建设备
- hr = d3d9->CreateDevice(
- D3DADAPTER_DEFAULT, // primary adapter
- deviceType, // device type
- hwnd, // window associated with device
- vp, // vertex processing
- &d3dpp, // present parameters
- device);
- if( FAILED(hr) )
- {
- // try again using a 16-bit depth buffer
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- hr = d3d9->CreateDevice(
- D3DADAPTER_DEFAULT,
- deviceType,
- hwnd,
- vp,
- &d3dpp,
- device);
- if( FAILED(hr) )
- {
- d3d9->Release(); // done with d3d9 object
- ::MessageBox(0, _T("CreateDevice() - FAILED"), 0, 0);
- return false;
- }
- }
- d3d9->Release(); // done with d3d9 object
- return true;
- }
- // 消息循环
- int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )
- {
- MSG msg;
- ::ZeroMemory(&msg, sizeof(MSG));
- static float lastTime = (float)timeGetTime();
- while(msg.message != WM_QUIT)
- {
- if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
- else
- {
- float currTime = (float)timeGetTime();
- float timeDelta = (currTime - lastTime)*0.001f;
- ptr_display(timeDelta);
- lastTime = currTime;
- }
- }
- return msg.wParam;
- }
- D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction ,D3DXCOLOR* color)
- {
- D3DLIGHT9 light;
- ::ZeroMemory(&light,sizeof(light));
- light.Type = D3DLIGHT_DIRECTIONAL;
- light.Ambient = *color*0.6f;
- light.Diffuse = *color;
- light.Specular = *color*0.0f;
- light.Direction = *direction;
- return light;
- }
- D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position ,D3DXCOLOR* color)
- {
- D3DLIGHT9 light;
- ::ZeroMemory(&light,sizeof(light));
- light.Type = D3DLIGHT_POINT;
- light.Ambient = *color*0.6f;
- light.Diffuse = *color;
- light.Specular = *color*0.6f;
- light.Position = *position;
- light.Range = 1000.0f;
- light.Falloff = 1.0f;
- light.Attenuation0 =1.0f;
- light.Attenuation1 = 0.0f;
- light.Attenuation2 = 0.0f;
- return light;
- }
- D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
- {
- D3DLIGHT9 light;
- ::ZeroMemory(&light, sizeof(light));
- light.Type = D3DLIGHT_SPOT;
- light.Ambient = *color * 0.0f;
- 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.4f;
- light.Phi = 0.9f;
- return light;
- }
- D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
- {
- D3DMATERIAL9 mtrl;
- mtrl.Ambient = a;
- mtrl.Diffuse = d;
- mtrl.Specular = s;
- mtrl.Emissive = e;
- mtrl.Power = p;
- return mtrl;
- }
stencilmirror.cpp 主要包括 渲染 镜子,茶壶,设置模板缓冲区。
- #include "d3dUtility.h"
- //
- // Globals
- //
- IDirect3DDevice9* Device = 0;
- const int Width = 640;
- const int Height = 480;
- IDirect3DVertexBuffer9* VB = 0;
- IDirect3DTexture9* FloorTex = 0;
- IDirect3DTexture9* WallTex = 0;
- IDirect3DTexture9 *MirrorTex = 0;
- D3DMATERIAL9 FloorMtrl = d3d::WHITE_MTRL;
- D3DMATERIAL9 WallMtrl = d3d::WHITE_MTRL;
- D3DMATERIAL9 MirrorMtrl = d3d::WHITE_MTRL;
- ID3DXMesh* Teapot = 0;
- D3DXVECTOR3 TeapotPosition(0.0f,3.0f,-7.5f);
- D3DMATERIAL9 TeapotMtrl = d3d::YELLOW_MTRL;
- void RenderScene();
- void RenderMirror();
- /*
- Classes and structures
- */
- //定义Vertex 类型
- struct Vertex
- {
- Vertex(){}
- Vertex(float x,float y,float z,
- float nx,float ny,float nz ,float u,float v)
- {
- _x = x ; _y = y ;_z= z;
- _nx = nx;_ny = ny; _nz = nz;
- _u = u; _v = v;
- }
- float _x,_y,_z;
- float _nx,_ny,_nz;
- float _u,_v;
- static const DWORD FVF;
- };
- const DWORD Vertex::FVF =D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1;
- //
- // Framework Functions
- //
- bool Setup()
- {
- WallMtrl.Specular = d3d::WHITE*0.2f;
- //
- //Create the teapot
- //
- D3DXCreateTeapot(Device,&Teapot,0);
- Device->CreateVertexBuffer(
- 24*sizeof(Vertex),
- D3DUSAGE_WRITEONLY,
- Vertex::FVF,
- D3DPOOL_MANAGED,
- &VB,
- 0);
- //用Quad 数据填充vertex buffer
- Vertex* v = 0;
- VB->Lock(0,0,(void**)&v,0);
- // floor地面 UV都是在[0,1]之间
- v[0] = Vertex(-7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
- v[1] = Vertex(-7.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
- v[2] = Vertex( 7.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
- v[3] = Vertex(-7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
- v[4] = Vertex( 7.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
- v[5] = Vertex( 7.5f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
- // wall墙体
- v[6] = Vertex(-7.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[7] = Vertex(-7.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
- v[8] = Vertex(-2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- v[9] = Vertex(-7.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[10] = Vertex(-2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- v[11] = Vertex(-2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
- // 在墙体哈镜子之间留一些空隙
- v[12] = Vertex(2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[13] = Vertex(2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
- v[14] = Vertex(7.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- v[15] = Vertex(2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[16] = Vertex(7.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- v[17] = Vertex(7.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
- // mirror 镜子
- v[18] = Vertex(-2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[19] = Vertex(-2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
- v[20] = Vertex( 2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- v[21] = Vertex(-2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[22] = Vertex( 2.5f, 5.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- v[23] = Vertex( 2.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
- VB ->Unlock();
- D3DXCreateTextureFromFile( //加载纹理:草
- Device,
- _T("grass.bmp"),
- &FloorTex);
- D3DXCreateTextureFromFile( //加载纹理:墙
- Device,
- _T("brick0.jpg"),
- &WallTex);
- D3DXCreateTextureFromFile( //加载纹理:镜面
- Device,
- _T("ice.bmp"),
- &MirrorTex);
- Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_ANISOTROPIC/*D3DTEXF_LINEAR*/);
- Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_ANISOTROPIC/*D3DTEXF_LINEAR*/);
- Device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_ANISOTROPIC);
- //设置,开放 灯光,使用灯光
- D3DXVECTOR3 lightDir(0.707f,-0.707f,0.707f);
- D3DXCOLOR color(1.0f,1.0f,1.0f,1.0f);
- D3DLIGHT9 light = d3d::InitDirectionalLight(&lightDir,&color);
- Device->SetLight(0,&light);
- Device->LightEnable(0,true);
- //设置渲染状态,重新计算法线方向,打开镜面光
- Device->SetRenderState(D3DRS_NORMALIZENORMALS,true);
- Device->SetRenderState(D3DRS_SPECULARENABLE,true);
- //设置照相机
- D3DXVECTOR3 pos(-10.0f,3.0f,-15.0f);
- D3DXVECTOR3 target(0.0f,0.0f,0.0f);
- D3DXVECTOR3 up(0.0f,1.0f,0.0f);
- D3DXMATRIX V;
- D3DXMatrixLookAtLH(&V,&pos,&target,&up);
- Device->SetTransform(D3DTS_VIEW,&V);
- //设置投影矩阵
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI /4.0f,
- (float)Width/(float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION,&proj);
- return true;
- }
- void Cleanup()
- {
- //清理创建的buffer
- d3d::Release<IDirect3DVertexBuffer9* >(VB);
- d3d::Release<IDirect3DTexture9*>(FloorTex);
- d3d::Release<IDirect3DTexture9*>(WallTex);
- d3d::Release<IDirect3DTexture9*>(MirrorTex);
- d3d::Release<ID3DXMesh*>(Teapot);
- }
- bool Display(float timeDelta)
- {
- if (Device)
- {
- //接收用户输入 更新场景
- static float radius = 20.0f;
- if (::GetAsyncKeyState(VK_LEFT)&0x8000f)
- {
- TeapotPosition.x -=3.0f*timeDelta;
- }
- if (::GetAsyncKeyState(VK_RIGHT)&0x8000f)
- {
- TeapotPosition.x +=3.0f*timeDelta;
- }
- if(::GetAsyncKeyState(VK_UP)& 0x8000f)
- {
- radius -=2.0f*timeDelta;
- }
- if (::GetAsyncKeyState(VK_DOWN)&0x8000)
- {
- radius +=2.0f*timeDelta;
- }
- static float angle = (3.0f*D3DX_PI)/2.0f;
- if (::GetAsyncKeyState('A')&0x8000f)
- {
- angle -=0.5f * timeDelta;
- }
- if(::GetAsyncKeyState('S')&0x8000)
- {
- angle +=0.5f * timeDelta;
- }
- D3DXVECTOR3 position(cosf(angle)*radius,3.0f,sinf(angle)*radius);
- D3DXVECTOR3 target(0.0f,0.0f,0.0f);
- D3DXVECTOR3 up(0.0f,1.0f,0.0f);
- D3DXMATRIX V;
- D3DXMatrixLookAtLH(&V,&position,&target,&up);
- Device->SetTransform(D3DTS_VIEW,&V);
- Device->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL,
- 0x00000000,1.0f,0);
- Device->BeginScene();
- RenderScene();
- RenderMirror();
- Device->EndScene();
- Device->Present(0,0,0,0);
- }
- return true;
- }
- void RenderScene()
- {
- // 画茶壶
- Device->SetMaterial(&TeapotMtrl);
- Device->SetTexture(0, 0);
- D3DXMATRIX W;
- D3DXMatrixTranslation(&W,
- TeapotPosition.x,
- TeapotPosition.y,
- TeapotPosition.z);
- Device->SetTransform(D3DTS_WORLD, &W);
- Teapot->DrawSubset(0);
- D3DXMATRIX I;
- D3DXMatrixIdentity(&I);
- Device->SetTransform(D3DTS_WORLD, &I);
- Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
- Device->SetFVF(Vertex::FVF);
- // 画地板
- Device->SetMaterial(&FloorMtrl);
- Device->SetTexture(0, FloorTex);
- Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
- // 画墙体
- Device->SetMaterial(&WallMtrl);
- Device->SetTexture(0, WallTex);
- Device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 4);
- // 画镜子
- Device->SetMaterial(&MirrorMtrl);
- Device->SetTexture(0, MirrorTex);
- Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);
- }
- void RenderMirror()
- {
- //渲染 镜子
- Device->SetRenderState(D3DRS_STENCILENABLE, true);//开启模板缓冲区
- Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); //模板测试为 肯定通过
- Device->SetRenderState(D3DRS_STENCILREF, 0x1); //模板参考值 为1
- Device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);//模板掩码:用于设置模板参考值与模板缓冲区值value进行屏蔽操作
- Device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
- Device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);//不更改模板缓冲区的值
- Device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
- Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);//用模板缓冲区中的值代替原模板缓冲区的值
- // 禁用向模板 深度(Z)缓冲区进行写
- Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
- Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
- //设置 原混合因子和 目标混合因子
- Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
- Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
- // 将镜子写入到模板缓冲区中
- Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
- Device->SetFVF(Vertex::FVF);
- Device->SetMaterial(&MirrorMtrl);
- Device->SetTexture(0, MirrorTex);
- D3DXMATRIX I;
- D3DXMatrixIdentity(&I);
- Device->SetTransform(D3DTS_WORLD, &I);
- Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);
- // 开启深度 (Z)缓冲区进行写
- Device->SetRenderState( D3DRS_ZWRITEENABLE, true );
- // only draw reflected teapot to the pixels where the mirror
- // was drawn to.当且仅当映射出 茶壶像素在镜子前的镜像
- Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
- Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
- // position reflection
- D3DXMATRIX W, T, R;
- D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); //xy平面
- D3DXMatrixReflect(&R, &plane);
- D3DXMatrixTranslation(&T,
- TeapotPosition.x,
- TeapotPosition.y,
- TeapotPosition.z);
- W = T * R;
- //清空深度缓冲区,并且在镜子中投影茶壶
- Device->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
- Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR);
- Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
- // 最后绘制 茶壶在镜子中的镜像
- Device->SetTransform(D3DTS_WORLD, &W);
- Device->SetMaterial(&TeapotMtrl);
- Device->SetTexture(0, 0);
- Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
- Teapot->DrawSubset(0);
- // 存储渲染状态
- Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
- Device->SetRenderState( D3DRS_STENCILENABLE, false);
- Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
- }
- //回调函数
- LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch( msg )
- {
- case WM_DESTROY:
- ::PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- if( wParam == VK_ESCAPE )
- ::DestroyWindow(hwnd);
- break;
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }
- //
- // 主函数
- //
- int WINAPI WinMain(HINSTANCE hinstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- if(!d3d::InitD3D(hinstance,
- Width, Height, true, D3DDEVTYPE_HAL, &Device))
- {
- ::MessageBox(0, _T("InitD3D() - FAILED"), 0, 0);
- return 0;
- }
- if(!Setup())
- {
- ::MessageBox(0, _T("Setup() - FAILED"), 0, 0);
- return 0;
- }
- d3d::EnterMsgLoop( Display );
- Cleanup();
- Device->Release();
- return 0;
- }
欢迎大家跟帖说说 模板缓冲区的理解^---^