2011-11-14 17 views
0

我在背景中繪製三角形的世界中的三角形,但是當我走到它後面時,三角形不在前景中(就像它應該是) 。OpenGL渲染順序導致前景中的三角形被繪製在背景中

我把顯示問題here(請務必閱讀說明)在YouTube上的視頻

編輯:

//create instance of camera class 
mainCamera = [[Camera alloc]init]; 
//Tell window to log mouse events 
[[self window] setAcceptsMouseMovedEvents:YES]; 
//Timer for updating frames 
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1/30 target:self selector:@selector(redraw) userInfo:nil repeats:YES]; 
//keep timer running during event tracking 
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; 
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSModalPanelRunLoopMode]; 

glEnable(GL_DEPTH_TEST); 
glClearDepth(1.0f); 
glShadeModel(GL_SMOOTH);  
glEnable(GL_LINE_SMOOTH); 


glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
//self in this case is my view. The code below is getting the size of the view. 
gluPerspective(45.0f, [self frame].size.width/[self frame].size.height, 1.0f, 1000.0f); 

glMatrixMode(GL_MODELVIEW); 
//creating triangle objects 
+0

我認爲這個紫色三角形在前景中,但是如果你改變了它,可能會更容易辨別。 –

+0

@JamesBlack:不,我發現其他三角形正在繪製在它上面。深度測試 –

回答

1

你創建一個具有深度緩衝的情況下?

您是否使用glEnable(GL_DEPTH_TEST);激活深度測試?

您是否正在使用合適的glDepthFunc深度測試?

+0

我激活了'GL_DEPTH_TEST',不確定其他2(有點新OpenGL) –

+0

看看我的代碼,它看起來像我有一個深度緩衝區(假設命令是'glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);' ) –

+0

我意識到沒有DepthFunc,所以我用LEQUAL添加了它,但仍然沒有改變。 –