2021-05-09 19:45:57 -07:00

84 lines
2.2 KiB
C

/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "grid.h"
griddebug_t * gridDebugCreate() {
griddebug_t *grid = malloc(sizeof(griddebug_t));
float vertices[GRID_VERTICE_COUNT];
indice_t indices[GRID_INDICE_COUNT];
float overall = GRID_COLUMN_ROWS_SIZE*GRID_COLUMN_ROWS;
float off = overall/2.0;
int32_t i = 0;
for (int32_t y = 0; y <= GRID_COLUMN_ROWS; ++y) {
for (int32_t x = 0; x <= GRID_COLUMN_ROWS; ++x) {
float fx = ((float)x/(float)GRID_COLUMN_ROWS) * overall;
float fy = ((float)y/(float)GRID_COLUMN_ROWS) * overall;
vertices[i] = off - fx, vertices[i+1] = 0, vertices[i+2] = off - fy;
i += 3;
}
}
i = 0;
for (int32_t y = 0; y < GRID_COLUMN_ROWS; ++y) {
for (int32_t x = 0; x < GRID_COLUMN_ROWS; ++x) {
int row1 = y * (GRID_COLUMN_ROWS+1);
int row2 = (y+1) * (GRID_COLUMN_ROWS+1);
// Line 0
indices[i+0] = (indice_t)(row1+x);
indices[i+1] = (indice_t)(row1+x+1);
// Line 1
indices[i+2] = (indice_t)(row1+x+1);
indices[i+3] = (indice_t)(row2+x+1);
// Line 3
indices[i+4] = (indice_t)(row1+x);
indices[i+5] = (indice_t)(row2+x);
// Line 4
indices[i+6] = (indice_t)(row2+x);
indices[i+7] = (indice_t)(row2+x+1);
i += 8;
}
}
glGenBuffers(1, &grid->vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, grid->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,
GRID_VERTICE_COUNT*sizeof(float), vertices, GL_STATIC_DRAW
);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &grid->indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, grid->indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
GRID_INDICE_COUNT * sizeof(indice_t), indices, GL_STATIC_DRAW
);
return grid;
}
void gridDebugRender(griddebug_t *grid) {
glBindBuffer(GL_ARRAY_BUFFER, grid->vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, grid->indexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glDrawElements(GL_LINES, GRID_INDICE_COUNT, GL_UNSIGNED_INT, 0);
}
void gridDebugDispose(griddebug_t *grid) {
free(grid);
}