Direct9之一:初始化创建

来源:互联网 发布:python日期格式化 编辑:程序博客网 时间:2024/05/21 17:34

以创建一个Cube为列子,进行创建。用的是Direct 2010的SDK,以龙书作为模板来讲解。

首先导入include 和lib并手动添加lib,这三个步骤一般教程都有,但是实际上还遇到了一些容易被遗忘的问题,比如说添加库文件的链接选项,如:

#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib)#pragma comment(lib,"winmm.lib")
还有在进行Direct9的开发的时候,会发现d3dx9.h不见了,你可以选择在VS2012中编译。不过我认为Direct9最终还是会被淘汰的,只不过9他市面上参考的书比较多,入门比较简单,到时候有机会再看11吧。如果用的是龙书的窗口初始化模块的话,还得在项目属性中的常规把字符集改为多字节。下面是龙书的例子,通过注释配合龙书肯定很容易能理解:

d3dUtility.h

//////////////////////////////////////////////////////////////////////////////////////////////////// // File: d3dUtility.h// // Author: miaobeihai//// Desc: Provides utility functions for simplifying common tasks.//          //////////////////////////////////////////////////////////////////////////////////////////////////#ifndef __d3dUtilityH__#define __d3dUtilityH__#include <d3dx9.h>#include <string>#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 REFIDirect3DDevice9** 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;}}}#endif // __d3dUtilityH__

d3dUtility.cpp

//////////////////////////////////////////////////////////////////////////////////////////////////// // File: d3dUtility.cpp// #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 = "Direct3D9App";if( !RegisterClass(&wc) )  //创建窗口四部曲之二之注册窗口类{::MessageBox(0, "RegisterClass() - FAILED", 0, 0);return false;}HWND hwnd = 0;//创建窗口四部曲之三之正式创建窗口hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App", WS_EX_TOPMOST,0, 0, width, height,0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/); if( !hwnd ){::MessageBox(0, "CreateWindow() - FAILED", 0, 0);return false;}//创建窗口四部曲之四之显示和更新窗口::ShowWindow(hwnd, SW_SHOW);::UpdateWindow(hwnd);//// Init D3D: //HRESULT hr = 0;// Step 1: Create the IDirect3D9 object.//D3D初始化四部曲之一,创接口IDirect3D9* d3d9 = 0;    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);    if( !d3d9 ){::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);return false;}// Step 2: Check for hardware vp.//D3D初始化四部曲之二,获取硬件信息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.    //D3D初始化四部曲之三,填内容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.//D3D初始化之四部曲之四,创设备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, "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;}

Cube.cpp


//////////////////////////////////////////////////////////////////////////////////////////////////// // File: cube.cpp// #include "d3dUtility.h"//// Globals//IDirect3DDevice9* Device = 0; const int Width  = 640;const int Height = 480;IDirect3DVertexBuffer9* VB = 0;IDirect3DIndexBuffer9*  IB = 0;//// Classes and Structures//struct Vertex{Vertex(){}Vertex(float x, float y, float z){_x = x;  _y = y;  _z = z;}float _x, _y, _z;static const DWORD FVF;};const DWORD Vertex::FVF = D3DFVF_XYZ; //灵活顶点绘制方式,向Direct表示Vertex是顶点结构//// Framework Functions//bool Setup() //绘制物体资源的初始化{//// Create vertex and index buffers.//Device->CreateVertexBuffer(8 * sizeof(Vertex), D3DUSAGE_WRITEONLY,Vertex::FVF,D3DPOOL_MANAGED,&VB,0);Device->CreateIndexBuffer(36 * sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&IB,0);//// Fill the buffers with the cube data.//// define unique vertices:Vertex* vertices;VB->Lock(0, 0, (void**)&vertices, 0);// vertices of a unit cubevertices[0] = Vertex(-1.0f, -1.0f, -1.0f);vertices[1] = Vertex(-1.0f,  1.0f, -1.0f);vertices[2] = Vertex( 1.0f,  1.0f, -1.0f);vertices[3] = Vertex( 1.0f, -1.0f, -1.0f);vertices[4] = Vertex(-1.0f, -1.0f,  1.0f);vertices[5] = Vertex(-1.0f,  1.0f,  1.0f);vertices[6] = Vertex( 1.0f,  1.0f,  1.0f);vertices[7] = Vertex( 1.0f, -1.0f,  1.0f);VB->Unlock();// define the triangles of the cube:WORD* indices = 0;IB->Lock(0, 0, (void**)&indices, 0);// front sideindices[0]  = 0; indices[1]  = 1; indices[2]  = 2;indices[3]  = 0; indices[4]  = 2; indices[5]  = 3;// back sideindices[6]  = 4; indices[7]  = 6; indices[8]  = 5;indices[9]  = 4; indices[10] = 7; indices[11] = 6;// left sideindices[12] = 4; indices[13] = 5; indices[14] = 1;indices[15] = 4; indices[16] = 1; indices[17] = 0;// right sideindices[18] = 3; indices[19] = 2; indices[20] = 6;indices[21] = 3; indices[22] = 6; indices[23] = 7;// topindices[24] = 1; indices[25] = 5; indices[26] = 6;indices[27] = 1; indices[28] = 6; indices[29] = 2;// bottomindices[30] = 4; indices[31] = 0; indices[32] = 3;indices[33] = 4; indices[34] = 3; indices[35] = 7;IB->Unlock();//// Position and aim the camera.//D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);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);//// Set the projection matrix.//D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f, // 90 - degree(float)Width / (float)Height,1.0f,1000.0f);Device->SetTransform(D3DTS_PROJECTION, &proj);//// Switch to wireframe mode.//Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);return true;}void Cleanup()//清理COM资源或者其他资源{d3d::Release<IDirect3DVertexBuffer9*>(VB);d3d::Release<IDirect3DIndexBuffer9*>(IB);}bool Display(float timeDelta) //渲染代码的书写{if( Device ){//int charCount =swprintf_s(g_strFPS,20,_T("FPS:%0.3f"),Get_FPS());//// spin the cube://D3DXMATRIX Rx, Ry;// rotate 45 degrees on x-axisD3DXMatrixRotationX(&Rx, 3.14f / 4.0f);// incremement y-rotation angle each framestatic float y = 0.0f;D3DXMatrixRotationY(&Ry, y);y += timeDelta;// reset angle to zero when angle reaches 2*PIif( y >= 6.28f )y = 0.0f;// combine x- and y-axis rotation transformations.D3DXMATRIX p = Rx * Ry;Device->SetTransform(D3DTS_WORLD, &p);//// draw the scene://Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); //渲染五部曲1:清屏操作Device->BeginScene(); //渲染五部曲2:开始绘制//渲染五部曲3:正式绘制,一般没有固定模式Device->SetStreamSource(0, VB, 0, sizeof(Vertex));  //如果要采取顶点缓存的格式,则需要3个步骤,1.SetStreamSource,把数据关联Device->SetIndices(IB);                             //2指定顶点的宏名称Device->SetFVF(Vertex::FVF);// Draw cube.Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);  //3完成最终的绘制操作,根据顶点缓存来绘制Device->EndScene();//渲染五部曲4:结束绘制Device->Present(0, 0, 0, 0);//渲染五部曲5:翻转显示}return true;}//// 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, "InitD3D() - FAILED", 0, 0);return 0;}if(!Setup()){::MessageBox(0, "Setup() - FAILED", 0, 0);return 0;}d3d::EnterMsgLoop( Display );Cleanup();Device->Release();return 0;}float Get_FPS(){static float fps =0;static int framCount =0;static float currentTime =0.0f;static float lastTime =0.0f;framCount++;currentTime =timeGetTime() *0.001f;if(currentTime -lastTime>1.0f){fps =(float) framCount/(currentTime-lastTime);lastTime =currentTime;framCount =0;}return fps;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 脱产考博社保卡怎么办 幼儿上课不认真听讲怎么办 手机恢复的音频文件打不开怎么办 高考志愿填报不记得密码怎么办 经济纠纷案被告没有证据怎么办 管家婆管理员密码忘记了怎么办 人离职了公司扣发工资怎么办? 美国给我们断网怎么办 sci发表后发现错误怎么办 pos机按键是英文怎么办 蔚县县医院慢病本怎么办 知网下载的论文乱码怎么办 被期刊网骗了怎么办? 缝针缝到神经上怎么办 单位有个事特别多的领导怎么办 网上申请公司核名核不下来怎么办 老公把小三晒朋友圈老婆该怎么办 小三怀孕不愿意打掉怎么办 小三怀孕了引产怎么办 小三发现小四小三发现小四后怎么办 七十岁老人肺癌还有小三样怎么办 留学出国学历公认证怎么办 学历认证报告弄丢了怎么办 想读大专没考上怎么办 学信网身份证被注册了怎么办 学信网手机号换了密保忘了怎么办 学信网手机号改了密保忘了怎么办 学信网手机号忘了密保也忘了怎么办 学信网账号被注销了怎么办 原味奶酪太难吃怎么办 cma年费太贵了怎么办 会计证3年没年检怎么办 幼师面试没有什么特长怎么办 电子学历注册表过期了怎么办 学信网查不到电子注册登记表怎么办 学历证书电子注册备案表过期怎么办 举报人对处理结果不满意怎么办 亚马逊账号申诉后余额怎么办 怎么办北京市工会会员互助服务卡 国家发改委录用公示后怎么办 慕课的账号忘了怎么办