Use one VAO per hex
This commit is contained in:
parent
7a95e4515d
commit
4e375f7e6a
3 changed files with 68 additions and 58 deletions
4
Makefile
4
Makefile
|
|
@ -1,5 +1,5 @@
|
|||
CFLAGS=-Wall -Wextra -g -fenable-matrix
|
||||
CFLAGS=-Wall -Wextra -g -pedantic -fenable-matrix -Wno-gnu-zero-variadic-macro-arguments
|
||||
LIBS=`pkg-config --libs glfw3 glew opengl` -lm
|
||||
|
||||
hexnando: main.c
|
||||
hexnando: main.c Makefile
|
||||
clang ${CFLAGS} ${LIBS} main.c -o hexnando
|
||||
|
|
|
|||
|
|
@ -38,6 +38,12 @@ typedef struct {
|
|||
typedef struct {
|
||||
vec4f position; // center pos
|
||||
float radius; // side length
|
||||
uint32_t vao;
|
||||
uint32_t ebo;
|
||||
uint32_t vbo;
|
||||
float vertices[7*3]; // 7 vert per hex
|
||||
unsigned int indices[6*3]; // 6 triangles to render a hex
|
||||
|
||||
} hex_t;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
112
main.c
112
main.c
|
|
@ -9,7 +9,8 @@
|
|||
|
||||
#include "hex_math.h"
|
||||
|
||||
#define log_err(x) do { fprintf(stderr, "[ERROR] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
|
||||
#define log_err(str, ...) do { fprintf(stderr, "[ERROR] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
|
||||
#define log_debug(str, ...) do { fprintf(stderr, "[DEBUG] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
|
||||
|
||||
/* Globals */
|
||||
const char *vertexShaderSource = "#version 330 core\n"
|
||||
|
|
@ -25,10 +26,6 @@ const char *fragmentShaderSource = "#version 330 core\n"
|
|||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
||||
"}\n\0";
|
||||
|
||||
GLuint vao;
|
||||
GLuint vbo;
|
||||
GLuint ebo;
|
||||
|
||||
static void glfw_error_callback(int errcode, const char *description)
|
||||
{
|
||||
fprintf(stderr, "[GLFW ERROR][ERRCODE %d] %s\n", errcode, description);
|
||||
|
|
@ -101,76 +98,77 @@ int main()
|
|||
|
||||
|
||||
hexgrid_t grid;
|
||||
grid.rows = 3;
|
||||
grid.columns = 3;
|
||||
grid.rows = 2;
|
||||
grid.columns = 2;
|
||||
grid.hexes = calloc(1, sizeof(hex_t) * grid.rows * grid.columns);
|
||||
|
||||
float scale = 1.0f / 5.0f;
|
||||
|
||||
for (size_t i = 0; i < grid.rows; ++i)
|
||||
for (size_t i = 0; i < grid.columns; ++i)
|
||||
{
|
||||
for (size_t j = 0; j < grid.columns; ++j)
|
||||
for (size_t j = 0; j < grid.rows; ++j)
|
||||
{
|
||||
float offset = 0.0f;
|
||||
if (j % 2)
|
||||
offset = sint(1.0f/6);
|
||||
|
||||
grid.hexes[i*grid.columns+j].position.x = (0.0f + j*(1 + cost(1.0f/6)));
|
||||
grid.hexes[i*grid.columns+j].position.y = (0.0f + i*(sint(1.0f/6))*2.0f) + offset;
|
||||
grid.hexes[i*grid.columns+j].position.z = 0.0f;
|
||||
grid.hexes[i*grid.columns+j].position.w = 1.0f;
|
||||
fprintf(stdout, "Position of hex %ld %ld is %f %f\n", i, j, grid.hexes[i*grid.columns+j].position.x, grid.hexes[i*grid.columns+j].position.y);
|
||||
grid.hexes[i*grid.rows+j].position.x = (0.0f + j*(1 + cost(1.0f/6)));
|
||||
grid.hexes[i*grid.rows+j].position.y = (0.0f + i*(sint(1.0f/6))*2.0f) + offset;
|
||||
grid.hexes[i*grid.rows+j].position.z = 0.0f;
|
||||
grid.hexes[i*grid.rows+j].position.w = 1.0f;
|
||||
fprintf(stdout, "Position of hex %ld %ld is %f %f\n", i, j, grid.hexes[i*grid.rows+j].position.x, grid.hexes[i*grid.rows+j].position.y);
|
||||
}
|
||||
}
|
||||
|
||||
// Get OpenGL objects
|
||||
|
||||
float *vertices = calloc(1, 7*3*grid.rows*grid.columns*sizeof(float));
|
||||
|
||||
for (uint64_t i = 0; i < grid.columns * grid.rows; ++i)
|
||||
for (size_t i = 0; i < grid.rows * grid.columns; ++i)
|
||||
{
|
||||
vertices[i*21] = grid.hexes[i].position.x * scale;
|
||||
vertices[i*21+1] = grid.hexes[i].position.y * scale;
|
||||
vertices[i*21+2] = grid.hexes[i].position.z;
|
||||
hex_t *hex = &grid.hexes[i];
|
||||
// VAO setup
|
||||
glGenVertexArrays(1, &hex->vao);
|
||||
glBindVertexArray(hex->vao);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
|
||||
|
||||
// VBO setup
|
||||
float *vertices = hex->vertices;
|
||||
vertices[0] = hex->position.x * scale;
|
||||
vertices[1] = hex->position.y * scale;
|
||||
vertices[2] = hex->position.z;
|
||||
log_debug("Adding vertex %f %f %f", vertices[0], vertices[1], vertices[2]);
|
||||
for (int j = 1; j < 7; ++j)
|
||||
{
|
||||
vertices[i*21+j*3] = (grid.hexes[i].position.x + cost((j-1)/6.0f)) * scale;
|
||||
vertices[i*21+j*3+1] = (grid.hexes[i].position.y + sint((j-1)/6.0f)) * scale;
|
||||
vertices[i*21+j*3+2] = grid.hexes[i].position.z;
|
||||
}
|
||||
vertices[j*3] = (hex->position.x + cost((j-1)/6.0f)) * scale;
|
||||
vertices[j*3+1] = (hex->position.y + sint((j-1)/6.0f)) * scale;
|
||||
vertices[j*3+2] = hex->position.z;
|
||||
|
||||
log_debug("Adding vertex %f %f %f", vertices[j*3], vertices[j*3+1], vertices[j*3+2]);
|
||||
|
||||
}
|
||||
glGenBuffers(1,&hex->vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, hex->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 7*3*sizeof(float), vertices, GL_STATIC_DRAW);
|
||||
|
||||
size_t nindices = grid.columns * grid.rows * 3 * 6;
|
||||
unsigned int *indices = calloc(nindices, sizeof(unsigned int));
|
||||
// EBO setup
|
||||
unsigned int *indices = hex->indices;
|
||||
indices[0] = 0; indices[1] = 1; indices[2] = 2;
|
||||
indices[3] = 0; indices[4] = 2; indices[5] = 3;
|
||||
indices[6] = 0; indices[7] = 3; indices[8] = 4;
|
||||
indices[9] = 0; indices[10] = 4; indices[11] = 5;
|
||||
indices[12] = 0; indices[13] = 5; indices[14] = 6;
|
||||
indices[15] = 0; indices[16] = 6; indices[17] = 1;
|
||||
glGenBuffers(1,&hex->ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, hex->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*3 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
|
||||
|
||||
|
||||
for (size_t i = 0; i < grid.columns*grid.rows; ++i)
|
||||
{
|
||||
indices[i*6*3] = 0 + i*7; indices[i*6*3+1] = 1 + i*7; indices[i*6*3+2] = 2 + i*7;
|
||||
indices[i*6*3+3] = 0 + i*7; indices[i*6*3+4] = 2 + i*7; indices[i*6*3+5] = 3 + i*7;
|
||||
indices[i*6*3+6] = 0 + i*7; indices[i*6*3+7] = 3 + i*7; indices[i*6*3+8] = 4 + i*7;
|
||||
indices[i*6*3+9] = 0 + i*7; indices[i*6*3+10] = 4 + i*7; indices[i*6*3+11] = 5 + i*7;
|
||||
indices[i*6*3+12] = 0 + i*7; indices[i*6*3+13] = 5 + i*7; indices[i*6*3+14] = 6 + i*7;
|
||||
indices[i*6*3+15] = 0 + i*7; indices[i*6*3+16] = 6 + i*7; indices[i*6*3+17] = 1 + i*7;
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1,&vbo);
|
||||
glGenBuffers(1,&ebo);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 7*3*grid.rows*grid.columns*sizeof(float), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
//glBindVertexArray(0);
|
||||
//glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
//glBindVertexArray(0);
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
|
|
@ -182,10 +180,16 @@ int main()
|
|||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shader_program);
|
||||
glBindVertexArray(vao);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glDrawElements(GL_TRIANGLES, 3*6*grid.rows*grid.columns, GL_UNSIGNED_INT, 0);
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
for (size_t i = 0; i < grid.rows * grid.columns; ++i)
|
||||
{
|
||||
glBindVertexArray(grid.hexes[i].vao);
|
||||
log_debug("Binding VAO %d VBO %d EBO %d", grid.hexes[i].vao, grid.hexes[i].vbo, grid.hexes[i].ebo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, grid.hexes[i].vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, grid.hexes[i].ebo);
|
||||
glDrawElements(GL_TRIANGLES, 3*6, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue