PDA

View Full Version : Texture mapping a jpg image, crashes.



Waleed
12-25-2011, 01:25 PM
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 = &amp;T(group->triangles[i]);

if (mode & GLM_FLAT)
glNormal3fv(&amp;model->facetnorms[3 * triangle->findex]);

if (mode & GLM_SMOOTH)
glNormal3fv(&amp;model->normals[3 * triangle->nindices[0]]);
if (mode & GLM_TEXTURE)
glTexCoord2fv(&amp;model->texcoords[2 * triangle->tindices[0]]);
glVertex3fv(&amp;model->vertices[3 * triangle->vindices[0]]);

if (mode & GLM_SMOOTH)
glNormal3fv(&amp;model->normals[3 * triangle->nindices[1]]);
if (mode & GLM_TEXTURE)
glTexCoord2fv(&amp;model->texcoords[2 * triangle->tindices[1]]);
glVertex3fv(&amp;model->vertices[3 * triangle->vindices[1]]);

if (mode & GLM_SMOOTH)
glNormal3fv(&amp;model->normals[3 * triangle->nindices[2]]);
if (mode & GLM_TEXTURE)
glTexCoord2fv(&amp;model->texcoords[2 * triangle->tindices[2]]);
glVertex3fv(&amp;model->vertices[3 * triangle->vindices[2]]);

}
glEnd();

group = group->next;
}
}

Waleed
12-25-2011, 01:55 PM
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.

awhig
12-29-2011, 12:28 PM
Are you sure you model->texcoords is valid?

Check inside your Model class whether texcoords has been assigned memory.