c++ - OpenGL 和 Xcode 颜色问题

标签 c++ xcode opengl colors depth-buffer

从 Visual Studio 转移到 Xcode,再到使用 OpenGL 和 C++ 编写代码时,我遇到了一个问题。

尝试在 Xcode 中绘制一个简单的立方体有一个问题(可能与深度缓冲区有关)这意味着立方体的渲染是 p

我创建了一个使用四边形绘制立方体面板的函数。我的代码运行并在 Visual Studio 上正确显示立方体。然而,在 Xcode 中,它似乎优先考虑最新绘制的四边形,以便立方体的面板看起来在它后面的面板前面。附件是我在 Xcode 中获得的显示示例。还有,我写的代码。

我想问的是,以前有没有人遇到过这个问题?此外,是否有人知道导致此问题的原因以及是否有解决方案?

#include <stdlib.h>
#include <OpenGL/gl.h> 
#include <OpenGL/glu.h>
#include <GLUT/GLUT.h>
#include <math.h>

char rendermode; // global variable for current rendering mode

/*
 * Scene initialisation
 */
void InitGL(GLvoid)
{
    glShadeModel(GL_SMOOTH);                        // Enable smooth shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);           // Black background
    glClearDepth(1.0f);                             // Depth buffer setup
    glEnable(GL_DEPTH_TEST);                        // Enables depth testing

    glDepthFunc(GL_LEQUAL);                         // The type of depth testing to do
    glEnable(GL_COLOR_MATERIAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void drawCube(float size){
    glBegin(GL_QUADS);
    //Front Panel (Black)
    glColor3f(0.0, 0.0, 0.0);
    glVertex3f(size, size, size);
    glVertex3f(-size, size, size);
    glVertex3f(-size, -size, size);
    glVertex3f(size, -size, size);

    //Back Panel (Blue)
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(size, size, -size);
    glVertex3f(-size, size, -size);
    glVertex3f(-size, -size, -size);
    glVertex3f(size, -size, -size);

    //Left Panel (Yellow)
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(-size, size, size);
    glVertex3f(-size, -size, size);
    glVertex3f(-size, -size, -size);
    glVertex3f(-size, size, -size);

    //Right Panel (Green)
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(size, size, size);
    glVertex3f(size, -size, size);
    glVertex3f(size, -size, -size);
    glVertex3f(size, size, -size);

    //Bottom Panel (Light Blue)
    glColor3f(0.0, 1.0, 1.0);
    glVertex3f(size, -size, size);
    glVertex3f(-size, -size, size);
    glVertex3f(-size, -size, -size);
    glVertex3f(size, -size, -size);

    //Top Panel (Pink)
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(size, size, size);
    glVertex3f(-size, size, size);
    glVertex3f(-size, size, -size);
    glVertex3f(size, size, -size);

    glEnd();
}

void idle (void)
{
    glutPostRedisplay();   // trigger display callback
}

void display (void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    // set the camera
    gluLookAt(5.0f, 5.0f, 10.0f,
              0.0f, 0.0f, 0.0f,
              0.0f, 1.0f, 0.0f);

    //TO DO: Draw a cube rather than a square

    // different render mode
    switch ( rendermode ) {

        case 'f': // to display faces
            drawCube(2);
            break;

        case 'v': // to display points
            glBegin(GL_POINTS);
            glColor3f(0.0f,1.0f,0.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();

            glPointSize(5);
            break;

        case 'e': // to display edges
            glBegin(GL_LINES);
            glColor3f(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);

            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();
            break;
    }



    glutSwapBuffers();
}

/*
 * The reshape function sets up the viewport and projection
 */
void reshape ( int width, int height )
{
    // Prevent a divide by zero error by making height equal 1
    if (height==0)
    {
        height=1;
    }

    glViewport(0,0,width,height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Need to calculate the aspect ratio of the window for gluPerspective
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

    // Return to ModelView mode for future operations
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

/*
 * Callback for standard keyboard presses
 */
void keyboard ( unsigned char key, int x, int y )
{
    switch(key) {
        // Exit the program when escape is pressed
        case 27:
            exit(0);
            break;

        // Switch render mode for v,e,f
        case 'v':
            rendermode='v';
            break;

        case 'e':
            rendermode='e';
            break;

        case 'f':
            rendermode='f';
            break;

        default:
            break;
    }

    glutPostRedisplay();
}



// Arrow keys need to be handled in a separate function from other keyboard presses
void arrow_keys ( int a_keys, int x, int y )
{
    switch ( a_keys ) {
        case GLUT_KEY_UP:
            glutFullScreen();
            break;
        case GLUT_KEY_DOWN:
            glutReshapeWindow(500, 500);
            break;
        default:
            break;
    }
}

void mouseButton(int button, int state, int x, int y)
{
}

void mouseMove(int x, int y)
{
}




/*
 * Entry point to the application
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutCreateWindow("CW1 OpenGL Framework");
//    glutFullScreen();          // Uncomment to start in full screen
    InitGL();
    rendermode='f';

    // Callback functions
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(arrow_keys);  // For special keys
    glutMouseFunc(mouseButton);
    glutMotionFunc(mouseMove);
    glutIdleFunc(idle);

    glutMainLoop();
}

enter image description here

最佳答案

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
...
glEnable(GL_DEPTH_TEST);

GL_DEPTH_TEST 需要一个深度缓冲区才能工作。如果您不要求,则操作系统不需要为您提供深度缓冲区。

如果您使用大写字母 N 需要深度缓冲区,请确保明确请求一个:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
                                              ^^^^^^^^^^

关于c++ - OpenGL 和 Xcode 颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353685/

相关文章:

c++ - 升级到1.2.162.1后 : vkQueueWaitIdle == VK_ERROR_DEVICE_LOST

c++ - 随机未在范围内声明

c++ - OpenGL - GL_LINE_STRIP 的行为类似于 GL_LINE_LOOP

xcode - 找到 X 轴的中间以放置标签 Swift Xcode 6

c++ - OpenGL,纹理输出为纯色

c++ - 访问 vector 的动态 vector

xcode - 为什么我不能让我的类方法定义为索引包含变量的实例数组?

c - 使用 Xcode 在 Mac 上的 C 中 fputs 崩溃

c++ - 使用 GLFW 列出可用的视频模式

opengl - 在 OpenGL 的顶点着色器中获取元素 ID