Added rendering of a cube, moving the camera around.
parent
0c77b3a230
commit
d7a5507774
132
src/srender.h
132
src/srender.h
|
@ -6,6 +6,7 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "vectorial/simd4x4f.h"
|
#include "vectorial/simd4x4f.h"
|
||||||
|
|
||||||
typedef struct srndr srndr;
|
typedef struct srndr srndr;
|
||||||
|
@ -15,7 +16,7 @@ typedef struct srcmdbuf srcmdbuf;
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SRndrCmdTypeFrame = 0,
|
SRndrCmdTypeFrame = 0,
|
||||||
SRndrCmdTypeGrid,
|
SRndrCmdTypeGrid,
|
||||||
SRndrCmdTypeBox,
|
SRndrCmdTypeCube,
|
||||||
SRndrCmdTypeLight,
|
SRndrCmdTypeLight,
|
||||||
SRndrCmdTypeSphere
|
SRndrCmdTypeSphere
|
||||||
} SRndrCmdType;
|
} SRndrCmdType;
|
||||||
|
@ -61,10 +62,10 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf);
|
||||||
|
|
||||||
#ifdef SRENDER_IMPLEMENTATION
|
#ifdef SRENDER_IMPLEMENTATION
|
||||||
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
typedef struct srndr {
|
typedef struct srndr {
|
||||||
} srndr;
|
} srndr;
|
||||||
|
@ -129,6 +130,7 @@ typedef struct srmeshdata {
|
||||||
|
|
||||||
static srmeshdata gCoordFrameMesh = {0};
|
static srmeshdata gCoordFrameMesh = {0};
|
||||||
static srmeshdata gGridMesh = {0};
|
static srmeshdata gGridMesh = {0};
|
||||||
|
static srmeshdata gCubeMesh = {0};
|
||||||
|
|
||||||
typedef struct srshader {
|
typedef struct srshader {
|
||||||
const char* vert_fname;
|
const char* vert_fname;
|
||||||
|
@ -284,7 +286,11 @@ void init_debug_meshes() {
|
||||||
|
|
||||||
glGenBuffers(1, &gDebugShapesVBOId);
|
glGenBuffers(1, &gDebugShapesVBOId);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 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;
|
GLintptr dbg_vbuf_offset = 0;
|
||||||
|
|
||||||
|
@ -368,13 +374,9 @@ void init_debug_meshes() {
|
||||||
gGridMesh.vb_id = gDebugShapesVBOId;
|
gGridMesh.vb_id = gDebugShapesVBOId;
|
||||||
gGridMesh.nvertices = 44;
|
gGridMesh.nvertices = 44;
|
||||||
|
|
||||||
srvrtxdata* grid_vertices = calloc(44, sizeof(srvrtxdata));
|
srvrtxdata grid_vertices[44];
|
||||||
srvrtxdata grid_def_vertex = {
|
srvrtxdata grid_def_vertex =
|
||||||
0.f, 0.f, 0.f, 1.f,
|
{0.f, 0.f, 0.f, 1.f, 0.f, 1.f, 0.f, 0.f, 0.f, 255, 255, 255, 255};
|
||||||
0.f, 1.f, 0.f,
|
|
||||||
0.f, 0.f,
|
|
||||||
255, 255, 255, 255
|
|
||||||
};
|
|
||||||
// lines along parallel to x axis
|
// lines along parallel to x axis
|
||||||
for (int i = 0; i <= 10; i++) {
|
for (int i = 0; i <= 10; i++) {
|
||||||
memcpy(&grid_vertices[2 * i], &grid_def_vertex, sizeof(srvrtxdata));
|
memcpy(&grid_vertices[2 * i], &grid_def_vertex, sizeof(srvrtxdata));
|
||||||
|
@ -402,8 +404,6 @@ void init_debug_meshes() {
|
||||||
grid_vertices);
|
grid_vertices);
|
||||||
dbg_vbuf_offset += sizeof(srvrtxdata) * 44;
|
dbg_vbuf_offset += sizeof(srvrtxdata) * 44;
|
||||||
|
|
||||||
free(grid_vertices);
|
|
||||||
|
|
||||||
GLuint grid_indices[44];
|
GLuint grid_indices[44];
|
||||||
for (int i = 0; i < 22; i++) {
|
for (int i = 0; i < 22; i++) {
|
||||||
grid_indices[2 * i] = 2 * i + 6;
|
grid_indices[2 * i] = 2 * i + 6;
|
||||||
|
@ -421,6 +421,87 @@ void init_debug_meshes() {
|
||||||
gGridMesh.mode = GL_LINES;
|
gGridMesh.mode = GL_LINES;
|
||||||
gGridMesh.count = 44;
|
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);
|
glBindVertexArray(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -557,7 +638,11 @@ void srndr_run_frame_command(const srcmd* cmd) {
|
||||||
glBindVertexArray(gCoordFrameMesh.vao_id);
|
glBindVertexArray(gCoordFrameMesh.vao_id);
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gCoordFrameMesh.idb_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);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
@ -573,12 +658,26 @@ void srndr_run_grid_command(const srcmd* cmd) {
|
||||||
glBindVertexArray(0);
|
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) {
|
void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, sview->mFrameBufferId);
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, sview->mFrameBufferId);
|
||||||
|
|
||||||
glViewport(0, 0, sview->width, sview->height);
|
glViewport(0, 0, sview->width, sview->height);
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
|
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
|
glCullFace(GL_BACK);
|
||||||
|
|
||||||
|
|
||||||
glUseProgram(gDefaultShader.program_id);
|
glUseProgram(gDefaultShader.program_id);
|
||||||
GLint view_mat_loc =
|
GLint view_mat_loc =
|
||||||
|
@ -606,6 +705,9 @@ void srndr_render(srndr* srndr, srview* sview, srcmdbuf* scmdbuf) {
|
||||||
case SRndrCmdTypeGrid:
|
case SRndrCmdTypeGrid:
|
||||||
srndr_run_grid_command(cmd);
|
srndr_run_grid_command(cmd);
|
||||||
break;
|
break;
|
||||||
|
case SRndrCmdTypeCube:
|
||||||
|
srndr_run_cube_command(cmd);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
gLog("Invalid command type %d at index %d", cmd->type, i);
|
gLog("Invalid command type %d at index %d", cmd->type, i);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -165,9 +165,10 @@ void DoRender() {
|
||||||
simd4x4f view;
|
simd4x4f view;
|
||||||
simd4x4f proj;
|
simd4x4f proj;
|
||||||
simd4x4f_translation(&view, 0.1f, 0.1f, -0.5f);
|
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(
|
simd4x4f_lookat(
|
||||||
&view,
|
&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, 0.f, 0.f, 1.f),
|
||||||
simd4f_create(0.f, 1.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,
|
0.5f * view_height,
|
||||||
-50.0f,
|
-50.0f,
|
||||||
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_view(gView, view);
|
||||||
srview_set_proj(gView, proj);
|
srview_set_proj(gView, proj);
|
||||||
|
|
||||||
|
@ -197,6 +200,33 @@ void DoRender() {
|
||||||
rcmd.type = SRndrCmdTypeGrid;
|
rcmd.type = SRndrCmdTypeGrid;
|
||||||
srcmdbuf_add(gRndrCmds, &rcmd);
|
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
|
// Perform the actual render
|
||||||
srndr_render(gRndr, gView, gRndrCmds);
|
srndr_render(gRndr, gView, gRndrCmds);
|
||||||
GLuint view_texture;
|
GLuint view_texture;
|
||||||
|
|
Loading…
Reference in New Issue