Browse Source

moved everything to c++

master
Joshua Moerman 13 years ago
parent
commit
50b16f5b11
  1. 22
      GLGameTemplate.xcodeproj/project.pbxproj
  2. 161
      GLGameTemplate/App.h
  3. 134
      GLGameTemplate/ViewController.mm

22
GLGameTemplate.xcodeproj/project.pbxproj

@ -53,6 +53,7 @@
4280A9391553DD4C00664DC2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPhone.xib; sourceTree = "<group>"; }; 4280A9391553DD4C00664DC2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPhone.xib; sourceTree = "<group>"; };
4280A93C1553DD4C00664DC2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPad.xib; sourceTree = "<group>"; }; 4280A93C1553DD4C00664DC2 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController_iPad.xib; sourceTree = "<group>"; };
42E092841553DFB7002EA900 /* J.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = J.xcodeproj; path = ../J/J.xcodeproj; sourceTree = "<group>"; }; 42E092841553DFB7002EA900 /* J.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = J.xcodeproj; path = ../J/J.xcodeproj; sourceTree = "<group>"; };
42E092971553F367002EA900 /* App.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = App.h; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -105,14 +106,10 @@
4280A9251553DD4C00664DC2 /* GLGameTemplate */ = { 4280A9251553DD4C00664DC2 /* GLGameTemplate */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
4280A92E1553DD4C00664DC2 /* AppDelegate.h */, 42E092971553F367002EA900 /* App.h */,
4280A92F1553DD4C00664DC2 /* AppDelegate.m */,
4280A9311553DD4C00664DC2 /* Shader.fsh */, 4280A9311553DD4C00664DC2 /* Shader.fsh */,
4280A9331553DD4C00664DC2 /* Shader.vsh */, 4280A9331553DD4C00664DC2 /* Shader.vsh */,
4280A9351553DD4C00664DC2 /* ViewController.h */, 42E092981553F7E6002EA900 /* GLViews */,
4280A9361553DD4C00664DC2 /* ViewController.mm */,
4280A9381553DD4C00664DC2 /* ViewController_iPhone.xib */,
4280A93B1553DD4C00664DC2 /* ViewController_iPad.xib */,
4280A9261553DD4C00664DC2 /* Supporting Files */, 4280A9261553DD4C00664DC2 /* Supporting Files */,
); );
path = GLGameTemplate; path = GLGameTemplate;
@ -121,6 +118,8 @@
4280A9261553DD4C00664DC2 /* Supporting Files */ = { 4280A9261553DD4C00664DC2 /* Supporting Files */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
4280A92E1553DD4C00664DC2 /* AppDelegate.h */,
4280A92F1553DD4C00664DC2 /* AppDelegate.m */,
4280A9271553DD4C00664DC2 /* GLGameTemplate-Info.plist */, 4280A9271553DD4C00664DC2 /* GLGameTemplate-Info.plist */,
4280A9281553DD4C00664DC2 /* InfoPlist.strings */, 4280A9281553DD4C00664DC2 /* InfoPlist.strings */,
4280A92B1553DD4C00664DC2 /* main.m */, 4280A92B1553DD4C00664DC2 /* main.m */,
@ -137,6 +136,17 @@
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
42E092981553F7E6002EA900 /* GLViews */ = {
isa = PBXGroup;
children = (
4280A9351553DD4C00664DC2 /* ViewController.h */,
4280A9361553DD4C00664DC2 /* ViewController.mm */,
4280A9381553DD4C00664DC2 /* ViewController_iPhone.xib */,
4280A93B1553DD4C00664DC2 /* ViewController_iPad.xib */,
);
name = GLViews;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */

161
GLGameTemplate/App.h

@ -0,0 +1,161 @@
//
// App.h
// GLGameTemplate
//
// Created by Joshua Moerman on 5/4/12.
// Copyright (c) 2012 Vadovas. All rights reserved.
//
#ifndef GLGameTemplate_App_h
#define GLGameTemplate_App_h
#import <GLKit/GLKit.h>
#include <memory>
#include <cmath>
#include "shader.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
struct App {
std::unique_ptr<J::shader> program;
GLKMatrix4 modelViewProjectionMatrix;
GLKMatrix3 normalMatrix;
float rotation;
float aspect;
GLuint vertexArray;
GLuint vertexBuffer;
App()
: program(nullptr)
, modelViewProjectionMatrix({{0}})
, normalMatrix({{0}})
, rotation(0)
, aspect(0.75)
, vertexArray(0)
, vertexBuffer(0)
{
std::cout << "APP CONSTRUCTED" << std::endl;
glEnable(GL_DEPTH_TEST);
// shader
NSString * vshader_file = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"];
NSString * fshader_file = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"];
std::vector<std::string> v;
v.emplace_back("position");
v.emplace_back("normal");
program.reset(new J::shader([vshader_file UTF8String], [fshader_file UTF8String], std::move(v)));
// vertex-buffer
glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0);
}
~App(){
glDeleteBuffers(1, &vertexBuffer);
glDeleteVertexArraysOES(1, &vertexArray);
std::cout << "APP DESTRUCTED" << std::endl;
}
void resize(float width, float height){
aspect = std::abs(width/height);
}
void update(float dt){
// projection matrix (perspective)
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
// model matrix (rotation)
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, rotation, 0.0f, 1.0f, 0.0f);
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, rotation, 1.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
// setting stuff for later use in the shader
normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
// time update
rotation += dt * 0.5f;
}
void draw(){
// clear
float red = rand() / (float)RAND_MAX;
glClearColor(red, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind and set up
glBindVertexArrayOES(vertexArray);
program->begin();
program->set_uniform("modelViewProjectionMatrix", modelViewProjectionMatrix.m, 0);
program->set_uniform("normalMatrix", normalMatrix.m, 0);
// draw
glDrawArrays(GL_TRIANGLES, 0, 36);
}
constexpr GLfloat gCubeVertexData[216] = {
// Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 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 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f
};
};
#endif

134
GLGameTemplate/ViewController.mm

@ -7,66 +7,12 @@
// //
#import "ViewController.h" #import "ViewController.h"
#import "shader.h" #import "App.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
GLfloat gCubeVertexData[216] = {
// Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f
};
@interface ViewController () { @interface ViewController () {
J::shader * program; App * app;
float width;
GLKMatrix4 _modelViewProjectionMatrix; float height;
GLKMatrix3 _normalMatrix;
float _rotation;
GLuint _vertexArray;
GLuint _vertexBuffer;
} }
@property (strong, nonatomic) EAGLContext *context; @property (strong, nonatomic) EAGLContext *context;
- (void)setupGL; - (void)setupGL;
@ -84,6 +30,7 @@ GLfloat gCubeVertexData[216] = {
#pragma mark - View stuff #pragma mark - View stuff
- (void)viewDidLoad { - (void)viewDidLoad {
self.preferredFramesPerSecond = 60;
[super viewDidLoad]; [super viewDidLoad];
self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease]; self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease];
@ -92,6 +39,7 @@ GLfloat gCubeVertexData[216] = {
GLKView* view = (GLKView* )self.view; GLKView* view = (GLKView* )self.view;
view.context = self.context; view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24; view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableMultisample = GLKViewDrawableMultisample4X;
[self setupGL]; [self setupGL];
} }
@ -118,78 +66,28 @@ GLfloat gCubeVertexData[216] = {
#pragma mark - openGL setup #pragma mark - openGL setup
- (void)setupGL { - (void)setupGL {
[EAGLContext setCurrentContext:self.context]; [EAGLContext setCurrentContext:self.context];
glEnable(GL_DEPTH_TEST); app = new App;
// shader
NSString * vshader_file = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"];
NSString * fshader_file = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"];
std::vector<std::string> v;
v.emplace_back("position");
v.emplace_back("normal");
program = new J::shader([vshader_file UTF8String], [fshader_file UTF8String], std::move(v));
// vertex-buffer
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0);
} }
- (void)tearDownGL { - (void)tearDownGL {
[EAGLContext setCurrentContext:self.context]; [EAGLContext setCurrentContext:self.context];
glDeleteBuffers(1, &_vertexBuffer); delete app;
glDeleteVertexArraysOES(1, &_vertexArray); app = 0;
if (program) {
delete program;
program = 0;
}
} }
#pragma mark - GLKView and GLKViewController delegate methods #pragma mark - GLKView and GLKViewController delegate methods
- (void)update { - (void)update {
// projection matrix (perspective) if(width != self.view.bounds.size.width || height != self.view.bounds.size.height){
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height); width = self.view.bounds.size.width;
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f); height = self.view.bounds.size.height;
app->resize(width, height);
// model matrix (rotation) }
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f); app->update(self.timeSinceLastUpdate);
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
// setting stuff for later use in the shader
_normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
// time update
_rotation += self.timeSinceLastUpdate * 0.5f;
} }
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
// clear app->draw();
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArray);
program->begin();
program->set_uniform("modelViewProjectionMatrix", _modelViewProjectionMatrix.m, 0);
program->set_uniform("normalMatrix", _normalMatrix.m, 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
} }
@end @end