polyadvent

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

commit 37ae4c6c6e23f596e31e92ea49734871fbc13042
parent e5e6566c9fd688d5b051186c0cc5c68038812f1b
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  8 Nov 2018 16:49:12 -0800

more skybox progress

Diffstat:
Metc/shaders/skybox.v.glsl | 3++-
Msrc/game.c | 8++++++--
Msrc/game.h | 1+
Msrc/geometry.c | 3++-
Msrc/mat4.c | 7+++++++
Msrc/mat4.h | 3+++
Msrc/node.c | 19+++++++++++--------
Msrc/orbit.c | 27+++++++++++++++++++++++++++
Msrc/orbit.h | 2++
Msrc/render.c | 13+++++++++----
Msrc/skybox.c | 18++++++++++++------
Msrc/skybox.h | 6++++--
Msrc/window.c | 2+-
13 files changed, 87 insertions(+), 25 deletions(-)

diff --git a/etc/shaders/skybox.v.glsl b/etc/shaders/skybox.v.glsl @@ -7,5 +7,6 @@ uniform mat4 mvp; void main() { tex_coords = position; - gl_Position = mvp * vec4(position, 1.0); + vec4 pos = mvp * vec4(position.xyz, 0.00018); + gl_Position = pos.xyzw; } diff --git a/src/game.c b/src/game.c @@ -75,7 +75,9 @@ void game_init(struct game *game, int width, int height) { check_gl(); init_terrain(terrain, size); - create_skybox(&res->skybox); + create_skybox(&res->skybox, &res->programs[SKYBOX_PROGRAM]); + /* node_translate(&res->skybox.node, V3(-100.0, -100.0, 0.0)); */ + /* node_scale(&res->skybox.node, size/4.0); */ mat4_id(mvp); @@ -98,8 +100,8 @@ void game_init(struct game *game, int width, int height) { game->test_resources.diffuse_on = 0; node_init(root); - node_init(camera); node_init(sun_camera); + init_orbit(&res->camera); // ENTITIES @@ -109,6 +111,8 @@ void game_init(struct game *game, int width, int height) { assert(ok); player->node.label = "player"; node_attach(&player->node, root); + node_attach(&res->skybox.node, &player->node); + /* node_attach(&res->camera.node, &player->node); */ node_translate(&player->node, V3(terrain->size/2.,terrain->size/2.,0.0)); res->camera.coords.azimuth = -quat_yaw(player->node.orientation) - RAD(90.0); diff --git a/src/game.h b/src/game.h @@ -23,6 +23,7 @@ enum program_type { DEFAULT_PROGRAM, TERRAIN_PROGRAM, UI_PROGRAM, + SKYBOX_PROGRAM, NUM_PROGRAMS, }; diff --git a/src/geometry.c b/src/geometry.c @@ -69,7 +69,8 @@ void render_geometry(struct geometry *geom, struct attributes *attrs, check_gl(); } else { - glDrawArrays(type, 0, geom->num_verts); + /* printf("nverts %d\n", geom->num_verts); */ + glDrawArrays(type, 0, geom->num_verts / 3); check_gl(); } } diff --git a/src/mat4.c b/src/mat4.c @@ -229,6 +229,13 @@ mat4 *mat4_inverse(mat4 *src, mat4 *dest) { return dest; } +mat4 *mat4_remove_translations(mat4 *dest) { + dest[12] = 0.0; + dest[13] = 0.0; + dest[14] = 0.0; + dest[15] = 1.0; + return dest; +} mat4 *mat4_multiply(const mat4 *a, const mat4 *b, mat4 *dst) { float a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; diff --git a/src/mat4.h b/src/mat4.h @@ -27,3 +27,6 @@ void mat4_print(const mat4 *a); mat4 *mat4_rotate(const mat4 *mat, const float angle, const float *v3_axis, mat4 *dest); + + +mat4 *mat4_remove_translations(mat4 *mat); diff --git a/src/node.c b/src/node.c @@ -96,16 +96,19 @@ int node_recalc(struct node *node) { node->needs_recalc = 0; - quat_to_mat3(node->orientation, rot); - mat4_create_transform(node->pos, node->scale, rot, node->mat); + if (!node->custom_update) { + quat_to_mat3(node->orientation, rot); + mat4_create_transform(node->pos, node->scale, rot, node->mat); - if (node->parent) { - assert(!node->parent->needs_recalc); - mat4_multiply(node->parent->mat, node->mat, node->mat); - } + if (node->parent) { + assert(!node->parent->needs_recalc); + mat4_multiply(node->parent->mat, node->mat, node->mat); + } - if (node->custom_update) - node->custom_update(node); + } + else { + node->custom_update(node); + } node_recalc_children(node); diff --git a/src/orbit.c b/src/orbit.c @@ -48,4 +48,31 @@ vec3 *spherical_look_at(struct spherical *s, vec3 *target, mat4 *mat) { } +static void orbit_node_update(struct node *node) { + if (!node->parent) + return; + float *a = node->parent->mat; + float *b = node->pos; + float *dst = node->mat; + + float b30 = b[0], b31 = b[1], b32 = b[2], b33 = 1.0; + + float a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; + float a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; + float a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; + float a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; + + + dst[12] = b30*a00 + b31*a10 + b32*a20 + b33*a30; + dst[13] = b30*a01 + b31*a11 + b32*a21 + b33*a31; + dst[14] = b30*a02 + b31*a12 + b32*a22 + b33*a32; + dst[15] = b30*a03 + b31*a13 + b32*a23 + b33*a33; +} + + +void init_orbit(struct orbit *orbit) { + node_init(&orbit->node); + /* orbit->node.custom_update = orbit_node_update; */ + /* orbit->node.custom_update_data = orbit; */ +} diff --git a/src/orbit.h b/src/orbit.h @@ -17,6 +17,8 @@ struct orbit { struct node node; }; +void init_orbit(struct orbit *orbit); + vec3 *spherical_dir(struct spherical s, vec3 *dir); vec3 *spherical_pos(struct spherical *s, vec3 *from, vec3 *to); vec3 *spherical_look_at(struct spherical *s, vec3 *target, mat4 *mat); diff --git a/src/render.c b/src/render.c @@ -273,9 +273,6 @@ void render (struct game *game, struct render_config *config) { struct gpu_program *default_program = &game->test_resources.programs[DEFAULT_PROGRAM]; - mat4_inverse(camera, view); - mat4_multiply(projection, view, view_proj); - if (config->is_depth_pass) { glDisable(GL_CULL_FACE); mat4_multiply(bias_matrix, view_proj, config->depth_vp); @@ -284,7 +281,9 @@ void render (struct game *game, struct render_config *config) { glCullFace(GL_BACK); } - render_skybox(&res->skybox, view_proj); + + mat4_inverse(camera, view); + mat4_multiply(projection, view, view_proj); for (u32 i = 0; i < num_entities; ++i) { struct entity *entity = &entities[i]; @@ -348,6 +347,12 @@ void render (struct game *game, struct render_config *config) { if (config->draw_ui) render_ui(&game->ui, view); + mat4_inverse(camera, view); + mat4_remove_translations(view); + mat4_multiply(projection, view, view_proj); + /* mat4_multiply(view_proj, res->skybox.node.mat, mvp); */ + render_skybox(&res->skybox, view_proj); + //player // y tho diff --git a/src/skybox.c b/src/skybox.c @@ -48,13 +48,18 @@ static float skybox_vertices[] = { 0.0f, -1.0f, 1.0f }; -void create_skybox(struct skybox *skybox) { +void create_skybox(struct skybox *skybox, struct gpu_program *program) { struct shader vertex, frag; struct shader *shaders[] = {&vertex, &frag}; + int ok; + node_init(&skybox->node); init_model(&skybox->model); + + skybox->program = program; skybox->model.geom.vertices = skybox_vertices; skybox->model.geom.num_verts = ARRAY_SIZE(skybox_vertices); + make_buffer_geometry(&skybox->model.geom); static const char *faces[6] = { @@ -72,14 +77,15 @@ void create_skybox(struct skybox *skybox) { make_shader(GL_FRAGMENT_SHADER, SHADER("skybox.f.glsl"), &frag); check_gl(); - make_program_from_shaders(shaders, 2, &skybox->program); + ok = make_program_from_shaders(shaders, 2, skybox->program); + assert(ok); check_gl(); skybox->uniforms.mvp = - glGetUniformLocation(skybox->program.handle, "mvp"); + glGetUniformLocation(skybox->program->handle, "mvp"); skybox->attrs.position = (gpu_addr) - glGetAttribLocation(skybox->program.handle, "position"); + glGetAttribLocation(skybox->program->handle, "position"); } @@ -87,7 +93,7 @@ void render_skybox(struct skybox *skybox, mat4 *camera) { glDepthMask(GL_FALSE); check_gl(); - glUseProgram(skybox->program.handle); + glUseProgram(skybox->program->handle); check_gl(); glUniformMatrix4fv(skybox->uniforms.mvp, 1, 0, camera); @@ -96,7 +102,7 @@ void render_skybox(struct skybox *skybox, mat4 *camera) { glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->model.texture); check_gl(); - render_geometry(&skybox->model.geom, &skybox->attrs, &skybox->program); + render_geometry(&skybox->model.geom, &skybox->attrs, skybox->program); check_gl(); glDepthMask(GL_TRUE); diff --git a/src/skybox.h b/src/skybox.h @@ -4,18 +4,20 @@ #include "model.h" #include "shader.h" +#include "node.h" #include "mat4.h" struct skybox { - struct gpu_program program; + struct gpu_program *program; struct model model; struct attributes attrs; + struct node node; struct { int mvp; } uniforms; }; -void create_skybox(struct skybox *skybox); +void create_skybox(struct skybox *skybox, struct gpu_program *program); void render_skybox(struct skybox *skybox, mat4 *camera); #endif /* SKYBOX_H */ diff --git a/src/window.c b/src/window.c @@ -11,7 +11,7 @@ void handle_resize(struct game *game, int width, int height) { /* printf("resizing %d %d\n", width, height); */ glViewport( 0, 0, width, height ); - mat4_perspective(75 /* fov */, (float)width / height, 1, 20000, + mat4_perspective(75 /* fov */, (float)width / height, 50.0, 10000.0, game->test_resources.proj_persp); resize_fbos(get_entity(&game->test_resources.player_id),