2016-08-27 18:54:34 +00:00
|
|
|
#include <iostream>
|
2016-08-27 19:59:52 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <main.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <GL/glew.h>
|
2016-08-28 09:31:10 +00:00
|
|
|
#include <renderer/shader.h>
|
2016-08-28 12:55:35 +00:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
2016-08-27 19:59:52 +00:00
|
|
|
using namespace std;
|
|
|
|
bool TestGame::init()
|
|
|
|
{
|
2016-08-28 09:31:10 +00:00
|
|
|
GLint link_ok = GL_FALSE;
|
2016-08-27 19:59:52 +00:00
|
|
|
//Compile Vertex shader
|
2016-08-28 09:31:10 +00:00
|
|
|
vs = new VertexShader("shaders/triangle.v.glsl");
|
|
|
|
if(!vs->compile()) {
|
|
|
|
throw nullptr;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
|
|
|
//Compile Fragment shader
|
2016-08-28 09:31:10 +00:00
|
|
|
fs = new FragmentShader("shaders/triangle.f.glsl");
|
|
|
|
if(!fs->compile()) {
|
|
|
|
throw nullptr;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
|
|
|
//Linking
|
2016-08-28 09:31:10 +00:00
|
|
|
vector<Shader *> shaders;
|
|
|
|
shaders.push_back(vs);
|
|
|
|
shaders.push_back(fs);
|
|
|
|
program=link(shaders);
|
|
|
|
//Set attributes
|
2016-08-28 12:55:35 +00:00
|
|
|
const char* attribute_name = "coord3d";
|
2016-08-27 19:59:52 +00:00
|
|
|
attribute_coord2d = glGetAttribLocation(program, attribute_name);
|
|
|
|
if (attribute_coord2d == -1) {
|
|
|
|
cerr << "Could not bind attribute " << attribute_name << endl;
|
2016-08-28 09:31:10 +00:00
|
|
|
throw nullptr;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
2016-08-28 10:20:52 +00:00
|
|
|
attribute_name = "v_color";
|
|
|
|
attribute_v_color = glGetAttribLocation(program, attribute_name);
|
|
|
|
if (attribute_v_color == -1) {
|
|
|
|
cerr << "Could not bind attribute " << attribute_name << endl;
|
2016-08-28 12:55:35 +00:00
|
|
|
throw nullptr;
|
2016-08-28 10:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLfloat triangle_colors[] = {
|
|
|
|
1.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0,
|
|
|
|
1.0, 0.0, 0.0,
|
|
|
|
};
|
2016-08-28 12:55:35 +00:00
|
|
|
const char* uniform_name = "m_transform";
|
|
|
|
uniform_m_transform = glGetUniformLocation(program, uniform_name);
|
|
|
|
if (uniform_m_transform == -1) {
|
|
|
|
cerr << "Could not bind uniform " << uniform_name << endl;
|
|
|
|
throw nullptr;
|
|
|
|
}
|
2016-08-28 13:25:35 +00:00
|
|
|
t1.init(triangle_colors);
|
|
|
|
t2.init(triangle_colors);
|
|
|
|
t3.init(triangle_colors);
|
2016-08-27 19:59:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void TestGame::stop()
|
|
|
|
{
|
|
|
|
//To be written
|
|
|
|
}
|
2016-08-28 12:55:35 +00:00
|
|
|
void TestGame::logic()
|
2016-08-27 19:59:52 +00:00
|
|
|
{
|
2016-08-28 11:39:13 +00:00
|
|
|
//Calculate colors
|
|
|
|
if(state.bright)
|
|
|
|
state.brightness+=0.001;
|
|
|
|
else
|
|
|
|
state.brightness-=0.001;
|
|
|
|
if(state.bright && state.brightness >= 1.0)
|
|
|
|
state.bright=false;
|
|
|
|
if(!state.bright && state.brightness < 0.0)
|
|
|
|
state.bright=true;
|
|
|
|
//Apply colors
|
|
|
|
GLfloat triangle_colors[] = {
|
|
|
|
state.brightness, 0.0, 0.0,
|
|
|
|
0.0, state.brightness, 0.0,
|
|
|
|
0.0, 0.0, state.brightness,
|
|
|
|
};
|
2016-08-28 13:25:35 +00:00
|
|
|
GLfloat quad_colors[] = {
|
|
|
|
state.brightness, 0.0, 0.0,
|
|
|
|
0.0, state.brightness, 0.0,
|
|
|
|
0.0, 0.0, state.brightness,
|
|
|
|
state.brightness, state.brightness, state.brightness
|
|
|
|
};
|
2016-08-28 12:55:35 +00:00
|
|
|
float angle = SDL_GetTicks() / 1000.0 * 45;
|
|
|
|
glm::vec3 axis_z(0, 0, 1);
|
|
|
|
float move = sinf(SDL_GetTicks() / 1000.0 * (2*3.14) / 5);
|
|
|
|
glm::mat4 m_transform = glm::rotate(glm::mat4(1.0f), glm::radians(angle), axis_z) * glm::translate(glm::mat4(1.0f), glm::vec3(move, 0.0, 0.0));
|
|
|
|
glUniformMatrix4fv(uniform_m_transform, 1, GL_FALSE, glm::value_ptr(m_transform));
|
2016-08-28 13:25:35 +00:00
|
|
|
t1.update(triangle_colors);
|
|
|
|
t2.update(triangle_colors);
|
|
|
|
t3.update(quad_colors);
|
2016-08-28 12:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TestGame::tick()
|
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
while (SDL_PollEvent(&ev)) {
|
|
|
|
if (ev.type == SDL_QUIT)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
logic();
|
2016-08-27 19:59:52 +00:00
|
|
|
render();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void TestGame::render()
|
|
|
|
{
|
|
|
|
//Clear Background
|
|
|
|
glClearColor(1.0, 1.0, 1.0, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2016-08-28 09:59:58 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2016-08-27 19:59:52 +00:00
|
|
|
glUseProgram(program);
|
|
|
|
glEnableVertexAttribArray(attribute_coord2d);
|
2016-08-28 10:20:52 +00:00
|
|
|
glEnableVertexAttribArray(attribute_v_color);
|
2016-08-28 09:31:10 +00:00
|
|
|
|
2016-08-28 10:20:52 +00:00
|
|
|
t1.render(attribute_coord2d, attribute_v_color);
|
|
|
|
t2.render(attribute_coord2d, attribute_v_color);
|
|
|
|
t3.render(attribute_coord2d, attribute_v_color);
|
2016-08-27 19:59:52 +00:00
|
|
|
|
2016-08-28 10:20:52 +00:00
|
|
|
glDisableVertexAttribArray(attribute_v_color);
|
2016-08-27 19:59:52 +00:00
|
|
|
glDisableVertexAttribArray(attribute_coord2d);
|
|
|
|
|
|
|
|
// Display the result
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
|
|
}
|
|
|
|
TestGame::~TestGame()
|
|
|
|
{
|
|
|
|
glDeleteProgram(program);
|
2016-08-28 09:31:10 +00:00
|
|
|
delete fs;
|
|
|
|
delete vs;
|
2016-08-27 19:59:52 +00:00
|
|
|
}
|
2016-08-27 18:54:34 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2016-08-28 09:31:10 +00:00
|
|
|
TestGame game;
|
|
|
|
game.start();
|
|
|
|
return 0;
|
2016-08-27 18:54:34 +00:00
|
|
|
}
|