프로젝트의 경우 NSOpenGLView를 구현해야합니다. 시작할 수있는 리소스 (튜토리얼, 사과 문서 ...)를 찾았지만 gluPerspective 및 GluLookAt을 사용하려고하면 이상한 동작이 발생합니다 (내 큐브의 꼭지점이 트론 케이 팅되고 얼굴이 서로 위에 그려집니다. 깊이 버퍼가없는 경우). 다음은 내 OpenGLView (NSOpenGLView 클래스)의 구현입니다.
참고 1 : 내 응용 프로그램이 OSX 10.6과 호환되어야합니다. 즉, OpenGL 2.1을 사용해야합니다.
참고 2 : Interface Builder를 사용할 수 없습니다. NSOpenGLView를 프로그래밍 방식으로 초기화해야합니다.
업데이트 : gluPerspective zNear 및 zFar, gluLookAt eye 매개 변수가 수정되어 정점 문제를 수정했습니다. 그러나 제대로 그려지지 않는 얼굴 문제는 여전히 존재합니다. 설명 할 이미지는 다음과 같습니다.
업데이트 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
호출이 잘못된 경우, 그 결과, 단지 오류를 생성하고 훼손되지 않은 현재 행렬을 떠날 것이다. 모두 near
와 far
매개 변수가 있어야합니다 긍정적 인 , 그들은 또한 서로 동일하지 않아야합니다 (주석 값이 유효하지 않도록).
이러한 값은 투영 중심에서 근거리 및 원거리 클리핑 평면의 부호있는 거리를 정의하므로 음수 값은 카메라 뒤에 있는 스펙을 나타내며 , 이는 약간의 의미가 없습니다 (또한 몇 가지 클리핑 관련 카메라 평면과 교차하는 프리미티브 문제).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다