I’m trying to load a md2 model using SDL for textures. I’ve read and seen this done before but all the tutorials and links are dead. So, I’ve made my own code.
Sadly, it doesn’t work. Because my problem is that my md2 model loads fine but it doesn’t texture! I thought I’ve set up everything right but I can’t figure it out?
I’ve narrowed it down to the line gluBuild2DMipmaps, which crashes the program if I leave it uncommented.
I’ve been trying to figure this out for a while now and I can’t put it off much longer (its hard to make a game enger without 3d models)! I’m posting because I know a pro opengl/sdl guru could take one look at this and know what’s going wrong.
Thanks for taking a look.
class CMD2Model
{
public:
etc. etc.
private:
modelHeader_t m_info;
GLuint finaltexture[1];
GLfloat texcoord[4];
SDL_Surface *md2image;
mesh_t *m_tris; // triangle list
texCoord_t *m_texCoords; // texture coordinate list
xyz_t *m_verts; // vertex list
xyz_t *m_currentVerts; // working vertex list
GLfloat m_miny;
xyz_t m_pos;
GLfloat m_rotate;
};
bool CMD2Model::Load(char *modelFile, char *skinFile, GLfloat scale)
{
// open the model file
etc. etc.
// texture the model
md2image = load_image(skinFile);
finaltexture[0] = SDL_GL_LoadTexture( md2image, texcoord );
glBindTexture(GL_TEXTURE_2D, finaltexture[0]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, md2image->w, md2image->h, GL_RGB, GL_UNSIGNED_BYTE, md2image->pixels);
SDL_FreeSurface( md2image );
m_currentFrame = 0;
m_nextFrame = 1;
m_interpol = 0.0;
// animate at least one frame to make sure that m_currentVerts gets initialized
SetAnimation(IDLE);
Animate(0);
Log("%s loaded succesfully!
", modelFile );
return TRUE;
}
void CMD2Model::Render()
{
glPushMatrix();
glTranslatef(m_pos.x, m_pos.y, m_pos.z);
glRotatef(m_rotate, 0, 1, 0);
glBindTexture(GL_TEXTURE_2D, finaltexture[0]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, m_currentVerts);
glTexCoordPointer(2, GL_FLOAT, 0, m_texCoords);
glDrawArrays(GL_TRIANGLES, 0, m_info.numTris * 3);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}