2015-10-26 125 views
1

从Visual Studio转到Xcode以在OpenGL和C++中编写代码我碰到过一个问题。OpenGL和Xcode颜色问题

企图拉拢在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

回答

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

你还挺需要GL_DEPTH_TEST工作的深度缓冲。如果你不需要一个操作系统,操作系统不需要给你一个深度缓冲区。

如果资本-N需要一个深度缓冲确保明确要求之一:

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

现在看来那么明显。谢谢。 – user202051