seb2086
Messages postés
96
Date d'inscription
lundi 25 avril 2005
Statut
Membre
Dernière intervention
1 février 2010
9 févr. 2007 à 09:32
Je te met le code que j'utilise pour charger, initialiser et afficher un objet.
En fait je ne connais pas a l'avance le nombre d'objets qui seront chargés.
Merci pour votre aide.
Main.h
// Permet d'éviter les problémes liés aux include
#ifndef _TEST_CUBE01_H
#define _TEST_CUBE01_H
/*-----------------------------------------------------------------
Déclaration des fichiers Includes
-----------------------------------------------------------------*/
#include <d3d9.h>
#include <d3dx9.h>
#include <windows.h> // Pour utiliser API 32
#include <mmsystem.h> // Fonction timeGetTime() (winmm.lib)
#include "Fonctions_Utiles.h"
/*-----------------------------------------------------------------
Déclaration des variables globales
-----------------------------------------------------------------*/
HWND hWindow = NULL ;
LPD3DXMESH g_pTeapotMesh = NULL ;
LPD3DXMESH g_pTeapotMesh2 = NULL ;
LPDIRECT3D9 g_pD3D = NULL ;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL ;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL ;
D3DLIGHT9 g_pLumiere0 ;
D3DMATERIAL9 g_Couleur_Bleue ;
D3DMATERIAL9 g_Couleur_Rouge ;
D3DMATERIAL9 g_Couleur_Verte ;
D3DXMATRIX matRot ;
// Axes de rotation du cube
float g_fSpinX = 0.0f ;
float g_fSpinY = 0.0f ;
float g_fSpinZ = 0.0f ;
// Vitesse de rotation du cube
float g_fTime = 0.0f ;
// Récupération de la position du cube dans le temps
double g_dCurrentTime = 0.0f ;
double g_dLastTime = 0.0f ;
// Active ou désactive la rotation du cube
bool rotation = true ;
// Gestion de l'affichage du texte
Gestion_Texte Texte ;
int MODE = 0 ;
struct Vertex
{
float x, y, z ; // Position des vertex dans l'espace 3D
float nx, ny, nz ; // Calculs pour l'éclairage
DWORD diffuse ; // Couleur diffuse du vertex
enum FVF
{
FVF_Flags = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE
};
};
#endif
Main.cpp
#define STRICT
#define WIN32_LEAN_AND_MEAN // Accélére les temps de génération
/*-----------------------------------------------------------------
Déclaration des fichiers Includes
-----------------------------------------------------------------*/
#include "Test_Cube01.h"
#include "Ressources.h"
/*-----------------------------------------------------------------
Déclaration des fonctions globales et des procédures
-----------------------------------------------------------------*/
void Rendu_Objet() ;
void Liberer_Ressources_Objet() ;
void Rotation_Cube(float vitesseX) ;
void Init_Objet() ;
HRESULT Init_Transformations() ;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow) ;
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam,
LPARAM lParam) ;
/*-----------------------------------------------------------------
Nom : WinMain()
Description : Point d'entrée de l'application
-----------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Test Cube01") ;
WNDCLASSEX wndclass ;
MSG msg ;
memset(&msg, 0, sizeof(msg)) ;
// Création de la classe fenêtre
wndclass.cbSize = sizeof(wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon(hInstance, (LPCTSTR)(IDI_GRAND_CUBE)) ;
wndclass.hIconSm = LoadIcon(hInstance, (LPCTSTR)(IDI_PETIT_CUBE)) ;
wndclass.hCursor = LoadCursor(hInstance, (LPCTSTR)(IDC_TEST_CUBE01)) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
// Enregistrement de la classe fenêtre
if(!RegisterClassEx(&wndclass))
return 0 ;
// Création de la fenêtre
hWindow = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, 600, 480, NULL, NULL, hInstance, NULL) ;
if(hWindow == NULL)
return E_FAIL ;
// Initialisations
Init_Objet() ;
Texte.InitFont(g_pd3dDevice) ;
// Afficher et rafraîchir la fenêtre
ShowWindow(hWindow, iCmdShow) ;
UpdateWindow(hWindow) ;
// Boucle de message de l'application
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg) ;
DispatchMessage(&msg) ;
}
else
{
// Récupére les différents temps afin de faire tourner le cube
g_dCurrentTime = timeGetTime() ;
g_fTime = (float)((g_dCurrentTime - g_dLastTime) * 0.01f) ;
g_dLastTime = g_dCurrentTime ;
Rendu_Objet() ;
}
}
Liberer_Ressources_Objet() ;
Texte.ReleaseFont() ;
return (int)msg.wParam ;
}
/*-----------------------------------------------------------------
Nom : WindowProc()
Description : Messages du handle envoyés à la fenêtre
-----------------------------------------------------------------*/
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg,
WPARAM wParam, LPARAM lParam)
{
HDC hDC ;
PAINTSTRUCT ps ;
RECT rect ;
switch (msg)
{
case WM_KEYDOWN :
{
switch(wParam)
{
case VK_ESCAPE :
{
PostQuitMessage(0) ;
break ;
}
case VK_SPACE :
{
if(rotation == true)
{
rotation = false ;
MODE = 1 ;
}
else
{
rotation = true ;
MODE = 0 ;
}
break ;
}
}
}
break ;
case WM_PAINT :
{
// Dessine la fenêtre
hDC = BeginPaint(hWindow, &ps) ;
GetClientRect(hWindow, &rect) ;
EndPaint(hWindow, &ps) ;
return 0 ;
}
case WM_CLOSE :
{
PostQuitMessage(0) ;
}
case WM_DESTROY :
{
// Quitte l'application
PostQuitMessage(0) ;
return 0 ;
}
}
return DefWindowProc(hWindow, msg, wParam, lParam) ;
}
/*-----------------------------------------------------------------------------
Nom : Init_Objet()
Description : Initialise et charge les objets
-----------------------------------------------------------------------------*/
void Init_Objet()
{
D3DDISPLAYMODE d3ddm ;
D3DPRESENT_PARAMETERS d3dpp ;
D3DXMATRIX matProj ;
LPD3DXMESH pTempTeapotMesh = NULL ;
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION) ;
g_pD3D -> GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm) ;
ZeroMemory(&d3dpp, sizeof(d3dpp)) ;
// Initialisation de D3D
// Si l'application est en mode Windows ou Plein écran
d3dpp.Windowed = TRUE ;
// Mode d'échange entre le FontBuffer et le BackBuffer
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ;
// Format du pixel du BackBuffer
d3dpp.BackBufferFormat = d3ddm.Format ;
// Activation du Depth et Stencil Buffer
d3dpp.EnableAutoDepthStencil = TRUE ;
// Format du Depth et Stencil
d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ;
// Type de synchonisation de l'échange des Buffers
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE ;
// Initialisation du Device
g_pD3D -> CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice) ;
D3DXMatrixPerspectiveFovLH(&matProj, D3DXToRadian(45.0f), 640.0f / 480.0f, 0.1f, 100.0f) ;
g_pd3dDevice -> SetTransform(D3DTS_PROJECTION, &matProj) ;
g_pd3dDevice -> SetRenderState(D3DRS_ZENABLE, TRUE) ; // Activation du Z-Buffer
g_pd3dDevice -> SetRenderState(D3DRS_LIGHTING, TRUE) ; // Activation des lumiéres
g_pd3dDevice -> SetRenderState(D3DRS_SPECULARENABLE, TRUE) ; // Activation des lumiéres spéculaires
// Colore le cube en bleu
ZeroMemory(&g_Couleur_Bleue, sizeof(D3DMATERIAL9)) ;
g_Couleur_Bleue.Diffuse.r = 0.0f ;
g_Couleur_Bleue.Diffuse.g = 0.0f ;
g_Couleur_Bleue.Diffuse.b = 3.0f ;
g_Couleur_Bleue.Diffuse.a = 0.0f ;
g_pd3dDevice -> SetRenderState(D3DRS_COLORVERTEX, FALSE) ;
// Colore le cube en rouge
ZeroMemory(&g_Couleur_Rouge, sizeof(D3DMATERIAL9)) ;
g_Couleur_Rouge.Diffuse.r = 3.0f ;
g_Couleur_Rouge.Diffuse.g = 0.0f ;
g_Couleur_Rouge.Diffuse.b = 0.0f ;
g_Couleur_Rouge.Diffuse.a = 0.0f ;
// Colore le cube en vert
ZeroMemory(&g_Couleur_Verte, sizeof(D3DMATERIAL9)) ;
g_Couleur_Verte.Diffuse.r = 0.0f ;
g_Couleur_Verte.Diffuse.g = 3.0f ;
g_Couleur_Verte.Diffuse.b = 0.0f ;
g_Couleur_Verte.Diffuse.a = 0.0f ;
g_pd3dDevice -> SetRenderState(D3DRS_COLORVERTEX, FALSE) ;
// Eclairage : Lampe directionnelle
g_pLumiere0.Type = D3DLIGHT_DIRECTIONAL ;
g_pLumiere0.Direction = D3DXVECTOR3(1.0f, 0.0f, 1.0f) ;
g_pLumiere0.Diffuse.r = 1.0f ;
g_pLumiere0.Diffuse.g = 1.0f ;
g_pLumiere0.Diffuse.b = 1.0f ;
g_pLumiere0.Diffuse.a = 1.0f ;
g_pLumiere0.Specular.r = 1.0f ;
g_pLumiere0.Specular.g = 1.0f ;
g_pLumiere0.Specular.b = 1.0f ;
g_pLumiere0.Specular.a = 1.0f ;
g_pd3dDevice -> SetLight(0, &g_pLumiere0) ;
g_pd3dDevice -> LightEnable(0, TRUE) ;
g_pd3dDevice -> SetRenderState(D3DRS_AMBIENT, D3DCOLOR_COLORVALUE(0.2f, 0.2f, 0.2f, 1.0f)) ;
// Chargement du cube
D3DXLoadMeshFromX("./Ressources/Objets/Cube.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice,
NULL, NULL, NULL, NULL, &pTempTeapotMesh) ;
pTempTeapotMesh -> CloneMeshFVF(0, Vertex :: FVF_Flags, g_pd3dDevice, &g_pTeapotMesh) ;
pTempTeapotMesh -> Release() ;
}
/*-----------------------------------------------------------------------------
Nom : Rendu_Objet()
Description : Affiche l'objet à l'écran
-----------------------------------------------------------------------------*/
void Rendu_Objet()
{
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL ;
g_pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(255,255,255), 1.0f, 0) ;
g_pd3dDevice -> BeginScene() ;
D3DXMATRIX matView ;
D3DXMATRIX matWorld ;
D3DXMATRIX matTrans ;
D3DXMatrixTranslation(&matTrans, 5.0f, 0.7f, 18.0f) ;
matView = matTrans ;
g_pd3dDevice -> SetTransform(D3DTS_VIEW, &matView) ;
// Positionnement du cube
D3DXMatrixRotationYawPitchRoll(&matRot,
D3DXToRadian(g_fSpinX),
D3DXToRadian(g_fSpinY),
D3DXToRadian(g_fSpinZ)) ;
float c 2.0f , d 5.0f , e = 0.0f ;
// Rendu du cube et placement dans l'espace 3D
D3DXMatrixTranslation(&matTrans, c, d, e) ;
matWorld = matRot * matTrans ;
g_pd3dDevice -> SetTransform(D3DTS_WORLD, &matWorld) ;
g_pd3dDevice -> SetMaterial(&g_Couleur_Bleue);
g_pTeapotMesh -> DrawSubset(0) ;
D3DXMatrixIdentity(&matWorld);
/*D3DXMatrixTranslation(&matTrans, 0.0f, -3.0f, e) ;
matWorld = matRot * matTrans ;*/
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
g_pd3dDevice -> SetMaterial(&g_Couleur_Verte);
g_pTeapotMesh -> DrawSubset(0) ;
// Rotation du cube
Rotation_Cube(5.0f) ;
// Gestion de l'affichage du texte suivant si l'animation du cube est exécutée ou pas
switch(MODE)
{
case 0 :
{
// Affichage du texte
Texte.DrawText(0, "Appuyer sur la touche Espace pour arrêter l'animation", Texte.noir) ;
Texte.DrawText(1, "Rotation : Activée", Texte.noir) ;
break ;
}
case 1 :
{
// Affichage du texte
Texte.DrawText(0, "Appuyer sur la touche Espace pour reprendre l'animation", Texte.noir) ;
Texte.DrawText(1, "Rotation : Désactivée", Texte.noir) ;
break ;
}
}
g_pd3dDevice -> EndScene() ;
g_pd3dDevice -> Present(NULL, NULL, NULL, NULL) ;
}
/*-----------------------------------------------------------------------------
Nom : Rotation_Cube
Description : Permet d'effectuer une rotation du cube
-----------------------------------------------------------------------------*/
void Rotation_Cube(float vitesseX)
{
if(rotation == true)
{
// Permet de gérer la vitesse de rotation
g_fSpinX += g_fTime * vitesseX ;
D3DXMatrixRotationYawPitchRoll(&matRot,
D3DXToRadian(g_fSpinX),
D3DXToRadian(g_fSpinY),
D3DXToRadian(g_fSpinZ)) ;
}
}
/*-----------------------------------------------------------------------------
Nom : Liberer_Ressources_Objet()
Description : Libere les ressources utilisées par le chargement des objets
-----------------------------------------------------------------------------*/
void Liberer_Ressources_Objet()
{
if(g_pTeapotMesh != NULL)
g_pTeapotMesh -> Release() ;
if(g_pd3dDevice != NULL)
g_pd3dDevice -> Release() ;
if(g_pD3D != NULL)
g_pD3D -> Release() ;
}