polyadvent

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

commit 4df7fc68011e38534b7ad9fbab4b0ba5de0f5881
parent bdf02825db1a952b4c4aa051bd6331e8d4918319
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  2 May 2018 18:43:47 -0700

wip

Diffstat:
Metc/shaders/test.f.glsl | 21++++++++++++++-------
Metc/shaders/test.v.glsl | 7+++----
Msrc/game.c | 2+-
Msrc/game.h | 3+++
Msrc/render.c | 26++++++++++++--------------
5 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -8,22 +8,27 @@ in vec3 v_ray; out vec4 fragmentColor; uniform vec3 camera_position; +uniform mat4 normal; vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) { - const float b = 0.0000032; + const float b = 0.00004; const float v = 1.0; const float zs = 1.0; const float c = 1.0; - float fog_amount = - exp(-ray_orig.z*b*zs) * (1.0-exp( -distance*ray_dir.z*b*zs ))/ray_dir.z*b; + // 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(-distance*0.0000032); + float fog_amount = 1.0 - exp(-distance*b) ; vec3 fog_color = vec3(0.5,0.6,0.7); - return mix( rgb, fog_color, fog_amount * 100.0 ); + return mix( rgb, fog_color, fog_amount); +} + +vec3 gamma_correct(vec3 color) { + return pow(color, vec3(1.0/2.2)); } void main() { @@ -37,8 +42,10 @@ void main() { // fragmentColor = v_color * (1.0 - distance*0.0001) * v_light; - vec3 fog = apply_fog((v_color * v_light).xyz, distance, camera_position, -v_ray); - fragmentColor = vec4(fog, 0.0); + // vec3 diffuse = dot() + + vec3 fog = apply_fog((v_color * v_light * 0.1).xyz, distance, camera_position, v_ray); + fragmentColor = vec4(gamma_correct(fog), 1.0); // fragmentColor = vec4((v_color * v_light).xyz, 0.0); // } } diff --git a/etc/shaders/test.v.glsl b/etc/shaders/test.v.glsl @@ -3,8 +3,9 @@ in vec3 position; in vec3 normal; -uniform mat4 view; +uniform mat4 world; uniform mat4 mvp; +uniform mat4 world_normal; flat out float v_light; flat out vec4 v_color; @@ -31,7 +32,5 @@ void main() else v_color = vec4(1.0, 1.0, 1.0, 1.0); - v_ray = (gl_Position - (mvp * vec4(camera_position, 1.0))).xyz; - - v_color = v_color * 0.5; + v_ray = (vec4(camera_position, 1.0) - (world * v4pos)).xyz; } diff --git a/src/game.c b/src/game.c @@ -27,7 +27,7 @@ void game_init(struct game *game) { struct terrain *terrain = &game->terrain; mat4 *light_dir = game->test_resources.light_dir; - const double size = 10000; + const double size = 6000; const double pdist = 1.7; terrain->settings = (struct perlin_settings){ diff --git a/src/game.h b/src/game.h @@ -21,7 +21,10 @@ struct resources { GLint camera_position; GLint light_dir; GLint mvp; + GLint mv; + GLint normal; GLint view; + GLint world; } uniforms; struct attributes { 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.view = glGetUniformLocation(resources->program, "view"); + resources->uniforms.world + = glGetUniformLocation(resources->program, "world"); + resources->attributes.normal = (gpu_addr)glGetAttribLocation(resources->program, "normal"); @@ -130,19 +133,10 @@ init_gl(struct resources *resources, int width, int height) { } -static mat4 * -calc_normals(mat4 *mvp, mat4 *normal) { +static void +recalc_normals(mat4 *mvp, mat4 *normal) { mat4_inverse(mvp, normal); mat4_transpose(normal, normal); - return normal; -} - - -static void -recalc_normals(GLint norm_uniform, mat4 *mvp, mat4 *normal) { - mat4 *calc = calc_normals(mvp, normal); - glUniformMatrix4fv(norm_uniform, 1, 0, calc); - check_gl(); } @@ -194,6 +188,7 @@ void render (struct game *game, struct geometry *geom) { static float id[MAT4_ELEMS] = { 0 }; static float view[MAT4_ELEMS] = { 0 }; + static float normal[MAT4_ELEMS] = { 0 }; mat4_id(id); struct resources *res = &game->test_resources; @@ -215,8 +210,12 @@ void render (struct game *game, struct geometry *geom) { /* mat4_multiply(persp, camera->mat, mvp); */ mat4_inverse(camera->mat, view); mat4_multiply(persp, view, mvp); + recalc_normals(mvp, normal); + check_gl(); + glUniformMatrix4fv(res->uniforms.normal, 1, 0, normal); /* mat4_multiply(mvp, tmp_matrix, tmp_matrix); */ + glUniformMatrix4fv(res->uniforms.view, 1, 0, view); glUniform3f(res->uniforms.camera_position, camera->mat[M_X], camera->mat[M_Y], @@ -227,15 +226,14 @@ void render (struct game *game, struct geometry *geom) { //player mat4_multiply(mvp, player->mat, tmp_matrix); glUniformMatrix4fv(res->uniforms.mvp, 1, 0, tmp_matrix); - mat4_multiply(view, player->mat, tmp_matrix); - glUniformMatrix4fv(res->uniforms.view, 1, 0, tmp_matrix); + glUniformMatrix4fv(res->uniforms.world, 1, 0, player->mat); /* mat4_multiply(persp, tmp_matrix, mvp); */ /* mat4_print(player->mat); */ render_cube(res); // terrain glUniformMatrix4fv(res->uniforms.mvp, 1, 0, mvp); - glUniformMatrix4fv(res->uniforms.view, 1, 0, view); + glUniformMatrix4fv(res->uniforms.world, 1, 0, id); render_geom(res, geom, GL_TRIANGLES); /* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */ /* render_geom(res, geom, GL_TRIANGLES); */