commit bdf02825db1a952b4c4aa051bd6331e8d4918319
parent 34d097387db5c8a4ab92f52a6a8d6c98b813192b
Author: William Casarin <jb55@jb55.com>
Date: Wed, 2 May 2018 12:53:00 -0700
fog progress
Diffstat:
4 files changed, 68 insertions(+), 39 deletions(-)
diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl
@@ -1,15 +1,44 @@
#version 320 es
-
precision mediump float;
-uniform float fade_factor;
-flat in vec3 v_norm;
-flat in float v_dot;
-flat in vec4 v_hcol;
+flat in float v_light;
+flat in vec4 v_color;
+in vec3 v_ray;
out vec4 fragmentColor;
+uniform vec3 camera_position;
+
+vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) {
+ const float b = 0.0000032;
+ 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 = 1.0 - exp(-distance*0.0000032);
+
+ vec3 fog_color = vec3(0.5,0.6,0.7);
+ return mix( rgb, fog_color, fog_amount * 100.0 );
+}
void main() {
- fragmentColor = vec4(v_hcol * v_dot);
+ float distance = length(v_ray);
+
+ // 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;
+
+ // 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);
+ // fragmentColor = vec4((v_color * v_light).xyz, 0.0);
+ // }
}
diff --git a/etc/shaders/test.v.glsl b/etc/shaders/test.v.glsl
@@ -3,31 +3,35 @@
in vec3 position;
in vec3 normal;
-uniform mat4 local;
+uniform mat4 view;
uniform mat4 mvp;
-flat out float v_dot;
-flat out vec3 v_norm;
-flat out vec4 v_hcol;
+flat out float v_light;
+flat out vec4 v_color;
+out vec3 v_ray;
+uniform vec3 camera_position;
uniform vec3 light_dir;
void main()
{
vec4 trans_normal = vec4(normal, 1);
- gl_Position = mvp * vec4(position.xyz, 1.0);
- v_dot = dot(trans_normal, vec4(light_dir, 0));
+ vec4 v4pos = vec4(position, 1.0);
+ gl_Position = mvp * v4pos;
+ v_light = dot(trans_normal, vec4(light_dir, 0));
if (position.z <= 1.0)
- v_hcol = vec4(0.0, 0.5, 0.79, 1.0);
+ v_color = vec4(0.0, 0.5, 0.79, 1.0);
else if (position.z <= 2.0)
- v_hcol = vec4(0.9176, 0.8156, 0.6588, 1.0);
+ v_color = vec4(0.9176, 0.8156, 0.6588, 1.0);
else if (position.z <= 5.0)
- v_hcol = vec4(0.6274, 0.749, 0.156, 1.0);
+ v_color = vec4(0.6274, 0.749, 0.156, 1.0);
else if (position.z <= 100.0)
- v_hcol = vec4(0.5, 0.5, 0.5, 1.0);
+ v_color = vec4(0.5, 0.5, 0.5, 1.0);
else
- v_hcol = vec4(1.0, 1.0, 1.0, 1.0);
+ v_color = vec4(1.0, 1.0, 1.0, 1.0);
- v_hcol = v_hcol * 0.5;
+ v_ray = (gl_Position - (mvp * vec4(camera_position, 1.0))).xyz;
+
+ v_color = v_color * 0.5;
}
diff --git a/src/game.h b/src/game.h
@@ -18,12 +18,10 @@ struct resources {
GLuint vertex_shader, fragment_shader, program;
struct uniforms {
- GLint fade_factor;
+ GLint camera_position;
GLint light_dir;
GLint mvp;
- GLint local;
- GLint tscale;
- GLint normal_matrix;
+ GLint view;
} uniforms;
struct attributes {
@@ -39,7 +37,6 @@ struct resources {
float test_mvp[MAT4_ELEMS];
float light_dir[3];
float camera_persp[MAT4_ELEMS];
- GLfloat fade_factor;
};
struct game {
diff --git a/src/render.c b/src/render.c
@@ -108,23 +108,17 @@ init_gl(struct resources *resources, int width, int height) {
assert(resources->program != 0);
// Program variables
- resources->uniforms.fade_factor
- = glGetUniformLocation(resources->program, "fade_factor");
+ resources->uniforms.camera_position
+ = glGetUniformLocation(resources->program, "camera_position");
resources->uniforms.light_dir
= glGetUniformLocation(resources->program, "light_dir");
- resources->uniforms.normal_matrix
- = glGetUniformLocation(resources->program, "normal_matrix");
-
resources->uniforms.mvp
= glGetUniformLocation(resources->program, "mvp");
- resources->uniforms.tscale
- = glGetUniformLocation(resources->program, "tscale");
-
- resources->uniforms.local
- = glGetUniformLocation(resources->program, "local");
+ resources->uniforms.view
+ = glGetUniformLocation(resources->program, "view");
resources->attributes.normal
= (gpu_addr)glGetAttribLocation(resources->program, "normal");
@@ -199,6 +193,7 @@ void render (struct game *game, struct geometry *geom) {
check_gl();
static float id[MAT4_ELEMS] = { 0 };
+ static float view[MAT4_ELEMS] = { 0 };
mat4_id(id);
struct resources *res = &game->test_resources;
@@ -209,8 +204,6 @@ void render (struct game *game, struct geometry *geom) {
struct node *player = &res->player;
struct node *camera = &res->camera;
- float fade_factor = res->fade_factor;
-
glUseProgram(res->program);
/* static float v3[] = { 1, 1, 0 }; */
@@ -220,23 +213,29 @@ void render (struct game *game, struct geometry *geom) {
/* mat4_print(camera->mat); */
/* node_recalc(&res->camera); */
/* mat4_multiply(persp, camera->mat, mvp); */
- mat4_inverse(camera->mat, tmp_matrix);
- mat4_multiply(persp, tmp_matrix, mvp);
+ mat4_inverse(camera->mat, view);
+ mat4_multiply(persp, view, mvp);
/* mat4_multiply(mvp, tmp_matrix, tmp_matrix); */
+ glUniform3f(res->uniforms.camera_position,
+ camera->mat[M_X],
+ camera->mat[M_Y],
+ camera->mat[M_Z]);
+
glUniform3f(res->uniforms.light_dir, light[0], light[1], light[2]);
- glUniform1f(res->uniforms.fade_factor, fade_factor);
- glUniform1f(res->uniforms.tscale, res->uniforms.tscale);
//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);
/* 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);
render_geom(res, geom, GL_TRIANGLES);
/* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */
/* render_geom(res, geom, GL_TRIANGLES); */