我今天开始学习OpenGL,这简直太棒了。但是,我无法终生根据深度而不是按顺序绘制对象,所以我希望有人可以告诉我我做错了什么。
这是我用来创建多维数据集的极其简单的代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Simple translation
glBegin(GL_QUADS);
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);
glColor3f(1.0f,0.5f,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);
glColor3f(1.0f,0.0f,0.0f);
//You get the point, continue with all sides
glEnd(); // End Drawing The Cube
SDL_GL_SwapBuffers();
这是设置代码:
if (SDL_Init(SDL_INIT_EVERYTHING)<0)
return -1;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
SDL_Surface* screen = SDL_SetVideoMode(screen_x,screen_y,32,SDL_HWSURFACE|SDL_GL_DOUBLEBUFFER|SDL_OPENGL);
if (screen == NULL)
return -2;
//glEnable(GL_DEPTH_TEST); //<-If this is uncommented look at figure 1
glDepthFunc(GL_LESS);
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, screen_x, screen_y);
glMatrixMode(GL_PROJECTION); //projection with ortho, model otherwise
glLoadIdentity();
gluPerspective(60.0,1.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
好吧,当我
glEnable(GL_DEPTH_TEST);
...
如果我不启用它
那我该怎么办?我肯定错过了什么?我该如何解决深度问题?
最佳答案
我找到了答案!
http://www.opengl.org/resources/faq/technical/depthbuffer.htm
这是我找到答案的地方,我的近帧恰好在0处,为了使深度缓冲区的精度生效,它必须稍微远离0。 (如果我没看错的话)
一切都很好。
关于c++ - OpenGL深度Spaz攻击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7458654/