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;}
阅读全文
0 0
- Direct9之一:初始化创建
- Direct9
- Direct9入门教程|Direct9官方提供的tutorial文件自己翻译的|DirectX创建设备
- 初始化与清理之一 (初始化)
- direct9.0光照
- direct9.0纹理映射
- Direct9学习之--------------------------地形
- Direct9 ID3DXFont绘制
- [ActiveRecord] 之一:初始化和配置
- 【静态变量的初始化之一】
- Apache Log4j 架构之一 初始化
- sqlite数据库之一创建
- Direct9学习之 ------------------- 天空盒
- direct9.0b 下载地址
- 创建和初始化bean
- 关于创建类,初始化
- 对象创建及初始化
- 创建单利以及初始化
- 黑科技之手机与电脑之间的远程管理
- bzoj 3679: 数字之积
- msp430输出PWM波
- JavaScript
- python学习-转换物体颜色
- Direct9之一:初始化创建
- 什么是红黑树?
- SSL2831 2017年11月3日提高组T1 跃动(dp)
- linux学习笔记之三:一些常用vim命令符
- HZNUOJ 1960
- MovieTaster-使用Item2Vec做电影推荐
- C++进阶—>const、define和enum的区别和用途
- DHCP
- margin重叠现象