|
Dernière réponse | |
---|---|
Sujet : [OpenGL] Pb chargement textures avec fichier 3ds ! Venez voir pliz ! | |
WhitePoney | Voilà le bout de code où est peut-être le pb :
/////////////////////////// MAIN.CPP ///////////////////////// UINT gamesession_textures[30]; ///////////////////////////// GAMESESSION.CPP ///////////////////// void GameSession::init() { glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 4/3, 0.1f, 200.0f); } ///////////////////////////// 3DS.H ///////////////////////////////////////////: void load_textures(t3DModel *pModel) { // Go through all the materials for(int i = 0; i < pModel->numOfMaterials; i++) { // Check to see if there is a file name to load in this material if(strlen(pModel->pMaterials[i].strFile) > 0) { CreateTexture(gamesession_textures, pModel->pMaterials[i].strFile, i); } // Set the texture ID for this material pModel->pMaterials[i].texureId = i; } // Here, we turn on a lighting and enable lighting. We don't need to // set anything else for lighting because we will just take the defaults. // We also want color, so we turn that on glEnable(GL_LIGHT0); // Turn on a light with defaults set glEnable(GL_LIGHTING); // Turn on lighting glEnable(GL_COLOR_MATERIAL); // Allow color } void CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID) { char ouca [20]; AUX_RGBImageRec *pBitmap = NULL; FILE *pFile = NULL; if(!strFileName) return; strcpy(ouca,"data/" ); strcat(ouca,strFileName); pFile = fopen(ouca,"r" ); if(pFile) { pBitmap = auxDIBImageLoad(ouca); } else { MessageBox(NULL, "Couldn't find a texture!", "Error!", MB_OK); exit(0); } glGenTextures(1, &textureArray[textureID]); glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glBindTexture(GL_TEXTURE_2D, textureArray[textureID]); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (pBitmap) { if (pBitmap->data) { free(pBitmap->data); } free(pBitmap); } } void draw() { int i; for( i = 0; i < theobject->numOfObjects; i++) { if(theobject->pObject.size() <= 0) break; t3DObject *pObject = &theobject->pObject[i]; if(pObject->bHasTexture) { // Turn on texture mapping and turn off color glEnable(GL_TEXTURE_2D); // Reset the color to normal again glColor3ub(255, 255, 255); // Bind the texture map to the object by it's materialID glBindTexture(GL_TEXTURE_2D, gamesession_textures[pObject->materialID]); } else { // Turn off texture mapping and turn on color glDisable(GL_TEXTURE_2D); // Reset the color to normal again glColor3ub(255, 255, 255); } // This determines if we are in wireframe or normal mode glBegin(GL_TRIANGLES); // Begin drawing with our selected mode (triangles or lines) // Go through all of the faces (polygons) of the object and draw them for(int j = 0; j < pObject->numOfFaces; j++) { // Go through each corner of the triangle and draw it. for(int whichVertex = 0; whichVertex < 3; whichVertex++) { // Get the index for each point of the face int index = pObject->pFaces[j].vertIndex[whichVertex]; // Give OpenGL the normal for this vertex. glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z); // If the object has a texture associated with it, give it a texture coordinate. if(pObject->bHasTexture) { // Make sure there was a UVW map applied to the object or else it won't have tex coords. if(pObject->pTexVerts) { glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y); } } else { // Make sure there is a valid material/color assigned to this object. // You should always at least assign a material color to an object, // but just in case we want to check the size of the material list. // if the size is at least one, then we have a valid material. if(theobject->pMaterials.size() < pObject->materialID) { // Get and set the color that the object is, since it must not have a texture BYTE *pColor = theobject->pMaterials[pObject->materialID].color; // Assign the current color to this model glColor3ub(pColor[0], pColor[1], pColor[2]); } } glVertex3f(pObject->pVerts[ index ].x , pObject->pVerts[ index ].y, pObject->pVerts[ index ].z); } } glEnd(); } } Voilà les bouts de code (très semblables à ceux du tutorial ^^) ; a mon avis, dans la boucle de la fonction load_textures() il ne faudrait pas utiliser i comme index mais plutôt un indice général qui ne reviens pas à 0 à chaque fois (j'ai essayé mais ça ne marche pas non plus :/ ). A noter que glGenTexture me renvoie 0 à chaque fois. J'ai essayé en n'utilisant pas cette fonction mais en mettant "à la main" un entier unique dans gamesession_textures mais ça ne marche pas non plus. Si mon objet à plusieurs matériaux, il faudrait que j'ajoute une variable dans la structure t3DObject qui indique où commencer à chercher les matériaux ? Mais comment savoir quelle texture utiliser à quel moment, si mon objet en à plusieurs ? Et comment savoir comment les "épingler" ? Voilà, j'attends vos expliquations :hello: ! |
Vue Rapide de la discussion |
---|