對於一個項目,我需要實現一個NSOpenGLView。我發現一些資源(教程,蘋果文檔...)能夠開始,但是當我嘗試使用gluPerspective和GluLookAt時,我有一些奇怪的行爲(我的立方體的頂點是指向的,並且表面被繪製爲彼此之上)如果沒有深度緩衝區)。這裏是我的OpenGLView(NSOpenGLView類)的實現。gluPerspective和gluLookAt的奇怪行爲
注1:我需要我的應用程序與OSX 10.6兼容,這意味着我必須使用OpenGL 2.1。
注2:我無法使用Interface Builder。我需要以編程方式初始化一個NSOpenGLView。
更新:gluPerspective zNear和zFar,和gluLookAt眼睛參數已被修改,以糾正指定頂點的問題。但不正確繪製的面部問題仍然存在。下面是圖片說明:
更新2:我終於找到了答案通過閱讀標題爲「OpenGL的GL_DEPTH_TEST不工作」的問題。解決方案是:如果你想創建一個沒有Interface Builder的NSOpenGLView,你必須以編程方式設置pixelFormat。我糾正了我遇到同樣問題的人的代碼。
OpenGLView.m
#import "OpenGLView.h"
@interface OpenGLView()
{
NSTimer *updateTimer;
float cubeRotationAngle;
}
@end
@implementation OpenGLView
-(id)initWithFrame:(NSRect)contentRect pixelFormat:(NSOpenGLPixelFormat *)format
{
if(self = [super initWithFrame:(NSRect)contentRect pixelFormat:format])
{
[self setFrame:contentRect];
updateTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(redraw) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
}
return self;
}
- (void)prepareOpenGL
{
[[self openGLContext] makeCurrentContext];
}
- (void)drawRect:(NSRect)rect
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 10.0);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLdouble eyeX = 2.0;
GLdouble eyeY = 2.0;
GLdouble eyeZ = 2.0;
GLdouble targetX = 0.0;
GLdouble targetY = 0.0;
GLdouble targetZ = 0.0;
GLdouble verticalVectorX = 0.0;
GLdouble verticalVectorY = 0.0;
GLdouble verticalVectorZ = 1.0;
gluLookAt(eyeX, eyeY, eyeZ, targetX, targetY, targetZ, verticalVectorX, verticalVectorY, verticalVectorZ);
cubeRotationAngle = cubeRotationAngle + 0.5;
glRotatef(cubeRotationAngle, 2.0, 0.5, 0.5);
float size = 0.4;
glBegin(GL_QUADS);
{
//Blue face
glColor3f(0, 0, 1);
glVertex3f(-1*size, 1*size, -1*size);
glVertex3f(1*size, 1*size, -1*size);
glVertex3f(1*size, -1*size, -1*size);
glVertex3f(-1*size, -1*size, -1*size);
//Red face
glColor3f(1, 0, 0);
glVertex3f(-1*size, -1*size, -1*size);
glVertex3f(1*size, -1*size, -1*size);
glVertex3f(1*size, -1*size, 1*size);
glVertex3f(-1*size, -1*size, 1*size);
//Green face
glColor3f(0, 1, 0);
glVertex3f(-1*size, 1*size, 1*size);
glVertex3f(1*size, 1*size, 1*size);
glVertex3f(1*size, -1*size, 1*size);
glVertex3f(-1*size, -1*size, 1*size);
//Yellow face
glColor3f(1, 1, 0);
glVertex3f(-1*size, 1*size, 1*size);
glVertex3f(-1*size, 1*size, -1*size);
glVertex3f(-1*size, -1*size, -1*size);
glVertex3f(-1*size, -1*size, 1*size);
//Pink face
glColor3f(1, 0, 1);
glVertex3f(-1*size, 1*size, 1*size);
glVertex3f(1*size, 1*size, 1*size);
glVertex3f(1*size, 1*size, -1*size);
glVertex3f(-1*size, 1*size, -1*size);
//light blue face
glColor3f(0, 1, 1);
glVertex3f(1*size, 1*size, -1*size);
glVertex3f(1*size, 1*size, 1*size);
glVertex3f(1*size, -1*size, 1*size);
glVertex3f(1*size, -1*size, -1*size);
}
glEnd();
glPopMatrix();
glFlush();
}
- (void)redraw
{
[self setNeedsDisplay:YES];
}
然後,你必須實例化這樣的OpenGLView:
NSOpenGLPixelFormatAttribute attrs[] = {NSOpenGLPFADepthSize, 32, 0};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
openGLView = [[OpenGLView alloc] initWithFrame:NSMakeRect(20, 20, 760, 560) pixelFormat:format];
嗨!感謝您的建議和解釋。我改變這樣的值:gluPerspective(45.0,1.0,1.0,10.0)和眼睛參數爲2.0。它解決了我的頂點問題。但是我仍然有深度緩衝區的問題,因爲我的多維數據集的某些面沒有繪製或繪製錯誤。一個主意 ? –
@JeffDolphin:也許你沒有深度緩衝區?我不知道默認情況下NSOpenGLView提供了什麼。 – derhass
非常感謝derhass,你找到了第一個問題的答案,並且讓我找到第二個問題的答案。 –