将其加载到项目后,模型的形状是不同的

时间:2013-12-04 19:30:01

标签: opengl glut

我有Barrack.objBarrack.mtl文件对象。还有几个.tga个文件。 我想将此对象加载到我的opengl项目中。 为此,我解析了.obj.mtl个文件。然后绘制模型并设置这样的材料(模型对象中的方法draw

for(int g = 0; g < faces.size(); g++)
{
    glMaterialfv(GL_FRONT, GL_SPECULAR, materials[g].Ks);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, materials[g].Kd);
    glMaterialfv(GL_FRONT, GL_AMBIENT, materials[g].Ka);
    glMaterialf(GL_FRONT, GL_SHININESS, materials[g].Ns);

    glBegin(GL_TRIANGLES);

    for(int f = 0; f < faces[g].size(); f++)
    {
        glNormal3f(normals[faces[g][f].vn1 - 1].x, normals[faces[g][f].vn1 - 1].y, normals[faces[g][f].vn1 - 1].z);
        glVertex3f(vertices[faces[g][f].v1 - 1].x, vertices[faces[g][f].v1 - 1].y, vertices[faces[g][f].v1 - 1].z);

        glNormal3f(normals[faces[g][f].vn2 - 1].x, normals[faces[g][f].vn2 - 1].y, normals[faces[g][f].vn2 - 1].z);
        glVertex3f(vertices[faces[g][f].v2 - 1].x, vertices[faces[g][f].v2 - 1].y, vertices[faces[g][f].v2 - 1].z);

        glNormal3f(normals[faces[g][f].vn3 - 1].x, normals[faces[g][f].vn3 - 1].y, normals[faces[g][f].vn3 - 1].z);
        glVertex3f(vertices[faces[g][f].v3 - 1].x, vertices[faces[g][f].v3 - 1].y, vertices[faces[g][f].v3 - 1].z);
    }

    glEnd();
} 

normalsmaterials等是包含.mtl.obj文件数据的数组。 ks, kd, ka, ns来自.mtl文件的相同值。 在调试器中,似乎所有数组(向量)都正确填充了文件中的数据。但物体在屋顶上有一些扼杀peaksenter image description here

我像这样初始化glut

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenWidth, screenHeight);
glutCreateWindow("LOADER");
reshape(screenWidth, screenHeight);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);

glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); 
glEnable(GL_DEPTH_TEST);

GLfloat light_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);

if(!model.load("Barrack.obj"))
{
    cout << "Could not load model" << endl;
    return false;
}

glutDisplayFunc(draw);

函数draw

void draw()
{
glPushMatrix();

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glCallList(g_persp);

gluLookAt(  0, 3, 3,    //  eye pos
            0, 0, 0,    //  aim point
            0, 1, 0);   //  up direction

glRotatef(angle, 0.0, 1.0, 0.0);
glColor4f(1.0, 1.0, 1.0, 1.0);
glScalef(0.1, 0.1, 0.1);
model.draw(); // method draw mentioned earlier

glCallList(g_ortho);

glPopMatrix();

glutSwapBuffers();
}

有什么问题?为什么屋顶上有山峰? 在assimpviewer模型看起来像这样 enter image description here

重塑功能

void reshape(int w, int h)
{
if(h == 0) 
{
    h = 1;
}

screenWidth = w;
screenHeight = h;

glDeleteLists(g_persp, 1);
glDeleteLists(g_ortho, 1);

glViewport(0, 0, w, h);

g_persp = glGenLists(1);
g_ortho = glGenLists(1);

glNewList(g_persp, GL_COMPILE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45, (float)w / h, 0.1, 100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

glEndList();

glNewList(g_ortho, GL_COMPILE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, w, h, 0, -100, 100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

glEndList();
}

1 个答案:

答案 0 :(得分:1)

这可能只是建筑多边形,用于模型设计者的一些计算。它们很可能在obj文件中被标记为不可见,如果您有权访问加载代码,则只需在加载模型时将其过滤掉。