|
|
|
|
@ -4,6 +4,57 @@
|
|
|
|
|
#include "tangerine.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
initLights(RenderState* rs) |
|
|
|
|
{ |
|
|
|
|
// NOTE: add a directional light
|
|
|
|
|
LightsBuffer* lb = rs->lights_buf; |
|
|
|
|
u32 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_intensities[idx] = glm::uvec4(1, 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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// FIXME: we need an interface function to update lights
|
|
|
|
|
glBufferSubData(lights_ubo->target, 0, lb->buf_size, lb); |
|
|
|
|
|
|
|
|
|
// FIXME: this only makes sense for point lights
|
|
|
|
|
#if 0 |
|
|
|
|
// NOTE: add a debug mesh to view the light source
|
|
|
|
|
RenderGroup* debug_group = getFreeRenderGroup(rs); |
|
|
|
|
assert(debug_group); |
|
|
|
|
ShaderProgram* s_debug = getShaderByName("debug", rs->gl_ctx); |
|
|
|
|
assert(s_debug); |
|
|
|
|
initRenderGroup(debug_group, rs->rg_arena, s_debug, 256, "debug_lights"); |
|
|
|
|
Entity* e = getFreeEntity(debug_group); |
|
|
|
|
assert(e); |
|
|
|
|
if (!initEntity(e, |
|
|
|
|
rs->gl_ctx, |
|
|
|
|
rs->rg_arena, |
|
|
|
|
&rs->assets.models[0], // tex_cube from loadScene()
|
|
|
|
|
s_debug->num_vertex_attribs, |
|
|
|
|
s_debug->attrib_mappings)) |
|
|
|
|
{ |
|
|
|
|
LOGF(Error, "Error initializing debug entity for light\n"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setEntityPosition(e, glm::vec3(lb->dl_directions[idx])); |
|
|
|
|
#endif |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
loadScene(RenderState* rs) |
|
|
|
|
{ |
|
|
|
|
@ -56,39 +107,11 @@ loadScene(RenderState* rs)
|
|
|
|
|
scaleEntity(e, 3); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NOTE: add a point light
|
|
|
|
|
PointLight* l1 = &rs->lights[rs->num_lights++]; |
|
|
|
|
l1->position = glm::vec3(-20, 10, 30); |
|
|
|
|
l1->color = glm::vec3(1, 1, 1); |
|
|
|
|
l1->intensity = 256; |
|
|
|
|
|
|
|
|
|
// NOTE: add a debug mesh to view the light source
|
|
|
|
|
RenderGroup* debug_group = getFreeRenderGroup(rs); |
|
|
|
|
assert(debug_group); |
|
|
|
|
ShaderProgram* s_debug = getShaderByName("debug", rs->gl_ctx); |
|
|
|
|
assert(s_debug); |
|
|
|
|
initRenderGroup(debug_group, rs->rg_arena, s_debug, 256, "debug_lights"); |
|
|
|
|
|
|
|
|
|
for (u32 i = 0; i < rs->num_lights; i++) { |
|
|
|
|
PointLight* l = &rs->lights[i]; |
|
|
|
|
Entity* e = getFreeEntity(debug_group); |
|
|
|
|
assert(e); |
|
|
|
|
if (!initEntity(e, |
|
|
|
|
rs->gl_ctx, |
|
|
|
|
rs->rg_arena, |
|
|
|
|
tex_cube, |
|
|
|
|
s_debug->num_vertex_attribs, |
|
|
|
|
s_debug->attrib_mappings)) |
|
|
|
|
{ |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setEntityPosition(e, l->position); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
return initLights(rs); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#include <glm/gtc/matrix_transform.hpp> |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
render_cb_pre(RenderState* rs) |
|
|
|
|
{ |
|
|
|
|
@ -103,6 +126,45 @@ 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; |
|
|
|
|
|
|
|
|
|
if (cam_pos.x < -20 || cam_pos.x > 20) { |
|
|
|
|
direction *= -1; |
|
|
|
|
cam_mov.x *= direction; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cam_pos += cam_mov; |
|
|
|
|
rs->xforms->view_xform = glm::lookAt(cam_pos, look_pos, glm::vec3(0, 1, 0)); |
|
|
|
|
|
|
|
|
|
#if 1 |
|
|
|
|
// TODO: make helper function for getting UBO by name in shader.h
|
|
|
|
|
static GLBuffer* xform_ubo = nullptr; |
|
|
|
|
u64 h_search = utilFNV64a_str("matrices"); |
|
|
|
|
|
|
|
|
|
if (!xform_ubo) { |
|
|
|
|
for (u32 i = 0; i < rs->gl_ctx->num_ubos; i++) { |
|
|
|
|
GLBuffer* buf = &rs->gl_ctx->uniform_buffers[i]; |
|
|
|
|
|
|
|
|
|
if (buf->name != nullptr) { |
|
|
|
|
u64 hash = utilFNV64a_str(buf->name); |
|
|
|
|
if (hash == h_search) |
|
|
|
|
xform_ubo = buf; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!xform_ubo) |
|
|
|
|
LOGF(Error, "matrices ubo not found\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateCameraTransforms(rs->xforms, xform_ubo); |
|
|
|
|
////
|
|
|
|
|
#endif |
|
|
|
|
#if 0 |
|
|
|
|
for (u32 i = 0; i < rs->num_render_groups; i++) { |
|
|
|
|
RenderGroup& rg = rs->render_groups[i]; |
|
|
|
|
|
|
|
|
|
@ -114,17 +176,17 @@ render_cb_pre(RenderState* rs)
|
|
|
|
|
direction * (float) M_PI / (3 * 60)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
main() |
|
|
|
|
{ |
|
|
|
|
RenderState* rs = initRenderState(); |
|
|
|
|
rs->clear_col = {0.2, 0.2, 0.2, 1}; |
|
|
|
|
|
|
|
|
|
if (rs) { |
|
|
|
|
if (!loadScene(rs)) { |
|
|
|
|
LOG(Error) << "error loading scene\n"; |
|
|
|
|
LOGF(Error, "error loading scene\n"); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -134,7 +196,7 @@ main()
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::cout << "error loading scene\n"; |
|
|
|
|
LOGF(Error, "error loading scene\n"); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|