OpenGL - 如何使用mtl文件使用GLM或任何其他加载器将纹理映射到obj

时间:2011-12-26 08:26:36

标签: opengl textures

我正在尝试将jpg图像映射到我的目标文件,但我的程序在我的glmDraw函数内崩溃:

glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL );

完全在这一行:

if (mode & GLM_TEXTURE)
                glTexCoord2fv(&model->texcoords[2 * triangle->tindices[0]]);

我不知道为什么,我所知道的是它与我的纹理有关。它在第一次执行此行时崩溃,我的意思是while(group)循环的第一次迭代。 这就是我的mtl文件的样子:

newmtl mymtl
    Ns -----
    Ni -----
    d -----
    Tr -----
    Tf ----- ----- ----- 
    illum -----
    Ka ----- ----- -----
    Kd ----- ----- -----
    Ks ----- ----- -----
    Ke ----- ----- -----
    map_Ka image.jpg
    map_Kd image.jpg

//**Note: ----- represents a number

如果有人想看一下整个glmDraw函数:

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;
    }
}

当我调试错误时,我得到:

Unhandled exception at 0x695d1789 in programname.exe: 0xC0000005: Access violation reading location 0x75d46ea8.

另外,如果我改变:

glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL );

为:

glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL );

它有效,但当然没有我的纹理。

GLM是否支持这种具有纹理映射的材质?如果我使用.jpg文件进行纹理贴图,这样可以吗?

如果没有,那么你能告诉我怎么样吗?或者GLM支持哪种类型的图像?或者,如果有任何其他加载器支持图像?

非常感谢您的帮助。 -W

1 个答案:

答案 0 :(得分:1)

GLM不支持图像纹理。有一个增强版的GLM,虽然它不常见,即使它是独立于平台的,但由于它最初是为Linux设计的,因此需要大量工作才能在Windows上正常运行。

这是link

这是Nate Robin的原始GLM,在提供的链接中提到了一些tweek。我认为它不那么容易理解,但它有更多的功能和警告等等。