polyadvent

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

commit 21508f95652d60b0647d707007a28f05f4b93905
parent 14ce37ac0c869a05c8c4c8704a0d82fb3710b305
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  3 Nov 2021 19:07:43 -0700

show grid

Diffstat:
M.gitignore | 1+
Metc/shaders/rogue/grid.v.glsl | 2+-
Mrogue/rogue.c | 18+++++++++++-------
Mrogue/rogue.h | 1+
Msrc/chess.c | 2+-
Msrc/engine.c | 4+---
Msrc/grid.c | 23+++++++++++++----------
Msrc/grid.h | 2+-
Msrc/model.h | 1+
Msrc/shader.c | 6++++++
10 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -15,3 +15,4 @@ polyadvent data/models/third-party-src/ /.build-result /.buildcmd +/.direnv diff --git a/etc/shaders/rogue/grid.v.glsl b/etc/shaders/rogue/grid.v.glsl @@ -14,7 +14,7 @@ out shader_data { void main() { - vec3 color = is_white == 1 ? vec3(0.9, 0.9, 0.9) : vec3(0.3, 0.3, 0.3); + vec3 color = position; #include standard_vtxos.glsl gl_Position = mvp * v4_pos; } diff --git a/rogue/rogue.c b/rogue/rogue.c @@ -55,7 +55,7 @@ static int compile_shaders(struct gpu *gpu) static int init_grid(struct grid *grid) { - make_grid_geom(&grid->geom, 1.0); + make_grid_geom(&grid->geom, 32, 32, 1.0); return 1; } @@ -81,6 +81,8 @@ int init_rogue_game(struct engine *engine, struct rogue_game *game) int cell_size = 1.0; init_ortho(game); + + engine->gpu.num_programs = NUM_ROGUE_PROGRAMS; if (!compile_shaders(&engine->gpu)) return 0; return init_grid(&game->grid); @@ -103,7 +105,6 @@ static void render(struct engine *engine, struct rogue_game *game, struct render static float id[MAT4_ELEMS] = { 0 }; static float view[MAT4_ELEMS] = { 0 }; - static float view_proj[MAT4_ELEMS] = { 0 }; static float normal_matrix[MAT4_ELEMS] = { 0 }; mat4_id(id); @@ -117,14 +118,17 @@ static void render(struct engine *engine, struct rogue_game *game, struct render const mat4 *camera = camera_node->mat; mat4_inverse((float*)camera, view); - mat4_multiply(projection, view, view_proj); + mat4_multiply(projection, view, cvars->mvp); cvars->camera_position = (float*)&camera[M_X]; - */ - // no camera for now - mat4_copy(projection, view_proj); + mat4_copy(projection, cvars->mvp); + + mat4_translate(cvars->mvp, V3(-0.5, -0.5, 0.0), cvars->mvp); + mat4_scale(cvars->mvp, V3(32.0/1024.0, 32.0/1024.0, 0.0), cvars->mvp); + + glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); render_grid(&game->grid, &engine->gpu, cvars); @@ -139,7 +143,7 @@ static void render(struct engine *engine, struct rogue_game *game, struct render if (!config->is_depth_pass) { //mat4_inverse((float*)camera, view); mat4_remove_translations(view); - mat4_multiply(projection, view, view_proj); + mat4_multiply(projection, view, cvars->mvp); //render_skybox(&game->skybox, view_proj); } diff --git a/rogue/rogue.h b/rogue/rogue.h @@ -20,6 +20,7 @@ struct common_vars { enum shader_programs { GRID_PROGRAM, + NUM_ROGUE_PROGRAMS, }; struct grid diff --git a/src/chess.c b/src/chess.c @@ -140,7 +140,7 @@ struct entity_id make_chessboard(struct gpu *gpu) assert(node); // setup chessboard - make_grid_geom(geom, cell_size); + make_grid_geom(geom, 10, 10, cell_size); chessboard_model = new_model(&chessboard_model_id, "chessboard"); chessboard_model->geom_id = chessboard_geom_id; diff --git a/src/engine.c b/src/engine.c @@ -47,7 +47,7 @@ static void init_sdl(SDL_Window **window, int width, int height) /* SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); */ - *window = SDL_CreateWindow("SDL2/OpenGL Demo", 0, 0, width, height, + *window = SDL_CreateWindow("polyrogue", 0, 0, width, height, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); SDL_GL_CreateContext(*window); @@ -114,6 +114,4 @@ void init_engine(struct engine *game, int width, int height) //init_controller(&game->input); debug("init misc...\n"); init_misc(game); - - printf("gpu programs %d\n", game->gpu.num_programs); } diff --git a/src/grid.c b/src/grid.c @@ -77,21 +77,24 @@ static void make_grid(float *verts, float vert_capacity, } } -void make_grid_geom(struct geometry *geom, float cell_size) +void make_grid_geom(struct geometry *geom, int width, int height, float cell_size) { struct make_geometry mkgeom; init_make_geometry(&mkgeom); - static float verts[12*8*8]; // array_size(quad) 12 * 8 * 8 - static float colors[12*8*8]; // array_size(quad) 12 * 8 * 8 - static float normals[12*8*8]; // array_size(quad) 12 * 8 * 8 - static u32 indices[6*8*8]; // array_size(quad) 6 * 8 * 8 + int size = 12 * width * height; + int ind_size = 6 * width * height; - make_grid(verts, ARRAY_SIZE(verts), - indices, ARRAY_SIZE(indices), - normals, ARRAY_SIZE(normals), - colors, ARRAY_SIZE(colors), - 8, 8, cell_size); + float verts[size]; // array_size(quad) 12 * w * h + float colors[size]; // array_size(quad) 12 * w * h + float normals[size]; // array_size(quad) 12 * w * h + u32 indices[ind_size]; // array_size(quad) 6 * w * h + + make_grid(verts, size, + indices, ind_size, + normals, size, + colors, size, + width, height, cell_size); mkgeom.indices = indices; mkgeom.vertices = verts; diff --git a/src/grid.h b/src/grid.h @@ -3,4 +3,4 @@ #include "geometry.h" -void make_grid_geom(struct geometry *geom, float cell_size); +void make_grid_geom(struct geometry *geom, int width, int height, float cell_size); diff --git a/src/model.h b/src/model.h @@ -21,6 +21,7 @@ struct model { /* geometry_id geom_id; */ struct geometry_id geom_id; struct pose poses[MAX_POSES]; // TODO: separate animated_model buffer? + int shader; int nposes; u32 texture; u32 name_hash; diff --git a/src/shader.c b/src/shader.c @@ -109,6 +109,8 @@ int make_shader(GLenum type, const char *filename, struct shader *shader) { glShaderSource(shader->handle, nlines, (const char**)lines, (const int*)line_lens); + check_gl(); + // shader dependency stuff for (int i = 0; i < file_buf_count; ++i) { assert(i < MAX_SHADER_INCLUDES); @@ -127,12 +129,16 @@ int make_shader(GLenum type, const char *filename, struct shader *shader) { file_buf_count = 0; glCompileShader(shader->handle); + check_gl(); + glGetShaderiv(shader->handle, GL_COMPILE_STATUS, &shader_ok); + check_gl(); if (!shader_ok) { fprintf(stderr, "Failed to compile %s:\n", filename); show_shader_info_log(shader->handle); glDeleteShader(shader->handle); + check_gl(); return 0; }