Add some rotation

This commit is contained in:
Morten Delenk 2016-08-28 14:55:35 +02:00
parent c67302589e
commit 3395abcc7a
No known key found for this signature in database
GPG key ID: 3F818D0F65DCB490
6 changed files with 173 additions and 23 deletions

View file

@ -12,7 +12,7 @@ Build Type=Debug
CMake Binary=file:///usr/local/bin/cmake
Environment Profile=
Extra Arguments=
Install Directory=
Install Directory=file:///usr/local
[CustomDefinesAndIncludes][ProjectPath0]
Defines=\x00\x00\x00\x00
@ -24,6 +24,134 @@ Name=GCC
Path=gcc
Type=GCC
[Filters]
size=25
[Filters][0]
inclusive=0
pattern=.*
targets=3
[Filters][1]
inclusive=0
pattern=.git
targets=2
[Filters][10]
inclusive=0
pattern=*.o
targets=1
[Filters][11]
inclusive=0
pattern=*.a
targets=1
[Filters][12]
inclusive=0
pattern=*.so
targets=1
[Filters][13]
inclusive=0
pattern=*.so.*
targets=1
[Filters][14]
inclusive=0
pattern=moc_*.cpp
targets=1
[Filters][15]
inclusive=0
pattern=*.moc
targets=1
[Filters][16]
inclusive=0
pattern=ui_*.h
targets=1
[Filters][17]
inclusive=0
pattern=qrc_*.cpp
targets=1
[Filters][18]
inclusive=0
pattern=*~
targets=1
[Filters][19]
inclusive=0
pattern=*.orig
targets=1
[Filters][2]
inclusive=0
pattern=CVS
targets=2
[Filters][20]
inclusive=0
pattern=.*.kate-swp
targets=1
[Filters][21]
inclusive=0
pattern=.*.swp
targets=1
[Filters][22]
inclusive=0
pattern=*.pyc
targets=1
[Filters][23]
inclusive=0
pattern=*.pyo
targets=1
[Filters][24]
inclusive=0
pattern=/testgame.core
targets=1
[Filters][3]
inclusive=0
pattern=.svn
targets=2
[Filters][4]
inclusive=0
pattern=_svn
targets=2
[Filters][5]
inclusive=0
pattern=SCCS
targets=2
[Filters][6]
inclusive=0
pattern=_darcs
targets=2
[Filters][7]
inclusive=0
pattern=.hg
targets=2
[Filters][8]
inclusive=0
pattern=.bzr
targets=2
[Filters][9]
inclusive=0
pattern=__pycache__
targets=2
[Launch]
Launch Configurations=Launch Configuration 0

View file

@ -4,6 +4,9 @@
#include <SDL.h>
#include <GL/glew.h>
#include <renderer/shader.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
bool TestGame::init()
{
@ -24,7 +27,7 @@ bool TestGame::init()
shaders.push_back(fs);
program=link(shaders);
//Set attributes
const char* attribute_name = "coord2d";
const char* attribute_name = "coord3d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
@ -34,7 +37,7 @@ bool TestGame::init()
attribute_v_color = glGetAttribLocation(program, attribute_name);
if (attribute_v_color == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
return false;
throw nullptr;
}
GLfloat triangle_colors[] = {
@ -42,6 +45,12 @@ bool TestGame::init()
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
};
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;
}
t1.init(triangle_colors,sizeof(triangle_colors));
t2.init(triangle_colors,sizeof(triangle_colors));
t3.init(triangle_colors,sizeof(triangle_colors));
@ -51,13 +60,8 @@ void TestGame::stop()
{
//To be written
}
bool TestGame::tick()
void TestGame::logic()
{
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
return false;
}
//Calculate colors
if(state.bright)
state.brightness+=0.001;
@ -73,9 +77,24 @@ bool TestGame::tick()
0.0, state.brightness, 0.0,
0.0, 0.0, state.brightness,
};
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));
t1.update(triangle_colors, sizeof(triangle_colors));
t2.update(triangle_colors, sizeof(triangle_colors));
t3.update(triangle_colors, sizeof(triangle_colors));
}
bool TestGame::tick()
{
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
return false;
}
logic();
render();
return true;
}

2
main.h
View file

@ -9,10 +9,12 @@ private:
GLuint program;
GLint attribute_coord2d;
auto render() -> void;
void logic();
VertexShader *vs;
FragmentShader *fs;
GLint attribute_v_color;
GameState state;
GLint uniform_m_transform;
protected:
virtual auto tick() -> bool;
virtual auto init() -> bool;

View file

@ -17,9 +17,9 @@ Triangle::Triangle(float ax, float ay, float bx, float by, float cx, float cy)
}
auto Triangle::init(GLfloat * colors, int size) -> void {
GLfloat triangle_vertices[] = {
A.x, A.y, colors[0], colors[1], colors[2],
B.x, B.y, colors[3], colors[4], colors[5],
C.x, C.y, colors[6], colors[7], colors[8]
A.x, A.y, 0.0, colors[0], colors[1], colors[2],
B.x, B.y, 0.0, colors[3], colors[4], colors[5],
C.x, C.y, 0.0, colors[6], colors[7], colors[8]
};
glGenBuffers(1, &vbo_triangle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle); //Create VBO
@ -33,8 +33,8 @@ Triangle::~Triangle()
bool Triangle::render(GLint attrib, GLint color_attrib)
{
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glVertexAttribPointer(attrib, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0);
glVertexAttribPointer(color_attrib, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (GLvoid*) (2 * sizeof(GLfloat)));
glVertexAttribPointer(attrib, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), 0);
glVertexAttribPointer(color_attrib, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (GLvoid*) (3 * sizeof(GLfloat)));
glDrawArrays(GL_TRIANGLES, 0, 3);
return true;
}
@ -51,9 +51,9 @@ auto Triangle::update(GLfloat* colors, int size) -> void
{
glDeleteBuffers(1, &vbo_triangle);
GLfloat triangle_vertices[] = {
A.x, A.y, colors[0], colors[1], colors[2],
B.x, B.y, colors[3], colors[4], colors[5],
C.x, C.y, colors[6], colors[7], colors[8]
A.x, A.y, 0.0, colors[0], colors[1], colors[2],
B.x, B.y, 0.0, colors[3], colors[4], colors[5],
C.x, C.y, 0.0, colors[6], colors[7], colors[8]
};
glGenBuffers(1, &vbo_triangle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle); //Create VBO

View file

@ -6,10 +6,10 @@
# endif
#endif
varying vec3 f_color;
varying vec2 f_coord2d;
varying vec3 f_coord3d;
void main(void) {
gl_FragColor[0] = f_color[0];
gl_FragColor[1] = f_color[1];
gl_FragColor[2] = f_color[2];
gl_FragColor[3] = f_coord2d.y+1;
gl_FragColor[3] = f_coord3d.y+1;
}

View file

@ -1,9 +1,10 @@
attribute vec2 coord2d;
attribute vec3 coord3d;
attribute vec3 v_color;
varying vec3 f_color;
varying vec2 f_coord2d;
varying vec3 f_coord3d;
uniform mat4 m_transform;
void main(void) {
gl_Position = vec4(coord2d, 0.0, 1.0);
gl_Position = m_transform*vec4(coord3d, 1.0);
f_color = v_color;
f_coord2d = coord2d;
f_coord3d = (m_transform*vec4(coord3d,1.0)).xyz;
}