Skip to content

Commit

Permalink
draw multiple cubes
Browse files Browse the repository at this point in the history
  • Loading branch information
ianterrell committed Jul 20, 2011
1 parent 02dac28 commit 113971b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Cube/ColorfulCube.h
Expand Up @@ -9,7 +9,11 @@
#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>

@interface ColorfulCube : NSObject
@interface ColorfulCube : NSObject {
GLKVector3 position, rotation, scale;
}

@property GLKVector3 position, rotation, scale;

- (void)draw;

Expand Down
20 changes: 13 additions & 7 deletions Cube/ColorfulCube.m
Expand Up @@ -18,11 +18,15 @@

@implementation ColorfulCube

@synthesize position, rotation, scale;

- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
position = GLKVector3Make(0,0,0);
rotation = GLKVector3Make(0,0,0);
scale = GLKVector3Make(1,1,1);
}

return self;
Expand Down Expand Up @@ -82,16 +86,18 @@ + (void)initialize {

- (void)draw
{
GLKMatrix4 yRotation = GLKMatrix4MakeYRotation(1.0/8.0*M_TAU);
GLKMatrix4 xRotation = GLKMatrix4MakeXRotation(1.0/8.0*M_TAU);
GLKMatrix4 scale = GLKMatrix4MakeScale(0.5, 0.5, 0.5);
GLKMatrix4 translate = GLKMatrix4MakeTranslation(0, 0.5, 0);
GLKMatrix4 xRotationMatrix = GLKMatrix4MakeXRotation(rotation.x);
GLKMatrix4 yRotationMatrix = GLKMatrix4MakeYRotation(rotation.y);
GLKMatrix4 zRotationMatrix = GLKMatrix4MakeZRotation(rotation.z);
GLKMatrix4 scaleMatrix = GLKMatrix4MakeScale(scale.x, scale.y, scale.z);
GLKMatrix4 translateMatrix = GLKMatrix4MakeTranslation(position.x, position.y, position.z);

GLKMatrix4 modelMatrix = GLKMatrix4Multiply(translateMatrix,GLKMatrix4Multiply(scaleMatrix,GLKMatrix4Multiply(zRotationMatrix, GLKMatrix4Multiply(yRotationMatrix, xRotationMatrix))));

GLKMatrix4 modelMatrix = GLKMatrix4Multiply(translate,GLKMatrix4Multiply(scale,GLKMatrix4Multiply(xRotation, yRotation)));
GLKMatrix4 viewMatrix = GLKMatrix4MakeLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
effect.transform.modelviewMatrix = GLKMatrix4Multiply(viewMatrix, modelMatrix);

effect.transform.projectionMatrix = GLKMatrix4MakePerspective(0.125*M_TAU, 1.0, 2, -1);
effect.transform.projectionMatrix = GLKMatrix4MakePerspective(0.125*M_TAU, 2.0/3.0, 2, -1);

[effect prepareToDraw];

Expand Down
4 changes: 3 additions & 1 deletion Cube/CubeAppDelegate.h
Expand Up @@ -9,7 +9,9 @@
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface CubeAppDelegate : UIResponder <UIApplicationDelegate, GLKViewDelegate, GLKViewControllerDelegate>
@interface CubeAppDelegate : UIResponder <UIApplicationDelegate, GLKViewDelegate, GLKViewControllerDelegate> {
NSMutableArray *cubes;
}

@property (strong, nonatomic) UIWindow *window;

Expand Down
18 changes: 17 additions & 1 deletion Cube/CubeAppDelegate.m
Expand Up @@ -9,6 +9,8 @@
#import "CubeAppDelegate.h"
#import "ColorfulCube.h"

#define M_TAU (2*M_PI)

@implementation CubeAppDelegate

@synthesize window = _window;
Expand All @@ -29,6 +31,20 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];

cubes = [[NSMutableArray alloc] init];

ColorfulCube *cube = [[ColorfulCube alloc] init];
cube.position = GLKVector3Make(0.25, 0.25, 0.0);
cube.scale = GLKVector3Make(0.5, 0.5, 0.5);
cube.rotation = GLKVector3Make(1.0/8*M_TAU, 1.0/8*M_TAU, 0);
[cubes addObject:cube];

ColorfulCube *cube2 = [[ColorfulCube alloc] init];
cube2.position = GLKVector3Make(-0.5, -0.25, 0.0);
cube2.scale = GLKVector3Make(0.4, 0.4, 0.4);
cube2.rotation = GLKVector3Make(1.0/8*M_TAU, 0, 1.0/8*M_TAU);
[cubes addObject:cube2];

return YES;
}

Expand All @@ -39,7 +55,7 @@ - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
glClearColor(0.5, 0.5, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);

[[[ColorfulCube alloc] init] draw];
[cubes makeObjectsPerformSelector:@selector(draw)];
}

- (void)applicationWillResignActive:(UIApplication *)application
Expand Down

0 comments on commit 113971b

Please sign in to comment.