polyadvent

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

commit 8d819cf0288c0e64ae122e5228cfd58e341f9e8d
parent c8ef76a60cdd2922341c4faa5cd9440822bce3a1
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 26 Oct 2018 09:12:25 -0700

day/night cycle

Diffstat:
Msrc/common.h | 3++-
Msrc/game.c | 9+++++++--
Msrc/game.h | 4+++-
Msrc/render.c | 10+++++++---
Msrc/update.c | 41++++++++++++++++++++++++++++-------------
Msrc/window.c | 2+-
6 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/src/common.h b/src/common.h @@ -10,7 +10,8 @@ #define SLAB(f) "etc/slab/" f #define RESOURCE(f) "etc/shaders/" f -#define PI 3.14159265f; +#define PI 3.14159265f +#define TAU 6.2831853f typedef unsigned char u8; typedef signed char s8; diff --git a/src/game.c b/src/game.c @@ -29,6 +29,7 @@ void game_init(struct game *game) { struct entity *player = &res->player; struct terrain *terrain = &game->terrain; mat4 *light_dir = res->light_dir; + mat4 *light_intensity = res->light_intensity; int ok = 0; const double size = 10000; @@ -52,6 +53,10 @@ void game_init(struct game *game) { mat4_id(mvp); + light_intensity[0] = 0.8; + light_intensity[1] = 0.8; + light_intensity[2] = 0.8; + light_dir[0] = 0.8; light_dir[1] = 0.8; light_dir[2] = 0.8; @@ -65,7 +70,7 @@ void game_init(struct game *game) { node_init(&terrain->entity.node); // player init - ok = load_model(&player->model, "ship"); + ok = load_model(&player->model, "pirate-officer"); assert(ok); player->model.program = res->program.handle; player->node.label = "player"; @@ -85,7 +90,7 @@ void game_init(struct game *game) { /* vec3_all(camera->scale, -1); */ /* camera->mirrored = 1; */ - node_translate(&player->node, V3(terrain->size/2.,terrain->size/2.,0)); + node_translate(&player->node, V3(terrain->size/2.,terrain->size/2.,0.0)); /* vec3_scale(player->node.scale, 10.0, player->node.scale); */ node_rotate(camera, V3(100, 0, 0)); diff --git a/src/game.h b/src/game.h @@ -10,7 +10,7 @@ #include "entity.h" #include "terrain.h" -#define PLAYER_HEIGHT 1.7 +#define PLAYER_HEIGHT 1.73 /* * Global data used by our render callback: @@ -26,6 +26,7 @@ struct resources { struct uniforms { GLint camera_position; GLint light_dir; + GLint light_intensity; GLint mvp; GLint normal_matrix; GLint view; @@ -49,6 +50,7 @@ struct resources { float test_mvp[MAT4_ELEMS]; float light_dir[3]; + float light_intensity[3]; float camera_persp[MAT4_ELEMS]; }; diff --git a/src/render.c b/src/render.c @@ -127,6 +127,9 @@ init_gl(struct resources *resources, int width, int height) { resources->uniforms.camera_position = glGetUniformLocation(handle, "camera_position"); + resources->uniforms.light_intensity = + glGetUniformLocation(handle, "light_intensity"); + resources->uniforms.light_dir = glGetUniformLocation(handle, "light_dir"); @@ -217,7 +220,7 @@ static void render_geom (struct resources *res, void render (struct game *game) { - static const float adjust = 1.0f; + float adjust = game->test_resources.light_intensity[0]; 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 ); check_gl(); @@ -234,6 +237,7 @@ void render (struct game *game) { mat4 *mvp = res->test_mvp; mat4 *persp = res->camera_persp; mat4 *light = res->light_dir; + mat4 *light_intensity = res->light_intensity; struct node *camera = &res->camera; @@ -258,6 +262,8 @@ void render (struct game *game) { 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]); mat4_multiply(view_proj, entity->node.mat, mvp); mat4_copy(entity->node.mat, model_view); @@ -270,8 +276,6 @@ void render (struct game *game) { render_geom(res, &entity->model.geom, GL_TRIANGLES); - printf("i %ld\n", i); - check_gl(); } diff --git a/src/update.c b/src/update.c @@ -135,8 +135,7 @@ static void player_movement(struct game *game) { if (!vec3_eq(res->player.node.pos, last_pos, 0.0001)) { res->player.node.pos[2] = - game->terrain.fn(&game->terrain, res->player.node.pos[0], res->player.node.pos[1]) + - PLAYER_HEIGHT; + game->terrain.fn(&game->terrain, res->player.node.pos[0], res->player.node.pos[1]) + 0.2; node_recalc(&res->camera); @@ -173,10 +172,32 @@ static int try_reload_shaders(struct resources *res) { } #endif +static void day_night_cycle(float n, struct resources *res) { + float darkest = 0.25; + float val = n; + float roots = sin(val); + float circle = fmod(val, TAU); + float angle = circle/TAU; + float hour = 24.0*angle; + float intensity = angle <= 0.5 + ? clamp(roots, darkest, 1.0) + : clamp(-roots * 0.4, darkest, 0.5); + printf("intensity %f(%f) angle %f hour %f n %f\n", roots, intensity, angle, + hour, n); + + res->light_intensity[0] = intensity; + res->light_intensity[1] = intensity; + res->light_intensity[2] = intensity; + + res->light_dir[0] = -cos(val); + res->light_dir[1] = -sin(val); + res->light_dir[2] = 0.8; +} + void update (struct game *game, u32 dt) { static double last_ox, last_oy, last_oz; - static int toggle_fog = 0, toggle_diffuse = 0; - static float n = 1; + static int toggle_fog = 0; + static float n = 1.0; static int first = 1; struct resources *res = &game->test_resources; struct perlin_settings *ts = &game->terrain.settings; @@ -210,19 +231,11 @@ 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; - if (toggle_fog) { res->fog_on = !res->fog_on; toggle_fog = 0; } - if (toggle_diffuse) { - res->diffuse_on = !res->diffuse_on; - toggle_diffuse = 0; - } - double ox = tnode->pos[0]; double oy = tnode->pos[1]; @@ -237,7 +250,9 @@ void update (struct game *game, u32 dt) { n += 0.01f; } - n += 0.001f; + day_night_cycle(n, res); + + n += 0.00001f; node_recalc(root); } diff --git a/src/window.c b/src/window.c @@ -9,7 +9,7 @@ void handle_resize(float *camera, int width, int height) { printf("resizing %d %d\n", width, height); glViewport( 0, 0, width, height ); - mat4_perspective(90 /* fov */, (float)width / height, 1, 20000, camera); + mat4_perspective(75 /* fov */, (float)width / height, 1, 20000, camera); /* glMatrixMode( GL_PROJECTION ); //Switch to setting the camera perspective */ /* Set the camera perspective */