Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Texture mapping a jpg image, crashes.

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    16

    Texture mapping a jpg image, crashes.

    I'm trying to map a jpg image to my object file but my program crashes inside my glmDraw function:
    Code :
    glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL );
    at this line exactly:
    Code :
    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:
    Code :
    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:
    Code :
    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;
        }
    }

  2. #2
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    16

    Re: Texture mapping a jpg image, crashes.

    When I debug the error I get is:
    Code :
    Unhandled exception at 0x695d1789 in programname.exe: 0xC0000005: Access violation reading location 0x75d46ea8.
    Also, if I change:
    Code :
    glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL );
    to:
    Code :
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL );
    It works, but of course, without my textures.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jun 2009
    Location
    FL , USA
    Posts
    194

    Re: Texture mapping a jpg image, crashes.

    Are you sure you model->texcoords is valid?

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •