I’m trying to map a jpg image to my object file but my program crashes inside my glmDraw function:
glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL );
at this line exactly:
if (mode & GLM_TEXTURE)
glTexCoord2fv(&model->texcoords[2 * triangle->tindices[0]]);
I’m not sure why, all I know is that it has to do with my texture. It crashes at the first execution of this line, by that I mean the first iteration of the while(group) loop.
This is how my mtl file looks like:
newmtl mymtl
Ns -----
Ni -----
d -----
Tr -----
Tf ----- ----- -----
illum -----
Ka ----- ----- -----
Kd ----- ----- -----
Ks ----- ----- -----
Ke ----- ----- -----
map_Ka image.jpg
map_Kd image.jpg
//**Note: ----- represents a number
Here is the glmDraw function as a whole if someone wants to look at it:
GLvoid
glmDraw(GLMmodel* model, GLuint mode)
{
static GLuint i;
static GLMgroup* group;
static GLMtriangle* triangle;
static GLMmaterial* material;
assert(model);
assert(model->vertices);
/* do a bit of warning */
if (mode & GLM_FLAT && !model->facetnorms) {
printf("glmDraw() warning: flat render mode requested "
"with no facet normals defined.\n");
mode &= ~GLM_FLAT;
}
if (mode & GLM_SMOOTH && !model->normals) {
printf("glmDraw() warning: smooth render mode requested "
"with no normals defined.\n");
mode &= ~GLM_SMOOTH;
}
if (mode & GLM_TEXTURE && !model->texcoords) {
printf("glmDraw() warning: texture render mode requested "
"with no texture coordinates defined.\n");
mode &= ~GLM_TEXTURE;
}
if (mode & GLM_FLAT && mode & GLM_SMOOTH) {
printf("glmDraw() warning: flat render mode requested "
"and smooth render mode requested (using smooth).\n");
mode &= ~GLM_FLAT;
}
if (mode & GLM_COLOR && !model->materials) {
printf("glmDraw() warning: color render mode requested "
"with no materials defined.\n");
mode &= ~GLM_COLOR;
}
if (mode & GLM_MATERIAL && !model->materials) {
printf("glmDraw() warning: material render mode requested "
"with no materials defined.\n");
mode &= ~GLM_MATERIAL;
}
if (mode & GLM_COLOR && mode & GLM_MATERIAL) {
printf("glmDraw() warning: color and material render mode requested "
"using only material mode.\n");
mode &= ~GLM_COLOR;
}
if (mode & GLM_COLOR)
glEnable(GL_COLOR_MATERIAL);
else if (mode & GLM_MATERIAL)
glDisable(GL_COLOR_MATERIAL);
/* perhaps this loop should be unrolled into material, color, flat,
smooth, etc. loops? since most cpu's have good branch prediction
schemes (and these branches will always go one way), probably
wouldn't gain too much? */
group = model->groups;
while (group) {
if (mode & GLM_MATERIAL) {
material = &model->materials[group->material];
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material->ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material->diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material->specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material->shininess);
}
if (mode & GLM_COLOR) {
glColor3fv(material->diffuse);
}
glBegin(GL_TRIANGLES);
for (i = 0; i < group->numtriangles; i++) {
triangle = &T(group->triangles[i]);
if (mode & GLM_FLAT)
glNormal3fv(&model->facetnorms[3 * triangle->findex]);
if (mode & GLM_SMOOTH)
glNormal3fv(&model->normals[3 * triangle->nindices[0]]);
if (mode & GLM_TEXTURE)
glTexCoord2fv(&model->texcoords[2 * triangle->tindices[0]]);
glVertex3fv(&model->vertices[3 * triangle->vindices[0]]);
if (mode & GLM_SMOOTH)
glNormal3fv(&model->normals[3 * triangle->nindices[1]]);
if (mode & GLM_TEXTURE)
glTexCoord2fv(&model->texcoords[2 * triangle->tindices[1]]);
glVertex3fv(&model->vertices[3 * triangle->vindices[1]]);
if (mode & GLM_SMOOTH)
glNormal3fv(&model->normals[3 * triangle->nindices[2]]);
if (mode & GLM_TEXTURE)
glTexCoord2fv(&model->texcoords[2 * triangle->tindices[2]]);
glVertex3fv(&model->vertices[3 * triangle->vindices[2]]);
}
glEnd();
group = group->next;
}
}
When I debug the error I get is:
Unhandled exception at 0x695d1789 in programname.exe: 0xC0000005: Access violation reading location 0x75d46ea8.
Also, if I change:
glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL );
to:
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL );
It works, but of course, without my textures.
Does GLM support this kind of material that has texture mapping? And is it okay if I’m using .jpg files for the texture mapping?
If not, then can you tell me how? Or what type of images does GLM support? Or if there is any other loader that supports images?
Your help is much appreciated.
-W
GLM does not support textures that are images. There’s an enhanced version of GLM that does, but it is not very common and even though it is platform independent, it needs a lot of work to function on windows properly since it was initially designed for Linux.
Here is the link.
This is Nate Robin’s original GLM with a few tweeks mentioned in the link provided as well. I consider it less easier to understand but it has more functions and warnings and such.