运行不了,找了很久不知道错在什么地方,帮我改改,拜托了
程序代码:
#include<Windows.h> #include <d3d9.h> #include <d3dx9.h> #include <MMSystem.h> #pragma comment (lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #pragma comment(lib,"winmm.lib") LPDIRECT3D9 g_D3D = NULL; LPDIRECT3DDEVICE9 g_d3dDevice = NULL; LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL ; struct CUSTOMVERTEX { D3DXVECTOR3 position; D3DXVECTOR3 normal; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL) //////////////////////////////////////////////////////////// HRESULT InitD3D(HWND hWnd) { if (NULL==(g_D3D = Direct3DCreate9(D3D_SDK_VERSION))) { MessageBox(NULL,L"警告",L"g_D3D创建失败",MB_OK); return E_FAIL; } D3DPRESENT_PARAMETERS pddm; ZeroMemory( &pddm, sizeof( pddm ) ); pddm.Windowed = TRUE ; pddm.SwapEffect =D3DSWAPEFFECT_DISCARD; pddm.BackBufferFormat = D3DFMT_UNKNOWN; pddm.EnableAutoDepthStencil= TRUE ; pddm.AutoDepthStencilFormat = D3DFMT_D16; if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pddm,&g_d3dDevice) )) { MessageBox(NULL,L"g_d3dDevice创建失败",L"警告",MB_OK); return E_FAIL; } g_d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE ); g_d3dDevice->SetRenderState(D3DRS_ZENABLE,TRUE); return S_OK ; } ////////////////////////////////////////// HRESULT InitGeometry() { if (FAILED(g_d3dDevice->CreateVertexBuffer(50 * 2 * sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL))) { MessageBox(NULL,L"buffer创建失败",L"警告",MB_OK); return E_FAIL; } CUSTOMVERTEX* pVertices; if (FAILED(g_pVB->Lock(0,0,(void**)&pVertices,0))) { MessageBox(NULL,L"LOCK创建失败",L"警告",MB_OK); return E_FAIL; } for (DWORD i=0;i<50;i++) { FLOAT theta = (2*D3DX_PI*i)/(50-1);//第一个点和最后一个点是重合的所以是(50-1)个 pVertices[2*i + 0].position = D3DXVECTOR3 (sinf(theta),-1.0f,cosf(theta));//sinf==float sin pVertices[2*i + 0].normal = D3DXVECTOR3 (sinf(theta),0.0f,cosf(theta)); pVertices[2*i + 1].position = D3DXVECTOR3 (sinf(theta),1.0f,cosf(theta)); pVertices[2*i + 1].normal = D3DXVECTOR3 (sinf(theta),0.0f,cosf(theta)); } g_pVB->Unlock(); return S_OK; } ////////////////////////////////////////////////////////// VOID cleanup() { if (g_pVB != NULL) { g_pVB->Release(); } if (g_d3dDevice != NULL) { g_d3dDevice->Release(); } if (g_D3D != NULL) { g_D3D->Release(); } } ///////////////////////////////////// VOID SetMatrices() { D3DXMATRIXA16 matWord; D3DXMatrixIdentity(&matWord); D3DXMatrixRotationY(&matWord,timeGetTime()/1000.0f); g_d3dDevice->SetTransform(D3DTS_WORLD,&matWord); D3DXMATRIXA16 matView; D3DXVECTOR3 vEyePt(0.0f,3.0f,-5.0f); D3DXVECTOR3 vLookPt(0.0f,0.0f,0.0f); D3DXVECTOR3 vUpvec(0.0f,1.0f,0.0f); D3DXMatrixLookAtLH(&matView,&vEyePt,&vLookPt,&vUpvec); g_d3dDevice->SetTransform(D3DTS_VIEW,&matView); D3DXMATRIXA16 matPro; D3DXMatrixPerspectiveFovLH(&matPro,D3DX_PI/4.0,1.0f,1.0f,100.0f); g_d3dDevice->SetTransform(D3DTS_PROJECTION,&matPro); } ///////////////////////////////////// VOID SetupLights() { D3DMATERIAL9 mTrl; ZeroMemory(&mTrl,sizeof(D3DMATERIAL9));////////?? //ZeroMemory(&mTrl,sizeof(mTrl)); mTrl.Diffuse.b = mTrl.Ambient.b = 1.0f; mTrl.Diffuse.g = mTrl.Ambient.g = 1.0f; mTrl.Ambient.r = mTrl.Ambient.r = 1.0f; mTrl.Diffuse.a = mTrl.Ambient.a = 1.0f; g_d3dDevice->SetMaterial(&mTrl); D3DXVECTOR3 vecDir; D3DLIGHT9 light; ZeroMemory(&light,sizeof(D3DLIGHT9)); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b= 1.0f; vecDir = D3DXVECTOR3 (cosf(timeGetTime()/350.0f),1.0f,sinf(timeGetTime()/350.0f)); D3DXVec3Normalize((D3DXVECTOR3 *)&light.Direction,&vecDir); light.Range = 1000.0f; g_d3dDevice->SetLight(0,&light); g_d3dDevice->LightEnable(0,TRUE); g_d3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE); g_d3dDevice->SetRenderState(D3DRS_AMBIENT,0x00202020 ); } ////////////////////////////////////////////////////////////// VOID Render() { g_d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0); if(SUCCEEDED(g_d3dDevice->BeginScene())) { SetupLights(); SetMatrices(); g_d3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX)); g_d3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2 * 50 - 2); g_d3dDevice->EndScene(); } g_d3dDevice->Present(NULL,NULL,NULL,NULL); } ///////////////////////////////////////////// HRESULT WINAPI MsgPro(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch (msg) { case WM_DESTROY : cleanup(); PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,msg,wParam,lParam); } INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX wecx; wecx.cbSize = sizeof(WNDCLASSEX); wecx.lpfnWndProc = MsgPro; wecx.cbClsExtra = 0; wecx.cbWndExtra = 0; wecx.hbrBackground=NULL; wecx.hCursor =NULL; wecx.hIcon = NULL; wecx.hIconSm =NULL; wecx.hInstance = GetModuleHandle(NULL); wecx.lpszClassName =NULL; wecx.lpszMenuName = L"light"; RegisterClassEx(&wecx); //{ // MessageBox(NULL,L"注册窗口失败",L"警告",MB_OK); //} HWND hWnd; hWnd = CreateWindow(L"light",L"灯光",WS_OVERLAPPEDWINDOW,0,0, GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN), NULL,NULL,wecx.hInstance,NULL); if (SUCCEEDED(InitD3D(hWnd))) { if (SUCCEEDED(InitGeometry())) { ShowWindow(hWnd,SW_SHOWDEFAULT); UpdateWindow(hWnd); MSG msg; ZeroMemory(&msg,sizeof(msg)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else Render(); } } } UnregisterClass(L"light",wecx.hInstance); return 0; }
[ 本帖最后由 樵头 于 2011-2-14 21:09 编辑 ]