不带光C ++的OpenGL-平面着色

时间:2013-04-10 13:35:12

标签: c++ opengl

我已经实现了一个网格。它可以用线框渲染(三角形),但现在我想应用平面着色渲染,我得到的是没有体积的图,纯白色。

这是我使用的:

for(int i=0; i<mesh->nt; i++) {
    glShadeModel(GL_FLAT);
    glBegin(GL_TRIANGLES);


    int i0 = mesh->triangles[i].vInds[0];
    int i1 = mesh->triangles[i].vInds[1];
    int i2 = mesh->triangles[i].vInds[2];

    Vector n1 = GetNormal(mesh->vertices[i0], mesh->vertices[i1], mesh-                  >vertices[i2]);

    glNormal3dv((double *)&n1);

    glVertex3dv((double *)&mesh->vertices[i0]);
    glVertex3dv((double *)&mesh->vertices[i1]);
    glVertex3dv((double *)&mesh->vertices[i2]);

    glEnd();
}   

我计算了三角形的法线但没有任何效果。有什么想法吗?

2 个答案:

答案 0 :(得分:6)

如果您没有启用灯光/灯光,则三角形不会被加阴影。就这么简单。

答案 1 :(得分:1)

也许那更好?

glEnable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glBegin(GL_TRIANGLES); // <-- start triangles before the loop, much faster

for(int i=0; i<mesh->nt; i++) 
{
    int i0 = mesh->triangles[i].vInds[0];
    int i1 = mesh->triangles[i].vInds[1];
    int i2 = mesh->triangles[i].vInds[2];

    Vector n1 = GetNormal(mesh->vertices[i0], 
                          mesh->vertices[i1], 
                          mesh->vertices[i2]);

    glNormal3dv((double *)&n1);

    glVertex3dv((double *)&mesh->vertices[i0]);
    glVertex3dv((double *)&mesh->vertices[i1]);
    glVertex3dv((double *)&mesh->vertices[i2]);
}   
glEnd();