I followed the tutorial on this page:
http://iphone-3d-programming.labs.oreilly.com/ch01.html
I got down to the part where it says, "Compile and build and you should now see a solid gray screen. Hurray!" However, when I ran the program, I just get a black screen.
These are what the files look like:
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
I got the gray background by getting rid of the line
in application:didFinishLaunchingWithOptions: method in the file HelloArrowAppDelegate.h
I ran into this issue and fixed it in my case by making sure the layer class was implemented right after the @implementation line.
I'd try add the following to your above code:
In code I use I define a drawableProperties but you seem to be missing it.
whithout any code it might be difficult to tell you lol
your gray window comes from
check that this is called correclty
The most likely cause is that you missed something when following the tutorial. Or they got it wrong. Whichever is most likely :-)
So the next stage is to debug and figure out what went wrong. Mostly likely you have missed a line of code which adds stuff to the display. I'd look in that section of your code first and compare it to the tutorial.
If that doesn't work then I'd take a copy of you code as a back up, then start stripping stuff out of it until you have the absolute minimal amount of code. Then post that here. Without some code we cannot tell you what is wrong.
The initWithFrame is incorrect. You want to generate a framebuffer and a renderbuffer and link the two. Instead you generate two framebuffers and completely ignore one. You should also keep the references to them (the variables 'renderbuffer' and 'framebuffer') in your class, as you'll need to delete them later unless you want to leak memory.
Without fixing the second issue, I recommend:
Put framebuffer and renderbuffer somewhere you can get to them again at the relevant moment and you should also:
I've tested this against the code you provide. I get the grey screen. Changing the call to glClearColor changes the colour of the screen, so clearly the GL context is working.