使用glEnable(GL_DEPTH_TEST)时显示不正确。

5

我以下的程序必须显示一个带有简单光照的旋转立方体。

问题是,这个立方体在闪烁。当我撤销调用glEnable(GL_DEPTH_TEST)时,立方体不会闪烁,但我可以看到它内部的面(这是正常的,因为没有深度测试)。 然而,这个函数调用是必要的。 所以我不明白为什么这个函数的调用不能正常工作。

以下是我的代码:

#include <iostream>
#include <SDL/SDL.h>
#include <gl/glut.h>

const static int    WIDTH = 640;
const static int    HEIGHT = 480;

GLfloat angle = 0.0f;

static GLfloat  position[4] = {0.0, 50.0, -50.0, 1.0};
static GLfloat diffuse[3] = {0.64, 0.64, 0.64};
static GLfloat specular[3] = {0.64, 0.64, 0.64};
static GLfloat emissive[3] = {0.0, 0.0, 1.0};

static GLfloat vertices[72] =
{
    1.000000, -1.000000, -1.000000,     //V1
    1.000000, -1.000000, 1.000000,      //V2
    -1.000000, -1.000000, 1.000000,     //V3
    -1.000000, -1.000000, -1.000000,    //V4

    1.000000, 1.000000, -0.999999,      //V5
    -1.000000, 1.000000, -1.000000,     //V8
    -1.000000, 1.000000, 1.000000,      //V7
    0.999999, 1.000000, 1.000001,       //V6

    1.000000, -1.000000, -1.000000,     //V1
    1.000000, 1.000000, -0.999999,      //V5
    0.999999, 1.000000, 1.000001,       //V6
    1.000000, -1.000000, 1.000000,      //V2

    1.000000, -1.000000, 1.000000,      //V2
    0.999999, 1.000000, 1.000001,       //V6
    -1.000000, 1.000000, 1.000000,      //V7
    -1.000000, -1.000000, 1.000000,     //V3

    -1.000000, -1.000000, 1.000000,     //V3
    -1.000000, 1.000000, 1.000000,      //V7
    -1.000000, 1.000000, -1.000000,     //V8
    -1.000000, -1.000000, -1.000000,    //V4

    1.000000, 1.000000, -0.999999,      //V5
    1.000000, -1.000000, -1.000000,     //V1
    -1.000000, -1.000000, -1.000000,    //V4
    -1.000000, 1.000000, -1.000000      //V8
};

static GLfloat normals[72] =
{
    0.000000, -1.000000, 0.000000,
    0.000000, -1.000000, 0.000000,
    0.000000, -1.000000, 0.000000,
    0.000000, -1.000000, 0.000000,

    0.000000, 1.000000, 0.000000,
    0.000000, 1.000000, 0.000000,
    0.000000, 1.000000, 0.000000,
    0.000000, 1.000000, 0.000000,

    1.000000, 0.000000, 0.000000,
    1.000000, 0.000000, 0.000000,
    1.000000, 0.000000, 0.000000,
    1.000000, 0.000000, 0.000000,

    -0.000000, -0.000000, 1.000000,
    -0.000000, -0.000000, 1.000000,
    -0.000000, -0.000000, 1.000000,
    -0.000000, -0.000000, 1.000000,

    -1.000000, -0.000000, -0.000000,
    -1.000000, -0.000000, -0.000000,
    -1.000000, -0.000000, -0.000000,
    -1.000000, -0.000000, -0.000000,

    0.000000, -0.000000, -1.000000,
    0.000000, -0.000000, -1.000000,
    0.000000, -0.000000, -1.000000,
    0.000000, -0.000000, -1.000000
};

static void     eventListener(SDL_Event *pEvent, bool *pContinue)
{
    while (SDL_PollEvent(pEvent))
    {
        switch(pEvent->type)
        {
        case SDL_QUIT:
            *pContinue = false;
            break;
        case SDL_KEYDOWN:
            switch (pEvent->key.keysym.sym)
            {
            case SDLK_ESCAPE:
                *pContinue = false;
                break;
            }
        }
    }
}

static void     beginRender(void)
{
    /*Perspective*/

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, WIDTH, HEIGHT);
    gluPerspective(60.0, (float)(WIDTH/HEIGHT), 0.0f, 1000.0f);
    gluLookAt(0.0f, 0.0, 7.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    /*Clear screen*/

    glClearDepth(1.0f);
    glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /*Prepare model transformations*/

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    /*Light settings*/

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    /*Depth test*/

    glEnable(GL_DEPTH_TEST);  //PROBLEM COMES FROM HERE
}

static void     renderFrame(void)
{
    /*light position*/

    glLightfv(GL_LIGHT0, GL_POSITION, position);

    /*Model transformations*/

    glPushMatrix();
    glRotatef(angle, 0.0f, 1.0f, 1.0f);

    /*Light materials*/

    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
    glMaterialfv(GL_FRONT, GL_EMISSION, emissive);
    glMateriali(GL_FRONT, GL_SHININESS, 10);

    /*Model rendering*/

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glNormalPointer(GL_FLOAT, 0, normals);

    glDrawArrays(GL_QUADS, 0, 24);

    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    angle += 0.010f;
    glPopMatrix();
}

static void     endRender(void)
{
    glFlush();
    SDL_GL_SwapBuffers();
}

static void     startRendering(void)
{
    bool        continuer = true;
    SDL_Event   event;

    while (continuer)
    {
        eventListener(&event, &continuer);
        beginRender();
        renderFrame();
        endRender();
    }
}

int             main(int ac, char **av)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Test luminosity",NULL);
    SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL | SDL_DOUBLEBUF);

    startRendering();
    SDL_Quit();
    return (0);
}

这里是使用glEnable(GL_DEPTH_TEST)的屏幕截图(面部会闪烁): enter image description here 没有使用该调用时,显示不会闪烁但深度测试也不起作用: enter image description here 我尝试了几种代码组合,但都没有成功。有人可以帮我吗?非常感谢您的帮助。
1个回答

8

您的近裁剪平面没有正确设置,因此Z缓冲不起作用。

gluPerspective(60.0, (float)(WIDTH/HEIGHT), 0.0f, 1000.0f);
                                            ^^^^

将其设置为合理的距离 - 尽可能远离您可以接受的距离。
此外,不要将相机方向(即视图变换)放入投影矩阵中...
最后,请注意,大部分此功能已过时,并且在更新版本的GL中不再可用。建议的方法是处理自己的矩阵计算,并使用可编程管道渲染几何体。

你好,非常感谢您的回答。关于矩阵和几何管理,我将使用自己的矩阵管理和着色器,就像您所说的那样。这是我的主要目标。谢谢一切。再见。 - user1364743

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接