A sky roads like game with interesting shaders
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.
 
 
 
 

161 lines
5.1 KiB

//
// 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.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