OpenGL对象渲染顺序

时间:2015-04-15 23:30:27

标签: c++ opengl

问题:对象无法正确呈现。

我尝试做的事:搜索Stack Overflow。阅读我需要启用Depth_Test并执行此操作。仍然不起作用。读取我可能需要glDepthFunc(GL_LEQUAL);,不起作用。我写reshape / initscene / myDisplay的方式是否会导致这些常见方法不起作用?

以下代码。

void reshape (int w, int h)
{
    viewport.w = w;
    viewport.h = h;
    glViewport(0,0,viewport.w,viewport.h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h) {
        glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
    }
    else {
        glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void myDisplay() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the color buffer
    glMatrixMode(GL_MODELVIEW);   // indicate we are specifying camera transformations
    glLoadIdentity();             // make sure transformation is "zero'd"
    glScalef(zoom, zoom, zoom);
    gluLookAt(0.0, -9.0, 0.0,
      0.0, 0.0, 0.0,
      0.0, 0.0, 1.0);
    glRotatef(y_rotation, 0.0, 1.0, 0.0);
    glRotatef(x_rotation, 1.0, 0.0, 0.0);
    renderObjects();
    glFlush();
    glutSwapBuffers();                  // swap buffers (we earlier set double buffer)
}

void initScene(){

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    reshape(viewport.w, viewport.h);

    glShadeModel(GL_FLAT);
    glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

    GLfloat red[] = {1.0f, 0.2f, 0.2f, 1.0f};
    GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat gray[] = {0.5f, 0.5f, 0.5f, 1.0f};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, gray);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gray);

    GLfloat lightPos[] = {0.0f, 0.0f, -10.0f, 1.0f}; 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);    
    glEnable(GL_NORMALIZE);
}

0 个答案:

没有答案