polyadvent

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

commit 3e3c5e978dfc12d5ee7d768a5c5ed74760899352
parent ad85b8fa49b8688e8b4f23ef16176b483e16d671
Author: William Casarin <jb55@jb55.com>
Date:   Sun,  4 Nov 2018 00:30:37 -0700

misc tinkering

Diffstat:
Metc/shaders/lighting.glsl | 2+-
Msrc/geometry.c | 7+++++++
Msrc/geometry.h | 1+
Msrc/main.c | 4----
Msrc/render.c | 9+++++----
Msrc/update.c | 15++++++++++++---
Msrc/util.c | 4++++
7 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/etc/shaders/lighting.glsl b/etc/shaders/lighting.glsl @@ -20,7 +20,7 @@ vec3 gamma_correct(vec3 color) { } vec3 standard_light(vec3 color, vec3 position, vec3 normal) { - vec4 trans_normal = vec4(normal, 1); + vec4 trans_normal = normal_matrix * vec4(normal, 1.0); // vec3 light_dir = vec3() const float pi = 3.14159265; diff --git a/src/geometry.c b/src/geometry.c @@ -132,3 +132,10 @@ make_buffer_geometry(struct geometry *geom) { &geom->vbos.index ); } + + + +void geometry_centroid(struct geometry *geom, float *dest) { + vec3_subtract(geom->max, geom->min, dest); + vec3_scale(dest, 0.5, dest); +}; diff --git a/src/geometry.h b/src/geometry.h @@ -33,5 +33,6 @@ void bind_geometry(struct geometry *geom, struct attributes *); void init_geometry(struct geometry *geom); void make_buffer_geometry(struct geometry *geom); void destroy_buffer_geometry(struct geometry *geom); +void geometry_centroid(struct geometry *geom, float *v3); #endif /* GEOMETRY_H */ diff --git a/src/main.c b/src/main.c @@ -35,14 +35,10 @@ int main(void) int width = 640; int height = 480; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4); - SDL_Window *window = SDL_CreateWindow( "SDL2/OpenGL Demo", 0, 0, width, height, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); - SDL_GL_CreateContext(window); game_init(&game, width, height); diff --git a/src/render.c b/src/render.c @@ -68,8 +68,10 @@ init_gl(struct resources *resources, int width, int height) { int ok = 0; glEnable(GL_DEPTH_TEST); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4); // Shaders ok = make_shader(GL_VERTEX_SHADER, SHADER("vertex-color.glsl"), &vertex); @@ -231,7 +233,6 @@ 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 ); - glDisable(GL_CULL_FACE); check_gl(); @@ -277,7 +278,7 @@ 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); + /* glCullFace(GL_FRONT); */ } else { glUniformMatrix4fv(res->uniforms.depth_mvp, 1, 0, config->depth_mvp); diff --git a/src/update.c b/src/update.c @@ -220,11 +220,13 @@ void resize_fbos(struct game *game, int width, int height) { delete_fbo(&res->shadow_buffer); } - const float factor = 8.0; + // TODO: compute better bounds based + const float factor = 4.0; float left = res->player.model.geom.min[0] * factor; float right = res->player.model.geom.max[0] * factor; float bottom = res->player.model.geom.min[1] * factor; float top = res->player.model.geom.max[1] * factor; + const float near = -10000.0; const float far = 10000.0; @@ -249,6 +251,7 @@ void resize_fbos(struct game *game, int width, int height) { static void day_night_cycle(float time, struct resources *res) { float val = time * 0.0001; float intensity = max(0.0, vec3_dot(res->light_dir, V3(0.0, 0.0, 0.8))); + struct entity *player = &res->player; float light_pos[3]; @@ -281,9 +284,15 @@ static void day_night_cycle(float time, struct resources *res) { /* printf("intensity %f(%f) n %f light_dir %f %f\n", roots, intensity, */ /* n, res->light_dir[1], res->light_dir[2]); */ - vec3_add(res->player.node.pos, res->light_dir, light_pos); + vec3_add(player->node.pos, res->light_dir, light_pos); + + /* float target[3]; */ + /* float hh = player->model.geom.max[2] / 2.0; */ + /* vec3_copy(player->node.pos, target); */ + /* target[2] += 2.0; */ - look_at(light_pos, res->player.node.pos, V3(0, 0, 1.0), res->sun_camera.mat); + look_at(light_pos, player->node.pos, V3(0, 0, 1.0), res->sun_camera.mat); + /* look_at(light_pos, player->node.pos, V3(0, 0, 1.0), res->sun_camera.mat); */ } static void gravity(struct game *game) { diff --git a/src/util.c b/src/util.c @@ -67,21 +67,25 @@ void look_at(vec3 *eye, vec3 *target, vec3 *up, mat4 *dest) { dest[0] = x[0]; dest[1] = x[1]; dest[2] = x[2]; + /* dest[3] = 0.0; */ /* dest[3] = vec3_dot(x, eye); */ dest[4] = y[0]; dest[5] = y[1]; dest[6] = y[2]; + /* dest[7] = 0.0; */ /* dest[7] = vec3_dot(y, eye); */ dest[8] = z[0]; dest[9] = z[1]; dest[10] = z[2]; + /* dest[11] = 0.0; */ /* dest[11] = vec3_dot(z, eye); */ dest[12] = eye[0]; dest[13] = eye[1]; dest[14] = eye[2]; + /* dest[15] = 0.0; */ }