Browse Source

pretty colors!

master pretty_colors
dummy 9 years ago
parent
commit
bf139364cd
  1. 2
      .gitignore
  2. 6
      src/hexgame.cpp
  3. 93
      src/renderer.h

2
.gitignore vendored

@ -1,3 +1,5 @@
bin/
build/
src/tags
*.swp

6
src/hexgame.cpp

@ -23,11 +23,8 @@
*/
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <cmath>
#include <random>
#if defined(_WIN32)
#include <windows.h>
@ -475,7 +472,7 @@ enterLoop(SDL_Handles &handles)
}
}
render(hexInfoArray);
renderFrame(hexInfoArray);
SDL_GL_SwapWindow(handles.window);
}
@ -487,6 +484,7 @@ cleanUp(SDL_Handles &handles)
{
// TODO: make a generic cleanup function that can free other types of SDL objects too
//freeGooey();
freeBuffers(); // renderer.h
for (SDL_Surface *surface : handles.texSurfaces)
SDL_FreeSurface(surface);
SDL_GL_DeleteContext(handles.glContext);

93
src/renderer.h

@ -2,6 +2,8 @@
#ifndef RENDERER_H
#define RENDERER_H
#include <cstdlib> // rand, srand, RAND_MAX
#include <ctime> // time
// TODO: remove for better logging
#include <iostream> // std::cout
@ -19,7 +21,8 @@
bool initRenderer(SDL_Handles &handles, v2i vpDims);
// TODO: re-implement debug erender
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
@ -30,17 +33,20 @@ void openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
GLsizei length, const GLchar* message, const void* userParam);
void createScene(vector<hex_info>* hexes);
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_vertex_buffer_triangle_count;
GLuint g_color_buffer;
size_t g_buf_size;
GLfloat *g_color_buf_data;
GLuint g_programID;
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
initRenderer(SDL_Handles &handles, v2i vpDims)
{
@ -94,7 +100,7 @@ initRenderer(SDL_Handles &handles, v2i vpDims)
glEnable (GL_DEBUG_OUTPUT);
glDebugMessageCallback((GLDEBUGPROC) openglDebugCallback, 0);
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;
}
@ -110,18 +116,23 @@ initShaders()
const char * VertexShaderCode =
"#version 330 core\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"
"{\n"
" gl_Position = MVP * vec4(vertexPosition_modelspace, 1);\n"
" gl_Position = g_MVP * vec4(vertexPosition_modelspace, 1);\n"
" fragmentColor = vertexColor;\n"
"}";
const char * FragmentShaderCode =
"#version 330 core\n"
"in vec3 fragmentColor;\n"
"out vec3 color;\n"
"void main()\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);
@ -166,7 +177,7 @@ createScene(vector<hex_info>* hexes)
);
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
int buf_index = 54;
size_t hex_count = hexes->size();
size_t buf_size = 6 * 9 * hex_count;
GLfloat vbuf_data[buf_size] = { 0 };
g_buf_size = 6 * 9 * hex_count;
GLfloat vbuf_data[g_buf_size] = { 0 };
for (int i = 0; i < (int) hex_count; 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);
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
@ -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
{
real32 R = 75.0f / 255.0f;
@ -260,7 +289,7 @@ openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
}
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,
gl_clear_color.A);
@ -269,10 +298,10 @@ render(const vector<hex_info> *hexes)
glUseProgram(g_programID);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(g_matrixID, 1, GL_FALSE, &MVP[0][0]);
// in the "g_MVP" uniform
glUniformMatrix4fv(g_matrixID, 1, GL_FALSE, &g_MVP[0][0]);
// 1rst attribute buffer : vertices
// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, g_vertexbuffer);
glVertexAttribPointer(
@ -284,9 +313,33 @@ render(const vector<hex_info> *hexes)
(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);
glDisableVertexAttribArray(0);
}
void
freeBuffers()
{
free(g_color_buf_data);
g_color_buf_data = 0;
}
#endif // RENDERER_H

Loading…
Cancel
Save