Beaucoup de monde on de la misere a ouvrir un fichier fait avec studio max dans leur application OpenGL, du fait que le format .x est fait pour DX, et le .obj est en format texte. Et bien, pour me faciliter la vie, je me suis programmer un Importer et un exporter de models pour 3ds max.
Avec ca, vous pourrer ouvrir le models dans votre application en 1 ligne de code!
#include "E3DModel.h"
E3DModel::E3DModel()
{
NumOfVerts = 0;
IndexArraySize = 0;
pIndexArray = NULL;
pVertsArray = NULL;
pTexVertsArray = NULL;
pAuxTexVertsArray = NULL;
pNormalsArray = NULL;
pFogArray = NULL;
TrisCount = 0;
}
E3DModel::~E3DModel()
{
SAFE_DELETE_ARRAY(pIndexArray);
SAFE_DELETE_ARRAY(pVertsArray);
SAFE_DELETE_ARRAY(pTexVertsArray);
SAFE_DELETE_ARRAY(pAuxTexVertsArray);
SAFE_DELETE_ARRAY(pNormalsArray);
SAFE_DELETE_ARRAY(pFogArray);
}
bool E3DModel::LoadModelFromFile(char *pFName)
{
COptions Options;
ZeroMemory(&Options, sizeof(Options));
//Open the file
FILE *f = fopen(pFName, "rb");
if(!f){
char c[256];
sprintf(c, "Unable to open the file \"%s\"", pFName);
MessageBox(0, c, "Error!", 0);
return FALSE;
}
//This will tell us how many objects are in the file
int ObjCount = 0;
//Read this number...
fread(&ObjCount, 1, sizeof(int), f);
//Read the options
fread(&Options, 1, sizeof(Options), f);
//Past-Read the object name
{
UCHAR lBuf = 0xCD; //Can't init. to NULL, so use another value intead
while(lBuf != NULL){
fread(&lBuf, 1, sizeof(UCHAR), f);
}
}
//Read the length of the Index Array we have
fread(&IndexArraySize, 1, sizeof(int), f);
//Read the number of verts we have
fread(&NumOfVerts, 1, sizeof(int), f);
//Allocate our model's memory
pIndexArray = new UINT[IndexArraySize];
pVertsArray = new float3[NumOfVerts];
if(Options.TexVerts)
pTexVertsArray = new float2[NumOfVerts];
if(Options.AuxTexVerts)
pAuxTexVertsArray = new float2[NumOfVerts];
if(Options.Normals)
pNormalsArray = new float3[NumOfVerts];
if(Options.Fog)
pFogArray = new float[NumOfVerts];
//Read our data
fread(&pIndexArray[0], 1, IndexArraySize * sizeof(UINT),f);
fread(&pVertsArray[0], 1, NumOfVerts * sizeof(float3), f);
if(Options.TexVerts)
fread(&pTexVertsArray[0], 1, NumOfVerts * sizeof(float2), f);
if(Options.AuxTexVerts)
fread(&pAuxTexVertsArray[0], 1, NumOfVerts * sizeof(float2), f);
if(Options.Normals)
fread(&pNormalsArray[0], 1, NumOfVerts * sizeof(float3), f);
if(Options.Fog)
fread(&pFogArray[0], 1, NumOfVerts * sizeof(float), f);
//Get the number of tris of this mesh
TrisCount = IndexArraySize / 3;
return false;
}
Donc vous crée un object E3DModel comme ceci:
E3DModel Model;
Et vous loader le model comme cela:
Model.LoadModelFromFile("Nom_du_fichier.E3D");
La desallocation se fait automatiquement par le destructeur...
Pour dessiner:
//Enable vertex array
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); //Si nessessaire
glEnableClientState(GL_VERTEX_ARRAY); //Si nessessaire
glVertexPointer(3, GL_FLOAT, 0, Model.pVertsArray);
glTexCoordPointer(2, GL_FLOAT, 0, Model.pTexVertsArray); //Si nessessaire
glNormalPointer(GL_FLOAT, 0, Model.pNormalsArray); //Si nessessaire
glDrawElements(GL_TRIANGLES, Model.IndexArraySize, GL_UNSIGNED_INT, Model.pIndexArray);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); //Si nessessaire
glDisableClientState(GL_VERTEX_ARRAY); //Si nessessaire
*j'ai ecrit "Si nessaisaire, car si on exporte le fichier sans l'info des texture et/ou des normals, et qu'on l'utilise dans le code comme si dessu, le programme va "crasher"
Pour ce qui est des plugins(Importer/Exporter) pour studio max, les voici:
(copier les 2 dll dans le dossier Plugins de 3ds tout simplement)
Clicker sur Export... ou Import... et vous verer dans la liste des extension de fichiers le format .E3D
PS.
Il y a aussi le plugins Heighmap.dll, fait pour generer des heightmaps a partir de bitmap de n'importe quelle format. Pour l'utiliser, copier le a la meme place que les 2 autres, et, dans studio max, dans le combobox ecrit "Standart Primitive", selectionner "My Stuffs", et faite un clic pour ouvir une dialogbox permetant de selectionner le bitmap voulu.
en suivant un tuto j'avais réussi un truc similaire
- ouvrir le fichier
- charger les mesh, faces etc
c'est rapide, pour l'afficher aussi, là où j'ai eu du mal c'est pour trouver des modèles^^
les lowpoly courent pas les pages du web, et si ma boucle d'affichage dépasse les 10k tours (je crois) le programme se ferme purement et simplement^^
en se référant à la doc du format .x, il s'agit vraiment d'un format intéressant, en parcourant le fichier, au fil des headers on extrait tout ce dont on a besoin :)
__________________________
Spirit - modérateur casu ^-^'