Hi
I'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);
}