第一个Direct程序

来源:互联网 发布:jsp在线考试系统源码 编辑:程序博客网 时间:2024/06/06 01:40

1:建立win32窗体应用程序项目(不是控制台程序),配置如下:



2:将文件导入项目(也可以自己新建h,cpp文件,将代码拷贝进去):



3:修改项目属性配置:

在导入文件之后,打开代码,有很多错误。是因为没有将相应的D3D动态链接库文件包含进来,所有要在项目属性中声明文件所在位置。而这一步一般都会做。



4:修改字符集错误。在上述操作做好之后,出现下图错误:


 错误的原因:

因为你的程序在UNICODE(宽字节)字符集下运行,如果调用了 MessageBox ,实际上调用的是 MessageBoxW 函数;

如果你的程序在 ANSI 字符集运行,调用 MessageBox ,就相当于调用 MessageBoxA;

其中 MessageBoxW 支持 UNICODE;MessageBoxA 支持ANSI;

UNICODE与ANSI 有什么区别呢?简单的说,UNICODE版的字符比ANSI 的内存占用大,比如:Win32程式中出现的标准定义 char 占一个字节,

而 char 的UNICODE版被定义成这样:

typedef unsigned short wchar_t ;占2个字节。

所以有字符做参数的函数相应也用两个版本了。

在上述操作都做好之后,程序理应正常运行。但有时会有新的错误产生(一般是字符集的错误),让人不知所措。这时我们应该冷静下来,首先检查之前操作是否正确,在排除之前操作的情况下。我们可以上网搜索解决办法。

解决办法:


     加L之后,运行程序,正确!


三个文件代码:

d3dUtility.cpp:

//////////////////////////////////////////////////////////////////////////////////////////////////// // File: d3dUtility.cpp// // Author: Frank Luna (C) All Rights Reserved//// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 //// Desc: Provides utility functions for simplifying common tasks.//          //////////////////////////////////////////////////////////////////////////////////////////////////#include "d3dUtility.h"bool d3d::InitD3D(HINSTANCE hInstance,int width, int height,bool windowed,D3DDEVTYPE deviceType,IDirect3DDevice9** device){//// Create the main application window.//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 = L"Direct3D9App";if( !RegisterClass(&wc) ) {::MessageBox(0, L"RegisterClass() - FAILED", 0, 0);return false;}HWND hwnd = 0;hwnd = ::CreateWindow(L"Direct3D9App", L"Direct3D9App", WS_EX_TOPMOST,0, 0, width, height,0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/); if( !hwnd ){::MessageBox(0, L"CreateWindow() - FAILED", 0, 0);return false;}::ShowWindow(hwnd, SW_SHOW);::UpdateWindow(hwnd);//// Init D3D: //HRESULT hr = 0;// Step 1: Create the IDirect3D9 object.IDirect3D9* d3d9 = 0;    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);    if( !d3d9 ){::MessageBox(0, L"Direct3DCreate9() - FAILED", 0, 0);return false;}// Step 2: Check for hardware vp.D3DCAPS9 caps;d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);int vp = 0;if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;elsevp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;// Step 3: Fill out the D3DPRESENT_PARAMETERS structure. 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;// Step 4: Create the device.hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, // primary adapterdeviceType,         // device typehwnd,               // window associated with devicevp,                 // vertex processing    &d3dpp,             // present parameters    device);            // return created deviceif( FAILED(hr) ){// try again using a 16-bit depth bufferd3dpp.AutoDepthStencilFormat = D3DFMT_D16;hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT,deviceType,hwnd,vp,&d3dpp,device);if( FAILED(hr) ){d3d9->Release(); // done with d3d9 object::MessageBox(0, L"CreateDevice() - FAILED", 0, 0);return false;}}d3d9->Release(); // done with d3d9 objectreturn 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.4f;light.Diffuse   = *color;light.Specular  = *color * 0.6f;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.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;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.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;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;}

d3dUtility.h:
//////////////////////////////////////////////////////////////////////////////////////////////////// // File: d3dUtility.h// // Author: Frank Luna (C) All Rights Reserved//// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 //// Desc: Provides utility functions for simplifying common tasks.//          //////////////////////////////////////////////////////////////////////////////////////////////////#ifndef __d3dUtilityH__#define __d3dUtilityH__#include <d3dx9.h>#include <string>#include <limits>namespace d3d{//// Windows/Direct3D Initialization//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 REFIDirect3DDevice9** device);// [out]The created device.int EnterMsgLoop( bool (*ptr_display)(float timeDelta));LRESULT CALLBACK WndProc(HWND hwnd,UINT msg, WPARAM wParam,LPARAM lParam);//// Cleanup//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;}}//// Colors//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);//// Bounding Objects//struct BoundingBox{BoundingBox();bool isPointInside(D3DXVECTOR3& p);D3DXVECTOR3 _min;D3DXVECTOR3 _max;};struct BoundingSphere{BoundingSphere();D3DXVECTOR3 _center;float       _radius;};//// Constants//const float infinity = std::numeric_limits<float>::infinity();const float EPSILON  = 0.001f;//// Randomness//// Desc: Return random float in [lowBound, highBound] interval.float GetRandomFloat(float lowBound, float highBound);// Desc: Returns a random vector in the bounds specified by min and max.void GetRandomVector(D3DXVECTOR3* out,D3DXVECTOR3* min,D3DXVECTOR3* max);//// Conversion//DWORD FtoDw(float f);}#endif // __d3dUtilityH__

stencilmirror.cpp:

//////////////////////////////////////////////////////////////////////////////////////////////////// // File: stencilmirror.cpp// // Author: Frank Luna (C) All Rights Reserved//// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 //// Desc: Demonstrates mirrors with stencils.  Use the arrow keys//       and the 'A' and 'S' key to navigate the scene and translate the teapot.//          //////////////////////////////////////////////////////////////////////////////////////////////////#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//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(){//// Make walls have low specular reflectance - 20%.//WallMtrl.Specular = d3d::WHITE * 0.2f;//// Create the teapot.//D3DXCreateTeapot(Device, &Teapot, 0);//// Create and specify geometry.  For this sample we draw a floor// and a wall with a mirror on it.  We put the floor, wall, and// mirror geometry in one vertex buffer.//    //   |----|----|----|    //   |Wall|Mirr|Wall|//   |    | or |    |    //   /--------------/    //  /   Floor      /// /--------------///Device->CreateVertexBuffer(24 * sizeof(Vertex),0, // usageVertex::FVF,D3DPOOL_MANAGED,&VB,0);Vertex* v = 0;VB->Lock(0, 0, (void**)&v, 0);// floorv[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);// wallv[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);// Note: We leave gap in middle of walls for mirrorv[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);// mirrorv[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();//// Load Textures, set filters.//D3DXCreateTextureFromFile(Device, L"checker.jpg", &FloorTex);D3DXCreateTextureFromFile(Device, L"brick0.jpg", &WallTex);D3DXCreateTextureFromFile(Device,L"ice.bmp", &MirrorTex);Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);//// Lights.//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);//// Set Camera.//D3DXVECTOR3    pos(-10.0f, 3.0f, -15.0f);D3DXVECTOR3 target(0.0, 0.0f, 0.0f);D3DXVECTOR3     up(0.0f, 1.0f, 0.0f);D3DXMATRIX V;D3DXMatrixLookAtLH(&V, &pos, &target, &up);Device->SetTransform(D3DTS_VIEW, &V);//// Set projection matrix.//D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI / 4.0f, // 45 - degree(float)Width / (float)Height,1.0f,1000.0f);Device->SetTransform(D3DTS_PROJECTION, &proj);return true;}void Cleanup(){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 ){//// Update the scene://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) & 0x8000f )radius += 2.0f * timeDelta;static float angle = (3.0f * D3DX_PI) / 2.0f;if( ::GetAsyncKeyState('A') & 0x8000f )angle -= 0.5f * timeDelta;if( ::GetAsyncKeyState('S') & 0x8000f )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);//// Draw the scene://Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,0xff000000, 1.0f, 0L);Device->BeginScene();RenderScene();RenderMirror();Device->EndScene();Device->Present(0, 0, 0, 0);}return true;}void RenderScene(){// draw teapotDevice->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);// draw the floorDevice->SetMaterial(&FloorMtrl);Device->SetTexture(0, FloorTex);Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);// draw the wallsDevice->SetMaterial(&WallMtrl);Device->SetTexture(0, WallTex);Device->DrawPrimitive(D3DPT_TRIANGLELIST, 6, 4);// draw the mirrorDevice->SetMaterial(&MirrorMtrl);Device->SetTexture(0, MirrorTex);Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);}void RenderMirror(){//// Draw Mirror quad to stencil buffer ONLY.  In this way// only the stencil bits that correspond to the mirror will// be on.  Therefore, the reflected teapot can only be rendered// where the stencil bits are turned on, and thus on the mirror // only.//    Device->SetRenderState(D3DRS_STENCILENABLE,    true);    Device->SetRenderState(D3DRS_STENCILFUNC,      D3DCMP_ALWAYS);    Device->SetRenderState(D3DRS_STENCILREF,       0x1);    Device->SetRenderState(D3DRS_STENCILMASK,      0xffffffff);    Device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);    Device->SetRenderState(D3DRS_STENCILZFAIL,     D3DSTENCILOP_KEEP);    Device->SetRenderState(D3DRS_STENCILFAIL,      D3DSTENCILOP_KEEP);    Device->SetRenderState(D3DRS_STENCILPASS,      D3DSTENCILOP_REPLACE);// disable writes to the depth and back buffers    Device->SetRenderState(D3DRS_ZWRITEENABLE, false);    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);    Device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_ZERO);    Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);    // draw the mirror to the stencil bufferDevice->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);// re-enable depth writesDevice->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 reflectionD3DXMATRIX W, T, R;D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); // xy planeD3DXMatrixReflect(&R, &plane);D3DXMatrixTranslation(&T,TeapotPosition.x, TeapotPosition.y,TeapotPosition.z); W = T * R;// clear depth buffer and blend the reflected teapot with the mirrorDevice->Clear(0, 0, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);Device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_DESTCOLOR);    Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);// Finally, draw the reflected teapotDevice->SetTransform(D3DTS_WORLD, &W);Device->SetMaterial(&TeapotMtrl);Device->SetTexture(0, 0);Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);Teapot->DrawSubset(0);// Restore render states.Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);Device->SetRenderState( D3DRS_STENCILENABLE, false);Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);}//// WndProc//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);}//// WinMain//int WINAPI WinMain(HINSTANCE hinstance,   HINSTANCE prevInstance,    PSTR cmdLine,   int showCmd){if(!d3d::InitD3D(hinstance,Width, Height, true, D3DDEVTYPE_HAL, &Device)){::MessageBox(0, L"InitD3D() - FAILED", 0, 0);return 0;}if(!Setup()){::MessageBox(0, L"Setup() - FAILED", 0, 0);return 0;}d3d::EnterMsgLoop( Display );Cleanup();Device->Release();return 0;}


0 0
原创粉丝点击