polyadvent

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

commit a7eb945821cdaaf4e170fca1e3471828f79f12db
parent 3ef42fb903ead800a694ce6135f538a7bfff81c8
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 30 Oct 2018 01:26:53 -0700

various lightning updates

Diffstat:
Metc/shaders/lighting.glsl | 11++++-------
Aetc/shaders/standard_vtxos.glsl | 7+++++++
Metc/shaders/terrain.glsl | 10++++------
Metc/shaders/test.f.glsl | 41++++++++++++++++++++---------------------
Metc/shaders/uniforms.glsl | 2+-
Metc/shaders/vertex-color.glsl | 12++++--------
Msrc/game.c | 5+----
Msrc/game.h | 2+-
Msrc/render.c | 6++----
Msrc/update.c | 15+++++++--------
10 files changed, 51 insertions(+), 60 deletions(-)

diff --git a/etc/shaders/lighting.glsl b/etc/shaders/lighting.glsl @@ -1,12 +1,12 @@ vec3 standard_light(vec3 color) { - vec4 v4_normal = vec4(normal , 1); + vec4 v4_normal = vec4(v_normal , 1); vec4 trans_normal = normal_matrix * v4_normal; - vec3 L = light_dir; + vec3 L = light_dir * light_intensity; vec3 N = normalize(trans_normal.xyz); - float costheta = clamp(dot(L,N), 0.3, 1.0) * light_intensity; + float costheta = max(0.0, dot(L,N)); return color * costheta; } @@ -19,6 +19,7 @@ const vec3 spherical_harmonics[4] = vec3[]( vec3( -0.188884931542396, -0.277402551592231, -0.377844212327557 ) ); + vec3 irradiance_spherical_harmonics(const vec3 n) { return max( spherical_harmonics[0] @@ -27,7 +28,3 @@ vec3 irradiance_spherical_harmonics(const vec3 n) { + spherical_harmonics[3] * n.x , 0.0); } - -vec3 gamma_correct(vec3 color) { - return pow(color, vec3(1.0/2.2)); -} diff --git a/etc/shaders/standard_vtxos.glsl b/etc/shaders/standard_vtxos.glsl @@ -0,0 +1,7 @@ +vec4 v4_pos = vec4(position, 1.0); +gl_Position = mvp * v4_pos; +v_normal = normal; +v_color_smooth = v_color = standard_light(color); +shadow_coord = depth_mvp * v4_pos; +v_position = position; +v_ray = camera_position - (world * v4_pos).xyz; diff --git a/etc/shaders/terrain.glsl b/etc/shaders/terrain.glsl @@ -9,8 +9,11 @@ in vec3 normal; flat out float v_light; flat out vec3 v_color; +out vec3 v_color_smooth; +out vec3 v_normal; out vec3 v_ray; +out vec3 v_position; out vec4 shadow_coord; const int nlands = 6; @@ -29,10 +32,6 @@ const vec4 land[nlands] = vec4[]( void main() { - vec4 v4_pos = vec4(position, 1.0); - gl_Position = mvp * v4_pos; - shadow_coord = depth_mvp * v4_pos; - vec3 color = land[0].xyz; for (int i = 0; i < nlands-1; i++) { color = @@ -44,6 +43,5 @@ void main() // vec3 color = vec3(position.z*0.05, position.z*0.0095, position.z*0.0001) * 0.5; // v_color = vec3(position.z, position.z, position.z) * 0.005; - v_color = standard_light(color); - v_ray = camera_position - (world * v4_pos).xyz; +#include standard_vtxos.glsl } diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -3,52 +3,51 @@ precision mediump float; flat in vec3 v_color; +in vec3 v_color_smooth; in vec3 v_ray; +in vec3 v_normal; +in vec3 v_position; in vec4 shadow_coord; out vec4 fragmentColor; uniform vec3 camera_position; uniform bool fog_on; uniform bool diffuse_on; -uniform vec3 light_intensity; +uniform float light_intensity; uniform vec3 light_dir; uniform mat4 normal_matrix; uniform sampler2D shadow_map; vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) { - const float b = 0.00026; - const float v = 1.0; - const float zs = 1.0; + const float b = 0.00035; + float fog_amount = 1.0-exp(-(pow(distance * b, 3.0))) ; + float sun_amount = max( dot( ray_dir, -light_dir ), 0.0 ) * b; - const float c = 1.0; + vec3 fog_color = mix(vec3(0.5,0.6,0.7), + vec3(1.0,0.9,0.8), + sun_amount + ) * light_intensity; - // float fog_amount = - // exp(-ray_orig.z*b) * (1.0-exp( -distance*ray_dir.z*b))/ray_dir.z*b; - - float fog_amount = 1.0 - exp(-(pow(distance*b, 2.0))) ; - - vec3 fog_color = vec3(0.5,0.6,0.7) * light_intensity; - return mix( rgb, fog_color, fog_amount); + // vec3 final_color = rgb*(1.0-exp(-distance*b)) + fog_color*exp(-distance*b); + vec3 final_color = mix( rgb, fog_color, fog_amount); + return final_color; } - void main() { - vec3 light_position = vec3(2.0,1.0,5.0); + + const float smoothness = 0.0; float visibility = 1.0; - vec3 color; + float shadow_map_z = texture(shadow_map, shadow_coord.xy).z; - vec3 frag = v_color; + vec3 color = v_color * (1.0-smoothness) + + v_color_smooth * smoothness; if (fog_on) { - vec3 fog = apply_fog(frag, length(v_ray), camera_position, v_ray); + vec3 fog = apply_fog(color, length(v_ray), camera_position, v_ray); color = fog; } - else - color = frag; - - float shadow_map_z = texture(shadow_map, shadow_coord.xy).z; if (light_dir.z > 0.0 && shadow_map_z < shadow_coord.z ) { float factor = 1.0/dot(light_dir, vec3(0.0, 0.0, 1.0)); diff --git a/etc/shaders/uniforms.glsl b/etc/shaders/uniforms.glsl @@ -6,4 +6,4 @@ uniform mat4 model_view; uniform mat4 normal_matrix; uniform vec3 camera_position; uniform vec3 light_dir; -uniform vec3 light_intensity; +uniform float light_intensity; diff --git a/etc/shaders/vertex-color.glsl b/etc/shaders/vertex-color.glsl @@ -11,7 +11,9 @@ in vec3 color; flat out float v_light; flat out vec3 v_color; -flat out vec3 v_normal; +out vec3 v_color_smooth; +out vec3 v_normal; +out vec3 v_position; out vec3 v_ray; out vec4 shadow_coord; @@ -19,11 +21,5 @@ out vec4 shadow_coord; void main() { - vec4 v4_pos = vec4(position, 1.0); - gl_Position = mvp * v4_pos; - shadow_coord = depth_mvp * v4_pos; - - v_color = standard_light(color); - // v_normal = trans_normal.xyz; - v_ray = camera_position - (world * v4_pos).xyz; +#include standard_vtxos.glsl } diff --git a/src/game.c b/src/game.c @@ -38,7 +38,6 @@ void game_init(struct game *game, int width, int height) { struct terrain *terrain = &game->terrain; mat4 *light_dir = res->light_dir; - mat4 *light_intensity = res->light_intensity; int ok = 0; const double size = 10000; @@ -77,9 +76,7 @@ void game_init(struct game *game, int width, int height) { mat4_id(mvp); - light_intensity[0] = 0.8; - light_intensity[1] = 0.8; - light_intensity[2] = 0.8; + res->light_intensity = 0.8; light_dir[0] = 0.8; light_dir[1] = 0.8; diff --git a/src/game.h b/src/game.h @@ -58,7 +58,7 @@ struct resources { float test_mvp[MAT4_ELEMS]; float light_dir[3]; - float light_intensity[3]; + float light_intensity; float proj_persp[MAT4_ELEMS]; float proj_ortho[MAT4_ELEMS]; }; diff --git a/src/render.c b/src/render.c @@ -166,7 +166,7 @@ recalc_normals(GLint nm_uniform, mat4 *model_view, mat4 *normal) { void render (struct game *game, struct render_config *config) { - float adjust = game->test_resources.light_intensity[0]; + float adjust = game->test_resources.light_intensity; glEnable(GL_DEPTH_TEST); glClearColor( 0.5294f * adjust, 0.8078f * adjust, 0.9216f * adjust, 1.0f ); //clear background screen to black glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); @@ -185,7 +185,6 @@ void render (struct game *game, struct render_config *config) { mat4 *mvp = res->test_mvp; mat4 *projection = config->projection; mat4 *light = res->light_dir; - mat4 *light_intensity = res->light_intensity; float *camera = config->camera; @@ -226,8 +225,7 @@ void render (struct game *game, struct render_config *config) { glUniform1i(res->uniforms.fog_on, res->fog_on); glUniform1i(res->uniforms.diffuse_on, res->diffuse_on); glUniform3f(res->uniforms.light_dir, light[0], light[1], light[2]); - glUniform3f(res->uniforms.light_intensity, - light_intensity[0], light_intensity[1], light_intensity[2]); + glUniform1f(res->uniforms.light_intensity, res->light_intensity); mat4_multiply(view_proj, entity->node.mat, mvp); mat4_copy(entity->node.mat, model_view); diff --git a/src/update.c b/src/update.c @@ -234,21 +234,20 @@ void resize_fbos(struct game *game, int width, int height) { // TODO: match based on some real concept of time static void day_night_cycle(float n, struct resources *res) { - float darkest = 0.3; - float val = 9950.0; - float roots = vec3_dot(res->light_dir, V3(0.0, 0.0, 1.0)); - float intensity = clamp(roots, darkest, 1.0); + /* float val = 9950.0; */ + float val = n * 100.0; + float intensity = vec3_dot(res->light_dir, V3(0.0, 0.0, 1.0)); + intensity = clamp(intensity, 0.8, 1.0); float light_pos[3]; - float tmp[3] ; /* float intensity = angle <= 0.5 */ /* ? clamp(roots, darkest, 1.0) */ /* : clamp(-roots * 0.4, darkest, 0.5); */ - res->light_intensity[0] = intensity; - res->light_intensity[1] = intensity; - res->light_intensity[2] = intensity; + res->light_intensity = intensity; + + /* vec3_normalize(res->light_intensity, res->light_intensity); */ res->light_dir[0] = 0.0; res->light_dir[1] = -sin(val);