3D 立方体绘图,只有一侧始终在最上面

3D Cube Drawing, Only One Side Is Always On Top

我正在使用 OpenGL C++ 绘制形状,特别是立方体。在我当前的项目中,我设法正确地绘制了一个立方体,但是只有最后绘制的立方体的一侧在相机直接位于其上时不会透明。从正面看时,立方体的前两侧完全透​​明。有没有办法来解决这个问题?这里是picture。如您所见,前两侧没有显示。这是我的代码:

Main.cpp:

#include "Render.h"
#include <stdlib.h>

int screenHeight = 500;
int screenWidth = 500;
int screenFPS = 60;

void MainLoop(int val);

int main(int argc, char* args[])
{
    glutInit(&argc, args);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(350, 80);
    glutCreateWindow("Cube");
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho(0.0f, screenWidth, screenHeight, 0.0f, 0.0f, 1.0f);
    gluPerspective(40, 1, 0.5, 20);
    glutDisplayFunc(Render);
    glViewport(0, 0, screenWidth, screenHeight);
    glutKeyboardFunc(HandleKeys);
    glutIdleFunc(Animation);
    glutTimerFunc(1000 / screenFPS, MainLoop, 0);
    glutMainLoop();
    return 0;
}

void MainLoop(int val)
{
    Render();
    glutTimerFunc( 1000 / screenFPS, MainLoop, val );
}

Render.cpp:

#include "Render.h"
#include <iostream>
#include <stdlib.h>
#include <windows.h>

GLfloat xRot, yRot, zRot;

void Render()
{
    std::cout << xRot << " " << yRot << " " << zRot << "\n";
    glMatrixMode(GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-10.5);
    glRotatef(yRot, 1.0, 0.0, 0.0);
    glRotatef(yRot, 0.0, 1.0, 0.0);
    glRotatef(zRot, 0.0, 0.0, 1.0);
    glBegin(GL_QUADS);
       glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
        glVertex3f( 1.0f, 1.0f,-1.0f);
        glVertex3f(-1.0f, 1.0f,-1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);
       glColor4f(1.0f, 0.5f, 0.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glVertex3f( 1.0f,-1.0f,-1.0f);
       glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
       glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f,-1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glVertex3f(-1.0f, 1.0f,-1.0f);
        glVertex3f( 1.0f, 1.0f,-1.0f);
       glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f,-1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
       glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
        glVertex3f( 1.0f, 1.0f,-1.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f,-1.0f);
    glEnd();
    glutSwapBuffers();
}
void Animation()
{
    yRot += 0.03;
    xRot += 0.08;
    Render();
}
void HandleKeys(unsigned char key, int x, int y)
{
    if(key == 27)
        exit(0);
    else if(key == 'w')
        yRot += 0.55;
    else if(key == 'a')
        xRot -= 0.55;
    else if(key == 's')
        yRot -= 0.55;
    else if(key == 'd')
        xRot += 0.55;
}

Render.h

#include "GLLib.h"
extern int screenHeight;
extern int screenWidth;
extern int screenFPS;
void Render();
void Animation();
void HandleKeys(unsigned char key, int x, int y);

最后是我的图书馆,GLLib.h:

#ifndef GLLIB_H
#define GLLIB_H
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif

很可能所有的边都画得很好。问题是您没有深度缓冲区。因此,绘制的所有内容都会替换先前绘制的内容,无论它是在先前绘制的几何图形的前面还是后面。

要使用深度缓冲区,您必须在初始化期间请求它:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

并在开始渲染之前启用深度测试:

glEnable(GL_DEPTH_TEST);

然后,在开始渲染每一帧时,除了清除颜色缓冲区外,还需要清除深度缓冲区:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

这将确保最前面的脸可见,后面的脸隐藏,与绘制顺序无关。