diff --git a/src/tangerine.cpp b/src/tangerine.cpp index dbafd91..75d9515 100644 --- a/src/tangerine.cpp +++ b/src/tangerine.cpp @@ -10,21 +10,25 @@ bool initGraphics(SDLHandles* handles); +LightsBuffer initLights(MemoryArena* arena, GLContext* gl_ctx, u32 max_lights); // interface RenderState* -initRenderState(u32 max_models, +initRenderState(GLClearColor clear_col, + u32 max_models, u32 max_textures, u32 max_shaders, u32 max_render_groups, - u32 max_ubos) + u32 max_ubos, + u32 max_lights) { LOGF(Info, "Initializing Renderer\n"); RenderState* rs = UTIL_ALLOC(1, RenderState); if (rs) { + rs->clear_col = clear_col; rs->assets.arena = arenaInit(DEFAULT_ARENA_SIZE); rs->assets.max_models = max_models; rs->assets.models = ARENA_ALLOC(rs->assets.arena, Model, max_models); @@ -48,10 +52,11 @@ initRenderState(u32 max_models, max_ubos); rs->xforms = ARENA_ALLOC(rs->assets.arena, Transforms, 1); + GLBuffer* xforms_ubo = getFreeUBO(rs->gl_ctx); + assert(xforms_ubo); + initTransforms(rs->assets.arena, rs->xforms, xforms_ubo, rs->gl_ctx); - // FIXME: need to test this with another shader that has another UBO - GLBuffer& ubo = rs->gl_ctx->uniform_buffers[rs->gl_ctx->num_ubos++]; - initTransforms(rs->assets.arena, rs->xforms, &ubo, rs->gl_ctx); + rs->lights_buf = initLights(rs->rg_arena, rs->gl_ctx, max_lights); bool ret = loadDefaultShaders(rs); assert(ret); @@ -285,3 +290,39 @@ initGraphics(SDLHandles* handles) return handles->window != nullptr; } +LightsBuffer +initLights(MemoryArena* arena, GLContext* gl_ctx, u32 max_lights) +{ + // FIXME: revisit for 'Scene' abstraction + LightsBuffer lights_buf; + lights_buf.max_p_lights = max_lights; + lights_buf.max_d_lights = max_lights; + lights_buf.p_lights = ARENA_ALLOC(arena, PointLight, max_lights); + lights_buf.d_lights = ARENA_ALLOC(arena, PointLight, max_lights); + + GLBuffer* lights_ubo = getFreeUBO(gl_ctx); + assert(lights_ubo); + + lights_ubo->target = GL_UNIFORM_BUFFER; + lights_ubo->data_type = GL_BYTE; // NOTE: mixed types in structure + lights_ubo->name = arenaCopyCStr(arena, "lights"); + assert((GLint) gl_ctx->binding_count < gl_ctx->max_binding_points); + lights_ubo->binding_idx = gl_ctx->binding_count++; + + // FIXME: need to pad any vec3s to vec4 to match layout std140 + u32 buf_size = sizeof(LightsBuffer) + + max_lights * sizeof(PointLight) + + max_lights * sizeof(PointLight); + glGenBuffers(1, &lights_ubo->id); + glBindBuffer(lights_ubo->target, lights_ubo->id); + glBufferData(lights_ubo->target, + buf_size, + &lights_buf, + GL_DYNAMIC_DRAW); + glBindBufferBase(lights_ubo->target, + lights_ubo->binding_idx, + lights_ubo->id); + + return lights_buf; +} + diff --git a/src/tangerine.h b/src/tangerine.h index 8178c16..94942fe 100644 --- a/src/tangerine.h +++ b/src/tangerine.h @@ -136,9 +136,12 @@ struct RenderState // could have render groups, lights, camera // could match up with gltf scene graph where everyting is part of a node // TODO: will also need other types of lights: directional, spotlight + LightsBuffer lights_buf; +#if 0 u32 num_lights; u32 max_lights; PointLight* lights; +#endif }; @@ -147,11 +150,15 @@ struct RenderState #define DEFAULT_SHADER_COUNT 64 #define DEFAULT_UBO_COUNT 32 #define DEFAULT_RENDER_GROUP_COUNT 256 -RenderState* initRenderState(u32 max_models = DEFAULT_MODEL_COUNT, +#define DEFAULT_CLEAR_COLOR { 0.2, 0.2, 0.2, 1 } +#define DEFAULT_MAX_LIGHTS 32 // NOTE: needs to match NUM_LIGHTS in shaders +RenderState* initRenderState(GLClearColor clear_col = DEFAULT_CLEAR_COLOR, + u32 max_models = DEFAULT_MODEL_COUNT, u32 max_textures = DEFAULT_TEXTURE_COUNT, u32 max_shaders = DEFAULT_SHADER_COUNT, u32 max_render_groups = DEFAULT_RENDER_GROUP_COUNT, - u32 max_ubos = DEFAULT_UBO_COUNT); + u32 max_ubos = DEFAULT_UBO_COUNT, + u32 max_lights = DEFAULT_MAX_LIGHTS); void freeRenderState(RenderState*& rs);