pbr: transform light pos to camera space in vert shader; prepare to modularize
This commit is contained in:
parent
ec987a896f
commit
1d1dd59b80
6 changed files with 41 additions and 42 deletions
|
@ -2,9 +2,21 @@
|
|||
#define I_PBR_H
|
||||
|
||||
#include "standard.glslh"
|
||||
#include "../lib/pbr.glslh"
|
||||
|
||||
UNIFORM(1) mat4 camera_transform;
|
||||
UNIFORM(2) sampler2D roughness_map;
|
||||
UNIFORM(3) sampler2D normal_map;
|
||||
UNIFORM(4) sampler2D ambient_map;
|
||||
UNIFORM(5) float metallic;
|
||||
UNIFORM(6) int light_count;
|
||||
UNIFORM(7) vec3 ambient_color; // modulates ambient map
|
||||
UNIFORM(8) vec3 light_positions[PBR_MAX_LIGHTS];
|
||||
UNIFORM(14) vec3 light_colors[PBR_MAX_LIGHTS]; // layout-id also depends on PBR_MAX_LIGHTS
|
||||
|
||||
VARYING(3) vec3 pos;
|
||||
VARYING(4) vec3 tangent;
|
||||
VARYING(5) vec3 bitangent;
|
||||
VARYING(6) PointLight point_lights[PBR_MAX_LIGHTS];
|
||||
|
||||
#endif
|
||||
|
|
12
resources/00-taisei.pkgdir/shader/lib/pbr.glslh
Normal file
12
resources/00-taisei.pkgdir/shader/lib/pbr.glslh
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
#ifndef PBR_H
|
||||
#define PBR_H
|
||||
|
||||
#define PBR_MAX_LIGHTS 6
|
||||
|
||||
struct PointLight {
|
||||
vec3 dir; // vector from lit fragment to light, not normalized
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -4,18 +4,6 @@
|
|||
#include "lib/render_context.glslh"
|
||||
#include "interface/pbr.glslh"
|
||||
|
||||
UNIFORM(1) sampler2D roughness_map;
|
||||
UNIFORM(2) sampler2D normal_map;
|
||||
UNIFORM(3) sampler2D ambient_map;
|
||||
UNIFORM(4) float metallic;
|
||||
UNIFORM(5) int light_count;
|
||||
UNIFORM(6) vec3 ambient_color; // modulates ambient map
|
||||
#define MAX_LIGHT_COUNT 6
|
||||
|
||||
UNIFORM(7) vec3 light_positions[MAX_LIGHT_COUNT];
|
||||
UNIFORM(13) vec3 light_colors[MAX_LIGHT_COUNT]; // layout-id also depends on MAX_LIGHT_COUNT
|
||||
|
||||
|
||||
// taken from https://learnopengl.com/PBR/Lighting
|
||||
float distribution_ggx(float ndoth, float roughness) {
|
||||
float a = roughness*roughness;
|
||||
|
@ -79,11 +67,11 @@ void main(void) {
|
|||
|
||||
vec3 Lo = vec3(0.0);
|
||||
for(int i = 0; i < light_count; ++i) {
|
||||
vec3 cam_to_light = light_positions[i] - pos;
|
||||
PointLight light = point_lights[i];
|
||||
|
||||
vec3 l = normalize(cam_to_light);
|
||||
vec3 l = normalize(light.dir);
|
||||
vec3 h = normalize(v + l);
|
||||
vec3 radiance = light_colors[i] / dot(cam_to_light, cam_to_light);
|
||||
vec3 radiance = light.color / dot(light.dir, light.dir);
|
||||
|
||||
float NdotL = max(dot(n, l), 0.0);
|
||||
float NdotH = max(dot(n, h), 0.0);
|
||||
|
|
|
@ -12,4 +12,9 @@ void main(void) {
|
|||
gl_Position = r_projectionMatrix * vec4(pos, 1.0);
|
||||
texCoord = (r_textureMatrix * vec4(texCoordRawIn, 0.0, 1.0)).xy;
|
||||
texCoordRaw = texCoordRawIn;
|
||||
|
||||
for(int i = 0; i < light_count; ++i) {
|
||||
point_lights[i].dir = (camera_transform * vec4(light_positions[i], 1.0)).xyz - pos;
|
||||
point_lights[i].color = light_colors[i];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,23 +66,16 @@ static void stage2_bg_setup_pbr_lighting(int max_lights) {
|
|||
max_lights = 1;
|
||||
}
|
||||
|
||||
|
||||
mat4 camera_trans;
|
||||
glm_mat4_identity(camera_trans);
|
||||
camera3d_apply_transforms(&stage_3d_context.cam, camera_trans);
|
||||
|
||||
vec3 cam_light_positions[ARRAY_SIZE(light_pos)];
|
||||
for(int i = 0; i < ARRAY_SIZE(light_pos); i++) {
|
||||
glm_mat4_mulv3(camera_trans, light_pos[i], 1, cam_light_positions[i]);
|
||||
}
|
||||
|
||||
|
||||
r_uniform_vec3_array("light_positions[0]", 0, ARRAY_SIZE(cam_light_positions), cam_light_positions);
|
||||
r_uniform_mat4("camera_transform", camera_trans);
|
||||
r_uniform_vec3_array("light_positions[0]", 0, ARRAY_SIZE(light_pos), light_pos);
|
||||
r_uniform_vec3_array("light_colors[0]", 0, ARRAY_SIZE(light_colors), light_colors);
|
||||
int light_count = imin(max_lights, ARRAY_SIZE(light_pos));
|
||||
r_uniform_int("light_count", light_count);
|
||||
|
||||
|
||||
r_uniform_vec3("ambient_color",0.5,0.5,0.5);
|
||||
}
|
||||
|
||||
|
|
|
@ -52,10 +52,6 @@ static void stage4_lake_draw(vec3 pos) {
|
|||
glm_vec3_scale(r, 4, r);
|
||||
glm_vec3_add(cam->pos, r, light_pos[0]);
|
||||
|
||||
mat4 camera_trans;
|
||||
glm_mat4_identity(camera_trans);
|
||||
camera3d_apply_transforms(&stage_3d_context.cam, camera_trans);
|
||||
|
||||
r_mat_mv_push();
|
||||
r_mat_mv_translate(pos[0], pos[1], pos[2]);
|
||||
r_shader("pbr");
|
||||
|
@ -65,13 +61,12 @@ static void stage4_lake_draw(vec3 pos) {
|
|||
{4, 20, 22},
|
||||
};
|
||||
|
||||
vec3 cam_light_positions[ARRAY_SIZE(light_pos)];
|
||||
for(int i = 0; i < ARRAY_SIZE(light_pos); i++) {
|
||||
glm_mat4_mulv3(camera_trans, light_pos[i], 1, cam_light_positions[i]);
|
||||
}
|
||||
mat4 camera_trans;
|
||||
glm_mat4_identity(camera_trans);
|
||||
camera3d_apply_transforms(&stage_3d_context.cam, camera_trans);
|
||||
|
||||
|
||||
r_uniform_vec3_array("light_positions[0]", 0, ARRAY_SIZE(cam_light_positions), cam_light_positions);
|
||||
r_uniform_mat4("camera_transform", camera_trans);
|
||||
r_uniform_vec3_array("light_positions[0]", 0, ARRAY_SIZE(light_pos), light_pos);
|
||||
r_uniform_vec3_array("light_colors[0]", 0, ARRAY_SIZE(light_colors), light_colors);
|
||||
r_uniform_int("light_count", 2);
|
||||
|
||||
|
@ -82,7 +77,6 @@ static void stage4_lake_draw(vec3 pos) {
|
|||
r_uniform_sampler("ambient_map", "stage4/ground_ambient");
|
||||
r_uniform_vec3("ambient_color", 1, 1, 1);
|
||||
|
||||
|
||||
r_draw_model("stage4/ground");
|
||||
|
||||
r_uniform_float("metallic", 0);
|
||||
|
@ -127,7 +121,6 @@ static void stage4_corridor_draw(vec3 pos) {
|
|||
glm_mat4_identity(camera_trans);
|
||||
camera3d_apply_transforms(&stage_3d_context.cam, camera_trans);
|
||||
|
||||
|
||||
vec3 light_colors[] = {
|
||||
{1, 20, 20},
|
||||
{1, 20, 20},
|
||||
|
@ -137,10 +130,7 @@ static void stage4_corridor_draw(vec3 pos) {
|
|||
{1, 20, 20},
|
||||
};
|
||||
|
||||
vec3 cam_light_positions[ARRAY_SIZE(light_pos)];
|
||||
for(int i = 0; i < ARRAY_SIZE(light_pos); i++) {
|
||||
glm_mat4_mulv3(camera_trans, light_pos[i], 1, cam_light_positions[i]);
|
||||
|
||||
real t = global.frames*0.02;
|
||||
real mod1 = cos(13095434*light_pos[i][1]);
|
||||
real mod2 = sin(1242435*light_pos[i][0]*light_pos[i][1]);
|
||||
|
@ -149,17 +139,16 @@ static void stage4_corridor_draw(vec3 pos) {
|
|||
glm_vec3_scale(light_colors[i],0.6+0.4*f, light_colors[i]);
|
||||
}
|
||||
|
||||
|
||||
r_mat_mv_push();
|
||||
r_mat_mv_translate(pos[0], pos[1], pos[2]);
|
||||
//r_mat_mv_rotate(pos[1]/2000, 0, 1, 0);
|
||||
r_shader("pbr");
|
||||
|
||||
r_uniform_vec3_array("light_positions[0]", 0, ARRAY_SIZE(cam_light_positions), cam_light_positions);
|
||||
r_uniform_mat4("camera_transform", camera_trans);
|
||||
r_uniform_vec3_array("light_positions[0]", 0, ARRAY_SIZE(light_pos), light_pos);
|
||||
r_uniform_vec3_array("light_colors[0]", 0, ARRAY_SIZE(light_colors), light_colors);
|
||||
r_uniform_int("light_count", ARRAY_SIZE(light_pos));
|
||||
|
||||
|
||||
r_uniform_float("metallic", 0);
|
||||
r_uniform_sampler("tex", "stage4/corridor_diffuse");
|
||||
r_uniform_sampler("roughness_map", "stage4/corridor_roughness");
|
||||
|
|
Loading…
Reference in a new issue