OpenGL & Xcode 颜色问题
OpenGL & Xcode Colour Issue
从 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();
}
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
...
glEnable(GL_DEPTH_TEST);
GL_DEPTH_TEST
需要一个深度缓冲区才能工作。如果您不要求,OS 不需要为您提供深度缓冲区。
如果您大写-N 需要深度缓冲区,请确保明确请求一个:
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
^^^^^^^^^^
从 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();
}
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
...
glEnable(GL_DEPTH_TEST);
GL_DEPTH_TEST
需要一个深度缓冲区才能工作。如果您不要求,OS 不需要为您提供深度缓冲区。
如果您大写-N 需要深度缓冲区,请确保明确请求一个:
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
^^^^^^^^^^