   Texture mapping problem.
  HiI've followed NeHe's lesson 31, but using SDL. The model loads and displays fine except for the texture, it's just white. I've gone through my code a million times and can't seem to find anything. Can anyone please just look at my code and see if you find anything? Thanks.Code is taken from NeHe's lesson 31 and gpwiki.Content of load_model_data(...): // Filesize in bytes.    long fileSize;    ifstream inputFile(filename, ios::in | ios::binary);    if(inputFile.fail())    {   return(false);    }    // Get the file size.    inputFile.seekg(0, ios::end);    fileSize = inputFile.tellg();    inputFile.seekg(0, ios::beg);    // Read the file content into a temp buffer.    byte *pBuffer = new byte[fileSize];    inputFile.read(reinterpret_cast<char*>(pBuffer), fileSize);    inputFile.close();    // Create a pointer to point to a location in the buffer.    const byte *pPtr = pBuffer;    MS3DHeader *pHeader = (MS3DHeader*)pPtr;   // Get the header information.    pPtr += sizeof(MS3DHeader);  // Move along.    if(strncmp(pHeader->m_ID, "MS3D000000", 10) != 0)    {   return(false); // Not a valid Milkshape 3D file.    }    if(pHeader->m_version < 3 || pHeader->m_version > 4)    {   return(false); // Wrong version.    }    int nVertices = *(word*)pPtr;    m_numVertices = nVertices;    m_pVertices = new Vertex[nVertices];    pPtr += sizeof(word);    //printf("%i\n", pHeader->m_version);    printf("nVertices = %i\n", m_numVertices);    for(int i = 0; i < nVertices; i++)    {   MS3DVertex *pVertex = (MS3DVertex*)pPtr;   m_pVertices.m_boneID = pVertex->m_boneID;   memcpy(m_pVertices.m_location, pVertex->m_vertex, sizeof(float)*3);   pPtr += sizeof(MS3DVertex);    }   int nTriangles = *(word*)pPtr;    m_numTriangles = nTriangles;    m_pTriangles = new Triangle[nTriangles];    pPtr += sizeof(word);    for(int i = 0; i < nTriangles; i++)    {   MS3DTriangle *pTriangle = (MS3DTriangle*)pPtr;   int vertexIndices[3] = {pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2]};   float t[3] = {1.0f - pTriangle->m_t[0], 1.0f - pTriangle->m_t[1], 1.0f - pTriangle->m_t[2]};   memcpy(m_pTriangles.m_vertexNormals, pTriangle->m_vertexNormals, sizeof(float)*3*3);   memcpy(m_pTriangles.m_s, pTriangle->m_s, sizeof(float)*3);   memcpy(m_pTriangles.m_t, t, sizeof(float)*3);   memcpy(m_pTriangles.m_vertexIndices, vertexIndices, sizeof(int)*3);   pPtr += sizeof(MS3DTriangle);    }    int nGroups = *(word*)pPtr;    m_numMeshes = nGroups;    m_pMeshes = new Mesh[nGroups];    pPtr += sizeof(word);    for(int i = 0; i < nGroups; i++)    {   pPtr += sizeof(byte); // Flags.   pPtr += 32; // Name.   word nTriangles = *(word*)pPtr;   pPtr += sizeof(word);   int *pTriangleIndices = new int[nTriangles];   for(int j = 0; j < nTriangles; j++)   {  pTriangleIndices[j] = *(word*)pPtr;  pPtr += sizeof(word);   }   char materialIndex = *(char*)pPtr;   pPtr += sizeof(char);   m_pMeshes.m_materialIndex = materialIndex;   m_pMeshes.m_numTriangles = nTriangles;   m_pMeshes.m_pTriangleIndices = pTriangleIndices;    }    int nMaterials = *(word*)pPtr;    m_numMaterials = nMaterials;    m_pMaterials = new Material[nMaterials];    pPtr += sizeof(word);    for(int i = 0; i < nMaterials; i++)    {   MS3DMaterial *pMaterial = (MS3DMaterial*)pPtr;   memcpy(m_pMaterials.m_ambient, pMaterial->m_ambient, sizeof(float)*4);   memcpy(m_pMaterials.m_diffuse, pMaterial->m_diffuse, sizeof(float)*4);   memcpy(m_pMaterials.m_specular, pMaterial->m_specular, sizeof(float)*4);   memcpy(m_pMaterials.m_emissive, pMaterial->m_emissive, sizeof(float)*4);   m_pMaterials.m_shininess = pMaterial->m_shininess;   m_pMaterials.m_pTextureFilename = new char[strlen(pMaterial->m_texture + 1)];   strcpy(m_pMaterials.m_pTextureFilename, pMaterial->m_texture);   pPtr += sizeof(MS3DMaterial);   // printf("%s %s",m_pMaterials.m_pTextureFilename, "\n");    }    reload_textures();    delete[] pBuffer;    return(true);content of loadTexture(...):    GLuint texture = 0;    SDL_Surface *surface;    // SDL_Surface to temporarily hold the data.    GLenum texture_format;    GLint nColours;    // OpenGL can only work with textures which both dimenstions    // are a power of 2. So check.    surface = NULL;    printf("Goint to load the image...\n");    surface = SDL_LoadBMP(filename);    if(surface != NULL)    {   if((surface->w & (surface->w - 1)) != 0)   {  printf("Warning: image width is not a power of 2");   }   if((surface->h & (surface->h - 1)) != 0)   {  printf("Warning: image height is not a power of 2");   }   printf("bleh %i %i \n", surface->w, surface->h);   // Get the nuber of channels in the SDL surface.   nColours = surface->format->BytesPerPixel;   if(nColours == 4)// Contains an alpha channel.   {  if(surface->format->Rmask == 0x000000ff)  { texture_format = GL_RGBA;  }  else  { texture_format = GL_BGRA;  }   }   else if(nColours == 3)   {  if(surface->format->Rmask == 0x000000ff)  { texture_format == GL_RGB;  }  else  { texture_format == GL_BGR;  }   }   else   {  printf("Warning: The image isn't true colour.");   }   // Let OpenGL generate a texture object handle for us.   glGenTextures(1, &texture);   // Bind the texture object.   glBindTexture(GL_TEXTURE_2D, texture);   // Transfer the image data from the SDL surface to the texture.   glTexImage2D(GL_TEXTURE_2D, 0, nColours, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels); // Set the texture's stretching properties.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    }    else    {   printf("SDL could not load the image.");    }    // Free the surface if it was successfully created.    if(surface)    {   SDL_FreeSurface(surface);    }    return(texture);Content of draw():GLboolean texEnabled = glIsEnabled(GL_TEXTURE_2D);    for(int i = 0; i < m_numMeshes; i++)    {   int materialIndex = m_pMeshes.m_materialIndex;   if(materialIndex >= 0)   {  glMaterialfv(GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient);  glMaterialfv(GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse);  glMaterialfv(GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular);  glMaterialfv(GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive);  glMaterialf(GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess);  if (m_pMaterials[materialIndex].m_texture > 0)  { glBindTexture(GL_TEXTURE_2D, m_pMaterials[materialIndex].m_texture); glEnable(GL_TEXTURE_2D); //std::printf("%i \n", m_pMaterials[materialIndex].m_texture);  }  else  { glDisable(GL_TEXTURE_2D);  }   }   else   {  glDisable(GL_TEXTURE_2D);   } glBegin(GL_TRIANGLES);   {  for(int j = 0; j < m_pMeshes.m_numTriangles; j++)  { int triangleIndex = m_pMeshes.m_pTriangleIndices[j]; const Triangle* pTri = &m_pTriangles[triangleIndex]; for(int k = 0; k < 3; k++) {int index = pTri->m_vertexIndices[k];glNormal3fv(pTri->m_vertexNormals[k]);glTexCoord2f(pTri->m_s[k], pTri->m_t[k]);glVertex3fv(m_pVertices[index].m_location); }  }   }   glEnd();    }    if(texEnabled)    {   glEnable(GL_TEXTURE_2D);    }    else    {   glDisable(GL_TEXTURE_2D);    }
  You are doing too much at once. There's no way (for me) to be able to follow that spaghetti of glDisable(GL_TEXTURE_2D)/glEnable(GL_TEXTURE_2D)Could you drop that material-whatever and the disables altogether?So at least we'll have a clue that the texture is loaded correctly or not.BTW do you enable texturing anywhere before the draw-stuff you posted? Texturing is disabled by default.   ;  OK, I've commented out the glDisable's and the material functions. Still no change. I think I'll quickly write a small program to check if the image loading function works properly.I call glEnable(GL_TEXTURE_2D) once when initializing OpenGL.   ;  Yeah, maybe call glGetError() after the call to glTexImage2D()Also, I'd bind after enabling texturing in draw():glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, m_pMaterials[materialIndex].m_texture);;  OK, I get "invalid enumerant" after glTexImage2D().   ;  OK, I've figured it out. The problem is with this bit of code.nColours = surface->format->BytesPerPixel;   if(nColours == 4)// Contains an alpha channel.   {  if(surface->format->Rmask == 0x000000ff)  { texture_format = GL_RGBA; printf("GL_RGBA");  }  else  { texture_format = GL_BGRA; printf("GL_BGRA");  }   }   else if(nColours == 3)   {  if(surface->format->Rmask == 0x000000ff)  { texture_format == GL_RGB; printf("GL_RGB");  }  else  { texture_format == GL_BGR; printf("GL_BGR");  }   }   else   {  printf("Warning: The image isn't true colour.");   }The image colour format is RGB, for some reason it chooses GL_BGR...When I hard code GL_RGB right before glTexImage2D() the texture displays, but it looks blue, like the red component isn't showing.   ;  It's not what you think.format is always shown as RGB in Windows, but it's doesn't mean that the memory layout is rgb. It's more likely to be bgr.I hope that makes sense.So whatever the image returns as format, use that as internal format when creating the texture object. But that seems fine in your original code.This seems suspicious:if(surface->format->Rmask == 0x000000ff)I think it's should be eitherif(surface->format->Rmask & 0x000000ff == 0x000000ff)orif(surface->format->Rmask & 0x000000ff )more likely the latter (I'm pretty sure)   ;  Hi, thanks, but it's more stupid than that.When assigning the texture_format variable I used == instead of =, so the variable had junk in it. That's why I got the invalid enumerant error.   ;  Did you made the changes I suggested in my previous post?   ;  No, sorry, I just changed the operator. I works fine now.   