我一直在用這個撞牆撞牆。我讀過everydocument I couldfind以及更多關於OpenGL和Cocoa的主題。我只是不理解我的嘗試在哪裏破裂,其他人不會。只有第一幀在Cocoa中用OpenGL渲染
我的代碼如下所示,它相當短,因爲我所要做的只是渲染一個旋轉三角形。我對OpenGL或編程不是陌生的,但我是一個Cocoa和Objective-C的新手。
MyOpenGL.h
#import <Cocoa/Cocoa.h>
@interface MyOpenGL : NSOpenGLView
{
float rot;
NSTimer *timer;
}
//+(NSOpenGLPixelFormat*) basicPixelFormat;
-(void) animTimer : (NSTimer *) timer;
-(void) drawRect : (NSRect) bounds;
@end
MyOpenGL.m
#include <OpenGL/gl.h>
#import <Cocoa/Cocoa.h>
#import "MyOpenGL.h"
@implementation MyOpenGL
/*
+ (NSOpenGLPixelFormat*) basicPixelFormat
{
NSOpenGLPixelFormatAttribute attributes [] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16,
(NSOpenGLPixelFormatAttribute)nil
};
return [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
}
-(id) initWithFrame : (NSRect) frameRect
{
NSOpenGLPixelFormat * pf = [MyOpenGL basicPixelFormat];
return self = [super initWithFrame: frameRect pixelFormat: pf];
}
*/
-(void) awakeFromNib
{
rot = 0;
timer = [NSTimer timerWithTimeInterval: (1.0f/60.0f)
target:self
selector:@selector(animTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
}
-(void) animTimer : (NSTimer *) atime
{
[self setNeedsDisplay: YES];
[self drawRect:[self bounds]];
}
-(void) drawRect: (NSRect) bounds
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotatef(rot,1,0,0);
glBegin(GL_TRIANGLES);
{
glColor3f(1,0,0); glVertex3f(0,0.6,0);
glColor3f(0,1,0); glVertex3f(-0.2,-0.3,0);
glColor3f(0,0,1); glVertex3f(0.2,0.3,0);
}
glEnd();
glFlush();
rot++;
}
@end
在取消從標頭中的+(NSOpenGLPixelFormat*) basicPixelFormat;
線和在.m文件相關聯的功能和在該MyOpenGL方法-(id) initWithFrame : (NSRect) frameRect
.m文件似乎沒有區別。
drawRect
方法似乎只被調用一次。我在初始位置得到三角形,但沒有旋轉。任何意見,將不勝感激。
沒有斷點。我將取出extra -drawRect,然後在另一臺機器上嘗試一個新項目。 – 2009-11-24 10:00:00
不僅它是多餘的,但我敢肯定你會在運行日誌中發出投訴,並且它不會工作,因爲當時沒有當前上下文。你需要在'drawRect:'和'unlockFocus'之前發送'lockFocus'(如果你還沒有發送'setNeedsDisplay:',這通常和在這種情況下都會更好)。 – 2009-11-24 18:19:02
原來我的電腦太老了,無法正確編譯:/升級時間! – 2009-11-28 21:26:41