I've got a quad, which is rendered by drawing two triangles (of course), modeled and unwrapped using Blender. I'm exporting to MD2 for the time being, and using BMPs for my textures (all PNG tutorials and examples seemed to be broken when it came to actually generating the texture. I kept getting garbage, broken textures).
I'm drawing my objects using glDrawElements, using GL_TRIANGLES (I'm not smart enough to deal with Triangle Strips). In order to do this, I'm taking the texture coordinates from the MD2 file and putting them into an array (an array the size of however many indices I have, of course). When debugging it, everything seems to be printing out right.
Code: Select all
Index: 1
UV: 1,1
Index: 2
UV: 0,0
Index: 3
UV: 1,0
Index: 0
UV: 0,1
Index: 2
UV: 0,0
Index: 1
UV: 1,1
The problem is, the UV mapping doesn't seem to be working properly. The two triangles act independent of each other when it comes to displaying the texture.
Does anyone have any suggestions on how to fix this? I've been fighting with texturing my models for, like, four days straight and I keep running into stupid problems like this.
Let me preface this next part by saying my code is a mess -- I'm just trying to get functionality working. The BMP loading is done by using a modified version of the ImageLoad() function from GLdc/samples/letabot01. The MD2 model loading is done through a huge function that's based off the code from this site. After I read in all of the data using the aforementioned code, I setup some additional arrays to handle the indices, texture coordinates, and vertices.
Code: Select all
mdl->num_indices = mdl->header.num_tris*3;
// Allocate space for our list of indices
mdl->indices = (GLshort*) malloc (sizeof(GLshort) * mdl->num_indices);
// Populate our list of indices
j = 0;
for (i = 0; i < mdl->header.num_tris;i++){
mdl->indices[j] = mdl->tris[i].vertex[0];
mdl->indices[j+1] = mdl->tris[i].vertex[1];
mdl->indices[j+2] = mdl->tris[i].vertex[2];
j+=3;
}
mdl->t_coords = (GLfloat*) malloc (sizeof(GLfloat) * mdl->num_indices * 2);
k = 0;
for (i = 0; i < mdl->header.num_tris;i++){
for (j = 0; j < 3; j++){
mdl->t_coords[k] = (GLfloat) mdl->texcoords[mdl->tris[i].st[j]].s / (GLfloat) mdl->header.skinwidth;
mdl->t_coords[k+1] = (GLfloat) mdl->texcoords[mdl->tris[i].st[j]].t / (GLfloat) mdl->header.skinheight;
k+=2;
}
}
// Allocate space for our list of vertices.
mdl->verts = (GLfloat*) malloc (sizeof(GLfloat)*mdl->header.num_vertices * 3);
Drawing is done here:
Code: Select all
void Screen::DrawObject(md2_model_t* mdl, const Vector3f* position, const Vector3f* rotation, const Vector3f* scale, const float& angle){
md2_frame_t* mFrame = & mdl->frames[0]; // Our current frame of animation.
short i, p;
// Adjust vertices for MD2's compression
p = 0;
for (i = 0; i < mdl->header.num_vertices; i++){
mdl->verts[p] = (mFrame->scale.x * (float)mFrame->verts[i].v[0]) + mFrame->translate.x;
mdl->verts[p+1] = (mFrame->scale.y * (float)mFrame->verts[i].v[1]) + mFrame->translate.y;
mdl->verts[p+2] = (mFrame->scale.z * (float)mFrame->verts[i].v[2]) + mFrame->translate.z;
p+=3;
}
// glLoadIdentity() is called at the start of the drawing loop that calls this function.
// We're pushing that, along with our camera modifications, to the matrix stack, and
// popping it at the end so we don't have to continuously calculate the camera.
glPushMatrix();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
glTranslatef(position->x,position->y,position->z); // The object's position
glRotatef(angle, rotation->x, rotation->y, rotation->z); // The object's rotation
glScalef(scale->x, scale->y, scale->z); // The object's scale
glBindTexture(GL_TEXTURE_2D,mdl->tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexCoordPointer(2,GL_FLOAT,0,mdl->t_coords);
glVertexPointer(3,GL_FLOAT,0,mdl->verts);
glDrawElements(GL_TRIANGLES, mdl->num_indices,GL_SHORT,mdl->indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);
mFrame = NULL;
glPopMatrix();
}
Again, I'm not looking for optimization -- I'm sure most of this is terrible (really wanted to get rid of the nested for-loop but I just needed to get something working for now). I'm just looking for any reason why my object isn't displaying the textures properly.