polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit c8ef76a60cdd2922341c4faa5cd9440822bce3a1
parent 41ad468603367d34c9fafb577b5f63489e6da212
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 26 Oct 2018 09:12:07 -0700

clean up shaders a bit

really need modular shaders..

Diffstat:
Metc/shaders/terrain.glsl | 40+++++++++++++++++++++++++++++++---------
Metc/shaders/test.f.glsl | 19+++----------------
Metc/shaders/vertex-color.glsl | 38++++++++++++++++++++++++++------------
3 files changed, 60 insertions(+), 37 deletions(-)

diff --git a/etc/shaders/terrain.glsl b/etc/shaders/terrain.glsl @@ -11,10 +11,10 @@ uniform mat4 model_view; uniform mat4 normal_matrix; uniform vec3 camera_position; uniform vec3 light_dir; +uniform vec3 light_intensity; flat out float v_light; flat out vec3 v_color; -flat out vec3 v_normal; out vec3 v_ray; const int nlands = 6; @@ -28,20 +28,43 @@ const vec4 land[nlands] = vec4[]( vec4(1.0, 1.0, 1.0, 380.0) // 5 - snow ); +vec3 standard_light(vec3 color) { + vec4 v4_normal = vec4(normal , 1); + vec4 trans_normal = normal_matrix * v4_normal; -void main() -{ + float light = dot(trans_normal.xyz, normalize(light_dir)) ; + return color * light; +} + +vec3 hemispherical(vec3 color) { vec4 v4_normal = vec4(normal , 1); vec4 trans_normal = normal_matrix * v4_normal; + + vec3 L = light_dir; + vec3 N = normalize(trans_normal.xyz); + + float costheta = dot(L,N); + + float a = 0.5 + (0.5 * costheta); + return a * light_intensity * color + + (1.0-a) * vec3(0.0, 0.0, 0.0) * color; +} + +vec3 gamma_correct(vec3 color) { + return pow(color, vec3(1.0/2.2)); +} + +void main() +{ vec4 v4_pos = vec4(position, 1.0); gl_Position = mvp * v4_pos; - float light = dot(trans_normal.xyz, normalize(light_dir)) ; + // float light = dot(trans_normal.xyz, normalize(light_dir)) ; - v_color = land[0].xyz * light; + vec3 color = land[0].xyz; for (int i = 0; i < nlands-1; i++) { - v_color = - mix(v_color, + color = + mix(color, land[i+1].xyz, smoothstep(land[i].w, land[i+1].w, position.z)); } @@ -50,7 +73,6 @@ void main() // v_color = vec3(position.z, position.z, position.z) * 0.005; - v_color *= light; - v_normal = trans_normal.xyz; + v_color = hemispherical(color); v_ray = camera_position - (world * v4_pos).xyz; } diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -3,13 +3,13 @@ precision mediump float; flat in vec3 v_color; -flat in vec3 v_normal; in vec3 v_ray; out vec4 fragmentColor; uniform vec3 camera_position; uniform bool fog_on; uniform bool diffuse_on; +uniform vec3 light_intensity; uniform mat4 normal_matrix; vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) { @@ -25,25 +25,12 @@ vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir float fog_amount = 1.0 - exp(-(pow(distance*b, 2.0))) ; - vec3 fog_color = vec3(0.5,0.6,0.7); + vec3 fog_color = vec3(0.5,0.6,0.7) * light_intensity; return mix( rgb, fog_color, fog_amount); } -vec3 gamma_correct(vec3 color) { - return pow(color, vec3(1.0/2.2)); -} - -float lambert(vec3 N, vec3 L) -{ - vec3 nrmN = normalize(N); - vec3 nrmL = normalize(L); - float result = dot(nrmN, nrmL); - return max(result, 0.0); -} - void main() { - float distance = length(v_ray); vec3 light_position = vec3(2.0,1.0,5.0); vec3 color; @@ -51,7 +38,7 @@ void main() { vec3 frag = v_color; if (fog_on) { - vec3 fog = apply_fog(frag, distance, camera_position, v_ray); + vec3 fog = apply_fog(frag, length(v_ray), camera_position, v_ray); color = fog; } else diff --git a/etc/shaders/vertex-color.glsl b/etc/shaders/vertex-color.glsl @@ -13,6 +13,7 @@ uniform mat4 model_view; uniform mat4 normal_matrix; uniform vec3 camera_position; uniform vec3 light_dir; +uniform vec3 light_intensity; flat out float v_light; flat out vec3 v_color; @@ -20,26 +21,39 @@ flat out vec3 v_normal; out vec3 v_ray; -void main() -{ +vec3 standard_light(vec3 color) { + vec4 v4_normal = vec4(normal, 1); + vec4 trans_normal = normal_matrix * v4_normal; + + float light = dot(trans_normal.xyz, normalize(light_dir)); + return color * light; +} + +vec3 hemispherical(vec3 color) { vec4 v4_normal = vec4(normal, 1); vec4 trans_normal = normal_matrix * v4_normal; - vec4 v4_pos = vec4(position, 1.0); - gl_Position = mvp * v4_pos; - vec3 L = normalize(light_dir); - //float light = dot(trans_normal.xyz, L); - //Make sure the normal is actually unit length, - //and isolate the important coordinates + vec3 L = normalize(light_dir); vec3 N = normalize(trans_normal.xyz); - //Calculate cosine of angle between light direction and normal float costheta = dot(L,N); - float a = 0.5+(0.5*costheta); - v_color = a * vec3(1.0, 1.0, 1.0) * color - + (1.0-a) * vec3(0.2, 0.2, 0.2) * color; + float a = 0.5 + (0.5 * costheta); + return a * light_intensity * color + + (1.0-a) * vec3(0.0, 0.0, 0.0) * color; +} + +vec3 gamma_correct(vec3 color) { + return pow(color, vec3(1.0/2.2)); +} + + +void main() +{ + vec4 v4_pos = vec4(position, 1.0); + gl_Position = mvp * v4_pos; + v_color = hemispherical(color); // v_normal = trans_normal.xyz; v_ray = camera_position - (world * v4_pos).xyz; }