Added grid debug rendering primitive.

master
Martin Felis 2020-10-23 15:23:03 +02:00
parent ce88204c02
commit 40aec4a122
3 changed files with 130 additions and 20 deletions

View File

@ -25,8 +25,8 @@ typedef struct srview {
} srview;
typedef struct srcmdbuf {
int nbufsize;
int ncmds;
int idx;
srcmd* cmds;
} srcmdbuf;
@ -48,6 +48,13 @@ typedef union srvrtxdata {
};
} srvrtxdata;
//
// Vertex Buffer Object for all debug objects (coord frame, grid, cube, ...)
//
const GLint DEBUG_VBO_SIZE = 1024;
GLuint gDebugShapesVBOId = 0;
GLuint gDebugShapesVAId = 0;
//
// Simple Mesh Data
//
@ -58,9 +65,13 @@ typedef struct srmeshdata {
GLuint vao_id;
GLuint vb_id;
GLuint idb_id;
GLuint count;
GLenum mode;
} srmeshdata;
static srmeshdata gCoordFrameMesh = {0};
static srmeshdata gGridMesh = {0};
typedef struct srshader {
const char* vert_fname;
@ -211,11 +222,14 @@ void init_shaders() {
void init_debug_meshes() {
assert(gCoordFrameMesh.nvertices == 0);
glGenVertexArrays(1, &gCoordFrameMesh.vao_id);
glBindVertexArray(gCoordFrameMesh.vao_id);
glGenVertexArrays(1, &gDebugShapesVAId);
glBindVertexArray(gDebugShapesVAId);
glGenBuffers(1, &gCoordFrameMesh.vb_id);
glBindBuffer(GL_ARRAY_BUFFER, gCoordFrameMesh.vb_id);
glGenBuffers(1, &gDebugShapesVBOId);
glBindBuffer(GL_ARRAY_BUFFER, gDebugShapesVBOId);
glBufferData(GL_ARRAY_BUFFER, sizeof(union srvrtxdata) * DEBUG_VBO_SIZE, NULL, GL_STATIC_DRAW);
GLintptr dbg_vbuf_offset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(
@ -254,6 +268,11 @@ void init_debug_meshes() {
(sizeof(srvrtxdata)),
(void*)(sizeof(float) * 9));
//
// Coordinate Frame
//
gCoordFrameMesh.vao_id = gDebugShapesVAId;
gCoordFrameMesh.vb_id = gDebugShapesVBOId;
gCoordFrameMesh.nvertices = 6;
srvrtxdata coord_frame_vertices[] = {
@ -267,12 +286,12 @@ void init_debug_meshes() {
{0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255}};
GLuint coord_frame_indices[] = {0, 1, 2, 3, 4, 5};
glBufferData(
glBufferSubData(
GL_ARRAY_BUFFER,
dbg_vbuf_offset,
sizeof(coord_frame_vertices),
coord_frame_vertices,
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
coord_frame_vertices);
dbg_vbuf_offset = sizeof (coord_frame_vertices);
glGenBuffers(1, &gCoordFrameMesh.idb_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id);
@ -281,7 +300,69 @@ void init_debug_meshes() {
sizeof(coord_frame_indices),
coord_frame_indices,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
gCoordFrameMesh.mode = GL_LINES;
gCoordFrameMesh.count = 6;
//
// Coordinate Frame
//
gGridMesh.vao_id = gDebugShapesVAId;
gGridMesh.vb_id = gDebugShapesVBOId;
gGridMesh.nvertices = 44;
srvrtxdata* grid_vertices = calloc(44, sizeof(srvrtxdata));
srvrtxdata grid_def_vertex = {
0.f, 0.f, 0.f, 1.f,
0.f, 1.f, 0.f,
0.f, 0.f,
255, 255, 255, 255
};
// lines along parallel to x axis
for (int i = 0; i <= 10; i++) {
memcpy (&grid_vertices[2*i], &grid_def_vertex, sizeof(srvrtxdata));
memcpy (&grid_vertices[2*i + 1], &grid_def_vertex, sizeof(srvrtxdata));
grid_vertices[2 * i].pos[0] = -5.0f + i * 1.0f;
grid_vertices[2 * i].pos[2] = 5.0f;
grid_vertices[2 * i + 1].pos[0] = -5.0f + i * 1.0f;
grid_vertices[2 * i + 1].pos[2] = -5.0f;
}
// lines parallel to z axis
for (int i = 11; i <= 21; i++) {
memcpy (&grid_vertices[2*i], &grid_def_vertex, sizeof(srvrtxdata));
memcpy (&grid_vertices[2*i + 1], &grid_def_vertex, sizeof(srvrtxdata));
grid_vertices[2 * i].pos[0] = -5.0f;
grid_vertices[2 * i].pos[2] = -5.0f + (i - 11) * 1.0f;
grid_vertices[2 * i + 1].pos[0] = 5.0f;
grid_vertices[2 * i + 1].pos[2] = -5.0f + (i - 11) * 1.0f;
}
glBindBuffer(GL_ARRAY_BUFFER, gDebugShapesVBOId);
glBufferSubData(
GL_ARRAY_BUFFER,
dbg_vbuf_offset,
sizeof(srvrtxdata) * 44,
grid_vertices);
dbg_vbuf_offset += sizeof (srvrtxdata) * 44;
free(grid_vertices);
GLuint grid_indices[44];
for (int i = 0; i < 22; i++) {
grid_indices[2 * i] = 2*i + 6;
grid_indices[2 * i + 1] = (2*i+1) + 6;
}
glGenBuffers(1, &gGridMesh.idb_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gGridMesh.idb_id);
glBufferData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(grid_indices),
grid_indices,
GL_STATIC_DRAW);
gGridMesh.mode = GL_LINES;
gGridMesh.count = 44;
glBindVertexArray(0);
};
@ -419,7 +500,17 @@ void srndr_run_frame_command(const srcmd* cmd) {
glBindVertexArray(gCoordFrameMesh.vao_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id);
glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, (void*)0);
glDrawElements(gCoordFrameMesh.mode, gCoordFrameMesh.count, GL_UNSIGNED_INT, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void srndr_run_grid_command(const srcmd* cmd) {
glBindVertexArray(gGridMesh.vao_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gGridMesh.idb_id);
glDrawElements(gGridMesh.mode, gGridMesh.count, GL_UNSIGNED_INT, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
@ -455,6 +546,9 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
case SRndrCmdTypeFrame:
srndr_run_frame_command(cmd);
break;
case SRndrCmdTypeGrid:
srndr_run_grid_command(cmd);
break;
default:
gLog("Invalid command type %d at index %d", cmd->type, i);
break;
@ -469,19 +563,19 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
//
srcmdbuf* srcmdbuf_create(unsigned int size_max) {
srcmdbuf* result = malloc(sizeof(srcmdbuf));
result->ncmds = size_max;
result->idx = 0;
result->nbufsize = size_max;
result->ncmds = 0;
result->cmds = calloc(sizeof(srcmd), size_max);
return result;
}
void srcmdbuf_clear(srcmdbuf* cmdbuf) { cmdbuf->idx = 0; }
void srcmdbuf_clear(srcmdbuf* cmdbuf) { cmdbuf->ncmds = 0; }
srcmd* srcmd_create(srcmdbuf* cmdbuf) {
if (cmdbuf->idx == cmdbuf->ncmds) {
if (cmdbuf->ncmds == cmdbuf->nbufsize) {
gLog("Warning: number of render commands maxed out!");
return NULL;
}
return &(cmdbuf->cmds[cmdbuf->idx++]);
return &(cmdbuf->cmds[cmdbuf->ncmds++]);
}

View File

@ -20,8 +20,8 @@ typedef enum {
} SRndrCmdType;
typedef struct srcmd {
simd4x4f mat;
simd4f color;
simd4x4f mat;
simd4f color;
SRndrCmdType type;
} srcmd;
@ -46,7 +46,7 @@ void srview_get_output_texture(srview* sv, GLuint* texture);
// Command Buffer and Commands
//
srcmdbuf* srcmdbuf_create(unsigned int size_max);
void srcmdbuf_clear (srcmdbuf* cmdbuf);
void srcmdbuf_clear(srcmdbuf* cmdbuf);
srcmd* srcmd_create(srcmdbuf* cmdbuf);
void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf);

View File

@ -170,7 +170,18 @@ void DoRender() {
simd4f_create(1.f, 1.f, 1.f, 1.f),
simd4f_create(0.f, 0.f, 0.f, 1.f),
simd4f_create(0.f, 1.f, 0.f, 1.f));
simd4x4f_ortho(&proj, -5.0f, 5.0f, -5.0f, 5.0f, -5.0f, 5.0f);
float view_width = content_avail.x / 50.f;
float view_height = content_avail.y / 50.f;
simd4x4f_ortho(
&proj,
-0.5f * view_width,
0.5f * view_width,
-0.5f * view_height,
0.5f * view_height,
-50.0f,
50.0f);
srview_set_view(gView, view);
srview_set_proj(gView, proj);
@ -181,6 +192,11 @@ void DoRender() {
simd4x4f_identity(&cmd->mat);
cmd->color = simd4f_create(1.f, 1.f, 1.f, 1.f);
cmd = srcmd_create(gRndrCmds);
cmd->type = SRndrCmdTypeGrid;
simd4x4f_identity(&cmd->mat);
cmd->color = simd4f_create(1.f, 1.f, 1.f, 1.f);
// Perform the actual render
srndr_render(gRndr, gView, gRndrCmds);
GLuint view_texture;