我跟着這個頁面上的教程:爲什麼我沒有得到一個灰色的背景,這個節目
http://iphone-3d-programming.labs.oreilly.com/ch01.html
我下到的地方說,「編譯和構建,你現在應該看到部分純粹的灰色屏幕,華友世界!「但是,當我運行程序時,我只是看到一個黑屏。
這些都是什麼文件看起來像:
HelloArrowAppDelegate.h
#import <UIKit/UIKit.h>
#import "GLView.h"
@interface HelloArrowAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *m_window;
GLView* m_view;
}
@end
HelloArrowAppDelegate.mm
#import "HelloArrowAppDelegate.h"
@implementation HelloArrowAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
m_window = [[UIWindow alloc] initWithFrame: screenBounds];
m_view = [[GLView alloc] initWithFrame:screenBounds];
[m_window addSubview: m_view];
[m_window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[m_view release];
[m_window release];
[super dealloc];
}
@end
GLView.h
#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
@interface GLView : UIView {
EAGLContext* m_context;
}
-(void) drawView;
@end
GLView.mm
#import "GLView.h"
@implementation GLView
- (void) drawView
{
glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT);
[m_context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer;
eaglLayer.opaque = YES;
m_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!m_context || ![EAGLContext setCurrentContext:m_context]) {
[self release];
return nil;
}
// OpenGL Initialization
GLuint framebuffer, renderbuffer;
glGenFramebuffersOES(1, &framebuffer);
glGenFramebuffersOES(1, &renderbuffer);
[m_context
renderbufferStorage:GL_RENDERBUFFER_OES
fromDrawable: eaglLayer];
glFramebufferRenderbufferOES(
GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
GL_RENDERBUFFER_OES, renderbuffer);
glViewport(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
[self drawView];
}
return self;
}
- (void)dealloc {
if ([EAGLContext currentContext] == m_context) {
[EAGLContext setCurrentContext:nil];
}
[m_context release];
[super dealloc];
}
@end
不知道這是一個問題,但在視圖實際附加到窗口之前,您正在繪製它似乎很奇怪。如果將以下內容添加到-application:didFinishLaunchingWithOptions:call?的末尾,會發生什麼情況? '[m_view performSelector:@selector(drawView)withObject:nil afterDelay:0.0];' – 2010-11-14 21:36:00