绘制立方体
////////////////////////////////////////////////////////////////////////////////////////////////// // // File: d3dinit.cpp // // Author: Frank Luna (C) All Rights Reserved // // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 // // Desc: Demonstrates how to initialize Direct3D, how to use the book's framework // functions, and how to clear the screen to black. Note that the Direct3D // initialization code is in the d3dUtility.h/.cpp files. // ////////////////////////////////////////////////////////////////////////////////////////////////// #include "d3dUtility.h" #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") #pragma comment(lib, "winmm.lib") // // Globals // IDirect3DDevice9* Device = 0; // // Framework Functions // const int Width=640; const int Height=480; IDirect3DVertexBuffer9* VB=0; IDirect3DIndexBuffer9* IB=0; //定义三角形的顶点结构体 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; bool Setup() { Device->CreateVertexBuffer( 8*sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &VB, 0); Vertex* vertices; VB->Lock(0,0,(void**)&vertices,0); vertices[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(); Device->CreateIndexBuffer( 36*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &IB, 0); WORD* indices=0; IB->Lock(0,0,(void**)&indices,0); //前面 indices[0]=0; indices[1]=1; indices[2]=2; indices[3]=0; indices[4]=2; indices[5]=3; //后面 indices[6]=4; indices[7]=6; indices[8]=5; indices[9]=4; indices[10]=7; indices[11]=6; //左面 indices[12]=4; indices[13]=5; indices[14]=1; indices[15]=4; indices[16]=1; indices[17]=0; //右面 indices[18]=3; indices[19]=2; indices[20]=6; indices[21]=3; indices[22]=6; indices[23]=7; //上面 indices[24]=1; indices[25]=5; indices[26]=6; indices[27]=1; indices[28]=6; indices[29]=2; //下面 indices[30]=4; indices[31]=0; indices[32]=3; indices[33]=4; indices[34]=3; indices[35]=7; IB->Unlock(); //摄像机的位置和方向 D3DXVECTOR3 position(0.0f,0.0f,-5.0f);//摄像机默认位置为(0,0,0) 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);//观察坐标系转换 D3DXMATRIX proj;//投影变换用的矩阵 D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, (float)Width/(float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION,&proj); Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME); return true; } void Cleanup() { } bool Display(float timeDelta) { if( Device ) // Only use Device methods if we have a valid device. { // Instruct the device to set each pixel on the back buffer black - // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f. //Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0); Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); // Swap the back and front buffers. Device->BeginScene(); Device->SetStreamSource(0,VB,0,sizeof(Vertex)); Device->SetIndices(IB); Device->SetFVF(Vertex::FVF); Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12); Device->EndScene(); Device->Present(0, 0, 0, 0); } 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, 640, 480, 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; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~