diff options
Diffstat (limited to 'src/render/llr.c')
| -rw-r--r-- | src/render/llr.c | 121 |
1 files changed, 19 insertions, 102 deletions
diff --git a/src/render/llr.c b/src/render/llr.c index 76935f9..752b65b 100644 --- a/src/render/llr.c +++ b/src/render/llr.c | |||
| @@ -1,14 +1,15 @@ | |||
| 1 | #include "llr_impl.h" | 1 | #include "llr_impl.h" |
| 2 | 2 | ||
| 3 | #include "animation_impl.h" | 3 | #include "animation_impl.h" |
| 4 | #include "memory.h" | 4 | #include "scene/light_impl.h" |
| 5 | #include "scene/material_impl.h" | ||
| 6 | #include "scene/mesh_impl.h" | ||
| 5 | #include "scene/node_impl.h" | 7 | #include "scene/node_impl.h" |
| 6 | 8 | ||
| 7 | #include <gfx/core.h> | 9 | #include <gfx/core.h> |
| 8 | #include <gfx/util/ibl.h> | 10 | #include <gfx/util/ibl.h> |
| 9 | 11 | ||
| 10 | #include <cassert.h> | 12 | #include <cassert.h> |
| 11 | #include <error.h> | ||
| 12 | 13 | ||
| 13 | static const int IRRADIANCE_MAP_WIDTH = 1024; | 14 | static const int IRRADIANCE_MAP_WIDTH = 1024; |
| 14 | static const int IRRADIANCE_MAP_HEIGHT = 1024; | 15 | static const int IRRADIANCE_MAP_HEIGHT = 1024; |
| @@ -17,114 +18,30 @@ static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128; | |||
| 17 | static const int BRDF_INTEGRATION_MAP_WIDTH = 512; | 18 | static const int BRDF_INTEGRATION_MAP_WIDTH = 512; |
| 18 | static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; | 19 | static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; |
| 19 | 20 | ||
| 20 | static void make_environment_light( | ||
| 21 | Light* light, const EnvironmentLightDesc* desc) { | ||
| 22 | assert(light); | ||
| 23 | assert(desc); | ||
| 24 | light->type = EnvironmentLightType; | ||
| 25 | light->environment.environment_map = desc->environment_map; | ||
| 26 | } | ||
| 27 | |||
| 28 | Light* gfx_make_light(const LightDesc* desc) { | ||
| 29 | assert(desc); | ||
| 30 | |||
| 31 | Light* light = mem_alloc_light(); | ||
| 32 | |||
| 33 | switch (desc->type) { | ||
| 34 | case EnvironmentLightType: | ||
| 35 | make_environment_light(light, &desc->light.environment); | ||
| 36 | break; | ||
| 37 | default: | ||
| 38 | log_error("Unhandled light type"); | ||
| 39 | gfx_destroy_light(&light); | ||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | return light; | ||
| 44 | } | ||
| 45 | |||
| 46 | void gfx_destroy_light(Light** light) { | ||
| 47 | assert(light); | ||
| 48 | if (*light) { | ||
| 49 | mem_free_light(light); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | static void material_make(Material* material, const MaterialDesc* desc) { | ||
| 54 | assert(material); | ||
| 55 | assert(desc); | ||
| 56 | assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL); | ||
| 57 | material->num_uniforms = desc->num_uniforms; | ||
| 58 | for (int i = 0; i < desc->num_uniforms; ++i) { | ||
| 59 | material->uniforms[i] = desc->uniforms[i]; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | Material* gfx_make_material(const MaterialDesc* desc) { | ||
| 64 | assert(desc); | ||
| 65 | Material* material = mem_alloc_material(); | ||
| 66 | material_make(material, desc); | ||
| 67 | return material; | ||
| 68 | } | ||
| 69 | |||
| 70 | void gfx_destroy_material(Material** material) { mem_free_material(material); } | ||
| 71 | |||
| 72 | static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) { | ||
| 73 | switch (uniform->type) { | ||
| 74 | case UniformTexture: | ||
| 75 | gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture); | ||
| 76 | break; | ||
| 77 | case UniformMat4: | ||
| 78 | gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.mat4); | ||
| 79 | break; | ||
| 80 | case UniformVec3: | ||
| 81 | gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.vec3); | ||
| 82 | break; | ||
| 83 | case UniformVec4: | ||
| 84 | gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.vec4); | ||
| 85 | break; | ||
| 86 | case UniformFloat: | ||
| 87 | gfx_set_float_uniform(prog, uniform->name.str, uniform->value.scalar); | ||
| 88 | break; | ||
| 89 | case UniformMat4Array: | ||
| 90 | gfx_set_mat4_array_uniform( | ||
| 91 | prog, uniform->name.str, uniform->value.array.values, | ||
| 92 | uniform->value.array.count); | ||
| 93 | break; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | /// Activate the material. | 21 | /// Activate the material. |
| 98 | /// | 22 | /// |
| 99 | /// This configures the shader uniforms that are specific to the material. | 23 | /// This configures the shader uniforms that are specific to the material. |
| 100 | static void gfx_material_activate( | 24 | static void material_activate(ShaderProgram* shader, const Material* material) { |
| 101 | ShaderProgram* shader, const Material* material) { | ||
| 102 | assert(material); | 25 | assert(material); |
| 103 | for (int i = 0; i < material->num_uniforms; ++i) { | 26 | for (int i = 0; i < material->num_uniforms; ++i) { |
| 104 | const ShaderUniform* uniform = &material->uniforms[i]; | 27 | const ShaderUniform* uniform = &material->uniforms[i]; |
| 105 | set_uniform(shader, uniform); | 28 | gfx_set_uniform(shader, uniform); |
| 29 | } | ||
| 30 | if (material->alpha_mode != Opaque) { | ||
| 31 | gfx_set_uniform( | ||
| 32 | shader, &(ShaderUniform){.name = sstring_make("AlphaMode"), | ||
| 33 | .type = UniformInt, | ||
| 34 | .value.uniform_int = material->alpha_mode}); | ||
| 35 | } | ||
| 36 | if (material->alpha_mode == Mask) { | ||
| 37 | gfx_set_uniform( | ||
| 38 | shader, | ||
| 39 | &(ShaderUniform){.name = sstring_make("AlphaCutoff"), | ||
| 40 | .type = UniformFloat, | ||
| 41 | .value.uniform_float = material->alpha_cutoff}); | ||
| 106 | } | 42 | } |
| 107 | } | 43 | } |
| 108 | 44 | ||
| 109 | static void mesh_make(Mesh* mesh, const MeshDesc* desc) { | ||
| 110 | assert(mesh); | ||
| 111 | assert(desc); | ||
| 112 | assert(desc->geometry); | ||
| 113 | assert(desc->material); | ||
| 114 | assert(desc->shader); | ||
| 115 | mesh->geometry = desc->geometry; | ||
| 116 | mesh->material = desc->material; | ||
| 117 | mesh->shader = desc->shader; | ||
| 118 | } | ||
| 119 | |||
| 120 | Mesh* gfx_make_mesh(const MeshDesc* desc) { | ||
| 121 | Mesh* mesh = mem_alloc_mesh(); | ||
| 122 | mesh_make(mesh, desc); | ||
| 123 | return mesh; | ||
| 124 | } | ||
| 125 | |||
| 126 | void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); } | ||
| 127 | |||
| 128 | /// Initialize renderer state for IBL. | 45 | /// Initialize renderer state for IBL. |
| 129 | static bool init_ibl(LLR* renderer) { | 46 | static bool init_ibl(LLR* renderer) { |
| 130 | assert(renderer); | 47 | assert(renderer); |
| @@ -314,7 +231,7 @@ static void configure_state(LLR* renderer) { | |||
| 314 | 231 | ||
| 315 | // Geometry may be rendered without a material. | 232 | // Geometry may be rendered without a material. |
| 316 | if (renderer->material) { | 233 | if (renderer->material) { |
| 317 | gfx_material_activate(renderer->shader, renderer->material); | 234 | material_activate(renderer->shader, renderer->material); |
| 318 | } | 235 | } |
| 319 | } | 236 | } |
| 320 | 237 | ||
