似乎无法获得GLFW/OpenGL深度测试工作



我无法在任何项目中使用代码块使用GLFW进行深度测试,即使在启动新项目时使用代码块提供的默认模板,如下所示。

任何帮助都是非常感激的,因为我知道我一定是做错了什么,但我一直没能找到它。

#include <GL/glfw.h>
int main()
{
int     width, height;
int     frame = 0;
bool    running = true;
glfwInit();
if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
{
    glfwTerminate();
    return 0;
}
glfwSetWindowTitle("GLFW Application");
glEnable(GL_DEPTH_TEST);
while(running)
{
    frame++;
    glfwGetWindowSize( &width, &height );
    height = height > 0 ? height : 1;
    glViewport( 0, 0, width, height );
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );
    // Draw some rotating garbage
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt(0.0f, -10.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f );
    //glTranslatef( 1.0f, 1.0f, 0.0f );
    glRotatef(frame, 0.25f, 1.0f, 0.75f);
    glBegin( GL_TRIANGLES );
      glColor3f(0.1f, 0.0f, 0.0f );
      glVertex3f(0.0f, 3.0f, -4.0f);
      glColor3f(0.0f, 1.0f, 0.0f );
      glVertex3f(3.0f, -2.0f, -4.0f);
      glColor3f(0.0f, 0.0f, 1.0f );
      glVertex3f(-3.0f, -2.0f, -4.0f);
    glEnd();
    glBegin( GL_TRIANGLES );
      glColor3f(0.0f, 0.1f, 0.0f );
      glVertex3f(0.0f, 3.0f, -3.0f);
      glColor3f(0.0f, 0.0f, 1.0f );
      glVertex3f(3.0f, -2.0f, -2.0f);
      glColor3f(1.0f, 0.0f, 0.0f );
      glVertex3f(-3.0f, -2.0f, 2.0f);
    glEnd();
    glfwSwapBuffers();
    // exit if ESC was pressed or window was closed
    running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
}
glfwTerminate();
return 0;

}

来自GLFW2文档(强调我的):

int glfwOpenWindow
    (
    int width, int height,
    int redbits, int greenbits, int bluebits,
    int alphabits,
    int depthbits,
    int stencilbits,
    int mode
    )

depthbits:深度缓冲区使用的位数(0表示没有深度缓冲区)

和你的代码:

glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW )
                                      ^ no depth bits

你不能(有效地)使用没有任何深度位的深度缓冲区

最新更新