polyadvent

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

commit 69d38281d116861ba4501f8020f2ee7050a59539
parent f366447f7a634c0243c3c42369b1eb69f9512923
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  3 May 2018 12:39:18 -0700

trying diffuse

Diffstat:
Metc/shaders/test.f.glsl | 38++++++++++++++++++++++++--------------
Metc/shaders/test.v.glsl | 23+++++++++++++----------
Msrc/game.c | 1+
Msrc/game.h | 3++-
Msrc/render.c | 4++++
Msrc/update.c | 9++++++++-
6 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -3,12 +3,14 @@ precision mediump float; flat in float v_light; -flat in vec4 v_color; +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 mat4 normal_matrix; vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) { @@ -32,29 +34,37 @@ 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 light_diffuse = vec3(0.1); - // if (distance > 1000.0) - // fragmentColor = vec4(1.0, 0.0, 0.0, 0.0); - // else { - - // fragmentColor = (vec4(v_ray, 0.0) * 0.001 - v_color) * v_light; + vec3 diffuse; + if (diffuse_on) + diffuse = light_diffuse * lambert(v_normal, light_position); + else + diffuse = vec3(0.0,0.0,0.0); - // fragmentColor = v_color * (1.0 - distance*0.0001) * v_light; + vec3 color; - // vec3 diffuse = dot() + vec3 frag = v_color * v_light; - vec3 color; - vec3 frag = (v_color * v_light).xyz; if (fog_on) { vec3 fog = apply_fog(frag, distance, camera_position, v_ray); color = fog; } else color = frag; - // fragmentColor = vec4(fog, 1.0); - // vec3 color = (v_color * v_light).xyz; - fragmentColor = vec4(color, 1.0); - // } + + // fragmentColor = vec4(color + diffuse, 1.0); + fragmentColor = vec4(color + diffuse, 1.0); } diff --git a/etc/shaders/test.v.glsl b/etc/shaders/test.v.glsl @@ -13,27 +13,30 @@ uniform vec3 camera_position; uniform vec3 light_dir; flat out float v_light; -flat out vec4 v_color; +flat out vec3 v_color; +flat out vec3 v_normal; out vec3 v_ray; void main() { - vec4 trans_normal = normal_matrix * vec4(normal, 1); - vec4 v4pos = vec4(position, 1.0); - gl_Position = mvp * v4pos; + 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; v_light = dot(trans_normal, vec4(normalize(light_dir), 0)); if (position.z <= 1.0) - v_color = vec4(0.0, 0.5, 0.79, 1.0); + v_color = vec3(0.0, 0.5, 0.79); else if (position.z <= 2.0) - v_color = vec4(0.9176, 0.8156, 0.6588, 1.0); + v_color = vec3(0.9176, 0.8156, 0.6588); else if (position.z <= 5.0) - v_color = vec4(0.6274, 0.749, 0.156, 1.0); + v_color = vec3(0.6274, 0.749, 0.156); else if (position.z <= 100.0) - v_color = vec4(0.5, 0.5, 0.5, 1.0); + v_color = vec3(0.5, 0.5, 0.5); else - v_color = vec4(1.0, 1.0, 1.0, 1.0) * 0.9; + v_color = vec3(1.0, 1.0, 1.0) * 0.9; - v_ray = camera_position - (world * v4pos).xyz; + v_normal = trans_normal.xyz; + v_ray = camera_position - (world * v4_pos).xyz; } diff --git a/src/game.c b/src/game.c @@ -53,6 +53,7 @@ void game_init(struct game *game) { light_dir[2] = 0.8; game->test_resources.fog_on = 1; + game->test_resources.diffuse_on = 1; node_init(root); node_init(player); diff --git a/src/game.h b/src/game.h @@ -24,6 +24,7 @@ struct resources { GLint normal_matrix; GLint view; GLint fog_on; + GLint diffuse_on; GLint model_view; GLint world; } uniforms; @@ -38,7 +39,7 @@ struct resources { struct node camera; struct node terrain_node; - bool fog_on; + bool fog_on, diffuse_on; float test_mvp[MAT4_ELEMS]; float light_dir[3]; diff --git a/src/render.c b/src/render.c @@ -120,6 +120,9 @@ init_gl(struct resources *resources, int width, int height) { resources->uniforms.fog_on = glGetUniformLocation(resources->program, "fog_on"); + resources->uniforms.diffuse_on + = glGetUniformLocation(resources->program, "diffuse_on"); + resources->uniforms.mvp = glGetUniformLocation(resources->program, "mvp"); @@ -229,6 +232,7 @@ void render (struct game *game, struct geometry *geom) { camera->mat[M_Z]); 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]); //player diff --git a/src/update.c b/src/update.c @@ -150,7 +150,7 @@ void update (struct game *game, u32 dt) { static int passed = 0; static double last_ox, last_oy, last_oz; static int last_gen_time = 50; - static int toggle_fog = 0; + static int toggle_fog = 0, toggle_diffuse = 0; static float n = 1; static int first = 1; struct resources *res = &game->test_resources; @@ -181,6 +181,9 @@ void update (struct game *game, u32 dt) { if (game->input.keystates[SDL_SCANCODE_F]) toggle_fog = 1; + if (game->input.keystates[SDL_SCANCODE_G]) + toggle_diffuse = 1; + int space_down = game->input.keystates[SDL_SCANCODE_SPACE]; if (space_down) { @@ -204,6 +207,10 @@ void update (struct game *game, u32 dt) { res->fog_on = !res->fog_on; toggle_fog = 0; } + if (toggle_diffuse) { + res->diffuse_on = !res->diffuse_on; + toggle_diffuse = 0; + } passed = 0; double ox = tnode->pos[0];