Browse Source

add helper function for opengl draw commands

master
cinnaboot 9 years ago
parent
commit
1469446d14
  1. 2
      src/hexgame.cpp
  2. 68
      src/renderer.h

2
src/hexgame.cpp

@ -211,6 +211,8 @@ updateHexConeFill(int32 x, int32 y)
for (hex_info &h : *g_game_state->hex_array)
{
// TODO: define a system of linear inequalities in hex-space to represent
// cones/arbitrary polygons
if (&h == g_game_state->start_hex)
{
continue;

68
src/renderer.h

@ -113,6 +113,7 @@ void fillColorBuffer(GLfloat buf[], int len, std::vector<hex_info> *hexes);
void fillHexLineBuffer(GLfloat buf[], int len, std::vector<hex_info>* hexes);
bool checkGLBufferSize(GLenum buf_type, int expected_size, int line_num);
bool initGLBufferObject(gl_buffer* buf_obj, int len, GLenum usage, GLfloat data[]);
void drawRenderGroup(gl_render_group* rg, GLenum draw_mode, bool update_vertex_data);
bool
initRenderer(SDL_Handles &handles, v2i vpDims)
@ -416,16 +417,8 @@ openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
}
void
renderFrame(std::vector<hex_info> *hexes)
{
glClearColor(g_clear_col.R, g_clear_col.G, g_clear_col.B, g_clear_col.A);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// filled hexes
drawRenderGroup(gl_render_group* rg, GLenum draw_mode, bool update_vertex_data = false)
{
// TODO: learn how to use VAOs instead of these calls to glVertexAttribPointer
// https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object
gl_render_group* rg = &g_filled_hex_render_group;
glUseProgram(rg->program_id);
// Send our transformation to the currently bound shader, in the "g_MVP" uniform
glUniformMatrix4fv(rg->matrix_id, 1, GL_FALSE, &g_scene_matrices.MVP[0][0]);
@ -435,39 +428,48 @@ renderFrame(std::vector<hex_info> *hexes)
glBindBuffer(GL_ARRAY_BUFFER, rg->vertex_buffer.buffer_id);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
if (update_vertex_data)
{
glBufferSubData(GL_ARRAY_BUFFER, 0, rg->vertex_buffer.buffer_len * sizeof(GLfloat),
rg->vertex_buffer.buffer);
}
// 2nd attribute buffer : colors
if (rg->color_buffer.buffer)
{
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, rg->color_buffer.buffer_id);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
// get new colors every frame
fillColorBuffer(rg->color_buffer.buffer, rg->color_buffer.buffer_len, hexes);
// TODO: maybe add an option to not send color data every frame?
glBufferSubData(GL_ARRAY_BUFFER, 0, rg->color_buffer.buffer_len * sizeof(GLfloat),
rg->color_buffer.buffer);
}
// draw
glDrawArrays(GL_TRIANGLES, 0, rg->vertex_buffer.buffer_len / 3);
glDrawArrays(draw_mode, 0, rg->vertex_buffer.buffer_len / 3);
// cleanup
glDisableVertexAttribArray(0);
if (rg->color_buffer.buffer)
glDisableVertexAttribArray(1);
}
// hex lines
// TODO: conpress these calls into helper function
void
renderFrame(std::vector<hex_info> *hexes)
{
gl_render_group* rg = &g_hex_line_render_group;
glUseProgram(rg->program_id);
// Send our transformation to the currently bound shader, in the "g_MVP" uniform
glUniformMatrix4fv(rg->matrix_id, 1, GL_FALSE, &g_scene_matrices.MVP[0][0]);
glClearColor(g_clear_col.R, g_clear_col.G, g_clear_col.B, g_clear_col.A);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// 1st attribute buffer : vertices
GLuint attrib_index = 0; // TODO: line buffer doesn't render if this is anything other than 0
glEnableVertexAttribArray(attrib_index);
glBindBuffer(GL_ARRAY_BUFFER, rg->vertex_buffer.buffer_id);
glVertexAttribPointer(attrib_index, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
glDrawArrays(GL_LINES, 0, rg->vertex_buffer.buffer_len / 3);
glDisableVertexAttribArray(0);
}
// filled hexes
// get new colors every frame
gl_render_group* rg = &g_filled_hex_render_group;
fillColorBuffer(rg->color_buffer.buffer, rg->color_buffer.buffer_len, hexes);
drawRenderGroup(rg, GL_TRIANGLES);
// hex lines
drawRenderGroup(&g_hex_line_render_group, GL_LINES);
}
void
@ -509,19 +511,7 @@ renderDebug(hex_info *start_hex, hex_info *current_hex)
for (int i = 0; i < 12; i++)
rg->vertex_buffer.buffer[i] = buf[i];
// TODO: conpress these calls into helper function
glUseProgram(rg->program_id);
// Send our transformation to the currently bound shader, in the "g_MVP" uniform
glUniformMatrix4fv(rg->matrix_id, 1, GL_FALSE, &g_scene_matrices.MVP[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, rg->vertex_buffer.buffer_id);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
// send new vertex data
glBufferSubData(GL_ARRAY_BUFFER, 0, rg->vertex_buffer.buffer_len * sizeof(GLfloat),
rg->vertex_buffer.buffer);
glDrawArrays(GL_LINE_LOOP, 0, rg->vertex_buffer.buffer_len / 3);
glDisableVertexAttribArray(0);
drawRenderGroup(rg, GL_LINE_LOOP, true);
}
void

Loading…
Cancel
Save