OpenGL ES1 perspective with depth buffer fails. Square fails to render

Joined
Jun 24, 2008
Messages
196
Reaction score
1
Points
18
Your Mac's Specs
Macbook White 2.13Ghz 160GB 10.6.4 (Buggy Version :() Snow Leopard
Hello. I'm trying to set up the projection matrix for 3D graphics. Easy enough in normal OpenGL programming but not here for some reason.

I've taken the default OpenGL template and I removed the ES2 code and transferred parts of the ES1 Renderer class to the EAGLView class and that worked fine. Everything broke after trying to use the depth buffer. Clearing the screen works fine but rendering a square does nothing.

Here's the EAGLView class:

Code:
//
//  EAGLView.m
//  iPhone Monkey Curling (Yes it will eventually be a 3D curling game with monkeys)
//
//  Created by Matthew Mitchell on 18/08/2010.
//  Copyright Matthew Mitchell 2010. All rights reserved.
//

#import "EAGLView.h"

@implementation EAGLView

@synthesize animating;
@dynamic animationFrameInterval;

// You must implement this method
+ (Class) layerClass{
	return [CAEAGLLayer class];
}

//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id) initWithCoder:(NSCoder*) coder{	
	if ((self = [super initWithCoder:coder])) 
	{
		// Get the layer
		CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;

		eaglLayer.opaque = TRUE;
		eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
										[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

		animating = FALSE;
		displayLinkSupported = FALSE;
		displayLink = nil;
		animationTimer = nil;

		// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
		// class is used as fallback when it isn't available.
		NSString *reqSysVer = @"3.1";
		NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
		if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) 
			displayLinkSupported = TRUE;
	}
	context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
	
	if (!context || ![EAGLContext setCurrentContext:context]) 
	{
		[self release];
		return nil;
	}
	
	// Create default framebuffer object.
	glGenFramebuffersOES(1, &defaultFramebuffer);
	glGenRenderbuffersOES(1, &colorRenderbuffer);
	glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
	glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
	//Initialisation code for the game's graphics and to develop the scene
	//glEnable(GL_CULL_FACE);
	//glCullFace(GL_FRONT);
	//Setup projection matrix
	//glViewport(0, 0, backingWidth, backingHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	double xmax = 0.04142135624 * ((float) backingWidth)/backingHeight;
	glFrustumf(-xmax, xmax, -0.04142135624, 0.04142135624, 0.1, 2000); //The ymax and min have been precalculated
	glMatrixMode(GL_MODELVIEW); //Select The Modelview Matrix
	glLoadIdentity();
	glEnable(GL_DEPTH_TEST); //Enables Depth Testing
	glClearColor(0, 0, 0, 1);
	return self;
}

- (void) drawView:(id) sender{
	static const GLfloat squareVertices[] = {
		10,   10,10,
		-10,   10,10,
		10,  -10,-10,
		-10,  -10,-10,
	};
	
	static const GLubyte squareColors[] = {
		0,0,200,255,
		40,90,250,255,
		0,0,200,255,
		50,100,230,255,
	};
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClearDepthf(1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glVertexPointer(3, GL_FLOAT, 0, squareVertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
	glEnableClientState(GL_COLOR_ARRAY);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

- (void) layoutSubviews{
	// Allocate color buffer backing based on the current layer size
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
	[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*) self.layer];
	glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
	glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
	glGenRenderbuffersOES(1, &depthRenderbuffer);
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
	glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
	glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
	if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)  != GL_FRAMEBUFFER_COMPLETE_OES) 
	{
		NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) );
	}
	glViewport(0, 0, backingWidth, backingHeight);
	[self drawView:nil];
}

- (void) startAnimation{
	if (!animating) 
	{
		if (displayLinkSupported) 
		{
			// CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
			// if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
			// not be called in system versions earlier than 3.1.

			displayLink = [NSClassFromString(@"CADisplayLink")  displayLinkWithTarget:self selector:@selector(drawView:) ];
			[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
		}
		else
			animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)  0.03333333333 target:self selector:@selector(drawView:)  userInfo:nil repeats:TRUE];

		animating = TRUE;
	}
}

- (void) stopAnimation{
	if (animating) 
	{
		if (displayLinkSupported) 
		{
			[displayLink invalidate];
			displayLink = nil;
		}
		else
		{
			[animationTimer invalidate];
			animationTimer = nil;
		}

		animating = FALSE;
	}
}

- (void) dealloc{
	// Tear down GL
	if (defaultFramebuffer) 
	{
		glDeleteFramebuffersOES(1, &defaultFramebuffer);
		defaultFramebuffer = 0;
	}
	
	if (colorRenderbuffer) 
	{
		glDeleteRenderbuffersOES(1, &colorRenderbuffer);
		colorRenderbuffer = 0;
	}
	if (depthRenderbuffer) 
	{
		glDeleteRenderbuffersOES(1, &depthRenderbuffer);
		depthRenderbuffer = 0;
	}
	// Tear down context
	if ([EAGLContext currentContext] == context) 
		[EAGLContext setCurrentContext:nil];
	
	[context release];
	context = nil;

	[super dealloc];
}


@end

Thank you for any help on this problem. :)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top