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 <cmath>
#include <glm/gtc/matrix_transform.hpp>
#include "tangerine.h"
bool
initLights(RenderState* rs)
loadLights(RenderState* rs)
{
// NOTE: add a directional light
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_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);
#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 = 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;
}
GLBuffer* lights_ubo = getUBOByName(rs->gl_ctx, "lights");
assert(lights_ubo != nullptr);
// 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
#if 0
// NOTE: add a debug mesh to view the light source
// NOTE: add a debug mesh to view the point light source
RenderGroup* debug_group = getFreeRenderGroup(rs);
assert(debug_group);
ShaderProgram* s_debug = getShaderByName("debug", rs->gl_ctx);
@ -44,14 +46,15 @@ initLights(RenderState* rs)
rs->rg_arena,
&rs->assets.models[0], // tex_cube from loadScene()
s_debug->num_vertex_attribs,
s_debug->attrib_mappings))
s_debug->attrib_mappings,
"debug_light"))
{
LOGF(Error, "Error initializing debug entity for light\n");
return false;
}
setEntityPosition(e, glm::vec3(lb->dl_directions[idx]));
#endif
setEntityPosition(e, glm::vec3(lb->pl_positions[idx]));
return true;
}
@ -107,7 +110,16 @@ loadScene(RenderState* rs)
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
@ -138,20 +150,35 @@ render_cb_pre(RenderState* rs)
}
}
// NOTE: wiggle camera
static glm::vec3 cam_pos = glm::vec3(0, 0, 50);
static glm::vec3 cam_mov = glm::vec3(0.3, 0, 0);
static glm::vec3 look_pos = glm::vec3(0, 0, 0);
static float direction = -1;
// NOTE: orbit point light
LightsBuffer* lb = rs->lights_buf;
orbitPositionZ0(&lb->pl_positions[0], 2 * M_PI / 180);
RenderGroup* rg = getRenderGroupByName(rs, "debug_lights");
Entity* ent = &rg->entities[0];
setEntityPosition(ent, glm::vec3(lb->pl_positions[0]));
if (cam_pos.x < -20 || cam_pos.x > 20) {
direction *= -1;
cam_mov.x *= direction;
// FIXME: we need an interface function to update lights
GLBuffer* lights_ubo = getUBOByName(rs->gl_ctx, "lights");
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;
rs->xforms->view_xform = glm::lookAt(cam_pos, look_pos, glm::vec3(0, 1, 0));
orbitPositionZ0(&cam_pos, 0.5 * M_PI / 180);
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;
@ -161,19 +188,17 @@ render_cb_pre(RenderState* rs)
}
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++) {
Entity& e = rg.entities[j];
// NOTE: rotate cubes
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;
rotateEntity(&e,
glm::vec3(0, 1, 0),
direction * (float) M_PI / (3 * 60));
}
}
#endif
}
int

Loading…
Cancel
Save