Tutorial 1 : VertexBuffer simple et Matrics
Transcription
Tutorial 1 : VertexBuffer simple et Matrics
Brugevin Boris 2005/2006 Tutorial 1 : VertexBuffer simple et Matrics Tout d’abord il faut créer une application win32 et lier les includes et librairies de directX. #pragma comment(lib, "d3dx9.lib") #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "winmm.lib") #include <d3d9.h> #include <d3dx9.h> La fonction main s’occupe de créer la fenêtre windows : int WINAPI WinMain( HINSTANCE HINSTANCE LPSTR int hInstance, hPrevInstance, lpCmdLine, nCmdShow ) Après il faut initialiser un context D3D pour pouvoir écrire dans cette fenêtre avec directX : LPDIRECT3D9 LPDIRECT3DDEVICE9 g_pD3D g_pd3dDevice = NULL; = NULL; g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ); g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, (D3DDEVTYPE), (HWND fenêtre), (Mode du traitement des vertexes), (Format de la fenêtre), &g_pd3dDevice ); Une fois directX initialisé il nous reste plus qu’a créer notre primitive et mettre en place une matrice de projection : struct Vertex { float x, y, z; DWORD diffuse; enum FVF { FVF_Flags = D3DFVF_XYZ | D3DFVF_DIFFUSE }; }; const int NUM_VERTICES = 3; Vertex g_cubeVertices[NUM_VERTICES] = { // x y z r g b a // triangle {-1.0f, -1.0f,0.0f, D3DCOLOR_COLORVALUE(1.0f,0.0f,0.0f,1.0f) }, { 1.0f, -1.0f,0.0f, D3DCOLOR_COLORVALUE(0.0f,1.0f,0.0f,1.0f) }, { 0.0f, 1.0f,0.0f, D3DCOLOR_COLORVALUE(0.0f,0.0f,1.0f,1.0f) } }; // On crée le vertexBuffer et on le remplit Brugevin Boris 2005/2006 g_pd3dDevice->CreateVertexBuffer( NUM_VERTICES*sizeof(Vertex), D3DUSAGE_DYNAMIC, Vertex::FVF_Flags, D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL ); void *pVertices = NULL; g_pVertexBuffer->Lock( 0, sizeof(g_cubeVertices), (void**)&pVertices, 0); memcpy( pVertices, g_cubeVertices, sizeof(g_cubeVertices) ); g_pVertexBuffer->Unlock(); // init de la matrice de projection D3DXMATRIX matProj; D3DXMatrixIdentity(&matProj); D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ), 640.0f / 480.0f, 0.1f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); On initialise quelques état de directX comme le culling, la lumière… // on initialise qq état de directX g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD ); g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); Maintenant avant d’afficher notre triangle il faut lui appliquer les transformation que l’on souhaite : D3DXMATRIX matWorld; D3DXMATRIX matTrans; D3DXMATRIX matRot; // translation D3DXMatrixIdentity(&matTrans); D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, g_fDistance ); // rotation sur x, y, z D3DXMatrixIdentity(&matRot); D3DXMatrixRotationYawPitchRoll( &matRot, D3DXToRadian(g_fSpinX), D3DXToRadian(g_fSpinY), 0.0f ); D3DXMatrixIdentity(&matWorld); matWorld = matRot * matTrans; // Init de la matrice du monde g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); Et enfin le rendu du triangle : // // Rendu du triangle // g_pd3dDevice->BeginScene(); g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(Vertex) ); g_pd3dDevice->SetFVF( Vertex::FVF_Flags ); Brugevin Boris g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 2005/2006 0, 1 ); g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); Sans oublier à la fin de relâcher la mémoire prise par directX !!! void shutDown( void ) { if( g_pVertexBuffer != NULL ) g_pVertexBuffer->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); } Voilà ce tutorial prend fin ici ☺ En espérant que cela vous aide ;)