polyadvent

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

commit a7d4acdf196386e4fb3115ee89bfc80165775324
parent 2383374a49b5d0ddd8f9cd4431960274905627c0
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 12 Nov 2018 16:22:27 -0800

more tweaking

Diffstat:
Metc/shaders/fog.glsl | 9+++++++--
Metc/shaders/lighting.glsl | 11+++++++----
Metc/shaders/shadervars.glsl | 1-
Metc/shaders/standard_vtxos.glsl | 1-
Metc/shaders/terrain.g.glsl | 2+-
Metc/shaders/terrain.te.glsl | 2+-
Metc/shaders/terrain.v.glsl | 5++---
Metc/shaders/test.f.glsl | 26+++++++++++++-------------
Msrc/render.c | 1+
Msrc/update.c | 54++++++++++++++++++++++++++++++++++++++----------------
10 files changed, 70 insertions(+), 42 deletions(-)

diff --git a/etc/shaders/fog.glsl b/etc/shaders/fog.glsl @@ -1,12 +1,17 @@ -vec3 apply_fog(in vec3 rgb, in float distance, in vec3 ray_orig, in vec3 ray_dir) { + +vec3 apply_fog(in vec3 rgb, in vec3 vert_pos, in float distance, + in vec3 ray_orig, in vec3 ray_dir) +{ const float b = 0.00035; - float draw_dist = 1.4; + float draw_dist = 1.2; float fog_amount = 1.0-exp(-pow(distance * (1.0/draw_dist) * b, 6.0)); // float sun_amount = max( dot( ray_dir, -light_dir ), 0.0 ) * b; // sun_amount = pow(sun_amount, 0.0); + // vec3 I = normalize(vert_pos - camera_position); + // vec3 R = refract(I, vertex.normal, 1.0/2.0); vec3 fog_color = sun_color * sky_intensity; // vec3 fog_color = sun_color; diff --git a/etc/shaders/lighting.glsl b/etc/shaders/lighting.glsl @@ -80,7 +80,7 @@ vec3 standard_light(vec3 color, vec4 position, vec4 normal) { // tone mapping // final = final / (vec3(1.0) - final * exposure); // final = final / (vec3(1.0) + color); - final = uncharted_tonemap(final); + // final = uncharted_tonemap(final); return final; } @@ -126,10 +126,11 @@ vec3 fresnel_schlick(float cos_theta, vec3 F0) } vec3 pbr(vec3 albedo, vec3 V, vec3 normal) { - const float ao = 2.0; - const float metallic = 0.8; - const float roughness = 0.2; + const float ao = 5.0; + const float metallic = 0.6; + const float roughness = 0.4; vec3 radiance = sun_color * light_intensity; + // radiance += texture(skybox, vec3(0.0, 0.0, 1.0)).rgb; vec3 N = normalize(normal); vec3 L = normalize(light_dir); vec3 H = normalize(V + L); @@ -167,6 +168,8 @@ vec3 pbr(vec3 albedo, vec3 V, vec3 normal) { vec3 ambient = vec3(0.03) * albedo * ao; vec3 color = ambient + Lo; + // color = uncharted_tonemap(color); + //color = color / (vec3(1.0) + color); return color; } diff --git a/etc/shaders/shadervars.glsl b/etc/shaders/shadervars.glsl @@ -3,4 +3,3 @@ vec3 color_smooth; vec3 normal; vec3 position; vec4 shadow_coord; -vec3 frag_pos; diff --git a/etc/shaders/standard_vtxos.glsl b/etc/shaders/standard_vtxos.glsl @@ -6,5 +6,4 @@ data_out.normal = mat3(transpose(inverse(model))) * normal; data_out.position = vec3(model * v4_pos); data_out.color_smooth = data_out.color = color; data_out.shadow_coord = depth_mvp * v4_pos; -data_out.frag_pos = (model * v4_pos).xyz; // TODO: move shadow coord calc from frag to here diff --git a/etc/shaders/terrain.g.glsl b/etc/shaders/terrain.g.glsl @@ -24,7 +24,7 @@ void main() { vertex.color_smooth = vertices[i].color_smooth; vertex.normal = vertices[i].normal; // vertex.shadow_coord = depth_mvp * v4_pos; - vertex.frag_pos = (world * v4_pos).xyz; + vertex.world_pos = (world * v4_pos).xyz; vertex.shadow_coord = vertices[i].shadow_coord; // vertex.frag_pos = vertices[i].frag_pos; diff --git a/etc/shaders/terrain.te.glsl b/etc/shaders/terrain.te.glsl @@ -29,6 +29,6 @@ void main() data_out.color_smooth = data_in[0].color_smooth; data_out.normal = data_in[0].normal; // data_out.shadow_coord = depth_mvp * data_in[0].shadow_coord; - data_out.frag_pos = (world * v).xyz; + data_out.world_pos = (world * v).xyz; } diff --git a/etc/shaders/terrain.v.glsl b/etc/shaders/terrain.v.glsl @@ -33,13 +33,12 @@ void main() smoothstep(land[i].w, land[i+1].w, position.z)); } - // vec3 color = vec3(position.z*0.05, position.z*0.0095, position.z*0.0001) * 0.5; + // vec3 color = vec3(position.z*0.05, position.z*0.0055, position.z*0.0001) * 0.5; - // v_color = vec3(position.z, position.z, position.z) * 0.005; + // vec3 color = vec3(position.z, position.z, position.z) * 0.005; #include standard_vtxos.glsl gl_Position = mvp * v4_pos; - data_out.position = gl_Position.xyz; } diff --git a/etc/shaders/test.f.glsl b/etc/shaders/test.f.glsl @@ -22,30 +22,30 @@ vec3 reflect_env(vec3 vert_pos) { return color; } -vec3 refract_env(vec3 vert_pos) { - const float ratio = 1.00 / 1.52; +// vec3 refract_env(vec3 vert_pos) { +// const float ratio = 1.00 / 1.52; - vec3 I = normalize(vert_pos - camera_position); - vec3 R = refract(I, normalize(vertex.normal), ratio); - vec3 color = texture(skybox, R).rgb; - return color; -} +// vec3 I = normalize(vert_pos - camera_position); +// vec3 R = refract(I, normalize(vertex.normal), ratio); +// vec3 color = texture(skybox, R).rgb; +// return color; +// } void main() { - vec3 V = camera_position - vertex.frag_pos; + vec3 V = camera_position - vertex.position; vec4 v4_pos = vec4(vertex.position, 1.0); vec4 v4_normal = vec4(vertex.normal, 1.0); // vec3 color = standard_light(vertex.color, v4_pos, v4_normal); + // vec3 color = reflect_env(vertex.position); vec3 color = pbr(vertex.color, normalize(V), vertex.normal); - if (fog_on) { - vec3 fog = apply_fog(color, length(V), camera_position, V); - color = fog; - } + // if (fog_on) { + // vec3 fog = apply_fog(color, vertex.position, length(V), camera_position, V); + // color = fog; + // } color *= shadow_strength(v4_pos, v4_normal, vertex.shadow_coord); - // vec3 color = reflect_env(vertex.position); frag_color = vec4(gamma_correct(color), 1.0); } diff --git a/src/render.c b/src/render.c @@ -291,6 +291,7 @@ void render (struct game *game, struct render_config *config) { mat4_multiply(projection, view, view_proj); glBindTexture(GL_TEXTURE_CUBE_MAP, res->skybox.model.texture); + check_gl(); for (u32 i = 0; i < num_entities; ++i) { struct entity *entity = &entities[i]; diff --git a/src/update.c b/src/update.c @@ -175,17 +175,6 @@ static void player_terrain_collision(struct terrain *terrain, struct entity *pla static void player_movement(struct game *game, struct entity *player) { movement(game, &player->node, 2.0); - - if (!(get_entity(&game->terrain.entity_id)->flags & ENT_INVISIBLE)) { - vec3 *camera_world = node_world(&game->test_resources.camera.node); - float cam_terrain_z = - game->terrain.fn(&game->terrain, camera_world[0], camera_world[1]); - - const float bias = 20.0; - - if (camera_world[2] < cam_terrain_z + bias) - camera_world[2] = cam_terrain_z + bias; - } } @@ -311,7 +300,7 @@ void orbit_update_from_mouse(struct orbit *camera, struct input *input, node_recalc(target_node); vec3_copy(node_world(target_node), target); - vec3_add(target, V3(0.0, 0.0, player->model.geom.max[2]), target); + /* vec3_add(target, V3(0.0, 0.0, player->model.geom.max[2]), target); */ float mx = 0.0, my = 0.0; if (input_is_dragging(input, SDL_BUTTON_LEFT) || @@ -320,6 +309,16 @@ void orbit_update_from_mouse(struct orbit *camera, struct input *input, my = -input->mdy * mouse_sens * dt; } + // zoom + if (input->keystates[SDL_SCANCODE_V]) { + if (input->modifiers & KMOD_SHIFT) + camera->coords.radius += dt * 100.0; + else + camera->coords.radius -= dt * 100.0; + + camera->coords.radius = max(5.0, camera->coords.radius); + } + camera->coords.azimuth += mx; camera->coords.inclination += my; /* printf("coords azimuth %f inclination %f radius %f\n", */ @@ -328,18 +327,41 @@ void orbit_update_from_mouse(struct orbit *camera, struct input *input, /* camera->coords.radius); */ spherical_look_at(&camera->coords, target, camera->node.mat); + +} + +static void orbit_keep_above_ground(struct game *game) { + struct resources *res = &game->test_resources; + + if (!(get_entity(&game->terrain.entity_id)->flags & ENT_INVISIBLE)) { + float *camera_world = node_world(&res->camera.node); + /* float *target = node_world(&get_player(res)->node); */ + /* spherical_pos(&res->camera.coords, target, camera_world); */ + float cam_terrain_z = + game->terrain.fn(&game->terrain, camera_world[0], camera_world[1]); + + const float bias = 5.0; + + if (camera_world[2] < cam_terrain_z + bias) + camera_world[2] = cam_terrain_z + bias; + } } static void player_update(struct game *game, struct entity *player) { - orbit_update_from_mouse(&game->test_resources.camera, &game->input, - game->user_settings.mouse_sens, player, game->dt); + + struct orbit *camera = &game->test_resources.camera; + orbit_update_from_mouse(camera, &game->input, game->user_settings.mouse_sens, + player, game->dt); + + orbit_keep_above_ground(game); + // move player camera toward camera orientation if (input_is_dragging(&game->input, SDL_BUTTON_RIGHT)) { float yaw = game->test_resources.camera.coords.azimuth; quat_axis_angle(V3(0.0, 0.0, 1.0), -yaw - RAD(90), player->node.orientation); - node_recalc(&player->node); } player_terrain_collision(&game->terrain, player); + node_recalc(&player->node); } @@ -361,7 +383,6 @@ void update (struct game *game) { first = 0; } - player_update(game, player); /* spherical_dir(game->test_resources.camera.coords, camera_dir); */ /* vec3_scale(camera_dir, -1, camera_dir); */ @@ -371,6 +392,7 @@ void update (struct game *game) { else { player_movement(game, player); } + player_update(game, player); #ifdef DEBUG