Faces missing when drawing icosahedron in OpenGL following code in redBook

独自空忆成欢 提交于 2019-12-01 14:31:01
  1. missing faces

    most likely you just have wrong order of indices. In such case Reversing them will solve the issue. To check this you can try:

    glDisable(GL_CULL_FACE);
    

    if problem disappears I am right. If not it is different thing (like too close to camera cutting by Z_NEAR but that would look a bit different).

    To identify the correct face you can use glColor based on i for exaple

    if (i==5) glColor3f(1.0,0.0,0.0); else glColor3f(1.0,1.0,1.0);
    

    the red face would be the 6th in this case {8,3,10}

  2. lighting

    You are using vertex coordinates as normals so do not expect FLAT shading. Also I do not see you are setting any lights here (but that can be hidden in GLUT somewhere I do not use it). To remedy this use just single normal per triangle. so average the 3 normals you got and make an unit vector from that and use that (before first glVertex call of each triangle).

  3. orientation

    just rotate your GL_MODELVIEW to desired orientation. Standard perspective GL_PROJECTION has z axis as viewing direction and x,y axises matches the screen (while GL_MODELVIEW is unit)

[Edit1] I tried your code

So the problem is you got reverse order of indices then default polygon winding in OpenGL (at least in my environment) and wrong normals here fixed code:

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
const GLfloat vdata[12][3] =
    {
    {-X,0.0,Z}, {X,0.0,Z}, {-X,0.0,-Z}, {X,0.0,-Z},
    {0.0,Z,X}, {0.0,Z,-X}, {0.0,-Z,X}, {0.0,-Z,-X},
    {Z,X,0.0}, {-Z,X,0.0}, {Z,-X,0.0}, {-Z,-X,0.0},
    };

const GLuint tindices[20][3] =
    {
    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
    };

int i;
GLfloat nx,ny,nz;

glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glBegin(GL_TRIANGLES);
for (i = 0; i < 20; i++)
    {
    nx =vdata[tindices[i][0]][0];
    ny =vdata[tindices[i][0]][1];
    nz =vdata[tindices[i][0]][2];
    nx+=vdata[tindices[i][1]][0];
    ny+=vdata[tindices[i][1]][1];
    nz+=vdata[tindices[i][1]][2];
    nx+=vdata[tindices[i][2]][0]; nx/=3.0;
    ny+=vdata[tindices[i][2]][1]; ny/=3.0;
    nz+=vdata[tindices[i][2]][2]; nz/=3.0;
    glNormal3f(nx,ny,nz);
    glVertex3fv(vdata[tindices[i][0]]);
    glVertex3fv(vdata[tindices[i][1]]);
    glVertex3fv(vdata[tindices[i][2]]);
    }
glEnd();

And preview:

it is a screenshot and my object is rotating so do not expect correct orientation you expect.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!