Browse Source

update example appliction to use the recent LightsBuffer changes

main
cinnaboot 4 years ago
parent
commit
90a3fe7bad
  1. 107
      src/main.cpp

107
src/main.cpp

@ -1,37 +1,39 @@
#include <cassert> #include <cassert>
#include <cmath>
#include <glm/gtc/matrix_transform.hpp>
#include "tangerine.h" #include "tangerine.h"
bool bool
initLights(RenderState* rs) loadLights(RenderState* rs)
{ {
// NOTE: add a directional light
LightsBuffer* lb = rs->lights_buf; LightsBuffer* lb = rs->lights_buf;
u32 idx = lb->active_d_lights++; u32 idx = 0;
#if 0
// NOTE: add a directional light
idx = (*lb->active_d_lights)++;
lb->dl_directions[idx] = glm::vec4(-2, 1, 3, 1); lb->dl_directions[idx] = glm::vec4(-2, 1, 3, 1);
lb->dl_colors[idx] = glm::vec4(1, 0.8, 0.6, 1); lb->dl_colors[idx] = glm::vec4(0.5, 0.5, 0.3, 1);
lb->dl_intensities[idx] = glm::uvec4(1, 0, 0, 0); lb->dl_intensities[idx] = glm::uvec4(1, 0, 0, 0);
#endif
// NOTE: add a point light
idx = (*lb->active_p_lights)++;
lb->pl_positions[idx] = glm::vec4(-10, 0, -10, 1);
lb->pl_colors[idx] = glm::vec4(1, 1, 0.8, 1);
lb->pl_intensities[idx] = glm::vec4(3, 0, 0, 0);
// FIXME: we need an interface function to get uniforms by name GLBuffer* lights_ubo = getUBOByName(rs->gl_ctx, "lights");
GLBuffer* lights_ubo = nullptr; assert(lights_ubo != nullptr);
for (u32 i = 0; i < rs->gl_ctx->num_ubos; i++) {
GLBuffer* ubo = &rs->gl_ctx->uniform_buffers[i];
// FIXME: we need a utilty function for cstring comparison that takes
// into account the length of both strings so we don't match a longer
// string with a matching prefix
if (strncmp(ubo->name, "lights", 8))
lights_ubo = ubo;
}
// FIXME: we need an interface function to update lights // FIXME: we need an interface function to update lights
glBufferSubData(lights_ubo->target, 0, lb->buf_size, lb); glBindBuffer(lights_ubo->target, lights_ubo->id);
glBufferSubData(lights_ubo->target, 0, lb->buf_size, lb->buffer);
// FIXME: this only makes sense for point lights // NOTE: add a debug mesh to view the point light source
#if 0
// NOTE: add a debug mesh to view the light source
RenderGroup* debug_group = getFreeRenderGroup(rs); RenderGroup* debug_group = getFreeRenderGroup(rs);
assert(debug_group); assert(debug_group);
ShaderProgram* s_debug = getShaderByName("debug", rs->gl_ctx); ShaderProgram* s_debug = getShaderByName("debug", rs->gl_ctx);
@ -44,14 +46,15 @@ initLights(RenderState* rs)
rs->rg_arena, rs->rg_arena,
&rs->assets.models[0], // tex_cube from loadScene() &rs->assets.models[0], // tex_cube from loadScene()
s_debug->num_vertex_attribs, s_debug->num_vertex_attribs,
s_debug->attrib_mappings)) s_debug->attrib_mappings,
"debug_light"))
{ {
LOGF(Error, "Error initializing debug entity for light\n"); LOGF(Error, "Error initializing debug entity for light\n");
return false; return false;
} }
setEntityPosition(e, glm::vec3(lb->dl_directions[idx])); setEntityPosition(e, glm::vec3(lb->pl_positions[idx]));
#endif
return true; return true;
} }
@ -107,7 +110,16 @@ loadScene(RenderState* rs)
scaleEntity(e, 3); scaleEntity(e, 3);
} }
return initLights(rs); // NOTE: initilize the 'camera'
glm::vec3 cam_pos = { 0, 15, 40 };
glm::vec3 look_pos = { 0, 0, 0 };
glm::vec3 up = { 0, 1, 0 };
rs->xforms->view_xform = glm::lookAt(cam_pos, look_pos, up);
GLBuffer* xforms_ubo = getUBOByName(rs->gl_ctx, "matrices");
assert(xforms_ubo);
updateCameraTransforms(rs->xforms, xforms_ubo);
return loadLights(rs);
} }
void void
@ -138,20 +150,35 @@ render_cb_pre(RenderState* rs)
} }
} }
// NOTE: wiggle camera // NOTE: orbit point light
LightsBuffer* lb = rs->lights_buf;
static glm::vec3 cam_pos = glm::vec3(0, 0, 50); orbitPositionZ0(&lb->pl_positions[0], 2 * M_PI / 180);
static glm::vec3 cam_mov = glm::vec3(0.3, 0, 0); RenderGroup* rg = getRenderGroupByName(rs, "debug_lights");
static glm::vec3 look_pos = glm::vec3(0, 0, 0); Entity* ent = &rg->entities[0];
static float direction = -1; setEntityPosition(ent, glm::vec3(lb->pl_positions[0]));
if (cam_pos.x < -20 || cam_pos.x > 20) { // FIXME: we need an interface function to update lights
direction *= -1; GLBuffer* lights_ubo = getUBOByName(rs->gl_ctx, "lights");
cam_mov.x *= direction; assert(lights_ubo);
glBindBuffer(lights_ubo->target, lights_ubo->id);
glBufferSubData(lights_ubo->target, 0, lb->buf_size, lb->buffer);
// NOTE: orbit camera
static glm::vec4 cam_pos;
static glm::vec3 look_pos;
static glm::vec3 up;
static bool initialized = false;
if (!initialized) {
cam_pos = { 0, 15, 40, 1 };
look_pos = { 0, 0, 0 };
up = { 0, 1, 0 };
initialized = true;
} }
cam_pos += cam_mov; orbitPositionZ0(&cam_pos, 0.5 * M_PI / 180);
rs->xforms->view_xform = glm::lookAt(cam_pos, look_pos, glm::vec3(0, 1, 0)); rs->xforms->view_xform =
glm::lookAt(glm::vec3(cam_pos), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
static GLBuffer* xform_ubo = nullptr; static GLBuffer* xform_ubo = nullptr;
@ -161,19 +188,17 @@ render_cb_pre(RenderState* rs)
} }
updateCameraTransforms(rs->xforms, xform_ubo); updateCameraTransforms(rs->xforms, xform_ubo);
#if 0
for (u32 i = 0; i < rs->num_render_groups; i++) {
RenderGroup& rg = rs->render_groups[i];
for (u32 j = 0; j < rg.num_entities; j++) { // NOTE: rotate cubes
Entity& e = rg.entities[j]; RenderGroup* rg2 = getRenderGroupByName(rs, "textured_cubes");
for (u32 j = 0; j < rg2->num_entities; j++) {
Entity& e = rg2->entities[j];
float direction = (j % 2 == 0) ? 1 : -1; float direction = (j % 2 == 0) ? 1 : -1;
rotateEntity(&e, rotateEntity(&e,
glm::vec3(0, 1, 0), glm::vec3(0, 1, 0),
direction * (float) M_PI / (3 * 60)); direction * (float) M_PI / (3 * 60));
} }
}
#endif
} }
int int

Loading…
Cancel
Save