New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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


Post Reply New Thread Subscribe

 
Thread Tools
god0fgod

 
god0fgod's Avatar
 
Member Since: Jun 24, 2008
Posts: 196
god0fgod is an unknown at this point
Mac Specs: Macbook White 2.13Ghz 160GB 10.6.4 (Buggy Version :() Snow Leopard

god0fgod is offline
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« xCode 3.14 iPhone Development Problem | a few really simple questions »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
new macbook pro system log normal? micho.mad OS X - Operating System 3 04-24-2009 04:13 PM

All times are GMT -4. The time now is 10:57 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?