First opengl animation
This commit is contained in:
parent
e9d724467b
commit
c67302589e
7 changed files with 74 additions and 25 deletions
18
main.cpp
18
main.cpp
|
@ -58,6 +58,24 @@ bool TestGame::tick()
|
|||
if (ev.type == SDL_QUIT)
|
||||
return false;
|
||||
}
|
||||
//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,
|
||||
};
|
||||
t1.update(triangle_colors, sizeof(triangle_colors));
|
||||
t2.update(triangle_colors, sizeof(triangle_colors));
|
||||
t3.update(triangle_colors, sizeof(triangle_colors));
|
||||
render();
|
||||
return true;
|
||||
}
|
||||
|
|
7
main.h
7
main.h
|
@ -2,6 +2,7 @@
|
|||
#include <renderer/shader.h>
|
||||
#include <renderer/shapes/triangle.h>
|
||||
#include <GL/glew.h>
|
||||
#include <state.h>
|
||||
class TestGame: public MainClass {
|
||||
private:
|
||||
Triangle t1, t2, t3;
|
||||
|
@ -11,11 +12,15 @@ private:
|
|||
VertexShader *vs;
|
||||
FragmentShader *fs;
|
||||
GLint attribute_v_color;
|
||||
GameState state;
|
||||
protected:
|
||||
virtual auto tick() -> bool;
|
||||
virtual auto init() -> bool;
|
||||
public:
|
||||
TestGame(): MainClass(640,480), t1(0.0, 0.0, 0.5, 1.0, 1.0, 0.0), t2(1.0,0.0,1.5,1.0,2.0,0.0), t3(0.5,1.0,1.0,2.0,1.5,1.0) {};
|
||||
TestGame(): MainClass(640,480), t1(0.0, 0.0, 0.5, 1.0, 1.0, 0.0), t2(1.0,0.0,1.5,1.0,2.0,0.0), t3(0.5,1.0,1.0,2.0,1.5,1.0) {
|
||||
state.bright=true;
|
||||
state.brightness=0.0;
|
||||
};
|
||||
virtual ~TestGame();
|
||||
virtual auto stop() -> void;
|
||||
};
|
|
@ -17,25 +17,14 @@ 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,
|
||||
B.x, B.y,
|
||||
C.x, C.y
|
||||
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]
|
||||
};
|
||||
glGenBuffers(1, &vbo_triangle);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle); //Create VBO
|
||||
//Fill VBO
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
|
||||
|
||||
//Init colors
|
||||
if(!colors)
|
||||
useColors=false;
|
||||
else {
|
||||
useColors=true;
|
||||
glGenBuffers(1, &vbo_triangle_colors);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle_colors);
|
||||
glBufferData(GL_ARRAY_BUFFER, size, colors, GL_STATIC_DRAW);
|
||||
|
||||
}
|
||||
}
|
||||
Triangle::~Triangle()
|
||||
{
|
||||
|
@ -44,11 +33,30 @@ Triangle::~Triangle()
|
|||
bool Triangle::render(GLint attrib, GLint color_attrib)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
|
||||
glVertexAttribPointer(attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
if(useColors) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle_colors);
|
||||
glVertexAttribPointer(color_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
}
|
||||
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)));
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
return true;
|
||||
}
|
||||
auto Triangle::setPos(float ax, float ay, float bx, float by, float cx, float cy) -> void
|
||||
{
|
||||
A.x=ax-1.0;
|
||||
A.y=ay-1.0;
|
||||
B.x=bx-1.0;
|
||||
B.y=by-1.0;
|
||||
C.x=cx-1.0;
|
||||
C.y=cy-1.0;
|
||||
}
|
||||
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]
|
||||
};
|
||||
glGenBuffers(1, &vbo_triangle);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle); //Create VBO
|
||||
//Fill VBO
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
|
||||
}
|
||||
|
|
|
@ -4,16 +4,17 @@
|
|||
#include <renderer/shapes/float2D.h>
|
||||
class Triangle {
|
||||
private:
|
||||
GLuint vbo_triangle, vbo_triangle_colors;
|
||||
GLuint vbo_triangle;
|
||||
float2D A;
|
||||
float2D B;
|
||||
float2D C; //The three points
|
||||
bool useColors;
|
||||
public:
|
||||
Triangle(float x, float y, float alen, float blen, float clen);
|
||||
Triangle(float ax, float ay, float bx, float by, float cx, float cy);
|
||||
~Triangle();
|
||||
auto init(GLfloat * colors=nullptr, int size=0) -> void;
|
||||
auto init(GLfloat * colors, int size) -> void;
|
||||
auto render(GLint attrib, GLint colorAttrib) -> bool;
|
||||
auto setPos(float ax, float ay, float bx, float by, float cx, float cy) -> void;
|
||||
auto update(GLfloat * colors, int size) -> void;
|
||||
};
|
||||
#endif
|
|
@ -1,7 +1,15 @@
|
|||
#ifdef GL_ES
|
||||
# ifdef GL_FRAGMENT_PRECISION_HIGH
|
||||
precision highp float;
|
||||
# else
|
||||
precision mediump float;
|
||||
# endif
|
||||
#endif
|
||||
varying vec3 f_color;
|
||||
varying vec2 f_coord2d;
|
||||
void main(void) {
|
||||
gl_FragColor[0] = f_color[0];
|
||||
gl_FragColor[1] = f_color[1];
|
||||
gl_FragColor[2] = f_color[2];
|
||||
gl_FragColor[3] = floor(mod(gl_FragCoord.y, 2.0));
|
||||
gl_FragColor[3] = f_coord2d.y+1;
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
attribute vec2 coord2d;
|
||||
attribute vec3 v_color;
|
||||
varying vec3 f_color;
|
||||
varying vec2 f_coord2d;
|
||||
void main(void) {
|
||||
gl_Position = vec4(coord2d, 0.0, 1.0);
|
||||
f_color = v_color;
|
||||
f_coord2d = coord2d;
|
||||
}
|
||||
|
|
7
state.h
Normal file
7
state.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef __STATE_H
|
||||
#define __STATE_H
|
||||
struct GameState {
|
||||
bool bright;
|
||||
float brightness;
|
||||
};
|
||||
#endif
|
Loading…
Reference in a new issue