bonjour,
J'ai un probleme que je n'arrive pas a resoudre. pourriez vous m'aider svp.
J'arrive a afficher une meshe, maisla seconde ne s'affiche pas.
Mon code est separé en 2 fichiers :
Main.cpp : initialisation de directx
Palet .cpp : classe de mon objet.
Main . cpp :
//-----------------------------------------------------------------------------
// File: Pong.cpp
// Desc: Initialisation de l'application
// Date: 25/01/2006
// Modf: 25/01/2006
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//FICHIER A INCLURE
//-----------------------------------------------------------------------------
#include
<Windows.h> #include
<mmsystem.h> #include
<d3dx9.h> #include
<strsafe.h> #include
"Palet.h" //-----------------------------------------------------------------------------
//INITIALISATION DES VARIABLES GLOBALES
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL;
// Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
// Our rendering device CPalet Palet1;
// Instance du palet CPalet Palet2;
//-----------------------------------------------------------------------------
//INITIALISATION DIRECT 3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Since we are now // using more complex geometry, we will create a device with a zbuffer. D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp,
sizeof(d3dpp) ); d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL; }
// Turn on the zbuffer g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
// Turn on ambient lighting g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
return S_OK; }
//-----------------------------------------------------------------------------
// INITIALISATION DES OBJETS
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
Palet1.InitGeometry(g_pd3dDevice);
Palet2.InitGeometry(g_pd3dDevice);
Palet1.InitPosition(g_pd3dDevice,D3DXVECTOR3(0.0f,0.0f,0.0f));
Palet2.InitPosition(g_pd3dDevice,D3DXVECTOR3(0.0f,0.0f,-2.0f));
return S_OK; }
//-----------------------------------------------------------------------------
// NETTOYAGE DES OBJETS
//-----------------------------------------------------------------------------
VOID Cleanup()
{
Palet1.Cleanup();
Palet2.Cleanup();
}
//-----------------------------------------------------------------------------
// REGLAGE DE LA CAMERA
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
// Set up our view matrix. A view matrix can be defined given an eye point, // a point to lookat, and a direction for which way is up. Here, we set the // eye five units back along the z-axis and up three units, look at the // origin, and define "up" to be in the y-direction. D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
// For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// ANIMATION
//-----------------------------------------------------------------------------
VOID FrameMove()
{
// Setup the world, view, and projection matrices //Palet1.FrameMove(g_pd3dDevice); //Palet2.FrameMove(g_pd3dDevice); SetupMatrices();
}
//-----------------------------------------------------------------------------
// AFFICHAGE DE LA SCENE
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer and the zbuffer g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) {
// Meshes are divided into subsets, one for each material. Render them in // a loop Palet1.Render(g_pd3dDevice);
Palet2.Render(g_pd3dDevice);
// End the scene g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg ) {
case WM_DESTROY: Cleanup();
PostQuitMessage( 0 );
return 0; }
return DefWindowProc( hWnd, msg, wParam, lParam ); }
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class WNDCLASSEX wc = {
sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"PONG 3D", NULL }; RegisterClassEx( &wc );
// Create the application's window HWND hWnd = CreateWindow(
"PONG 3D", "PONG 3D", WS_OVERLAPPEDWINDOW, 100, 10, 790, 590,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) {
// Create the scene geometry if( SUCCEEDED( InitGeometry() ) ) {
// Show the window ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop MSG msg;
ZeroMemory( &msg,
sizeof(msg) ); while( msg.message!=WM_QUIT ) {
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else {
FrameMove();
Render();
}
}
}
}
UnregisterClass(
"PONG 3D", wc.hInstance ); return 0; }
Palet . cpp :
//-----------------------------------------------------------------------------
// File: Palet.h
// Desc: Implementation de la classe CPalet
// Date: 25/01/2006
// Modf: 25/01/2006
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//FICHIER A INCLURE
//-----------------------------------------------------------------------------
#include
<Windows.h> #include
<mmsystem.h> #include
<d3dx9.h> #include
<strsafe.h> #include
"Palet.h" //-----------------------------------------------------------------------------
//CONSTRUCTEUR / DESTRUCTEUR
//-----------------------------------------------------------------------------
CPalet::CPalet(
void) {
m_dwNumMaterials = 0L;
m_pMesh = NULL;
// Our mesh object in sysmem m_pMeshMaterials = NULL;
// Materials for our mesh m_pMeshTextures = NULL;
// Textures for our mesh m_dwNumMaterials = NULL;
// Number of mesh materials //m_pD3DXMtrlBuffer = NULL; D3DXMatrixIdentity(&m_matPalet);
D3DXMatrixIdentity(&m_matTemp);
}
CPalet::~CPalet(
void) {
}
//-----------------------------------------------------------------------------
// INITIALISATION DES OBJETS - CHARGEMENT DES MESHES
//-----------------------------------------------------------------------------
HRESULT CPalet::InitGeometry(LPDIRECT3DDEVICE9 pd3dDevice)
{
m_pd3dDevice = pd3dDevice;
m_dwNumMaterials = 0L;
LPD3DXBUFFER m_pD3DXMtrlBuffer;
// Load the mesh from the specified file if( FAILED( D3DXLoadMeshFromX( "palet.x", D3DXMESH_SYSTEMMEM, m_pd3dDevice, NULL,
&m_pD3DXMtrlBuffer, NULL, &m_dwNumMaterials,
&m_pMesh ) ) )
{
MessageBox(NULL,
"Could not find palet.x", "Meshes.exe", MB_OK); return E_FAIL; }
// We need to extract the material properties and texture names from the // pD3DXMtrlBuffer D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)m_pD3DXMtrlBuffer->GetBufferPointer();
m_pMeshMaterials =
new D3DMATERIAL9[m_dwNumMaterials]; m_pMeshTextures =
new LPDIRECT3DTEXTURE9[m_dwNumMaterials]; for( DWORD i=0; i<m_dwNumMaterials; i++ ) {
// Copy the material m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this) m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
m_pMeshTextures[i] = NULL;
if( d3dxMaterials[i].pTextureFilename != NULL && lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
{
// Create the texture if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, d3dxMaterials[i].pTextureFilename,
&m_pMeshTextures[i] ) ) )
{
MessageBox(NULL,
"Could not find texture map", "Meshes.exe", MB_OK); }
}
}
// Done with the material buffer m_pD3DXMtrlBuffer->Release();
return S_OK; }
//-----------------------------------------------------------------------------
// AFFICHAGE DE LA SCENE - AFFICHAGE DES MESHES
//-----------------------------------------------------------------------------
void
CPalet::Render(LPDIRECT3DDEVICE9 pd3dDevice) {
m_pd3dDevice = pd3dDevice;
for( DWORD i=0; i<m_dwNumMaterials; i++ ) {
// Set the material and texture for this subset m_pd3dDevice->SetMaterial( &m_pMeshMaterials[i] );
m_pd3dDevice->SetTexture( 0, m_pMeshTextures[i] );
m_pMesh->DrawSubset( i );
}
}
//-----------------------------------------------------------------------------
// NETTOYAGE DES OBJETS
//-----------------------------------------------------------------------------
void
CPalet::Cleanup(void) {
if( m_pMeshMaterials != NULL ) delete[] m_pMeshMaterials; if( m_pMeshTextures ) {
for( DWORD i = 0; i < m_dwNumMaterials; i++ ) {
if( m_pMeshTextures[i] ) m_pMeshTextures[i]->Release();
}
delete[] m_pMeshTextures; }
if( m_pMesh != NULL ) m_pMesh->Release();
}
//-----------------------------------------------------------------------------
// INITIALISATION DES OBJETS - POSITION DE DEPART
//-----------------------------------------------------------------------------
void
CPalet::InitPosition(LPDIRECT3DDEVICE9 pd3dDevice,D3DXVECTOR3 position) {
//Position de depart m_pd3dDevice = pd3dDevice;
D3DXMatrixIdentity(&m_matPalet);
D3DXMatrixTranslation(&m_matPalet, position.x, position.y, position.z);
m_pd3dDevice->SetTransform(D3DTS_WORLD,&m_matPalet);
}
//-----------------------------------------------------------------------------
// ANIMATION DES OBJETS
//-----------------------------------------------------------------------------
void
CPalet::FrameMove(LPDIRECT3DDEVICE9 pd3dDevice) {
//D3DXMatrixIdentity(&m_matPalet); //m_pd3dDevice->SetTransform(D3DTS_WORLD,&m_matPalet); }
merci de votre aide