2015-10-26 2 views
1

Перемещение с Visual Studio на Xcode для кода в OpenGL и C++. Я столкнулся с проблемой.OpenGL & Xcode Color Issue

Попытки нарисовать простой куб в Xcode есть проблема (возможно, с буфером глубины), что означает отрисовку кубы р

Я создал функцию, которая рисует куб панели с использованием квадрациклов. Мой код запускается и в 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 работать. OS не требуется предоставлять вам буфер глубины, если вы не запрашиваете его.

Если вы капитальная N Нужна буфер глубины убедитесь в явном виде запросить:

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

Кажется так сейчас очевидно. Спасибо. – user202051