3D编程-旋转的立方形

来源:互联网 发布:企业信用查询软件 编辑:程序博客网 时间:2024/06/04 17:50
#include <d3d9.h>#include <d3dx9.h>#include <stdio.h>#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib")#pragma comment(lib,"winmm.lib")#define KeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)#define Msg(info) MessageBox(NULL,info,"message",MB_OK)#define ErrorMsg(info) MessageBox(NULL,info,"error",MB_ICONSTOP)#defineSAFE_RELEASE_OBJ(pObject)if(pObject){pObject->Release();}#define WND_CLASS "wndclass"#define WND_TITLE "D3DApp Demo"#define WND_WIDTH    400#define WND_HEIGHT   300HWND g_hwnd;HINSTANCE g_hinst;LPDIRECT3D9 g_d3d;LPDIRECT3DDEVICE9 g_device;LPDIRECT3DVERTEXBUFFER9 g_vb;LPDIRECT3DINDEXBUFFER9 g_ib;#define D3DFVF_CUSTOM (D3DFVF_XYZ | D3DFVF_DIFFUSE)struct CustomVertex{float x,y,z;long color;};bool InitGeometry(){CustomVertex cube[]={-1,-1,-1,0X00FF00,-1,1,-1,0XFF0000,1,1,-1,0X0000FF,1,-1,-1,0XFF00FF,-1,-1,1,0XFFFF00,-1,1,1,0XABCDEF,1,1,1,0XFEDCBA,1,-1,1,0XFF00};int cube_index_list[]={0,1,2,0,2,3,4,6,5,4,7,6,4,5,1,4,1,0,3,2,6,3,6,7,1,5,6,1,6,2,4,0,3,4,3,7};if(FAILED(g_device->CreateVertexBuffer(sizeof(cube), 0, D3DFVF_CUSTOM, D3DPOOL_DEFAULT, &g_vb, NULL)))   return false;if(FAILED(g_device->CreateIndexBuffer(sizeof(cube_index_list), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &g_ib, 0)))return false;// fill vextex buffervoid * paddr;g_vb->Lock(0, 0, (void**)&paddr, 0);memcpy(paddr, cube, sizeof(cube));g_vb->Unlock();// fill index bufferint * pIndex = 0;g_ib->Lock(0, 0, (void**)&pIndex, 0);memcpy(pIndex, cube_index_list, sizeof(cube_index_list));g_ib->Unlock();return true;}bool InitD3D(){if(NULL==(g_d3d = Direct3DCreate9(D3D_SDK_VERSION)))return false;D3DDISPLAYMODE d3dpm;if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dpm)))return false;D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;d3dpp.hDeviceWindow = g_hwnd;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferFormat = d3dpm.Format;if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_device)))return false;g_device->SetRenderState(D3DRS_LIGHTING, FALSE);g_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);return true;}bool InitApp(){if(!InitD3D() || !InitGeometry()){ErrorMsg("Unable to Initialize D3D !");return false;}return true;}void SetupMatrix(){D3DXMATRIX world,view,proj;D3DXVECTOR3 eye(0.0f, 2.0f, 6.0f),lookat(0.0f, 0.0f, 0.0f), updir(0.0f, 1.0f, 0.0f);D3DXMatrixRotationY(&world, timeGetTime()/500.0f);g_device->SetTransform(D3DTS_WORLD, &world);D3DXMatrixLookAtLH(&view, &eye, &lookat, &updir);g_device->SetTransform(D3DTS_VIEW, &view);D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 2.0f, WND_WIDTH/WND_HEIGHT, 1.0f, 1000.0f);g_device->SetTransform(D3DTS_PROJECTION, &proj);}void Render(){g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);g_device->BeginScene();  SetupMatrix(); g_device->SetStreamSource(0, g_vb, 0, sizeof(CustomVertex));g_device->SetIndices(g_ib);g_device->SetFVF(D3DFVF_CUSTOM);g_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);g_device->EndScene();g_device->Present(NULL, NULL, NULL, NULL);}void ShutdownApp(){SAFE_RELEASE_OBJ(g_ib);SAFE_RELEASE_OBJ(g_vb);SAFE_RELEASE_OBJ(g_device);SAFE_RELEASE_OBJ(g_d3d); }LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){switch(msg){case WM_DESTROY:PostQuitMessage(0);break;default:break;}return DefWindowProc(hwnd, msg, wparam, lparam);}int WINAPI WinMain(HINSTANCE hinst,HINSTANCE hprevinst, LPSTR cmdlint, INT show){g_hinst = hinst;WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_CLASSDC;wcex.lpfnWndProc = MsgProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = GetModuleHandle(NULL);wcex.hIcon = LoadIcon(NULL, IDC_APPSTARTING);wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wcex.hCursor = LoadCursor( NULL, IDC_ARROW );wcex.lpszClassName = WND_CLASS;wcex.lpszMenuName = NULL;wcex.hIconSm = LoadIcon(NULL, IDC_APPSTARTING); if(!RegisterClassEx(&wcex)){ErrorMsg("RegisterClassEx failed!");return 0;}g_hwnd = CreateWindow(WND_CLASS, WND_TITLE, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, WND_WIDTH, WND_HEIGHT,NULL, NULL, hinst, NULL);if(!g_hwnd)return 0;ShowWindow(g_hwnd, SW_SHOWNORMAL);UpdateWindow(g_hwnd);MSG msg={0};if(!InitApp())msg.message = WM_QUIT;while(msg.message != WM_QUIT){if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){TranslateMessage(&msg);DispatchMessage(&msg);}Render();}ShutdownApp();UnregisterClass(WND_CLASS, wcex.hInstance);return 0;} 

原创粉丝点击