اخوتي لا أعرف لماذا لم يعمل البرنامج التالي, وهو لرسم اسطوانة مفرغة, فقط كانت ترسم نصف الأسطوانة: علما بأن اعداد الأسطوانة كان في الدالة Setup()
- [size="2"]#include "Utility.h"
- IDirect3DDevice9* Device = 0;
- const int Width = 640;
- const int Height = 480;
- const float h = 1.0f;
- const float r = 1.0f;
- const int N = 10;
- IDirect3DVertexBuffer9* cylinder = 0;
- struct Vertex
- {
- Vertex(){}
- Vertex(float x, float y, float z, float nx, float ny, float nz)
- {
- _x = x;
- _y = y;
- _z = z;
- _nx = nx;
- _ny = ny;
- _nz = nz;
- }
- float _x, _y, _z;
- float _nx, _ny, _nz;
- static const DWORD FVF;
- };
- const DWORD Vertex::FVF = D3DFVF_XYZ|D3DFVF_NORMAL;
- bool Setup()
- {
- Device->CreateVertexBuffer(N*6*sizeof(Vertex) ,D3DUSAGE_WRITEONLY
- , Vertex::FVF, D3DPOOL_MANAGED, &cylinder,0);
- Vertex* v;
- cylinder->Lock(0,0,(void**)&v, 0);
- int i = 0;
- float dt = 2.0f*D3DX_PI/N;
- float eps = 0.001f;
- for(float t = 0;t+eps<dt*N; t+=dt)
- {
- v[i+0] = Vertex(r*cos(t) , 0 , r*sin(t) , cos(t) , 0, sin(t));
- v[i+1] = Vertex(r*cos(t+dt), h , r*sin(t+dt), cos(t+dt), 0, sin(t+dt));
- v[i+2] = Vertex(r*cos(t+dt), 0 , r*sin(t+dt), cos(t+dt), 0, sin(t+dt));
- v[i+3] = Vertex(r*cos(t) , 0 , r*sin(t) , cos(t) , 0, sin(t));
- v[i+4] = Vertex(r*cos(t) , h , r*sin(t) , cos(t) , 0, sin(t));
- v[i+5] = Vertex(r*cos(t+dt), h , r*sin(t+dt), cos(t+dt), 0, sin(t+dt));
- i+=6;
- }
- cylinder->Unlock();
- D3DXVECTOR3 position(0.0f, 5.0f, 0.0f);
- D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
- D3DXVECTOR3 up(0.0f, 0.0f, 1.0f);
- D3DXMATRIX V;
- D3DXMatrixLookAtLH(&V, &position, &target, &up);
- Device->SetTransform(D3DTS_VIEW, &V);
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI * 0.5f, // 90 - degree
- (float)Width / (float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION, &proj);
- Device->SetMaterial(&d3d::WHITE_MTRL);
- D3DXCOLOR c = d3d::YELLOW;
- Device->SetLight(0, &d3d::InitDirectionalLight(&D3DXVECTOR3(1,0,0),&c));
- Device->LightEnable(0, true);
- Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
- Device->SetRenderState(D3DRS_SPECULARENABLE, true);
- Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
- return true;
- }
- void Cleanup()
- {
- d3d::Release<IDirect3DVertexBuffer9*>(cylinder);
- }
- bool Display(float timeDelta)
- {
- if( Device )
- {
- //static float y = 0;
- //D3DXMATRIX mat;
- //D3DXMatrixTranslation(&mat,3*cos(y),0.0f, 3*sin(y));
- //y += timeDelta;
- //if(y>2*D3DX_PI)
- // y = 0;
- //Device->SetTransform(D3DTS_WORLD, &mat);
- Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
- Device->BeginScene();
- Device->SetStreamSource(0, cylinder, 0, sizeof(Vertex));
- Device->SetFVF(Vertex::FVF);
- Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, N*2);
- Device->EndScene();
- Device->Present(0, 0, 0, 0);
- }
- return true;
- }
- 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);
- }
- int WINAPI WinMain(HINSTANCE hinstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- if(!d3d::InitD3D(hinstance,
- Width, Height, 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;
- }
- [/size]
