polyadvent

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

commit 1d1863cdee559d5323e7fcef06e9c6e1f8d1c49b
parent fd4f0eb83b28587f7f457aa0cdffdab0d882361b
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  2 May 2018 21:14:52 -0700

toggle fog...

Diffstat:
Metc/shaders/test.f.glsl | 14++++++++++----
Msrc/game.c | 6+++---
Msrc/game.h | 55+++++++++++++++++++++++++++++--------------------------
Msrc/gl.h | 1+
Msrc/render.c | 4++++
Msrc/update.c | 16+++++++++++-----
6 files changed, 58 insertions(+), 38 deletions(-)

diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -8,10 +8,11 @@ in vec3 v_ray; out vec4 fragmentColor; uniform vec3 camera_position; +uniform bool fog_on; uniform mat4 normal_matrix; vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) { - const float b = 0.00002; + const float b = 0.000046; const float v = 1.0; const float zs = 1.0; @@ -44,11 +45,16 @@ void main() { // vec3 diffuse = dot() + vec3 color; vec3 frag = (v_color * v_light).xyz; - // vec3 fog = apply_fog(frag, distance, camera_position, v_ray); - vec3 color = gamma_correct(frag); + 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, 0.0); + fragmentColor = vec4(gamma_correct(color), 1.0); // } } diff --git a/src/game.c b/src/game.c @@ -48,9 +48,9 @@ void game_init(struct game *game) { mat4_id(mvp); - light_dir[0] = 0.2; - light_dir[1] = 0.07; - light_dir[2] = 0.2; + light_dir[0] = 0.5; + light_dir[1] = 0.5; + light_dir[2] = 0.5; node_init(root); node_init(player); diff --git a/src/game.h b/src/game.h @@ -14,32 +14,35 @@ * NOTE: just for testing right now */ struct resources { - struct vbo vertex_buffer, element_buffer, normal_buffer; - GLuint vertex_shader, fragment_shader, program; - - struct uniforms { - GLint camera_position; - GLint light_dir; - GLint mvp; - GLint normal_matrix; - GLint view; - GLint model_view; - GLint world; - } uniforms; - - struct attributes { - gpu_addr position; - gpu_addr normal; - } attributes; - - struct node root; - struct node player; - struct node camera; - struct node terrain_node; - - float test_mvp[MAT4_ELEMS]; - float light_dir[3]; - float camera_persp[MAT4_ELEMS]; + struct vbo vertex_buffer, element_buffer, normal_buffer; + GLuint vertex_shader, fragment_shader, program; + + struct uniforms { + GLint camera_position; + GLint light_dir; + GLint mvp; + GLint normal_matrix; + GLint view; + GLint fog_on; + GLint model_view; + GLint world; + } uniforms; + + struct attributes { + gpu_addr position; + gpu_addr normal; + } attributes; + + struct node root; + struct node player; + struct node camera; + struct node terrain_node; + + bool fog_on; + + float test_mvp[MAT4_ELEMS]; + float light_dir[3]; + float camera_persp[MAT4_ELEMS]; }; struct game { diff --git a/src/gl.h b/src/gl.h @@ -29,6 +29,7 @@ void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * pointer); +void glUniform1i(GLint location, GLint i); void glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2); GLuint glCreateProgram(void); void glAttachShader(GLuint program, GLuint shader); diff --git a/src/render.c b/src/render.c @@ -117,6 +117,9 @@ init_gl(struct resources *resources, int width, int height) { resources->uniforms.world = glGetUniformLocation(resources->program, "world"); + resources->uniforms.fog_on + = glGetUniformLocation(resources->program, "fog_on"); + resources->uniforms.mvp = glGetUniformLocation(resources->program, "mvp"); @@ -223,6 +226,7 @@ void render (struct game *game, struct geometry *geom) { camera->mat[M_Y], camera->mat[M_Z]); + glUniform1i(res->uniforms.fog_on, res->fog_on); glUniform3f(res->uniforms.light_dir, light[0], light[1], light[2]); //player diff --git a/src/update.c b/src/update.c @@ -68,8 +68,8 @@ void update_terrain(struct game *game) { if (first) { terrain_init(terrain); - tnode->pos[0] = rand_0to1() * 1000.0; - tnode->pos[1] = rand_0to1() * 1000.0; + tnode->pos[0] = rand_0to1() * 100000.0; + tnode->pos[1] = rand_0to1() * 100000.0; first = 0; } @@ -150,6 +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 float n = 1; static int first = 1; struct resources *res = &game->test_resources; @@ -174,10 +175,11 @@ void update (struct game *game, u32 dt) { player_movement(game); } - if (game->input.keystates[SDL_SCANCODE_C]) { + if (game->input.keystates[SDL_SCANCODE_C]) printf("light_dir %f %f %f\n", light[0], light[1], light[2]); - } + if (game->input.keystates[SDL_SCANCODE_F]) + toggle_fog = 1; int space_down = game->input.keystates[SDL_SCANCODE_SPACE]; @@ -195,9 +197,13 @@ void update (struct game *game, u32 dt) { } } - if (space_down || passed < 50) { + if (space_down || passed < 500) { passed += dt; } else { + if (toggle_fog) { + res->fog_on = !res->fog_on; + toggle_fog = 0; + } passed = 0; double ox = tnode->pos[0];