polyadvent

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

commit 86dcd139cfc222aafa710dab3d941df5fa481301
parent 9c88e0889ab92786cf81d7b85960430b3278ef70
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  1 Nov 2018 17:03:13 -0700

progress

Diffstat:
Metc/shaders/lighting.glsl | 13++++++++-----
Msrc/event.c | 19++++++++++++-------
Msrc/game.c | 4++--
Msrc/input.c | 11+++++++++--
Msrc/input.h | 7++++---
Msrc/orbit.c | 15+++++++++++++++
Msrc/render.c | 4++++
Msrc/update.c | 7+++++--
8 files changed, 59 insertions(+), 21 deletions(-)

diff --git a/etc/shaders/lighting.glsl b/etc/shaders/lighting.glsl @@ -23,12 +23,14 @@ vec3 standard_light(vec3 color, vec3 position) { vec4 v4_normal = vec4(v_normal , 1); vec4 trans_normal = normal_matrix * v4_normal; + vec3 light_dir = vec3() const float pi = 3.14159265; const float shiny = 12.0; - const float exposure = 10.0; + const float exposure = 0.2; + const float ambient_str = 0.2; + float spec_str = 0.4 * light_intensity; - float ambient_str = 0.3; - float spec_str = 0.8 * light_intensity; + // float light_intensity = light_intensity * 0.01; // too much ambient during daytime is making things look weird // ambient_str =- light_intensity * ambient_str; @@ -67,8 +69,9 @@ vec3 standard_light(vec3 color, vec3 position) { vec3 specular = spec_str * spec * sun_color; vec3 final = (ambient + diffuse + specular) * color; - // tone mappink - final = final / (vec3(1.0) - exp(-final * exposure)); + // tone mapping + // final = final / (vec3(1.0) - final * exposure); + // final = final / (vec3(1.0) + color); return final; } diff --git a/src/event.c b/src/event.c @@ -17,16 +17,23 @@ void process_events(struct game *game, float *camera) { case SDL_KEYUP: handle_key(&game->input, event.key); break; + case SDL_MOUSEBUTTONDOWN: + if (event.button.button <= MOUSE_BUTTONS) + game->input.mbuttons[event.button.button-1] = 1; + + break; + case SDL_MOUSEBUTTONUP: + if (event.button.button <= MOUSE_BUTTONS) + game->input.mbuttons[event.button.button-1] = 0; + break; case SDL_MOUSEMOTION: if (event.button.button) { - if (event.button.button <= MOUSE_BUTTONS) - game->input.mbuttons[event.button.button-1] = 1; - game->input.is_dragging = 1; - mdx += event.motion.xrel; - mdy += event.motion.yrel; /* printf("drag... %d %d\n", event.motion.xrel, event.motion.yrel); */ + game->input.last_mx = game->input.mx; + game->input.last_my = game->input.mx; game->input.mx = event.motion.x; game->input.my = event.motion.y; + } break; case SDL_WINDOWEVENT: @@ -43,6 +50,4 @@ void process_events(struct game *game, float *camera) { } - game->input.mdx = mdx; - game->input.mdy = mdy; } diff --git a/src/game.c b/src/game.c @@ -54,7 +54,7 @@ void game_init(struct game *game, int width, int height) { .scale = 1.0 }; - static const int shadowmap_scale = 10.0; + static const int shadowmap_scale = 100.0; // default ortho screenspace projection mat4_ortho(-shadowmap_scale, // left @@ -97,7 +97,7 @@ void game_init(struct game *game, int width, int height) { // FBO STUFF init_fbo(&res->shadow_buffer); - resize_fbos(game, width * shadowmap_scale, height * shadowmap_scale); + resize_fbos(game, width, height); // FBO STUFF END diff --git a/src/input.c b/src/input.c @@ -7,8 +7,10 @@ void input_init(struct input *input) { /* memset(input->keys, 0, sizeof(input->keys[0]) * ARRAY_SIZE(input->keys)); */ input->keystates = SDL_GetKeyboardState(NULL); input->is_dragging = 0; - input->mdx = 0; - input->mdy = 0; + input->mx = 0; + input->my = 0; + input->last_mx = 0; + input->last_my = 0; assert(input->keystates); } @@ -23,3 +25,8 @@ void input_reset(struct input *input) { for (int i = 0; i < MOUSE_BUTTONS; ++i) input->mbuttons[i] = 0; } + +int input_is_dragging(struct input *input, int mouse_button) { + return input->mbuttons[mouse_button-1] && + (input->last_mx != input->mx || input->last_my != input->my); +} diff --git a/src/input.h b/src/input.h @@ -18,16 +18,17 @@ struct input { /* enum key_state keys[0x7F-0x1F]; */ u8 const *keystates; SDL_Keymod modifiers; - int mdx, mdy; - int mx, my; + int mx, my, last_mx, last_my; int is_dragging; int mbuttons[MOUSE_BUTTONS]; }; +int input_is_dragging(struct input *input, int mouse_button); + void input_init(struct input *input); void input_reset(struct input *input); -void handle_key(struct input *input, SDL_KeyboardEvent ke); +void handle_key(struct input *input, SDL_KeyboardEvent); #endif /* POLYADVENT_INPUT_H */ diff --git a/src/orbit.c b/src/orbit.c @@ -1,6 +1,7 @@ #include "orbit.h" #include "node.h" +#include "vec3.h" #include <math.h> void orbit_to_quat(struct orbit *orbit, float *quat) @@ -23,3 +24,17 @@ void orbit_to_node(struct orbit *orbit, struct node *node) { node_mark_for_recalc(node); } + +static void arcball_vector(int screen_width, int screen_height, int x, int y, float *p) +{ + p[0] = x/screen_width*2.0 - 1.0; + p[1] = -y/screen_height*2.0 - 1.0; + p[2] = 0.0; + + float xy_sq = p[0]*p[0] + p[1]*p[1]; + if (xy_sq < 1.0) + p[2] = sqrt(-xy_sq); + else + vec3_normalize(p, p); +} + diff --git a/src/render.c b/src/render.c @@ -195,6 +195,8 @@ void render (struct game *game, struct render_config *config) { glClearColor( gtmp[0], gtmp[1], gtmp[2], 1.0 ); //clear background screen to black /* 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 ); + glEnable(GL_CULL_FACE); + check_gl(); static float id[MAT4_ELEMS] = { 0 }; @@ -237,9 +239,11 @@ void render (struct game *game, struct render_config *config) { if (config->is_depth_pass) { mat4_multiply(bias_matrix, view_proj, config->depth_mvp); + glCullFace(GL_FRONT); } else { glUniformMatrix4fv(res->uniforms.depth_mvp, 1, 0, config->depth_mvp); + glCullFace(GL_BACK); } glUniform3f(res->uniforms.camera_position, diff --git a/src/update.c b/src/update.c @@ -244,9 +244,11 @@ static void day_night_cycle(float time, struct resources *res) { float light_pos[3]; + float g = 0.6; + float b = 0.4; res->sun_color[0] = 1.0; - res->sun_color[1] = 0.9; - res->sun_color[2] = 0.8; + res->sun_color[1] = g+intensity*(1.0-g); + res->sun_color[2] = b+intensity*(1.0-b); /* res->sun_color[0] = 1.0; */ /* res->sun_color[1] = 1.0; */ @@ -289,6 +291,7 @@ static void player_update(struct game *game, struct entity *player) { /* orbit_camera->azimuth += game->dt * 2.0; */ /* orbit_to_node(orbit_camera, &game->test_resources.camera); */ + player_terrain_collision(&game->terrain, player); }