diff --git a/main.cpp b/main.cpp index dd75448..7479d74 100644 --- a/main.cpp +++ b/main.cpp @@ -57,7 +57,25 @@ bool TestGame::tick() while (SDL_PollEvent(&ev)) { 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; } diff --git a/main.h b/main.h index 2334769..ebc7a91 100644 --- a/main.h +++ b/main.h @@ -2,6 +2,7 @@ #include #include #include +#include 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; }; \ No newline at end of file diff --git a/renderer/shapes/triangle.cpp b/renderer/shapes/triangle.cpp index 1cd8dbb..60fa707 100644 --- a/renderer/shapes/triangle.cpp +++ b/renderer/shapes/triangle.cpp @@ -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); +} diff --git a/renderer/shapes/triangle.h b/renderer/shapes/triangle.h index 57a22c2..9be26d0 100644 --- a/renderer/shapes/triangle.h +++ b/renderer/shapes/triangle.h @@ -4,16 +4,17 @@ #include 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 \ No newline at end of file diff --git a/shaders/triangle.f.glsl b/shaders/triangle.f.glsl index 2664498..281cced 100644 --- a/shaders/triangle.f.glsl +++ b/shaders/triangle.f.glsl @@ -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; } \ No newline at end of file diff --git a/shaders/triangle.v.glsl b/shaders/triangle.v.glsl index 33ed166..432d760 100644 --- a/shaders/triangle.v.glsl +++ b/shaders/triangle.v.glsl @@ -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; } diff --git a/state.h b/state.h new file mode 100644 index 0000000..005bf61 --- /dev/null +++ b/state.h @@ -0,0 +1,7 @@ +#ifndef __STATE_H +#define __STATE_H +struct GameState { + bool bright; + float brightness; +}; +#endif \ No newline at end of file