aboutsummaryrefslogtreecommitdiff
path: root/src/renderer/renderer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer/renderer.c')
-rw-r--r--src/renderer/renderer.c46
1 files changed, 30 insertions, 16 deletions
diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c
index c2a7dda..0c1fe78 100644
--- a/src/renderer/renderer.c
+++ b/src/renderer/renderer.c
@@ -1,10 +1,10 @@
1#include "renderer_impl.h" 1#include "renderer_impl.h"
2 2
3#include "llr/light_impl.h"
4#include "llr/material_impl.h"
5#include "llr/mesh_impl.h"
3#include "scene/animation_impl.h" 6#include "scene/animation_impl.h"
4#include "scene/camera_impl.h" 7#include "scene/camera_impl.h"
5#include "scene/light_impl.h"
6#include "scene/material_impl.h"
7#include "scene/mesh_impl.h"
8#include "scene/model_impl.h" 8#include "scene/model_impl.h"
9#include "scene/node_impl.h" 9#include "scene/node_impl.h"
10#include "scene/object_impl.h" 10#include "scene/object_impl.h"
@@ -12,16 +12,18 @@
12#include "scene/scene_memory.h" 12#include "scene/scene_memory.h"
13 13
14#include <gfx/core.h> 14#include <gfx/core.h>
15#include <gfx/llr/llr.h>
15#include <gfx/util/ibl.h> 16#include <gfx/util/ibl.h>
16#include <gfx/util/shader.h> 17#include <gfx/util/shader.h>
17 18
18#include <log/log.h> 19// #include <log/log.h>
20#include "gfx/gfx.h"
21
19#include <math/mat4.h> 22#include <math/mat4.h>
20#include <math/spatial3.h> 23#include <math/spatial3.h>
21 24
22#include <assert.h> 25#include <assert.h>
23 26
24// TODO: Move to a header like "constants.h".
25static const int IRRADIANCE_MAP_WIDTH = 1024; 27static const int IRRADIANCE_MAP_WIDTH = 1024;
26static const int IRRADIANCE_MAP_HEIGHT = 1024; 28static const int IRRADIANCE_MAP_HEIGHT = 1024;
27static const int PREFILTERED_ENVIRONMENT_MAP_WIDTH = 128; 29static const int PREFILTERED_ENVIRONMENT_MAP_WIDTH = 128;
@@ -29,16 +31,18 @@ static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128;
29static const int BRDF_INTEGRATION_MAP_WIDTH = 512; 31static const int BRDF_INTEGRATION_MAP_WIDTH = 512;
30static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; 32static const int BRDF_INTEGRATION_MAP_HEIGHT = 512;
31 33
32bool renderer_make(Renderer* renderer, GfxCore* gfxcore) { 34bool gfx_renderer_make(Renderer* renderer, LLR* llr, GfxCore* gfxcore) {
33 assert(renderer); 35 assert(renderer);
36 assert(llr);
34 assert(gfxcore); 37 assert(gfxcore);
35 38
36 renderer->gfxcore = gfxcore; 39 renderer->gfxcore = gfxcore;
40 renderer->llr = llr;
37 41
38 return true; 42 return true;
39} 43}
40 44
41void renderer_destroy(Renderer* renderer) { 45void gfx_renderer_destroy(Renderer* renderer) {
42 if (!renderer) { 46 if (!renderer) {
43 return; 47 return;
44 } 48 }
@@ -117,9 +121,9 @@ static ShaderProgram* load_shader(Renderer* renderer, RenderSceneMode mode) {
117// } 121// }
118// } 122// }
119 123
120/// Computes irradiance and prefiltered environment maps for the light if they 124/// Compute irradiance and prefiltered environment maps for the light if they
121/// have not been already computed. 125/// have not been already computed.
122static bool setup_environment_light( 126static bool set_up_environment_light(
123 Renderer* renderer, GfxCore* gfxcore, EnvironmentLight* light) { 127 Renderer* renderer, GfxCore* gfxcore, EnvironmentLight* light) {
124 assert(renderer); 128 assert(renderer);
125 assert(light); 129 assert(light);
@@ -168,6 +172,7 @@ cleanup:
168 172
169typedef struct RenderState { 173typedef struct RenderState {
170 GfxCore* gfxcore; 174 GfxCore* gfxcore;
175 LLR* llr;
171 Renderer* renderer; 176 Renderer* renderer;
172 ShaderProgram* shader; // Null to use scene shaders. 177 ShaderProgram* shader; // Null to use scene shaders.
173 const Scene* scene; 178 const Scene* scene;
@@ -209,6 +214,7 @@ static void draw_recursively(
209 214
210 // Anima. 215 // Anima.
211 if (node->type == AnimaNode) { 216 if (node->type == AnimaNode) {
217 // Save the anima so that we can animate objects.
212 state->anima = gfx_get_node_anima(node); 218 state->anima = gfx_get_node_anima(node);
213 } 219 }
214 // Activate light. 220 // Activate light.
@@ -217,7 +223,7 @@ static void draw_recursively(
217 assert(light); 223 assert(light);
218 224
219 if (light->type == EnvironmentLightType) { 225 if (light->type == EnvironmentLightType) {
220 bool result = setup_environment_light( 226 bool result = set_up_environment_light(
221 state->renderer, state->gfxcore, &light->environment); 227 state->renderer, state->gfxcore, &light->environment);
222 // TODO: Handle the result in a better way. 228 // TODO: Handle the result in a better way.
223 assert(result); 229 assert(result);
@@ -238,11 +244,13 @@ static void draw_recursively(
238 // TODO: Here we would frustum-cull the object. 244 // TODO: Here we would frustum-cull the object.
239 245
240 // TODO: Avoid computing matrices like Modelview or MVP if the shader does 246 // TODO: Avoid computing matrices like Modelview or MVP if the shader does
241 // not use them. 247 // not use them.
242 const mat4 model_matrix = node_transform; 248 const mat4 model_matrix = node_transform;
243 const mat4 modelview = mat4_mul(*state->view_matrix, model_matrix); 249 const mat4 modelview = mat4_mul(*state->view_matrix, model_matrix);
244 const mat4 mvp = mat4_mul(*state->projection, modelview); 250 const mat4 mvp = mat4_mul(*state->projection, modelview);
245 251
252 // A model/anima can have many skeletons. We need to animate the given
253 // object using its skeleton, not just any skeleton of the anima.
246 if (object->skeleton.val) { 254 if (object->skeleton.val) {
247 load_skeleton(state, object->skeleton); 255 load_skeleton(state, object->skeleton);
248 } 256 }
@@ -260,9 +268,11 @@ static void draw_recursively(
260 assert(mesh->material); 268 assert(mesh->material);
261 269
262 // TODO: Here we would frustum-cull the mesh. The AABB would have to be 270 // TODO: Here we would frustum-cull the mesh. The AABB would have to be
263 // transformed by the model matrix. Rotation would make the AABB 271 // transformed by the model matrix. Rotation would make the AABB
264 // relatively large, but still, the culling would be conservative. 272 // relatively large, but still, the culling would be conservative.
265 273
274 // TODO: Make sure we strictly set only the uniforms that are required by
275 // mesh rendering. See the other item below.
266 // Apply common shader uniforms not captured by materials. 276 // Apply common shader uniforms not captured by materials.
267 ShaderProgram* shader = state->shader ? state->shader : mesh->shader; 277 ShaderProgram* shader = state->shader ? state->shader : mesh->shader;
268 gfx_set_mat4_uniform(shader, "ModelMatrix", &model_matrix); 278 gfx_set_mat4_uniform(shader, "ModelMatrix", &model_matrix);
@@ -270,7 +280,10 @@ static void draw_recursively(
270 gfx_set_mat4_uniform(shader, "View", state->view_matrix); 280 gfx_set_mat4_uniform(shader, "View", state->view_matrix);
271 gfx_set_mat4_uniform(shader, "Projection", state->projection); 281 gfx_set_mat4_uniform(shader, "Projection", state->projection);
272 gfx_set_mat4_uniform(shader, "MVP", &mvp); 282 gfx_set_mat4_uniform(shader, "MVP", &mvp);
283 // TODO: CameraRotation is only used by the skyquad and cubemap_filtering
284 // shaders, not mesh rendering.
273 gfx_set_mat4_uniform(shader, "CameraRotation", state->camera_rotation); 285 gfx_set_mat4_uniform(shader, "CameraRotation", state->camera_rotation);
286 // TODO: Fovy and Aspect are only used by the skyquad, not necessary here.
274 gfx_set_float_uniform(shader, "Fovy", state->fovy); 287 gfx_set_float_uniform(shader, "Fovy", state->fovy);
275 gfx_set_float_uniform(shader, "Aspect", state->aspect); 288 gfx_set_float_uniform(shader, "Aspect", state->aspect);
276 if (state->camera) { 289 if (state->camera) {
@@ -297,9 +310,9 @@ static void draw_recursively(
297 shader, "PrefilteredEnvironmentMap", 310 shader, "PrefilteredEnvironmentMap",
298 light->prefiltered_environment_map); 311 light->prefiltered_environment_map);
299 gfx_set_float_uniform( 312 gfx_set_float_uniform(
300 shader, "MaxReflectionLOD", light->max_reflection_lod); 313 shader, "MaxReflectionLOD", (float)light->max_reflection_lod);
301 } 314 }
302 material_activate(shader, mesh->material); 315 gfx_material_activate(shader, mesh->material);
303 gfx_activate_shader_program(shader); 316 gfx_activate_shader_program(shader);
304 gfx_apply_uniforms(shader); 317 gfx_apply_uniforms(shader);
305 gfx_render_geometry(mesh->geometry); 318 gfx_render_geometry(mesh->geometry);
@@ -327,7 +340,7 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) {
327 const Scene* scene = params->scene; 340 const Scene* scene = params->scene;
328 const SceneCamera* camera = params->camera; 341 const SceneCamera* camera = params->camera;
329 342
330 GfxCore* gfxcore = renderer->gfxcore; 343 GfxCore* const gfxcore = renderer->gfxcore;
331 344
332 mat4 projection, camera_rotation, view_matrix; 345 mat4 projection, camera_rotation, view_matrix;
333 if (camera) { 346 if (camera) {
@@ -347,6 +360,7 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) {
347 360
348 RenderState state = { 361 RenderState state = {
349 .gfxcore = gfxcore, 362 .gfxcore = gfxcore,
363 .llr = renderer->llr,
350 .renderer = renderer, 364 .renderer = renderer,
351 .shader = shader, 365 .shader = shader,
352 .scene = scene, 366 .scene = scene,