Created
May 26, 2012 21:38
-
-
Save SecondReality/2795420 to your computer and use it in GitHub Desktop.
Frog Renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FrogRenderer.h | |
// Cat Toys | |
// | |
// Created by Steven Mark Rose on 15/10/2010. | |
// Copyright 2010 Second Reality. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@class Frog; | |
@class Texture2D; | |
@interface FrogRenderer : NSObject | |
{ | |
Texture2D * bodyTexture; | |
Texture2D * backLegTextureOne; | |
Texture2D * backLegTextureTwo; | |
Texture2D * shadowTexture; | |
Texture2D * frontLegTexture; | |
Texture2D * eyeTexture; | |
} | |
-(void)render:(Frog*) frog; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FrogRenderer.m | |
// Cat Toys | |
// | |
// Created by Steven Mark Rose on 15/10/2010. | |
// Copyright 2010 Second Reality. All rights reserved. | |
// | |
#import "FrogRenderer.h" | |
#import "Texture2D.h" | |
#import "Quad.h" | |
#import "Vector2D.h" | |
#import "Frog.h" | |
#import "GraphicsUtility.h" | |
#import <OpenGLES/ES1/gl.h> | |
#import <OpenGLES/ES1/glext.h> | |
@implementation FrogRenderer | |
-(id)init | |
{ | |
if(self=[super init]) | |
{ | |
bodyTexture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"frog.png"]]; | |
backLegTextureOne = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"frogBackLeg.png"]]; | |
backLegTextureTwo = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"frogBackLegTwo.png"]]; | |
shadowTexture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"frogShadow.png"]]; | |
frontLegTexture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"frogFrontLeg.png"]]; | |
eyeTexture =[[Texture2D alloc] initWithImage:[UIImage imageNamed:@"genericEye.png"]]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[shadowTexture release]; | |
[frontLegTexture release]; | |
[bodyTexture release]; | |
[backLegTextureOne release]; | |
[backLegTextureTwo release]; | |
[eyeTexture release]; | |
[super dealloc]; | |
} | |
-(void)render:(Frog*) frog | |
{ | |
static float f = 0.0f; | |
f+=1.0f; | |
float frogSize=frog.size; | |
float frontLegSize=frog.size*0.4f; | |
float legSize=frog.size*0.5f; | |
float frogHeight=-2.1f+frog.height*3.5f; | |
if(frogHeight>=-1.6f) | |
{ | |
frogHeight=-1.6f; | |
} | |
// Calculate the rotation of the body: | |
Vector2D up = {0.0f, 1.0f}; | |
float angle = -signedAngularDifference(up, frog.direction); | |
float rotationAngle = (angle+M_PI)*(180.0f/M_PI); | |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | |
glEnable(GL_BLEND); // Turn on blending | |
Quad * quad = [[Quad alloc] init]; | |
// Draw the shadow: | |
{ | |
glBindTexture(GL_TEXTURE_2D, shadowTexture.name); | |
glLoadIdentity(); | |
glTranslatef(frog.position.x, frog.position.y, -2.1f); | |
glScalef(frogSize, frogSize, frogSize); | |
float shadowScale = 1.0f+frog.height*0.5f; | |
glScalef(shadowScale, shadowScale, shadowScale); | |
[quad draw]; | |
} | |
// Draw the front arms: | |
{ | |
float frontLegOffset=M_PI*0.5f; | |
glBindTexture(GL_TEXTURE_2D, frontLegTexture.name); | |
glLoadIdentity(); | |
glTranslatef(frog.position.x, frog.position.y, frogHeight); | |
glRotatef(rotationAngle, 0.0f, 0.0f, -1.0f); | |
float armOffsetY = 0.35f * frog.size; | |
// Left Arm: | |
glPushMatrix(); | |
glTranslatef(0.45f*frog.size, -armOffsetY, 0.0f); | |
glScalef(frontLegSize, frontLegSize, 1.0f); | |
glRotatef(-RadiansToDegrees(frog.frontLegAngle+frontLegOffset), 0.0f, 0.0f, -1.0f); | |
glTranslatef(0.0f, -1.0f, 0.0f); | |
glScalef(-1.0f, 1.0f, 1.0f); | |
[quad draw]; | |
// Right Arm: | |
glPopMatrix(); | |
glTranslatef(-0.45f*frog.size, -armOffsetY, 0.0f); | |
glScalef(frontLegSize, frontLegSize, 1.0f); | |
glRotatef(RadiansToDegrees(frog.frontLegAngle+frontLegOffset), 0.0f, 0.0f, -1.0f); | |
glTranslatef(0.0f, -1.0f, 0.0f); | |
[quad draw]; | |
} | |
// Draw the back arms: | |
{ | |
// The angle the second part of the leg is at is a function of the first. | |
float frontLegOffset=M_PI*0.5f; | |
glLoadIdentity(); | |
glTranslatef(frog.position.x, frog.position.y, frogHeight); | |
glRotatef(rotationAngle, 0.0f, 0.0f, -1.0f); | |
// Back Left Leg: | |
glPushMatrix(); | |
// Part One | |
glTranslatef(0.1, 0.1, 0.0f); | |
glScalef(legSize, legSize, 1.0f); | |
glRotatef(-RadiansToDegrees(frog.frontLegAngle+frontLegOffset), 0.0f, 0.0f, -1.0f); | |
glTranslatef(0.0f, -1.0f, 0.0f); | |
glPushMatrix(); | |
// Part 2 | |
glBindTexture(GL_TEXTURE_2D, backLegTextureTwo.name); | |
glTranslatef(0.0f, -0.8f, 0.0f); | |
glRotatef(RadiansToDegrees(frog.frontLegAngle*2.0f+M_PI+0.5f), 0.0f, 0.0f, -1.0f); | |
glTranslatef(0.0f, -1.0f, 0.0f); | |
[quad draw]; | |
glPopMatrix(); | |
glBindTexture(GL_TEXTURE_2D, backLegTextureOne.name); | |
[quad draw]; | |
// Back Right Leg: | |
glPopMatrix(); | |
glTranslatef(-0.1, 0.1, 0.0f); | |
glScalef(legSize, legSize, 1.0f); | |
glRotatef(RadiansToDegrees(frog.frontLegAngle+frontLegOffset), 0.0f, 0.0f, -1.0f); | |
glTranslatef(0.0f, -1.0f, 0.0f); | |
glScalef(-1.0f, 1.0f, 1.0f); | |
glPushMatrix(); | |
// Part 2 | |
glBindTexture(GL_TEXTURE_2D, backLegTextureTwo.name); | |
glTranslatef(0.0f, -0.8f, 0.0f); | |
glRotatef(RadiansToDegrees(frog.frontLegAngle*2.0f+M_PI+0.5f), 0.0f, 0.0f, -1.0f); | |
glTranslatef(0.0f, -1.0f, 0.0f); | |
[quad draw]; | |
glPopMatrix(); | |
glBindTexture(GL_TEXTURE_2D, backLegTextureOne.name); | |
[quad draw]; | |
} | |
// Draw the body: | |
{ | |
glBindTexture(GL_TEXTURE_2D, bodyTexture.name); | |
glLoadIdentity(); | |
glTranslatef(frog.position.x, frog.position.y, frogHeight); | |
glScalef(frogSize, frogSize, 1.0f); | |
glRotatef(rotationAngle, 0.0f, 0.0f, -1.0f); | |
[quad draw]; | |
} | |
// Draw the eyes | |
float eyeSize=frogSize*0.08f; | |
Vector2D eyeOffset = multiply(normalize(subtract(frog.position, frog.lookAtPoint)), 0.0818f*frog.size); | |
glBindTexture(GL_TEXTURE_2D, eyeTexture.name); | |
glLoadIdentity(); | |
glTranslatef(frog.position.x-eyeOffset.x, frog.position.y-eyeOffset.y, frogHeight); | |
glRotatef(rotationAngle, 0.0f, 0.0f, -1.0f); | |
// Right Eye | |
glPushMatrix(); | |
{ | |
glTranslatef(-0.277f*frog.size, -0.545f*frog.size, 0.0f); | |
glScalef(eyeSize, eyeSize, 1.0f); | |
[quad draw]; | |
} | |
// Left Eye | |
glPopMatrix(); | |
{ | |
glTranslatef(0.359f*frog.size, -0.5*frog.size, 0.0f); | |
glScalef(eyeSize, eyeSize, 1.0f); | |
[quad draw]; | |
} | |
glDisable(GL_BLEND); | |
[quad release]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the OpenGL ES renderer for the frog in Cat Toys. Each limb has a joint, and the frogs eyes move. It also moves towards the camera as it jumps. The renderer for each toy is separate from its logic. There's a lot of constants in this file, and if I did it again I'd make the positioning of the limbs data driven.