diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-18 22:31:03 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-18 22:31:03 +1000 |
commit | 259da84075c62c2ab96c7cb922df2000ebefb735 (patch) | |
tree | 423106dd69711794a4d5b3d1f5031a65960d186d | |
parent | b2c9782ab2f4abec8f8548e667a8f2ad6e36bd4b (diff) |
pbr progress
-rw-r--r-- | assets/shaders/pbr_textured.frag | 32 | ||||
-rw-r--r-- | examples/pbr_textured/ex_pbr_textured.c | 10 | ||||
-rw-r--r-- | examples/shadow_maps/ex_shadow_maps.c | 0 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 2 | ||||
-rw-r--r-- | src/renderer/builtin_materials.h | 8 | ||||
-rw-r--r-- | src/renderer/ral.h | 4 |
6 files changed, 42 insertions, 14 deletions
diff --git a/assets/shaders/pbr_textured.frag b/assets/shaders/pbr_textured.frag index 29d59a0..e76a099 100644 --- a/assets/shaders/pbr_textured.frag +++ b/assets/shaders/pbr_textured.frag @@ -35,14 +35,32 @@ float DistributionGGX(vec3 N, vec3 H, float roughness); float GeometrySchlickGGX(float NdotV, float roughness); float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness); +vec3 getNormalFromMap() +{ + vec3 tangentNormal = texture(normalMap, fragTexCoords).xyz * 2.0 - 1.0; + + vec3 Q1 = dFdx(fragWorldPos); + vec3 Q2 = dFdy(fragWorldPos); + vec2 st1 = dFdx(fragTexCoords); + vec2 st2 = dFdy(fragTexCoords); + + vec3 N = normalize(fragNormal); + vec3 T = normalize(Q1*st2.t - Q2*st1.t); + vec3 B = -normalize(cross(N, T)); + mat3 TBN = mat3(T, B, N); + + return normalize(TBN * tangentNormal); +} + void main() { - vec3 albedo = pow(texture(albedoMap, fragTexCoords).rgb, vec3(2.2)); - // vec3 normal = getNormalFromNormalMap(); - float metallic = texture(metallicRoughnessMap, fragTexCoords).g; - float roughness = texture(metallicRoughnessMap, fragTexCoords).b; + // vec3 albedo = pow(texture(albedoMap, fragTexCoords).rgb, vec3(2.2)); + vec3 albedo = texture(albedoMap, fragTexCoords).rgb; + float metallic = texture(metallicRoughnessMap, fragTexCoords).b; + float roughness = texture(metallicRoughnessMap, fragTexCoords).g; float ao = texture(aoMap, fragTexCoords).r; - vec3 norm = normalize(fragNormal); // N + // vec3 norm = normalize(fragNormal); // N + vec3 norm = getNormalFromMap(); vec3 N = norm; vec3 viewDir = normalize(vec3(scene.viewPos) - fragWorldPos); // V vec3 V = viewDir; @@ -87,12 +105,12 @@ void main() { color = color / (color + vec3(1.0)); color = pow(color, vec3(1.0/2.2)); - // FragColor = vec4(color, 1.0); + FragColor = vec4(color, 1.0); // FragColor = vec4(1.0); // FragColor = vec4(scene.pointLights[0].position); // FragColor = vec4(albedo, 1.0); // FragColor = vec4(pbr.metallic, pbr.roughness, pbr.ao, 1.0); - FragColor = vec4(fragTexCoords, 0.0, 1.0); + // FragColor = vec4(fragTexCoords, 0.0, 1.0); } /* The below are from https://learnopengl.com/PBR/Lighting */ diff --git a/examples/pbr_textured/ex_pbr_textured.c b/examples/pbr_textured/ex_pbr_textured.c index 8c48b8d..fa1d555 100644 --- a/examples/pbr_textured/ex_pbr_textured.c +++ b/examples/pbr_textured/ex_pbr_textured.c @@ -37,7 +37,7 @@ int main() { point_lights[i].color = vec3(300.0, 300.0, 300.0); } - vec3 camera_pos = vec3(5., 0., 0.); + vec3 camera_pos = vec3(3., 2., 0.); vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); camera cam = camera_create(camera_pos, camera_front, VEC3_NEG_Z, deg_to_rad(45.0)); @@ -82,6 +82,8 @@ int main() { pbr_textured_bindgroup pbr_bind_data; + static f32 theta = 0.0; + while (!should_exit()) { input_update(&g_core.input); @@ -95,7 +97,11 @@ int main() { encode_bind_pipeline(enc, PIPELINE_GRAPHICS, pbr_pipeline); encode_set_default_settings(enc); - mat4 model_affine = mat4_ident(); + theta += 0.01; + transform transform = { .position = vec3(0,0,0), + .rotation = quat_from_axis_angle(VEC3_Z, theta, true), + .scale = 1.0 }; + mat4 model_affine = transform_to_mat(&transform); mat4 view, proj; camera_view_projection(&cam, 1000, 1000, &view, &proj); diff --git a/examples/shadow_maps/ex_shadow_maps.c b/examples/shadow_maps/ex_shadow_maps.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/shadow_maps/ex_shadow_maps.c diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index 18cd7ee..18d1617 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -211,6 +211,8 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d } else if (binding.type == SHADER_BINDING_TEXTURE) { gpu_texture* tex = TEXTURE_GET(binding.data.texture.handle); + GLuint tex_slot = glGetUniformLocation(encoder->pipeline->shader_id, binding.label); + glUniform1i(tex_slot, i); glActiveTexture(GL_TEXTURE0 + i); glBindTexture(GL_TEXTURE_2D, tex->id); } diff --git a/src/renderer/builtin_materials.h b/src/renderer/builtin_materials.h index f0e7d2f..f2db5f4 100644 --- a/src/renderer/builtin_materials.h +++ b/src/renderer/builtin_materials.h @@ -127,16 +127,16 @@ static shader_data_layout pbr_textured_shader_layout(void* data) { .stores_data = has_data, .data = { .bytes = { .size = sizeof(pbr_params_light_uniforms) } } }; - shader_binding b3 = {.label = "Albedo", + shader_binding b3 = {.label = "albedoMap", .type = SHADER_BINDING_TEXTURE, .stores_data = has_data }; - shader_binding b4 = {.label = "Metallic Roughness", + shader_binding b4 = {.label = "metallicRoughnessMap", .type = SHADER_BINDING_TEXTURE, .stores_data = has_data }; - shader_binding b5 = {.label = "Ambient Occlusion", + shader_binding b5 = {.label = "aoMap", .type = SHADER_BINDING_TEXTURE, .stores_data = has_data }; - shader_binding b6 = {.label = "Normal Vectors", + shader_binding b6 = {.label = "normalMap", .type = SHADER_BINDING_TEXTURE, .stores_data = has_data }; diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 067847b..cd3f19f 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -96,7 +96,9 @@ struct graphics_pipeline_desc { bool depth_test; }; -typedef struct gpu_renderpass_desc { /* TODO */ +typedef struct gpu_renderpass_desc { + texture_handle color_target; // for now only support one + texture_handle depth_stencil; } gpu_renderpass_desc; // --- Lifecycle functions |