summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/shaders/pbr_params.frag15
-rw-r--r--assets/shaders/pbr_params.vert1
-rw-r--r--examples/pbr_params/ex_pbr_params.c59
-rw-r--r--src/renderer/backends/opengl/backend_opengl.c11
-rw-r--r--src/renderer/backends/opengl/backend_opengl.h5
5 files changed, 65 insertions, 26 deletions
diff --git a/assets/shaders/pbr_params.frag b/assets/shaders/pbr_params.frag
index 0833c2f..ae4c1d2 100644
--- a/assets/shaders/pbr_params.frag
+++ b/assets/shaders/pbr_params.frag
@@ -36,15 +36,19 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
void main() {
vec3 norm = normalize(fragNormal); // N
+ vec3 N = norm;
vec3 viewDir = normalize(vec3(scene.viewPos) - fragWorldPos); // V
+ vec3 V = viewDir;
vec3 F0 = vec3(0.04);
F0 = mix(F0, pbr.albedo, pbr.metallic);
- vec3 Lo = vec3(0.0); // denoted L in the radiance equation
- for (int i = 0; i < 4; i++) {
+ vec3 Lo = vec3(0.0);
+ for (int i = 0; i < 4; ++i) {
vec3 lightVec = normalize(vec3(scene.pointLights[i].position) - fragWorldPos); // L
+ vec3 L = lightVec;
vec3 halfway = normalize(viewDir + lightVec); // H
+ vec3 H = halfway;
float distance = length(vec3(scene.pointLights[i].position) - fragWorldPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = vec3(scene.pointLights[i].color) * attenuation;
@@ -52,10 +56,11 @@ void main() {
// cook-torrance brdf
float NDF = DistributionGGX(norm, halfway, pbr.roughness);
float G = GeometrySmith(norm, viewDir, lightVec, pbr.roughness);
- vec3 F = fresnelSchlick(max(dot(halfway, viewDir), 0.0), F0);
+ // vec3 F = fresnelSchlick(max(dot(halfway, viewDir), 0.0), F0);
+ vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
vec3 numerator = NDF * G * F;
- float denominator = 4.0 * max(dot(norm, viewDir), 0.0) * max(dot(norm, lightVec), 0.0) + 0.0001;
+ float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
vec3 kS = F;
@@ -64,7 +69,7 @@ void main() {
kD *= 1.0 - pbr.metallic;
// add to outgoing radiance Lo
- float NdotL = max(dot(norm, lightVec), 0.0);
+ float NdotL = max(dot(N, L), 0.0);
Lo += (kD * pbr.albedo / PI + specular) * radiance * NdotL;
// Lo += radiance;
}
diff --git a/assets/shaders/pbr_params.vert b/assets/shaders/pbr_params.vert
index 01d0cfe..40a6f29 100644
--- a/assets/shaders/pbr_params.vert
+++ b/assets/shaders/pbr_params.vert
@@ -20,6 +20,7 @@ layout(location = 2) out vec2 fragTexCoords;
void main() {
fragWorldPos = vec3(mvp.model * vec4(inPosition, 1.0));
fragNormal = mat3(transpose(inverse(mvp.model))) * inNormal;
+ // fragNormal = inNormal;
fragTexCoords = inTexCoords;
diff --git a/examples/pbr_params/ex_pbr_params.c b/examples/pbr_params/ex_pbr_params.c
index 18e0fc9..fbe5f3a 100644
--- a/examples/pbr_params/ex_pbr_params.c
+++ b/examples/pbr_params/ex_pbr_params.c
@@ -1,4 +1,5 @@
#include <glfw3.h>
+#include <stdbool.h>
#include "builtin_materials.h"
#include "camera.h"
@@ -16,7 +17,7 @@
extern core g_core;
const vec3 pointlight_positions[4] = {
- { 10.0 / 1., 10.0 / 1., 10.0 / 5 },
+ { 10.0 / 1., 10.0 / 1., 10.0 },
{ -10.0 / 1., 10.0 / 1., 10.0 },
{ 10.0 / 1., -10.0 / 1., 10.0 },
{ -10.0 / 1., -10.0 / 1., 10.0 },
@@ -25,12 +26,16 @@ pbr_point_light point_lights[4];
// Lets define some constant PBR parameters here to test on one sphere with first
const rgba ALBEDO = RED_700;
-const f32 metallic = 0.7;
-const f32 roughness = 0.8;
+const f32 metallic = 1.0;
+const f32 roughness = 0.3;
const f32 ao = 1.0;
-const pbr_params_material_uniforms pbr_params = {
- .albedo = (vec3){ 0.6, 0.0, 0.0 }, .metallic = metallic, .roughness = roughness, .ao = ao
+const int num_rows = 7;
+const int num_cols = 7;
+const float spacing = 2.5;
+
+pbr_params_material_uniforms pbr_params = {
+ .albedo = (vec3){ 0.5, 0.0, 0.0 }, .metallic = metallic, .roughness = roughness, .ao = ao
};
int main() {
@@ -44,7 +49,7 @@ int main() {
point_lights[i].color = vec3(300.0, 300.0, 300.0);
}
- vec3 camera_pos = vec3(1.0, 1., -2.);
+ vec3 camera_pos = vec3(1.,1., -25.);
vec3 camera_front = vec3_normalise(vec3_negate(camera_pos));
camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0));
@@ -83,9 +88,12 @@ int main() {
gpu_pipeline* pbr_pipeline = gpu_graphics_pipeline_create(pipeline_description);
// Geometry
- geometry_data sphere_data = geo_create_uvsphere(1.0, 48, 48);
+ geometry_data sphere_data = geo_create_uvsphere(1.0, 64, 64);
mesh sphere = mesh_create(&sphere_data, false);
+ geometry_data cube_data = geo_create_cuboid(f32x3(1, 1, 1));
+ mesh cube = mesh_create(&cube_data, false);
+
pbr_params_bindgroup pbr_bind_data;
while (!should_exit()) {
@@ -116,14 +124,41 @@ int main() {
point_lights[2], point_lights[3] }
};
pbr_uniforms.data = &pbr_bind_data;
- encode_bind_shader_data(enc, 0, &pbr_uniforms);
+ // encode_bind_shader_data(enc, 0, &pbr_uniforms);
// Record draw call
/* draw_mesh(&sphere, &model_affine, &cam); */
- encode_set_vertex_buffer(enc, sphere.vertex_buffer);
- encode_set_index_buffer(enc, sphere.index_buffer);
- encode_draw_indexed(enc, sphere.geometry->indices->len);
-
+ #if 0
+ encode_set_vertex_buffer(enc, cube.vertex_buffer);
+ encode_set_index_buffer(enc, cube.index_buffer);
+ encode_draw_indexed(enc, cube.geometry->indices->len);
+ #endif
+
+ for (u32 row = 0; row < num_rows; row++) {
+ f32 metallic = (float)row / (float)num_rows;
+ for (u32 col = 0; col < num_cols; col++) {
+ f32 roughness = (float)col / (float)num_cols;
+ if (roughness == 0.0) { roughness += 0.05; };
+ if (roughness == 1.0) { roughness -= 0.05; };
+
+ pbr_bind_data.material.metallic = metallic;
+ pbr_bind_data.material.roughness = roughness;
+
+ f32 x = (col - ((f32)num_cols / 2.)) * spacing;
+ f32 y = (row - ((f32)num_rows / 2.)) * spacing;
+ mat4 model = mat4_translation(vec3(
+ x,
+ y,
+ 0.0f
+ ));
+ pbr_bind_data.mvp_matrices.model = model;
+ encode_bind_shader_data(enc, 0, &pbr_uniforms);
+ encode_set_vertex_buffer(enc, sphere.vertex_buffer);
+ encode_set_index_buffer(enc, sphere.index_buffer);
+ encode_draw_indexed(enc, sphere.geometry->indices->len);
+ }
+ }
+
// End recording
gpu_cmd_encoder_end_render(enc);
diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c
index 18e7d2a..9ae77b4 100644
--- a/src/renderer/backends/opengl/backend_opengl.c
+++ b/src/renderer/backends/opengl/backend_opengl.c
@@ -199,20 +199,15 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d
if (blockIndex < 0) {
WARN("Couldn't retrieve block index for uniform block '%s'", binding.label);
} else {
- // DEBUG("Retrived block index %d for %s", blockIndex, binding.label);
+ DEBUG("Retrived block index %d for %s", blockIndex, binding.label);
}
glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo);
glBindBufferBase(GL_UNIFORM_BUFFER, i, ubo_buf->id.ubo);
if (i == 2) {
- pbr_params_light_uniforms* u = binding.data.bytes.data;
- vec4* v = &u->viewPos;
- (*v).x = 0.0;
- (*v).y = 0.0;
- (*v).z = 1.0;
- // print_vec3(*v);
+ // pbr_params_light_uniforms* u = binding.data.bytes.data;
+ // vec4* v = &u->viewPos;
}
- // glBindBufferBase(GL_UNIFORM_BUFFER, i, ubo_buf->id.ubo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, binding.data.bytes.data);
} else if (binding.type == SHADER_BINDING_TEXTURE) {
diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h
index cde52a3..f52bd79 100644
--- a/src/renderer/backends/opengl/backend_opengl.h
+++ b/src/renderer/backends/opengl/backend_opengl.h
@@ -38,7 +38,10 @@ typedef struct gpu_buffer {
u32 ibo;
u32 ubo;
} id;
- u32 vao; // Optional
+ union {
+ u32 vao;
+ u32 ubo_binding_point
+ }; // Optional
u64 size;
} gpu_buffer;
typedef struct gpu_texture {