Genetic generation of cars in a 2D environment
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 

123 lines
3.1 KiB

//
// Game.m
// iOSGLEssentials
//
// Created by Joshua Moerman on 08/03/14.
//
//
#import "Game.h"
@import GLKit;
#ifdef ESSENTIAL_GL_PRACTICES_IOS
#define glBindVertexArray glBindVertexArrayOES
#define glGenVertexArrays glGenVertexArraysOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#endif
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
#ifdef ESSENTIAL_GL_PRACTICES_IOS
NSString * file = @"Fractal-iOS";
#else
NSString * file = @"Fractal";
#endif
const GLfloat quad[] = {
// x y z, r g b
1, -1, 0, 1, 0, 0,
-1, -1, 0, 0, 1, 0,
1, 1, 0, 0, 0, 1,
-1, 1, 0, 1, 1, 1
};
static void check_shader(GLuint s){
GLint logLength;
glGetShaderiv(s, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(s, logLength, &logLength, log);
NSLog(@"Shader compile log:\n%s", log);
free(log);
}
}
@implementation Game{
float time;
GLuint program;
GLint uniform;
GLuint vao;
GLuint posBufferName;
}
- (id)init{
if(self = [super init]){
time = 0;
program = glCreateProgram();
NSError * error = nil;
NSString * vs = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@"vsh"] encoding:NSASCIIStringEncoding error:&error];
NSString * fs = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@"fsh"] encoding:NSASCIIStringEncoding error:nil];
GLchar const * vs_cstr = [vs UTF8String];
GLchar const * fs_cstr = [fs UTF8String];
GLuint v = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(v, 1, &vs_cstr, NULL);
glCompileShader(v);
check_shader(v);
GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(f, 1, &fs_cstr, NULL);
glCompileShader(f);
check_shader(f);
glAttachShader(program, v);
glAttachShader(program, f);
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "color");
glLinkProgram(program);
uniform = glGetUniformLocation(program, "rotation");
glDetachShader(program, v);
glDeleteShader(v);
glDetachShader(program, f);
glDeleteShader(f);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &posBufferName);
glBindBuffer(GL_ARRAY_BUFFER, posBufferName);
glBufferData(GL_ARRAY_BUFFER, 4*6*4, quad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*4, BUFFER_OFFSET(0));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*4, BUFFER_OFFSET(3*4));
}
return self;
}
- (void)update:(float)dt{
time += dt;
}
- (void)draw{
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glUniform1f(uniform, time);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@end