|
|
|
@ -2,6 +2,8 @@ |
|
|
|
#ifndef RENDERER_H |
|
|
|
#ifndef RENDERER_H |
|
|
|
#define RENDERER_H |
|
|
|
#define RENDERER_H |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdlib> // rand, srand, RAND_MAX |
|
|
|
|
|
|
|
#include <ctime> // time |
|
|
|
// TODO: remove for better logging
|
|
|
|
// TODO: remove for better logging
|
|
|
|
#include <iostream> // std::cout |
|
|
|
#include <iostream> // std::cout |
|
|
|
|
|
|
|
|
|
|
|
@ -19,7 +21,8 @@ |
|
|
|
bool initRenderer(SDL_Handles &handles, v2i vpDims); |
|
|
|
bool initRenderer(SDL_Handles &handles, v2i vpDims); |
|
|
|
// TODO: re-implement debug erender
|
|
|
|
// TODO: re-implement debug erender
|
|
|
|
void debugRender(HexDrawMode mode, hex_info *startHex, hex_info *currentHex); |
|
|
|
void debugRender(HexDrawMode mode, hex_info *startHex, hex_info *currentHex); |
|
|
|
void render(const vector<hex_info> *hexes); |
|
|
|
void renderFrame(const vector<hex_info> *hexes); |
|
|
|
|
|
|
|
void freeBuffers(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// forward declarations
|
|
|
|
// forward declarations
|
|
|
|
@ -30,17 +33,20 @@ void openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, |
|
|
|
GLsizei length, const GLchar* message, const void* userParam); |
|
|
|
GLsizei length, const GLchar* message, const void* userParam); |
|
|
|
void createScene(vector<hex_info>* hexes); |
|
|
|
void createScene(vector<hex_info>* hexes); |
|
|
|
void parseHexToGLBuffer(GLfloat buf[], int idx, const hex_info &hex); |
|
|
|
void parseHexToGLBuffer(GLfloat buf[], int idx, const hex_info &hex); |
|
|
|
|
|
|
|
void fillColorBuffer(GLfloat* buf, int len); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glm::mat4 g_projection_matrix; |
|
|
|
|
|
|
|
glm::mat4 g_view_matrix; |
|
|
|
|
|
|
|
glm::mat4 g_model_matrix; |
|
|
|
|
|
|
|
glm::mat4 g_MVP; |
|
|
|
GLuint g_vertexbuffer; |
|
|
|
GLuint g_vertexbuffer; |
|
|
|
GLuint g_vertex_buffer_triangle_count; |
|
|
|
GLuint g_vertex_buffer_triangle_count; |
|
|
|
|
|
|
|
GLuint g_color_buffer; |
|
|
|
|
|
|
|
size_t g_buf_size; |
|
|
|
|
|
|
|
GLfloat *g_color_buf_data; |
|
|
|
GLuint g_programID; |
|
|
|
GLuint g_programID; |
|
|
|
GLuint g_matrixID; |
|
|
|
GLuint g_matrixID; |
|
|
|
glm::mat4 g_projection_matrix; |
|
|
|
|
|
|
|
glm::mat4 g_view_matrix; |
|
|
|
|
|
|
|
glm::mat4 g_model_matrix; |
|
|
|
|
|
|
|
glm::mat4 MVP; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: could break this up into initGL(), initShaders(), initVertices(), initMatrices() ?
|
|
|
|
|
|
|
|
bool |
|
|
|
bool |
|
|
|
initRenderer(SDL_Handles &handles, v2i vpDims) |
|
|
|
initRenderer(SDL_Handles &handles, v2i vpDims) |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -94,7 +100,7 @@ initRenderer(SDL_Handles &handles, v2i vpDims) |
|
|
|
glEnable (GL_DEBUG_OUTPUT); |
|
|
|
glEnable (GL_DEBUG_OUTPUT); |
|
|
|
glDebugMessageCallback((GLDEBUGPROC) openglDebugCallback, 0); |
|
|
|
glDebugMessageCallback((GLDEBUGPROC) openglDebugCallback, 0); |
|
|
|
g_programID = initShaders(); |
|
|
|
g_programID = initShaders(); |
|
|
|
g_matrixID = glGetUniformLocation(g_programID, "MVP"); // shader variable for mvp xform
|
|
|
|
g_matrixID = glGetUniformLocation(g_programID, "g_MVP"); // shader variable for mvp xform
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -110,18 +116,23 @@ initShaders() |
|
|
|
const char * VertexShaderCode = |
|
|
|
const char * VertexShaderCode = |
|
|
|
"#version 330 core\n" |
|
|
|
"#version 330 core\n" |
|
|
|
"in vec3 vertexPosition_modelspace;\n" |
|
|
|
"in vec3 vertexPosition_modelspace;\n" |
|
|
|
"uniform mat4 MVP;\n" |
|
|
|
"in vec3 vertexColor;\n" |
|
|
|
|
|
|
|
"out vec3 fragmentColor;\n" |
|
|
|
|
|
|
|
"uniform mat4 g_MVP;\n" |
|
|
|
"void main()\n" |
|
|
|
"void main()\n" |
|
|
|
"{\n" |
|
|
|
"{\n" |
|
|
|
" gl_Position = MVP * vec4(vertexPosition_modelspace, 1);\n" |
|
|
|
" gl_Position = g_MVP * vec4(vertexPosition_modelspace, 1);\n" |
|
|
|
|
|
|
|
" fragmentColor = vertexColor;\n" |
|
|
|
"}"; |
|
|
|
"}"; |
|
|
|
|
|
|
|
|
|
|
|
const char * FragmentShaderCode = |
|
|
|
const char * FragmentShaderCode = |
|
|
|
"#version 330 core\n" |
|
|
|
"#version 330 core\n" |
|
|
|
|
|
|
|
"in vec3 fragmentColor;\n" |
|
|
|
"out vec3 color;\n" |
|
|
|
"out vec3 color;\n" |
|
|
|
"void main()\n" |
|
|
|
"void main()\n" |
|
|
|
"{\n" |
|
|
|
"{\n" |
|
|
|
" color = vec3(0.5,0,0);\n" |
|
|
|
//" color = vec3(0.5,0,0);\n"
|
|
|
|
|
|
|
|
" color = fragmentColor;\n" |
|
|
|
"}"; |
|
|
|
"}"; |
|
|
|
|
|
|
|
|
|
|
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); |
|
|
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); |
|
|
|
@ -166,7 +177,7 @@ createScene(vector<hex_info>* hexes) |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
g_model_matrix = glm::mat4(1.0f); |
|
|
|
g_model_matrix = glm::mat4(1.0f); |
|
|
|
MVP = g_projection_matrix * g_view_matrix * g_model_matrix;
|
|
|
|
g_MVP = g_projection_matrix * g_view_matrix * g_model_matrix;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
////////////////////
|
|
|
|
@ -178,19 +189,27 @@ createScene(vector<hex_info>* hexes) |
|
|
|
// 6 triangles * 3 vertices per triange * 3 floats per vertex = 54
|
|
|
|
// 6 triangles * 3 vertices per triange * 3 floats per vertex = 54
|
|
|
|
int buf_index = 54; |
|
|
|
int buf_index = 54; |
|
|
|
size_t hex_count = hexes->size(); |
|
|
|
size_t hex_count = hexes->size(); |
|
|
|
size_t buf_size = 6 * 9 * hex_count; |
|
|
|
g_buf_size = 6 * 9 * hex_count; |
|
|
|
GLfloat vbuf_data[buf_size] = { 0 }; |
|
|
|
GLfloat vbuf_data[g_buf_size] = { 0 }; |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < (int) hex_count; i++) |
|
|
|
for (int i = 0; i < (int) hex_count; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
parseHexToGLBuffer(vbuf_data, buf_index * i, (*hexes)[i]); |
|
|
|
parseHexToGLBuffer(vbuf_data, buf_index * i, (*hexes)[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
g_vertex_buffer_triangle_count = buf_size / 3; |
|
|
|
g_vertex_buffer_triangle_count = g_buf_size / 3; |
|
|
|
|
|
|
|
|
|
|
|
glGenBuffers(1, &g_vertexbuffer); |
|
|
|
glGenBuffers(1, &g_vertexbuffer); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, g_vertexbuffer); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, g_vertexbuffer); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vbuf_data), vbuf_data, GL_STATIC_DRAW); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * g_buf_size, vbuf_data, GL_STATIC_DRAW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// make some pretty colors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
srand(time(0)); // seed c library rng
|
|
|
|
|
|
|
|
g_color_buf_data = (GLfloat*) std::malloc(sizeof(GLfloat) * g_buf_size); |
|
|
|
|
|
|
|
fillColorBuffer(g_color_buf_data, int(g_buf_size)); |
|
|
|
|
|
|
|
glGenBuffers(1, &g_color_buffer); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, g_color_buffer); |
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * g_buf_size, g_color_buf_data, GL_DYNAMIC_DRAW); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
@ -239,6 +258,16 @@ parseHexToGLBuffer(GLfloat buf[], int idx, const hex_info &hex) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
fillColorBuffer(GLfloat* buf, int len) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// should be normalized between 0 and 1
|
|
|
|
|
|
|
|
buf[i] = GLfloat((GLfloat) rand() / (GLfloat) RAND_MAX); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
struct
|
|
|
|
{ |
|
|
|
{ |
|
|
|
real32 R = 75.0f / 255.0f; |
|
|
|
real32 R = 75.0f / 255.0f; |
|
|
|
@ -260,7 +289,7 @@ openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
render(const vector<hex_info> *hexes) |
|
|
|
renderFrame(const vector<hex_info> *hexes) |
|
|
|
{ |
|
|
|
{ |
|
|
|
glClearColor(gl_clear_color.R, gl_clear_color.G, gl_clear_color.B, |
|
|
|
glClearColor(gl_clear_color.R, gl_clear_color.G, gl_clear_color.B, |
|
|
|
gl_clear_color.A); |
|
|
|
gl_clear_color.A); |
|
|
|
@ -269,10 +298,10 @@ render(const vector<hex_info> *hexes) |
|
|
|
glUseProgram(g_programID); |
|
|
|
glUseProgram(g_programID); |
|
|
|
|
|
|
|
|
|
|
|
// Send our transformation to the currently bound shader,
|
|
|
|
// Send our transformation to the currently bound shader,
|
|
|
|
// in the "MVP" uniform
|
|
|
|
// in the "g_MVP" uniform
|
|
|
|
glUniformMatrix4fv(g_matrixID, 1, GL_FALSE, &MVP[0][0]); |
|
|
|
glUniformMatrix4fv(g_matrixID, 1, GL_FALSE, &g_MVP[0][0]); |
|
|
|
|
|
|
|
|
|
|
|
// 1rst attribute buffer : vertices
|
|
|
|
// 1st attribute buffer : vertices
|
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, g_vertexbuffer); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, g_vertexbuffer); |
|
|
|
glVertexAttribPointer( |
|
|
|
glVertexAttribPointer( |
|
|
|
@ -284,9 +313,33 @@ render(const vector<hex_info> *hexes) |
|
|
|
(void*)0 // array buffer offset
|
|
|
|
(void*)0 // array buffer offset
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2nd attribute buffer : colors
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, g_color_buffer); |
|
|
|
|
|
|
|
glVertexAttribPointer( |
|
|
|
|
|
|
|
1, |
|
|
|
|
|
|
|
3, |
|
|
|
|
|
|
|
GL_FLOAT, |
|
|
|
|
|
|
|
GL_FALSE, |
|
|
|
|
|
|
|
0, |
|
|
|
|
|
|
|
(void*)0 |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get new colors every frame
|
|
|
|
|
|
|
|
fillColorBuffer(g_color_buf_data, int(g_buf_size)); |
|
|
|
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * g_buf_size, g_color_buf_data); |
|
|
|
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, g_vertex_buffer_triangle_count); |
|
|
|
glDrawArrays(GL_TRIANGLES, 0, g_vertex_buffer_triangle_count); |
|
|
|
glDisableVertexAttribArray(0); |
|
|
|
glDisableVertexAttribArray(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
freeBuffers() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
free(g_color_buf_data); |
|
|
|
|
|
|
|
g_color_buf_data = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif // RENDERER_H
|
|
|
|
#endif // RENDERER_H
|
|
|
|
|
|
|
|
|
|
|
|
|