diff --git a/src/srender.h b/src/srender.h index 9947afc..60f8a04 100644 --- a/src/srender.h +++ b/src/srender.h @@ -6,6 +6,7 @@ extern "C" { #endif #include + #include "vectorial/simd4x4f.h" typedef struct srndr srndr; @@ -15,7 +16,7 @@ typedef struct srcmdbuf srcmdbuf; typedef enum { SRndrCmdTypeFrame = 0, SRndrCmdTypeGrid, - SRndrCmdTypeBox, + SRndrCmdTypeCube, SRndrCmdTypeLight, SRndrCmdTypeSphere } SRndrCmdType; @@ -46,11 +47,11 @@ void srview_get_output_texture(srview* sv, GLuint* texture); // // Command Buffer and Commands // -void srcmd_clear (srcmd *cmd); +void srcmd_clear(srcmd* cmd); srcmdbuf* srcmdbuf_create(unsigned int size_max); void srcmdbuf_clear(srcmdbuf* cmdbuf); -bool srcmdbuf_add(srcmdbuf* cmdbuf, srcmd *cmd); +bool srcmdbuf_add(srcmdbuf* cmdbuf, srcmd* cmd); srcmd* srcmd_create(srcmdbuf* cmdbuf); void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf); @@ -61,10 +62,10 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf); #ifdef SRENDER_IMPLEMENTATION -#include "utils.h" - -#include #include +#include + +#include "utils.h" typedef struct srndr { } srndr; @@ -129,6 +130,7 @@ typedef struct srmeshdata { static srmeshdata gCoordFrameMesh = {0}; static srmeshdata gGridMesh = {0}; +static srmeshdata gCubeMesh = {0}; typedef struct srshader { const char* vert_fname; @@ -284,7 +286,11 @@ void init_debug_meshes() { glGenBuffers(1, &gDebugShapesVBOId); glBindBuffer(GL_ARRAY_BUFFER, gDebugShapesVBOId); - glBufferData(GL_ARRAY_BUFFER, sizeof(union srvrtxdata) * DEBUG_VBO_SIZE, NULL, GL_STATIC_DRAW); + glBufferData( + GL_ARRAY_BUFFER, + sizeof(union srvrtxdata) * DEBUG_VBO_SIZE, + NULL, + GL_STATIC_DRAW); GLintptr dbg_vbuf_offset = 0; @@ -348,7 +354,7 @@ void init_debug_meshes() { dbg_vbuf_offset, sizeof(coord_frame_vertices), coord_frame_vertices); - dbg_vbuf_offset = sizeof (coord_frame_vertices); + dbg_vbuf_offset = sizeof(coord_frame_vertices); glGenBuffers(1, &gCoordFrameMesh.idb_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id); @@ -368,17 +374,13 @@ void init_debug_meshes() { 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 - }; + srvrtxdata grid_vertices[44]; + 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)); + 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; @@ -386,8 +388,8 @@ void init_debug_meshes() { } // 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)); + 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; @@ -400,14 +402,12 @@ void init_debug_meshes() { dbg_vbuf_offset, sizeof(srvrtxdata) * 44, grid_vertices); - dbg_vbuf_offset += sizeof (srvrtxdata) * 44; - - free(grid_vertices); + dbg_vbuf_offset += sizeof(srvrtxdata) * 44; 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; + grid_indices[2 * i] = 2 * i + 6; + grid_indices[2 * i + 1] = (2 * i + 1) + 6; } glGenBuffers(1, &gGridMesh.idb_id); @@ -421,6 +421,87 @@ void init_debug_meshes() { gGridMesh.mode = GL_LINES; gGridMesh.count = 44; + // + // Cube + // + gCubeMesh.vao_id = gDebugShapesVAId; + gCubeMesh.vb_id = gDebugShapesVBOId; + gCubeMesh.nvertices = 48; + + // clang-format off + srvrtxdata cube_vertices[48] = { + // +x + { 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255}, + { 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255}, + { 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255}, + { 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255}, + + // +y + {-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255}, + { 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255}, + { 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255}, + {-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0, 255, 0, 255}, + + // +z + {-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255}, + { 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255}, + { 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255}, + {-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0, 0, 255, 255}, + + // -x + {-0.5f, -0.5f, -0.5f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 128, 0, 0, 255}, + {-0.5f, -0.5f, 0.5f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 128, 0, 0, 255}, + {-0.5f, 0.5f, 0.5f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 128, 0, 0, 255}, + {-0.5f, 0.5f, -0.5f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 128, 0, 0, 255}, + + // -y + { 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 128, 0, 255}, + {-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 128, 0, 255}, + {-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 128, 0, 255}, + { 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0, 128, 0, 255}, + + // -z + { 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 128, 255}, + {-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 128, 255}, + {-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 128, 255}, + { 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0, 0, 128, 255} + }; + // clang-format on + + glBindBuffer(GL_ARRAY_BUFFER, gDebugShapesVBOId); + glBufferSubData( + GL_ARRAY_BUFFER, + dbg_vbuf_offset, + sizeof(srvrtxdata) * 24, + cube_vertices); + dbg_vbuf_offset += sizeof(srvrtxdata) * 24; + + GLuint cube_indices[] = { + // clang-format off + 0, 1, 2, 0, 2, 3, + 4, 5, 6, 4, 6, 7, + 8, 9, 10, 8, 10, 11, + 12, 13, 14, 12, 14, 15, + 16, 17, 18, 16, 18, 19, + 20, 21, 22, 20, 22, 23 + // clang-format on + }; + + for (int i = 0; i < 36; i++) { + cube_indices[i] += gCoordFrameMesh.nvertices + gGridMesh.nvertices; + } + + glGenBuffers(1, &gCubeMesh.idb_id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCubeMesh.idb_id); + glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + sizeof(cube_indices), + cube_indices, + GL_STATIC_DRAW); + + gCubeMesh.mode = GL_TRIANGLES; + gCubeMesh.count = 36; + glBindVertexArray(0); }; @@ -557,7 +638,11 @@ void srndr_run_frame_command(const srcmd* cmd) { glBindVertexArray(gCoordFrameMesh.vao_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_id); - glDrawElements(gCoordFrameMesh.mode, gCoordFrameMesh.count, GL_UNSIGNED_INT, (void*)0); + glDrawElements( + gCoordFrameMesh.mode, + gCoordFrameMesh.count, + GL_UNSIGNED_INT, + (void*)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -573,12 +658,26 @@ void srndr_run_grid_command(const srcmd* cmd) { glBindVertexArray(0); } +void srndr_run_cube_command(const srcmd* cmd) { + glBindVertexArray(gCubeMesh.vao_id); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCubeMesh.idb_id); + glDrawElements(gCubeMesh.mode, gCubeMesh.count, GL_UNSIGNED_INT, (void*)0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, sview->mFrameBufferId); glViewport(0, 0, sview->width, sview->height); glClearColor(0.1f, 0.1f, 0.1f, 0.1f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glUseProgram(gDefaultShader.program_id); GLint view_mat_loc = @@ -606,6 +705,9 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) { case SRndrCmdTypeGrid: srndr_run_grid_command(cmd); break; + case SRndrCmdTypeCube: + srndr_run_cube_command(cmd); + break; default: gLog("Invalid command type %d at index %d", cmd->type, i); break; @@ -618,7 +720,7 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) { // // Render Commands // -void srcmd_clear (srcmd *cmd) { +void srcmd_clear(srcmd* cmd) { cmd->color = simd4f_create(1.f, 1.f, 1.f, 1.f); cmd->type = SRndrCmdTypeFrame; simd4x4f_identity(&cmd->mat); @@ -635,8 +737,8 @@ srcmdbuf* srcmdbuf_create(unsigned int size_max) { void srcmdbuf_clear(srcmdbuf* cmdbuf) { cmdbuf->ncmds = 0; } -bool srcmdbuf_add(srcmdbuf* cmdbuf, srcmd *cmd) { - srcmd *buf_cmd = srcmd_create(cmdbuf); +bool srcmdbuf_add(srcmdbuf* cmdbuf, srcmd* cmd) { + srcmd* buf_cmd = srcmd_create(cmdbuf); if (!buf_cmd) { return false; @@ -653,10 +755,10 @@ srcmd* srcmd_create(srcmdbuf* cmdbuf) { return &(cmdbuf->cmds[cmdbuf->ncmds++]); } -#endif // SRENDER_IMPLEMENTATION +#endif // SRENDER_IMPLEMENTATION #ifdef __cplusplus } #endif -#endif // SRENDER_H +#endif // SRENDER_H diff --git a/src/vissim.cc b/src/vissim.cc index 1ed3e20..c6d978c 100644 --- a/src/vissim.cc +++ b/src/vissim.cc @@ -165,9 +165,10 @@ void DoRender() { simd4x4f view; simd4x4f proj; simd4x4f_translation(&view, 0.1f, 0.1f, -0.5f); + simd4f eye = simd4f_create (2.f * sin(gTimer->mCurrentTime), 1.f, 2.f * cos (gTimer->mCurrentTime), 1.f); simd4x4f_lookat( &view, - simd4f_create(1.f, 1.f, 1.f, 1.f), + eye, simd4f_create(0.f, 0.f, 0.f, 1.f), simd4f_create(0.f, 1.f, 0.f, 1.f)); @@ -182,6 +183,8 @@ void DoRender() { 0.5f * view_height, -50.0f, 50.0f); + simd4x4f_perspective(&proj, + 110.f * (M_PI / 180.f), view_width / view_height, 0.1f, 50.f); srview_set_view(gView, view); srview_set_proj(gView, proj); @@ -197,6 +200,33 @@ void DoRender() { rcmd.type = SRndrCmdTypeGrid; srcmdbuf_add(gRndrCmds, &rcmd); + + rcmd.type = SRndrCmdTypeGrid; + simd4x4f_translation(&rcmd.mat, 10.f, 0.f, 10.f); + srcmdbuf_add(gRndrCmds, &rcmd); + simd4x4f_translation(&rcmd.mat, 10.f, 0.f, 0.f); + srcmdbuf_add(gRndrCmds, &rcmd); + simd4x4f_translation(&rcmd.mat, 10.f, 0.f, -10.f); + srcmdbuf_add(gRndrCmds, &rcmd); + + simd4x4f_translation(&rcmd.mat, 0.f, 0.f, 10.f); + srcmdbuf_add(gRndrCmds, &rcmd); + simd4x4f_translation(&rcmd.mat, 0.f, 0.f, 0.f); + srcmdbuf_add(gRndrCmds, &rcmd); + simd4x4f_translation(&rcmd.mat, 0.f, 0.f, -10.f); + srcmdbuf_add(gRndrCmds, &rcmd); + + simd4x4f_translation(&rcmd.mat, -10.f, 0.f, 10.f); + srcmdbuf_add(gRndrCmds, &rcmd); + simd4x4f_translation(&rcmd.mat, -10.f, 0.f, 0.f); + srcmdbuf_add(gRndrCmds, &rcmd); + simd4x4f_translation(&rcmd.mat, -10.f, 0.f, -10.f); + srcmdbuf_add(gRndrCmds, &rcmd); + + srcmd_clear(&rcmd); + rcmd.type = SRndrCmdTypeCube; + srcmdbuf_add(gRndrCmds, &rcmd); + // Perform the actual render srndr_render(gRndr, gView, gRndrCmds); GLuint view_texture;