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.
我刚刚将一个.obj加载器移植到objective-C,到目前为止,它可以工作,我可以得到我的顶点和法线和东西。每个法线都很好,指向正确的方向,我所有的脸都处于CCW缠绕状态,但是我在深度测试中遇到了一些问题。
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();
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.
我有2种不同的绘制对象的方法,一种使用glBegin()/ glEnd(),另一种使用顶点和普通数组,调用glDrawArrays()。它们都会导致同样的问题:显示应该被它们前面的面隐藏的面,因为深度测试不起作用。面按照它们在.obj文件中的顺序绘制。
You'll find an image here : http://img524.imageshack.us/img524/994/image2jgq.png
你会在这里找到一个图像:http://img524.imageshack.us/img524/994/image2jgq.png
I quite new to OpenGL and objective-C, so I guess my problem comes from a setting that I forgot. Here they are :
我对OpenGL和Objective-C很新,所以我猜我的问题来自于我忘记的设置。他们来了 :
-(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]];
}
Anyone can help me ? I've tried everyone glDepthFunc, glCullFace, glFrontFace combination possible, nothing works...
有人可以帮帮我吗?我已经尝试过每个人glDepthFunc,glCullFace,glFrontFace组合可能,没有任何作用......
Thanks ^^
4 个解决方案
#1
14
what do you get when you add this to your draw method?
将此添加到绘图方法后会得到什么?
int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
NSLog(@"%i bits depth", depth)
A few more things to try:
还有一些事情要尝试:
- make sure initWithFrame is being called
- if you're using an NSOpenGLView subclass from IB, set the depth buffer in IB, and move all your openGL initialization into
- (void)prepareOpenGL
确保调用initWithFrame
如果您正在使用IB的NSOpenGLView子类,请在IB中设置深度缓冲区,并将所有openGL初始化移动到 - (void)prepareOpenGL
#2
8
In case someone wonders how to set this without IB:
如果有人想知道如何在没有IB的情况下设置它:
NSOpenGLPixelFormatAttribute attrs[] = {
// NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 32,
0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:frame pixelFormat:format];
#3
1
Make sure you call glDepthFunc()
to set the depth buffer comparison function. Most applications use GL_LEQUAL
or GL_LESS
for the depth function. Also make sure you call glClearDepth()
to set the value that the depth buffer gets cleared to; you should probably use a parameter of 1.0
for this to clear to maximum depth.
确保调用glDepthFunc()来设置深度缓冲区比较功能。大多数应用程序使用GL_LEQUAL或GL_LESS作为深度函数。还要确保调用glClearDepth()来设置深度缓冲区被清除的值;你可能应该使用1.0的参数来清除最大深度。
#4
0
I'm not sure how the appropriate AGL calls should look like but you should make sure you actually allocate bit planes for the depth buffer.
我不确定适当的AGL调用应该如何,但是你应该确保为深度缓冲区实际分配位平面。
#1
14
what do you get when you add this to your draw method?
将此添加到绘图方法后会得到什么?
int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
NSLog(@"%i bits depth", depth)
A few more things to try:
还有一些事情要尝试:
- make sure initWithFrame is being called
- if you're using an NSOpenGLView subclass from IB, set the depth buffer in IB, and move all your openGL initialization into
- (void)prepareOpenGL
确保调用initWithFrame
如果您正在使用IB的NSOpenGLView子类,请在IB中设置深度缓冲区,并将所有openGL初始化移动到 - (void)prepareOpenGL
#2
8
In case someone wonders how to set this without IB:
如果有人想知道如何在没有IB的情况下设置它:
NSOpenGLPixelFormatAttribute attrs[] = {
// NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 32,
0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:frame pixelFormat:format];
#3
1
Make sure you call glDepthFunc()
to set the depth buffer comparison function. Most applications use GL_LEQUAL
or GL_LESS
for the depth function. Also make sure you call glClearDepth()
to set the value that the depth buffer gets cleared to; you should probably use a parameter of 1.0
for this to clear to maximum depth.
确保调用glDepthFunc()来设置深度缓冲区比较功能。大多数应用程序使用GL_LEQUAL或GL_LESS作为深度函数。还要确保调用glClearDepth()来设置深度缓冲区被清除的值;你可能应该使用1.0的参数来清除最大深度。
#4
0
I'm not sure how the appropriate AGL calls should look like but you should make sure you actually allocate bit planes for the depth buffer.
我不确定适当的AGL调用应该如何,但是你应该确保为深度缓冲区实际分配位平面。