Add Quadrangles

This commit is contained in:
Morten Delenk 2016-08-28 15:25:35 +02:00
parent 3395abcc7a
commit e59bea8574
No known key found for this signature in database
GPG key ID: 3F818D0F65DCB490
7 changed files with 89 additions and 13 deletions

View file

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.6)
project(testgame) project(testgame)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I..") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I..")
set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -L/usr/local/bin") set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -L/usr/local/bin")
add_executable(testgame main.cpp renderer/main.cpp renderer/shader.cpp renderer/shapes/triangle.cpp) add_executable(testgame main.cpp renderer/main.cpp renderer/shader.cpp renderer/shapes/triangle.cpp renderer/shapes/quad.cpp)
INCLUDE(FindPkgConfig) INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS}) INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})

View file

@ -51,9 +51,9 @@ bool TestGame::init()
cerr << "Could not bind uniform " << uniform_name << endl; cerr << "Could not bind uniform " << uniform_name << endl;
throw nullptr; throw nullptr;
} }
t1.init(triangle_colors,sizeof(triangle_colors)); t1.init(triangle_colors);
t2.init(triangle_colors,sizeof(triangle_colors)); t2.init(triangle_colors);
t3.init(triangle_colors,sizeof(triangle_colors)); t3.init(triangle_colors);
return true; return true;
} }
void TestGame::stop() void TestGame::stop()
@ -77,14 +77,20 @@ void TestGame::logic()
0.0, state.brightness, 0.0, 0.0, state.brightness, 0.0,
0.0, 0.0, state.brightness, 0.0, 0.0, state.brightness,
}; };
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
};
float angle = SDL_GetTicks() / 1000.0 * 45; float angle = SDL_GetTicks() / 1000.0 * 45;
glm::vec3 axis_z(0, 0, 1); glm::vec3 axis_z(0, 0, 1);
float move = sinf(SDL_GetTicks() / 1000.0 * (2*3.14) / 5); 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)); 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)); glUniformMatrix4fv(uniform_m_transform, 1, GL_FALSE, glm::value_ptr(m_transform));
t1.update(triangle_colors, sizeof(triangle_colors)); t1.update(triangle_colors);
t2.update(triangle_colors, sizeof(triangle_colors)); t2.update(triangle_colors);
t3.update(triangle_colors, sizeof(triangle_colors)); t3.update(quad_colors);
} }
bool TestGame::tick() bool TestGame::tick()

6
main.h
View file

@ -1,11 +1,13 @@
#include <renderer/main.h> #include <renderer/main.h>
#include <renderer/shader.h> #include <renderer/shader.h>
#include <renderer/shapes/triangle.h> #include <renderer/shapes/triangle.h>
#include <renderer/shapes/quad.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <state.h> #include <state.h>
class TestGame: public MainClass { class TestGame: public MainClass {
private: private:
Triangle t1, t2, t3; Triangle t1, t2;
Quad t3;
GLuint program; GLuint program;
GLint attribute_coord2d; GLint attribute_coord2d;
auto render() -> void; auto render() -> void;
@ -19,7 +21,7 @@ protected:
virtual auto tick() -> bool; virtual auto tick() -> bool;
virtual auto init() -> bool; virtual auto init() -> bool;
public: 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,0.5,2.0,1.5,2.0,1.5,1.0) {
state.bright=true; state.bright=true;
state.brightness=0.0; state.brightness=0.0;
}; };

50
renderer/shapes/quad.cpp Normal file
View file

@ -0,0 +1,50 @@
#include <renderer/shapes/quad.h>
void Quad::init(GLfloat* colors)
{
GLfloat colors1[] = {
colors[0], colors[1], colors[2],
colors[3], colors[4], colors[5],
colors[6], colors[7], colors[8]
};
GLfloat colors2[] = {
colors[3], colors[4], colors[5],
colors[6], colors[7], colors[8],
colors[9], colors[10], colors[11]
};
a.init(colors1);
b.init(colors2);
}
Quad::Quad(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy): a(ax, ay, bx, by, dx, dy), b(bx, by, dx, dy, cx, cy)
{
}
bool Quad::render(GLint attrib, GLint colorAttrib)
{
if(a.render(attrib, colorAttrib))
return b.render(attrib, colorAttrib);
return false;
}
void Quad::setPos(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy)
{
a.setPos(ax, ay, bx, by, dx, dy);
b.setPos(bx, by, dx, dy, cx, cy);
}
void Quad::update(GLfloat* colors)
{
GLfloat colors1[] = {
colors[0], colors[1], colors[2],
colors[3], colors[4], colors[5],
colors[6], colors[7], colors[8]
};
GLfloat colors2[] = {
colors[3], colors[4], colors[5],
colors[6], colors[7], colors[8],
colors[9], colors[10], colors[11]
};
a.update(colors1);
b.update(colors2);
}
Quad::~Quad()
{
}

18
renderer/shapes/quad.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef __SHAPE_QUAD_HPP
#define __SHAPE_QUAD_HPP
#include <GL/glew.h>
#include <renderer/shapes/float2D.h>
#include <renderer/shapes/triangle.h>
class Quad {
private:
GLuint vbo_triangle;
Triangle a, b;
public:
Quad(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy);
~Quad();
auto init(GLfloat * colors) -> void;
auto render(GLint attrib, GLint colorAttrib) -> bool;
auto setPos(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy) -> void;
auto update(GLfloat* colors) -> void;
};
#endif

View file

@ -15,7 +15,7 @@ Triangle::Triangle(float ax, float ay, float bx, float by, float cx, float cy)
C.y=cy-1.0; C.y=cy-1.0;
} }
auto Triangle::init(GLfloat * colors, int size) -> void { auto Triangle::init(GLfloat * colors) -> void {
GLfloat triangle_vertices[] = { GLfloat triangle_vertices[] = {
A.x, A.y, 0.0, colors[0], colors[1], colors[2], A.x, A.y, 0.0, colors[0], colors[1], colors[2],
B.x, B.y, 0.0, colors[3], colors[4], colors[5], B.x, B.y, 0.0, colors[3], colors[4], colors[5],
@ -47,7 +47,7 @@ auto Triangle::setPos(float ax, float ay, float bx, float by, float cx, float cy
C.x=cx-1.0; C.x=cx-1.0;
C.y=cy-1.0; C.y=cy-1.0;
} }
auto Triangle::update(GLfloat* colors, int size) -> void auto Triangle::update(GLfloat* colors) -> void
{ {
glDeleteBuffers(1, &vbo_triangle); glDeleteBuffers(1, &vbo_triangle);
GLfloat triangle_vertices[] = { GLfloat triangle_vertices[] = {

View file

@ -12,9 +12,9 @@ public:
Triangle(float x, float y, float alen, float blen, float clen); 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(float ax, float ay, float bx, float by, float cx, float cy);
~Triangle(); ~Triangle();
auto init(GLfloat * colors, int size) -> void; auto init(GLfloat * colors) -> void;
auto render(GLint attrib, GLint colorAttrib) -> bool; auto render(GLint attrib, GLint colorAttrib) -> bool;
auto setPos(float ax, float ay, float bx, float by, float cx, float cy) -> void; auto setPos(float ax, float ay, float bx, float by, float cx, float cy) -> void;
auto update(GLfloat * colors, int size) -> void; auto update(GLfloat* colors) -> void;
}; };
#endif #endif