OpenGL GL_DEPTH_TEST无法正常工作 [英] OpenGL GL_DEPTH_TEST not working

查看:102
本文介绍了OpenGL GL_DEPTH_TEST无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将.obj加载程序移植到了Objective-C,到目前为止,它可以正常工作,我可以得到顶点,法线和其他东西. 每个法线都很好,指向正确的方向,我所有的脸都朝着CCW方向弯曲,但是深度测试存在一些问题.

I just ported a .obj loader to objective-C and so far, it works, I can get my vertices and normals and stuff. Every normal is good, pointing in the right direction, all my faces are in CCW winding, but I have some issues with the depth test.

float rotX = 0;
float rotY = 0;
objModel* o = [[objModel alloc] initWithPath:@"/model.obj"]

glClearColor(0,0,0,0);

glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glCullFace(GL_BACK); 
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();   
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, -1, 0);
glRotatef(90,0,0,1);
glRotatef(90,0,1,0);
glRotatef(rotX,0,0,-1);
glRotatef(rotY,0,1,0);

[o drawObjWithArrays]

glFlush();

我有2种不同的绘制对象的方式,一种使用glBegin()/glEnd(),另一种使用顶点和法线数组调用glDrawArrays().两者都会导致相同的问题:由于深度测试无法正常工作,因此会显示应该被其前面的面孔隐藏的面孔.这些面是按照它们在.obj文件中的顺序绘制的.

I have 2 different ways of drawing my object, one uses glBegin() / glEnd(), the other uses vertex and normal arrays with a call to glDrawArrays(). Both of them result in the same problem : faces that should be hidden by faces in front of them are displayed, because the depth test isn't working. The faces are drawn in the order they come in the .obj file.

您将在此处找到图片: http://img524.imageshack.us/img524/994/image2jgq.png

You'll find an image here : http://img524.imageshack.us/img524/994/image2jgq.png

我对OpenGL和Objective-C还是很陌生,所以我想我的问题来自我忘记的设置.他们在这里:

I quite new to OpenGL and objective-C, so I guess my problem comes from a setting that I forgot. Here they are :

-(id) initWithFrame:(NSRect) frame {
NSLog(@"INIT GL VIEW\n");

GLuint attributes[] = {
    NSOpenGLPFANoRecovery,
    NSOpenGLPFAWindow,
    NSOpenGLPFAAccelerated,
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAColorSize, 24,
    NSOpenGLPFAAlphaSize, 8,
    NSOpenGLPFADepthSize, 24,
    NSOpenGLPFAStencilSize, 8,
    NSOpenGLPFAAccumSize, 0,
    0
};

NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attributes];
if (!fmt)
    NSLog(@"No OpenGL pixel format");

GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
GLfloat   mat_flash_shiny[] = {50.0};
GLfloat   light_position[]  = {100.0,-200.0,-200.0,0.0};
GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};

/* set the material */
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);

glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);  
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);

return self = [super initWithFrame:frame pixelFormat:[fmt autorelease]]; 
}

任何人都可以帮助我吗?我已经尝试过所有人glDepthFunc,glCullFace,glFrontFace的组合,但没有用...

Anyone can help me ? I've tried everyone glDepthFunc, glCullFace, glFrontFace combination possible, nothing works...

谢谢^^

推荐答案

将其添加到draw方法中会得到什么?

what do you get when you add this to your draw method?

int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
NSLog(@"%i bits depth", depth)

还有更多尝试的方法:

  • 确保正在调用initWithFrame
  • 如果您正在使用IB的NSOpenGLView子类,请在IB中设置深度缓冲区,然后将所有openGL初始化都移至- (void)prepareOpenGL

这篇关于OpenGL GL_DEPTH_TEST无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆