polyadvent

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

commit d4cf1ea89e155de7b6432fd0bb4ae9177dad8614
parent 21ddec754a19406503a6fb40a04aed97848dde52
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  4 Oct 2021 23:03:50 -0700

more cleanup and prep for rogue engine

Diffstat:
MMakefile | 8++++++--
Aetc/shaders/rogue/main.f.glsl | 19+++++++++++++++++++
Aetc/shaders/rogue/uniforms.glsl | 6++++++
Mmain.c | 27+++++++++++++--------------
Mrogue/rogue.c | 243+++++++++++++++++++++++++++++++++++--------------------------------------------
Mrogue/rogue.h | 19++++++++++++++++++-
Msrc/chess.c | 235+++++++++++++++++++++----------------------------------------------------------
Msrc/chess.h | 2+-
Msrc/engine.c | 47++++++++++++++++++++++++-----------------------
Msrc/engine.h | 4++--
Msrc/entity.c | 64++++++++++++++++++++++++++++++++--------------------------------
Msrc/entity.h | 14++++++--------
Msrc/geometry.c | 6+++---
Msrc/gpu.c | 13+++++++++++++
Msrc/gpu.h | 1+
Asrc/grid.c | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/grid.h | 6++++++
Msrc/mat4.c | 6------
Msrc/mat4.h | 9+++++++--
Msrc/model.c | 7++++---
Msrc/model.h | 2+-
Msrc/movement.h | 2+-
Msrc/node.c | 31+------------------------------
Msrc/node.h | 39++++++++++++++++++++++++++++++++++-----
Msrc/orbit_util.c | 22++++++++++------------
Msrc/resource.c | 12------------
Msrc/resource.h | 30++++++++++++++++++++++--------
Msrc/shader.h | 1+
Msrc/skybox.c | 4++--
Msrc/terrain.c | 4++--
Msrc/terrain.h | 6+++---
Msrc/test_game.c | 8++++----
Msrc/update.h | 2+-
33 files changed, 523 insertions(+), 485 deletions(-)

diff --git a/Makefile b/Makefile @@ -24,7 +24,7 @@ CFLAGS = -DGLFW_INCLUDE_NONE $(FLAGS) -Isrc \ LDFLAGS = $(TINYCC_LDFLAGS) -lSDL2 -lGL -lm SRC=src -SRCS=$(wildcard $(SRC)/*.c) +SRCS=$(wildcard $(SRC)/*.c) $(wildcard rogue/*.c) PLYS=$(wildcard data/models/*.ply) MODELS=$(PLYS:.ply=.mdl) @@ -41,6 +41,10 @@ all: $(BIN) $(MODELS) clean: rm -f main.o polyadvent.o src/main.o test/*.o tools/*.o $(TOOLS) $(MODELS) $(OBJS) $(TESTS) $(SHLIB) $(BIN) $(SRC)/*.d* +%.o: %.c %.h + @echo "$(CC) $<" + @$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $< + test/%: test/%.o $(OBJS) @echo "$(CC) $@" @$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ @@ -62,7 +66,7 @@ check: $(TESTS) $(MODELS) ./test/test_resource ./test/test_scene -$(BIN): main.c $(SRCS) +$(BIN): main.o $(OBJS) @echo "$(CC) $@" @$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ diff --git a/etc/shaders/rogue/main.f.glsl b/etc/shaders/rogue/main.f.glsl @@ -0,0 +1,19 @@ +#include profile + +out vec4 frag_color; + +#include rogue/uniforms.glsl + +in shader_data { +#include shadervars.glsl +} vertex; + + +void main() { + vec4 v4_pos = vec4(vertex.position, 1.0); + + vec3 color = v4_pos; + + frag_color = vec4(color, 1.0); +} + diff --git a/etc/shaders/rogue/uniforms.glsl b/etc/shaders/rogue/uniforms.glsl @@ -0,0 +1,6 @@ + +precision highp int; + +uniform mat4 mvp; +uniform mat4 model_view; + diff --git a/main.c b/main.c @@ -20,7 +20,7 @@ #include "update.h" #include "util.h" #include "window.h" -//#include "rogue/rogue.h" +#include "rogue/rogue.h" #include <assert.h> @@ -33,28 +33,27 @@ int main(void) /* int seed = time(NULL); */ /* int seed = 1588992474; */ int seed = 1589065328; + int width = 640; + int height = 480; + /* int seed = 42; */ printf("seed %d\n", seed); srand(seed); struct engine engine; - struct test_game game; - //struct rogue_game game; + //struct test_game game; + struct rogue_game rogue_game; engine.seed = seed; - /* SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); */ - int width = 640; - int height = 480; - - game_init(&engine, width, height); - //init_rogue_game(&game); + init_engine(&engine, width, height); + init_rogue_game(&rogue_game); - init_test_game(&engine, &game); + //init_test_game(&engine, &game); //reset_scene(&engine); - default_scene(&game); + //default_scene(&game); //pbr_scene(&game); - chess_scene(&engine, &game); + chess_scene(&engine, NULL, &game); check_gl(); double last = hires_time_in_seconds(); @@ -76,8 +75,8 @@ int main(void) last = new_time; // render our game frame here - test_game_frame(&engine, &game); - //rogue_frame(&engine, rogue_game); + //test_game_frame(&engine, &game); + rogue_frame(&engine, &rogue_game); /* Swap front and back buffers */ SDL_GL_SwapWindow(engine.window); diff --git a/rogue/rogue.c b/rogue/rogue.c @@ -1,101 +1,58 @@ #include "rogue.h" - -void init_rogue_game(struct rogue_game *game) -{ - make_grid_geom(&game->grid.geom); -} - -void rogue_frame(struct game *engine, struct rogue_game *game) +#include "render.h" +#include "gpu.h" +#include "grid.h" +#include "mat4.h" +#include "lens.h" + +enum rogue_shader_structs { + COMMON_VARS +}; + +#define CV_LENS(field, typ) { #field, offsetof(struct common_vars, field), DATA_ID_##typ } +static struct lens common_vars[] = { + //CV_LENS(depth_mvp, MAT4), + CV_LENS(mvp, MAT4), + CV_LENS(model_view, MAT4), + //CV_LENS(normal_matrix, MAT4), + //CV_LENS(camera_position, VEC3P), + //CV_LENS(light_dir, VEC3), +}; +#undef CV_LENS + +static void init_ortho(struct rogue_game *game) { - render(engine, game) + float left = 0.0; + float right = 1.0; + float bottom = 0.0; + float top = 1.0; + float near = -1.0; + float far = 2.0; + + mat4_ortho(left, right, bottom, top, near, far, game->ortho); } -static void make_grid(float *verts, float vert_capacity, - u32 *indices, float index_capacity, - float *normals, float normal_capacity, - float *colors, float color_capacity, - int width, int height) +void init_rogue_game(struct rogue_game *game) { - int c = 0; - for (int x = 0; x < width; x++) { - for (int y = 0; y < height; y++, c++) { - int grid_ind = y * height + x; - int vind = grid_ind * ARRAY_SIZE(quad_vertices); - int iind = grid_ind * ARRAY_SIZE(quad_indices); - int i = 0; - - for (i = 0; i < 4; i++) { - int nv = i*3; - assert(vind+2+nv < vert_capacity); - assert(vind+2+nv < color_capacity); - assert(vind+2+nv < normal_capacity); - verts[vind+0+nv] = (quad_vertices[0+nv] + x) * CELL_SIZE; - verts[vind+1+nv] = (quad_vertices[1+nv] + y) * CELL_SIZE; - verts[vind+2+nv] = quad_vertices[2+nv]; - - // recenter - verts[vind+0+nv] -= (width * CELL_SIZE)/2.0 - 0.5 * CELL_SIZE; - verts[vind+1+nv] -= (height * CELL_SIZE)/2.0 - 0.5 * CELL_SIZE; - verts[vind+2+nv] -= 0.5; - - normals[vind+0+nv] = quad_normals[0+nv]; - normals[vind+1+nv] = quad_normals[1+nv]; - normals[vind+2+nv] = quad_normals[2+nv]; - - float checkers = (x ^ y) & 1; - colors[vind+0+nv] = checkers; - colors[vind+1+nv] = checkers; - colors[vind+2+nv] = checkers; - } - - for (i = 0; i < 2; i++) { - int nv = i*3; - assert(iind+2+nv < index_capacity); - indices[iind+0+nv] = quad_indices[0+nv] + 4*c; - indices[iind+1+nv] = quad_indices[1+nv] + 4*c; - indices[iind+2+nv] = quad_indices[2+nv] + 4*c; - } - } - } -} + int cell_size = 1.0; + init_ortho(game); -static void make_grid_geom(geometry_id *id) -{ - 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 - - make_grid(verts, ARRAY_SIZE(verts), - indices, ARRAY_SIZE(indices), - normals, ARRAY_SIZE(normals), - colors, ARRAY_SIZE(colors), - 8, 8); - - mkgeom.indices = indices; - mkgeom.vertices = verts; - mkgeom.normals = normals; - mkgeom.colors = colors; - mkgeom.num_indices = ARRAY_SIZE(indices); - mkgeom.num_verts = ARRAY_SIZE(verts)/3; - mkgeom.num_uv_components = 0; - - init_id(id); - struct geometry *geom = new_geometry(id); - make_buffer_geometry(&mkgeom, geom); - check_gl(); + init_id(&game->grid.geom_id); + make_grid_geom(&game->grid.geom_id, cell_size); } +static void render(struct engine *engine, struct rogue_game *game, struct render_config *config) { + float gtmp[3]; + u32 num_entities; + + struct gpu_program *program = NULL; + struct common_vars *cvars = &game->common_vars; -static void render(struct game *engine, struct rogue_game *game) { glEnable(GL_DEPTH_TEST); - glClearColor( gtmp[0], gtmp[1], gtmp[2], 1.0 ); //clear background screen to black + glClearColor( 0.5, 0.5, 0.5, 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 ); @@ -105,47 +62,38 @@ static void render(struct game *engine, struct rogue_game *game) { static float view[MAT4_ELEMS] = { 0 }; static float view_proj[MAT4_ELEMS] = { 0 }; static float normal_matrix[MAT4_ELEMS] = { 0 }; - static float model_view[MAT4_ELEMS] = { 0 }; - static float depth_mvp[MAT4_ELEMS] = { 0 }; mat4_id(id); - mat4_id(model_view); - - mat4 *mvp = res->test_mvp; + mat4_id(cvars->model_view); mat4 *projection = config->projection; - mat4 *light = res->light_dir; + struct entity *entities = + get_all_entities(&num_entities, NULL); + + /* struct node *camera_node = get_node(&config->camera); assert(camera_node); const mat4 *camera = camera_node->mat; - u32 num_entities; - - struct entity *entities = - get_all_entities(&num_entities, NULL); - - struct gpu_program *program = NULL; mat4_inverse((float*)camera, view); mat4_multiply(projection, view, view_proj); + cvars->camera_position = (float*)&camera[M_X]; + + */ + + // no camera for now + mat4_copy(projection, view_proj); + if (config->is_depth_pass) { - glDisable(GL_CULL_FACE); - mat4_multiply(bias_matrix, view_proj, config->depth_vp); + glDisable(GL_CULL_FACE); + //mat4_multiply(bias_matrix, view_proj, config->depth_vp); } else { - glCullFace(GL_BACK); + glCullFace(GL_BACK); } - mat4_inverse((float *)camera, view); - mat4_multiply(projection, view, view_proj); - - struct model *skybox_model = get_model(&res->skybox.model_id); - assert(skybox_model); - - glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_model->texture); - check_gl(); - for (u32 i = 0; i < num_entities; ++i) { struct entity *entity = &entities[i]; struct model *model = get_model(&entity->model_id); @@ -154,44 +102,42 @@ static void render(struct game *engine, struct rogue_game *game) { assert(node); if (entity->flags & ENT_INVISIBLE) - continue; + continue; + + if (config->is_depth_pass && !(entity->flags & ENT_CASTS_SHADOWS)) + continue; + + program = &engine->gpu.programs[model->shader]; - program = &game->gpu.programs[model->shader]; glUseProgram(program->handle); check_gl(); - uniform_3f(program, UNIFORM_CAMERA_POSITION, &camera[M_X]); - - if (model->shader == CHESS_PIECE_PROGRAM) { - uniform_1i(program, UNIFORM_IS_WHITE, - (entity->flags & ENT_IS_WHITE) == ENT_IS_WHITE); - } + mat4_multiply(view_proj, node->mat, cvars->mvp); + mat4_copy(node->mat, cvars->model_view); + //mat4_multiply(config->depth_vp, cvars->model_view, cvars->depth_mvp); - uniform_1i(program, UNIFORM_FOG_ON, res->fog_on); - uniform_3f(program, UNIFORM_LIGHT_DIR, light); - uniform_1f(program, UNIFORM_LIGHT_INTENSITY, res->light_intensity); - uniform_1f(program, UNIFORM_SKY_INTENSITY, sky_intensity); - uniform_3f(program, UNIFORM_SUN_COLOR, res->sun_color); + void *shader_data[] = { + [COMMON_VARS] = cvars + }; - mat4_multiply(view_proj, node->mat, mvp); - mat4_copy(node->mat, model_view); - mat4_multiply(config->depth_vp, model_view, depth_mvp); - uniform_m4f(program, UNIFORM_DEPTH_MVP, depth_mvp); - uniform_m4f(program, UNIFORM_MVP, mvp); - uniform_m4f(program, UNIFORM_MODEL, node->mat); + bind_uniforms(program, shader_data, ARRAY_SIZE(shader_data)); + check_gl(); + /* recalc_normals(program->uniforms[UNIFORM_NORMAL_MATRIX].location, - model_view, normal_matrix); + model_view, normal_matrix); + check_gl(); + */ struct geometry *geo = get_geometry(&model->geom_id); /* debug("geo node %s\n", node->label); */ assert(geo); - render_geometry(geo, program->vertex_attrs, program); + render_geometry(geo, program); check_gl(); } - if (game->wireframe) { + if (engine->gpu.wireframe) { glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); } else { @@ -199,15 +145,15 @@ static void render(struct game *engine, struct rogue_game *game) { } if (!config->is_depth_pass) { - mat4_inverse((float*)camera, view); + //mat4_inverse((float*)camera, view); mat4_remove_translations(view); mat4_multiply(projection, view, view_proj); - render_skybox(&res->skybox, view_proj); + //render_skybox(&game->skybox, view_proj); } - if (config->draw_ui) - render_ui(&game->ui, view); + //if (config->draw_ui) + // render_ui(&game->ui, view); //player // y tho @@ -224,3 +170,30 @@ static void render(struct game *engine, struct rogue_game *game) { /* render_geom(res, geom, GL_TRIANGLES); */ } +void rogue_frame(struct engine *engine, struct rogue_game *game) +{ + static float depth_vp[MAT4_ELEMS]; + mat4_id(depth_vp); + + /* + struct render_config fbo_render_config = { + .draw_ui = 0, + .is_depth_pass = 1, + .camera = game->sun_camera_id, + .projection = game->proj_ortho, + .depth_vp = depth_vp + }; + */ + + struct render_config default_config = { + .draw_ui = 1, + .is_depth_pass = 0, + .camera = game->camera_node_id, + .projection = game->ortho, + .depth_vp = depth_vp + }; + + //render(engine, game, &fbo_render_config); + render(engine, game, &default_config); +} + diff --git a/rogue/rogue.h b/rogue/rogue.h @@ -4,19 +4,36 @@ #include "orbit.h" #include "entity.h" #include "node.h" +#include "engine.h" + +struct common_vars { +// int diffuse_on; + float depth_mvp[MAT4_ELEMS]; + // mat4 *depth_vp; + float model_view[MAT4_ELEMS]; + float mvp[MAT4_ELEMS]; + float normal_matrix[MAT4_ELEMS]; + // float time; + // float ambient_str; + float *camera_position; +}; struct grid { - struct geometry_id goem; + struct geometry_id geom_id; }; struct rogue_game { struct grid grid; struct entity_id player; + struct common_vars common_vars; struct orbit orbit_camera; float time; + struct node_id camera_node_id; + + float ortho[MAT4_ELEMS]; }; void init_rogue_game(struct rogue_game *game); diff --git a/src/chess.c b/src/chess.c @@ -4,34 +4,7 @@ #include "gpu.h" #include "shader.h" #include "hash.h" -#include "test_game.h" - -// v1------v0 -// | | -// | | -// | | -// v2------v3 - -static GLuint quad_indices[] = {0, 1, 2, 0, 2, 3}; - - -static GLfloat quad_normals[] = { - 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front -}; - -static GLfloat quad_vertices[] = { - 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, // v0-v1-v2-v3 front -}; - -static GLfloat quad_uvs[] = - { - 1.0, 1.0, // v0 - 0.0, 1.0, // v1 - 0.0, 0.0, // v2 - 1.0, 0.0, // v3 - }; - -#define CELL_SIZE 10.0 +#include "grid.h" struct chess_piece { int is_white; @@ -42,86 +15,7 @@ static struct lens chess_piece_lenses[] = { CP_LENS(is_white, INT), }; -void make_grid(float *verts, float vert_capacity, - u32 *indices, float index_capacity, - float *normals, float normal_capacity, - float *colors, float color_capacity, - int width, int height) -{ - int c = 0; - for (int x = 0; x < width; x++) { - for (int y = 0; y < height; y++, c++) { - int grid_ind = y * height + x; - int vind = grid_ind * ARRAY_SIZE(quad_vertices); - int iind = grid_ind * ARRAY_SIZE(quad_indices); - int i = 0; - - for (i = 0; i < 4; i++) { - int nv = i*3; - assert(vind+2+nv < vert_capacity); - assert(vind+2+nv < color_capacity); - assert(vind+2+nv < normal_capacity); - verts[vind+0+nv] = (quad_vertices[0+nv] + x) * CELL_SIZE; - verts[vind+1+nv] = (quad_vertices[1+nv] + y) * CELL_SIZE; - verts[vind+2+nv] = quad_vertices[2+nv]; - - // recenter - verts[vind+0+nv] -= (width * CELL_SIZE)/2.0 - 0.5 * CELL_SIZE; - verts[vind+1+nv] -= (height * CELL_SIZE)/2.0 - 0.5 * CELL_SIZE; - verts[vind+2+nv] -= 0.5; - - normals[vind+0+nv] = quad_normals[0+nv]; - normals[vind+1+nv] = quad_normals[1+nv]; - normals[vind+2+nv] = quad_normals[2+nv]; - - float checkers = (x ^ y) & 1; - colors[vind+0+nv] = checkers; - colors[vind+1+nv] = checkers; - colors[vind+2+nv] = checkers; - } - - for (i = 0; i < 2; i++) { - int nv = i*3; - assert(iind+2+nv < index_capacity); - indices[iind+0+nv] = quad_indices[0+nv] + 4*c; - indices[iind+1+nv] = quad_indices[1+nv] + 4*c; - indices[iind+2+nv] = quad_indices[2+nv] + 4*c; - } - } - } -} - -static void make_chessboard_geom(struct geometry_id *id) -{ - 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 - - make_grid(verts, ARRAY_SIZE(verts), - indices, ARRAY_SIZE(indices), - normals, ARRAY_SIZE(normals), - colors, ARRAY_SIZE(colors), - 8, 8); - - mkgeom.indices = indices; - mkgeom.vertices = verts; - mkgeom.normals = normals; - mkgeom.colors = colors; - mkgeom.num_indices = ARRAY_SIZE(indices); - mkgeom.num_verts = ARRAY_SIZE(verts)/3; - mkgeom.num_uv_components = 0; - - init_id(id); - struct geometry *geom = new_geometry(id); - make_buffer_geometry(&mkgeom, geom); - check_gl(); -} - -static void position_piece(struct entity *piece, int x, int y) +static void position_piece(struct entity *piece, int cell_size, int x, int y) { assert(x < 8); assert(y < 8); @@ -132,15 +26,16 @@ static void position_piece(struct entity *piece, int x, int y) assert(node); // reset to a1 - node->pos[0] = -3.5 * CELL_SIZE; - node->pos[1] = -3.5 * CELL_SIZE; + node->pos[0] = -3.5 * cell_size; + node->pos[1] = -3.5 * cell_size; node->pos[2] = 0.0; - node->pos[0] += x * CELL_SIZE; - node->pos[1] += y * CELL_SIZE; + node->pos[0] += x * cell_size; + node->pos[1] += y * cell_size; } -static void setup_pieces(struct node_id *chessboard) { +static void setup_pieces(struct node_id *chessboard, float cell_size) +{ struct model *pirate_officer, *pawn_model, *tower_model, *rook_model; struct model_id pawn_model_id, rook_model_id; u32 ent_type; @@ -152,15 +47,15 @@ static void setup_pieces(struct node_id *chessboard) { get_model_by_name("tower", &tower_model); // make pawn model - init_id(&pawn_model_id.id); - init_id(&rook_model_id.id); + init_id(&pawn_model_id); + init_id(&rook_model_id); - pawn_model = new_model(&pawn_model_id); + pawn_model = new_model(&pawn_model_id, "pawn"); pawn_model->geom_id = pirate_officer->geom_id; pawn_model->shader = CHESS_PIECE_PROGRAM; pawn_model->name = "pawn"; - rook_model = new_model(&rook_model_id); + rook_model = new_model(&rook_model_id, "rook"); rook_model->geom_id = tower_model->geom_id; rook_model->shader = CHESS_PIECE_PROGRAM; rook_model->name = "rook"; @@ -188,7 +83,7 @@ static void setup_pieces(struct node_id *chessboard) { node_attach(&ent->node_id, chessboard); - position_piece(ent, i % 8, i < 8 ? 1 : 6); + position_piece(ent, cell_size, i % 8, i < 8 ? 1 : 6); } for (int i = 0; i < 4; i++) { @@ -209,7 +104,7 @@ static void setup_pieces(struct node_id *chessboard) { rook->is_white = false; node_rotate(rook_node, V3(0.0,0.0,135.0)); } - position_piece(ent, i % 2 ? 0 : 7, i < 2 ? 0 : 7); + position_piece(ent, cell_size, i % 2 ? 0 : 7, i < 2 ? 0 : 7); node_scale(rook_node, 0.25); } @@ -232,11 +127,12 @@ static struct structure_binding *get_chess_piece_bindings(int *count) return bindings; } -static struct gpu_program *make_chess_program(struct gpu *gpu) +static struct gpu_program *make_chess_program(struct shader *fragment, + struct gpu_program *program) { - struct gpu_program *program, *default_program; - struct shader *fragment, chess_piece_vertex; + struct shader chess_piece_vertex; struct structure_binding *bindings; + int num_bindings; int ok; @@ -244,16 +140,8 @@ static struct gpu_program *make_chess_program(struct gpu *gpu) rtassert(ok, "chess-piece vertex shader"); check_gl(); - default_program = get_gpu_program_by_name(gpu, "vertex-color"); - rtassert(default_program, "failed to get default program"); - check_gl(); - - fragment = get_program_shader(default_program, GL_FRAGMENT_SHADER); - rtassert(fragment, "failed to get default fragment shader"); - bindings = get_chess_piece_bindings(&num_bindings); - program = &gpu->programs[CHESS_PIECE_PROGRAM]; ok = make_program("chess-piece", &chess_piece_vertex, fragment, program, bindings, num_bindings); rtassert(ok, "chess-piece program"); @@ -262,60 +150,63 @@ static struct gpu_program *make_chess_program(struct gpu *gpu) return program; } -void chess_scene(struct engine *engine, struct test_game *game) -{ - struct gpu_program *piece_program; - struct entity *ent = new_entity(NULL); - struct node *node = get_node(&ent->node_id); assert(node); - struct entity *player = get_entity(&game->player_id); - struct shader chess_piece_vertex; +/* needs some fragment shader, pro + + default_program = get_gpu_program_by_name(gpu, "vertex-color"); + rtassert(default_program, "failed to get default program"); + check_gl(); - ent->model_id = get_model_by_name("icosphere", NULL); - node_set_label(node, "sphere"); + fragment = get_program_shader(default_program, GL_FRAGMENT_SHADER); + rtassert(fragment, "failed to get default fragment shader"); + */ - // - // setup chessboard - // - struct geometry_id chessboard_geom; - make_chessboard_geom(&chessboard_geom); +struct entity_id make_chessboard(struct gpu *gpu, struct shader *fragment) +{ + static const float cell_size = 10.0; + struct entity_id chessboard_ent_id; + struct geometry_id chessboard_geom_id; struct model_id chessboard_model_id; - init_id(&chessboard_model_id.id); + struct model *chessboard_model; + struct gpu_program *program; + struct entity *chessboard; + struct node *node; - struct model *chessboard_model = new_model(&chessboard_model_id); - chessboard_model->name = "chessboard"; - chessboard_model->shader = 0; - chessboard_model->geom_id = chessboard_geom; + init_id(&chessboard_geom_id); + init_id(&chessboard_model_id); + init_id(&chessboard_ent_id); - player->model_id = chessboard_model_id; - player->flags &= ~ENT_CASTS_SHADOWS; + chessboard = new_entity(&chessboard_ent_id); + node = get_node(&chessboard->node_id); + assert(node); - // - // setup camera - // - struct spherical *coords = &game->orbit_camera.coords; - coords->radius = 72.0; - coords->inclination = 0.5; - coords->azimuth = -7.86; + // setup chessboard + make_grid_geom(&chessboard_geom_id, cell_size); - struct node *pnode = get_node(&player->node_id); - node_translate(pnode, V3(240.0, 252.0, 373.0)); - pnode->orientation[2] = -0.005; - pnode->orientation[3] = -1.0; + chessboard_model = new_model(&chessboard_model_id, "chessboard"); + chessboard_model->geom_id = chessboard_geom_id; - struct node_id *cam_id = &game->orbit_camera.node_id; - struct node *cam_node = get_node(cam_id); assert(cam_node); + chessboard->model_id = chessboard_model_id; + chessboard->flags &= ~ENT_CASTS_SHADOWS; - make_chess_program(&engine->gpu); - setup_pieces(&player->node_id); + program = new_gpu_program(gpu, "chess-piece"); + make_chess_program(fragment, program); - node_recalc(pnode); + setup_pieces(&chessboard->node_id, cell_size); - //player 1284.539062 1111.104126 14.273574 + return chessboard_ent_id; +} - // show terrain - //terrain_ent->flags |= ENT_INVISIBLE; +struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam, struct shader *fragment) +{ + struct entity_id chessboard = make_chessboard(gpu, fragment); - // show player - /* player->flags |= ENT_INVISIBLE; */ + // setup camera + struct spherical *coords = &orbcam->coords; + coords->radius = 72.0; + coords->inclination = 0.5; + coords->azimuth = -7.86; + + return chessboard; } + diff --git a/src/chess.h b/src/chess.h @@ -5,6 +5,6 @@ #include "test_game.h" #include "engine.h" -void chess_scene(struct engine *engine, struct test_game *game); +struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam, struct shader *fragment); #endif /* POLYADVENT_CHESS_H */ diff --git a/src/engine.c b/src/engine.c @@ -90,29 +90,30 @@ void init_controller(struct input *input) { } } -void game_init(struct engine *game, int width, int height) { +void init_engine(struct engine *game, int width, int height) +{ game->width = width; game->height = height; - debug("init sdl...\n"); - init_sdl(&game->window, width, height); - debug("init gl...\n"); - init_gl(); - debug("init entities...\n"); - init_entity_system(); - init_geometry_manager(); - init_model_manager(); - init_node_manager(); - init_user_settings(&game->user_settings); - - check_gl(); - - game->gpu.wireframe = 0; - - init_input(&game->input); - init_controller(&game->input); - //init_controller(&game->input); - debug("init misc...\n"); - init_misc(game); - - printf("gpu programs %d\n", game->gpu.num_programs); + debug("init sdl...\n"); + init_sdl(&game->window, width, height); + debug("init gl...\n"); + init_gl(); + debug("init entities...\n"); + init_entity_system(); + init_geometry_manager(); + init_model_manager(); + init_node_manager(); + init_user_settings(&game->user_settings); + + check_gl(); + + game->gpu.wireframe = 0; + + init_input(&game->input); + init_controller(&game->input); + //init_controller(&game->input); + debug("init misc...\n"); + init_misc(game); + + printf("gpu programs %d\n", game->gpu.num_programs); } diff --git a/src/engine.h b/src/engine.h @@ -41,8 +41,8 @@ struct engine { struct gpu gpu; }; -void game_init(struct engine *game, int width, int height); -void quit_game(struct engine *game); +void init_engine(struct engine *game, int width, int height); +void quit_engine(struct engine *game); void should_update(struct engine *game); bool was_key_pressed_this_frame(struct engine *game, int scancode); bool was_button_pressed_this_frame(struct engine *game, SDL_GameControllerButton button); diff --git a/src/entity.c b/src/entity.c @@ -10,17 +10,22 @@ static struct resource_manager esys; -struct resource_manager *_internal_get_entity_system() +static inline struct resource_manager *_internal_get_entity_system() { return &esys; } +static inline struct entity *new_uninitialized_entity(struct entity_id *id) +{ + return new_resource(&esys, &id->id); +} + struct entity *get_all_entities(u32 *count, struct entity_id **ids) { return get_all_resources(&esys, count, (struct resource_id**)ids); } struct entity *init_entity(struct entity *ent, struct node_id *id) { - init_id(&ent->model_id.id); + init_id(&ent->model_id); if (id == NULL) { init_id(&ent->node_id); new_node(&ent->node_id); @@ -37,24 +42,19 @@ struct entity *get_entity(struct entity_id *ent_id) { return get_resource(&esys, (struct resource_id *)ent_id); } -static inline struct entity *new_uninitialized_entity(entity_id *id) { - return new_resource(&esys, id); -} - - struct entity *new_entity_with_node(struct entity_id *id, struct node_id *node) { - entity_id new_id; + struct entity_id new_id; - if (id == NULL) { - id = &new_id; - init_id(&new_id); - } + if (id == NULL) { + id = &new_id; + init_id(&new_id); + } - return init_entity(new_uninitialized_entity(id), node); + return init_entity(new_uninitialized_entity(id), node); } -struct entity *add_entity(struct entity *e, entity_id *id) { +struct entity *add_entity(struct entity *e, struct entity_id *id) { struct entity *new = new_uninitialized_entity(id); *new = *e; return new; @@ -62,12 +62,12 @@ struct entity *add_entity(struct entity *e, entity_id *id) { void destroy_entities() { - entity_id *ids; - u32 count; - get_all_resources(&esys, &count, &ids); - for (u32 i = 0; i < count; i++) { - destroy_entity(&ids[i]); - } + struct resource_id *ids; + u32 count; + get_all_resources(&esys, &count, &ids); + for (u32 i = 0; i < count; i++) { + destroy_entity((struct entity_id*)&ids[i]); + } }; void destroy_entity(struct entity_id *id) @@ -82,7 +82,7 @@ void destroy_entity(struct entity_id *id) return; node_detach_from_parent(node); - destroy_resource(&esys, id); + destroy_resource(&esys, &id->id); } const char *entity_label(struct entity *ent) @@ -100,17 +100,17 @@ void init_entity_system() { MAX_ENTITIES, "entity"); } -struct entity *new_debug_entity(entity_id *ent_id, float *pos) +struct entity *new_debug_entity(struct entity_id *ent_id, float *pos) { - init_id(ent_id); - struct entity *ent = new_entity(ent_id); - ent->model_id = get_model_by_name("barrel", NULL); - struct node *enode = get_node(&ent->node_id); - node_set_label(enode, "debug"); - vec3_copy(pos, enode->pos); - node_scale(enode, 5.0); - node_mark_for_recalc(enode); - node_recalc(enode); - return ent; + init_id(ent_id); + struct entity *ent = new_entity(ent_id); + ent->model_id = get_model_by_name("barrel", NULL); + struct node *enode = get_node(&ent->node_id); + node_set_label(enode, "debug"); + vec3_copy(pos, enode->pos); + node_scale(enode, 5.0); + node_mark_for_recalc(enode); + node_recalc(enode); + return ent; } diff --git a/src/entity.h b/src/entity.h @@ -28,32 +28,30 @@ struct entity { u8 data[ENTITY_DATA_SIZE]; }; -typedef struct resource_id entity_id; - struct entity_id { struct resource_id id; }; struct entity *init_entity(struct entity *, struct node_id *id); void destroy_entities(); -struct resource_manager *_internal_get_entity_system(); void destroy_entity(struct entity_id *); void init_entity_system(); struct entity *get_entity(struct entity_id *); const char *entity_label(struct entity *); struct entity *get_all_entities(u32 *count, struct entity_id **ids); struct entity *new_entity_with_node(struct entity_id *, struct node_id *); -struct entity *new_debug_entity(entity_id *, float *pos); +struct entity *new_debug_entity(struct entity_id *, float *pos); void destroy_entity_system(); -static inline struct entity *new_entity_(entity_id *id) { - return new_entity_with_node(id, NULL); +static inline struct entity *new_entity_(struct entity_id *id) { + return new_entity_with_node(id, NULL); } -static inline struct entity *new_entity(entity_id *id) +static inline struct entity *new_entity(struct entity_id *id) { if (id) - assert((int)id->index == -1 && "error: " __FILE__ ":" STRIZE(__LINE__) " missing init_id or already initialized"); + assert((int)id->id.index == -1 && + "error: " __FILE__ ":" STRIZE(__LINE__) " missing init_id or already initialized"); return new_entity_(id); } diff --git a/src/geometry.c b/src/geometry.c @@ -159,12 +159,12 @@ void init_geometry_manager() { } struct geometry *get_geometry(struct geometry_id *geom_id) { - return get_resource(&geom_manager, geom_id); + return get_resource(&geom_manager, &geom_id->id); } struct geometry *new_geometry(struct geometry_id *geom_id) { - struct geometry *geom = new_resource(&geom_manager, geom_id); + struct geometry *geom = new_resource(&geom_manager, &geom_id->id); /* debug("new geometry %llu\n", geom_id->uuid); */ return geom; } @@ -184,6 +184,6 @@ void destroy_geometry(struct geometry_id *geom_id) glDeleteBuffers(1, &vbo->handle); } - destroy_resource(&geom_manager, geom_id); + destroy_resource(&geom_manager, &geom_id->id); } diff --git a/src/gpu.c b/src/gpu.c @@ -37,3 +37,16 @@ struct gpu_program *get_gpu_program_by_name(struct gpu *gpu, const char *name) return NULL; } + +struct gpu_program *new_gpu_program(struct gpu *gpu, const char *name) +{ + assert(gpu->num_programs + 1 < MAX_GPU_PROGRAMS); + + int id = gpu->num_programs++; + struct gpu_program *program = &gpu->programs[id]; + + program->id = id; + program->name = name; + + return program; +} diff --git a/src/gpu.h b/src/gpu.h @@ -12,3 +12,4 @@ struct gpu { int try_reload_shaders(struct gpu *gpu); struct gpu_program *get_gpu_program_by_name(struct gpu *gpu, const char *name); +struct gpu_program *new_gpu_program(struct gpu *gpu, const char *name); diff --git a/src/grid.c b/src/grid.c @@ -0,0 +1,109 @@ + + +#include "grid.h" +#include "util.h" + +// v1------v0 +// | | +// | | +// | | +// v2------v3 + +static GLuint quad_indices[] = {0, 1, 2, 0, 2, 3}; + + +static GLfloat quad_normals[] = { + 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front +}; + +static GLfloat quad_vertices[] = { + 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, // v0-v1-v2-v3 front +}; + +static GLfloat quad_uvs[] = + { + 1.0, 1.0, // v0 + 0.0, 1.0, // v1 + 0.0, 0.0, // v2 + 1.0, 0.0, // v3 + }; + +static void make_grid(float *verts, float vert_capacity, + u32 *indices, float index_capacity, + float *normals, float normal_capacity, + float *colors, float color_capacity, + int width, int height, float cell_size) +{ + int c = 0; + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++, c++) { + int grid_ind = y * height + x; + int vind = grid_ind * ARRAY_SIZE(quad_vertices); + int iind = grid_ind * ARRAY_SIZE(quad_indices); + int i = 0; + + for (i = 0; i < 4; i++) { + int nv = i*3; + assert(vind+2+nv < vert_capacity); + assert(vind+2+nv < color_capacity); + assert(vind+2+nv < normal_capacity); + verts[vind+0+nv] = (quad_vertices[0+nv] + x) * cell_size; + verts[vind+1+nv] = (quad_vertices[1+nv] + y) * cell_size; + verts[vind+2+nv] = quad_vertices[2+nv]; + + // recenter + verts[vind+0+nv] -= (width * cell_size)/2.0 - 0.5 * cell_size; + verts[vind+1+nv] -= (height * cell_size)/2.0 - 0.5 * cell_size; + verts[vind+2+nv] -= 0.5; + + normals[vind+0+nv] = quad_normals[0+nv]; + normals[vind+1+nv] = quad_normals[1+nv]; + normals[vind+2+nv] = quad_normals[2+nv]; + + float checkers = (x ^ y) & 1; + colors[vind+0+nv] = checkers; + colors[vind+1+nv] = checkers; + colors[vind+2+nv] = checkers; + } + + for (i = 0; i < 2; i++) { + int nv = i*3; + assert(iind+2+nv < index_capacity); + indices[iind+0+nv] = quad_indices[0+nv] + 4*c; + indices[iind+1+nv] = quad_indices[1+nv] + 4*c; + indices[iind+2+nv] = quad_indices[2+nv] + 4*c; + } + } + } +} + +void make_grid_geom(struct geometry_id *id, 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 + + make_grid(verts, ARRAY_SIZE(verts), + indices, ARRAY_SIZE(indices), + normals, ARRAY_SIZE(normals), + colors, ARRAY_SIZE(colors), + 8, 8, cell_size); + + mkgeom.indices = indices; + mkgeom.vertices = verts; + mkgeom.normals = normals; + mkgeom.colors = colors; + mkgeom.num_indices = ARRAY_SIZE(indices); + mkgeom.num_verts = ARRAY_SIZE(verts)/3; + mkgeom.num_uv_components = 0; + + init_id(id); + struct geometry *geom = new_geometry(id); + make_buffer_geometry(&mkgeom, geom); + check_gl(); +} + diff --git a/src/grid.h b/src/grid.h @@ -0,0 +1,6 @@ + +#pragma once + +#include "geometry.h" + +void make_grid_geom(struct geometry_id *id, float cell_size); diff --git a/src/mat4.c b/src/mat4.c @@ -7,12 +7,6 @@ #define PI 3.14159265f -mat4 *mat4_copy(const mat4 *src, mat4 *dst) { - memcpy(dst, src, sizeof(float) * 16); - return dst; -} - - mat4 *mat4_id(mat4 *dst) { dst[0] = 1.; dst[1] = 0.; dst[2] = 0.; dst[3] = 0.; dst[4] = 0.; dst[5] = 1.; dst[6] = 0.; dst[7] = 0.; diff --git a/src/mat4.h b/src/mat4.h @@ -2,7 +2,7 @@ #ifndef MAT4_H #define MAT4_H - +#include <string.h> typedef float mat4; @@ -25,7 +25,6 @@ mat4 *mat4_ortho(float left, float right, float bottom, float top, float near, mat4 *mat4_translate (mat4 *mat, float *vec3, mat4 *dest); mat4 *mat4_transpose(mat4 *src, mat4 *dest); mat4 *mat4_inverse(mat4 *src, mat4 *dest); -mat4 *mat4_copy(const mat4 *src, mat4 *dst); mat4 *mat4_id(mat4 *dest); mat4 *mat4_multiply(const mat4 *a, const mat4 *b, mat4 *dest); void mat4_print(const mat4 *a); @@ -36,5 +35,11 @@ mat4 *mat4_rotate(const mat4 *mat, const float angle, mat4 *mat4_remove_translations(mat4 *mat); +static inline mat4 *mat4_copy(const mat4 *src, mat4 *dst) +{ + memcpy(dst, src, sizeof(float) * 16); + return dst; +} + #endif /* MAT4_H */ diff --git a/src/model.c b/src/model.c @@ -30,13 +30,14 @@ static inline struct model *new_uninitialized_model(struct model_id *id) { return new_resource(&dyn_modelman, &id->id); } -struct model *new_model(struct model_id *model_id) +struct model *new_model(struct model_id *model_id, const char *name) { struct model *model = new_uninitialized_model(model_id); /* debug("new model %llu\n", model_id->dyn_model_id.uuid); */ init_model(model); init_id(&model->geom_id); new_geometry(&model->geom_id); + model->name = name; return model; } @@ -56,7 +57,7 @@ static struct model *load_model(const char *name, struct model_id *mid) { static char path[128] = {0}; - struct model *model = new_model(mid); + struct model *model = new_model(mid, name); struct geometry *geom = get_geometry(&model->geom_id); struct mdl_geometry lgeom; @@ -103,7 +104,7 @@ struct model_id get_model_by_name(const char *name, struct model **found) struct model *models; u32 count, i, name_hash; - init_id(&new_id.id); + init_id(&new_id); if (found) *found = NULL; diff --git a/src/model.h b/src/model.h @@ -40,7 +40,7 @@ struct model *init_model(struct model *model); struct model_id get_model_by_name(const char *name, struct model **); struct model *get_model(struct model_id *model_id); struct model *get_model_geom(struct model_id *, struct geometry **); -struct model *new_model(struct model_id *); +struct model *new_model(struct model_id *, const char *name); struct model *new_static_model(struct model_id *); void destroy_model(struct model_id *); diff --git a/src/movement.h b/src/movement.h @@ -2,6 +2,6 @@ #include "engine.h" -void movement(struct game *game, struct node *node, float speed_mult); +void movement(struct engine *game, struct node *node, float speed_mult); void entity_jump(struct entity *ent, float amount); void gravity(struct entity *ent, float dt); diff --git a/src/node.c b/src/node.c @@ -10,28 +10,10 @@ struct resource_manager node_manager = {0}; -static inline struct node *new_uninitialized_node(struct node_id *id) -{ - return new_resource(&node_manager, &id->id); -} - -struct node *new_node(struct node_id *id) -{ - struct node *n = node_init(new_uninitialized_node(&id->id)); - assert((int64_t)id->id.uuid != -1); - /* debug("new node %llu\n", id->uuid); */ - return n; -} - -struct node *get_node(struct node_id *id) -{ - return get_resource(&node_manager, &id->id); -} - void destroy_node(struct node_id *id) { #ifdef DEBUG - struct node *node = get_node(&id->id); + struct node *node = get_node(id); debug("destroying node %" PRIu64 " %s\n", id->id.uuid, node->label); #endif @@ -102,17 +84,6 @@ void node_rotate(struct node *node, vec3 *axis_angles) { node_mark_for_recalc(node); } -int node_needs_recalc(struct node *node) -{ - struct node *parent = get_node(&node->parent_id); - return (parent && parent->needs_recalc) || node->needs_recalc; -} - -vec3 *node_world(struct node *node) { - - return &node->mat[M_X]; -} - void node_mark_for_recalc(struct node *node) { static int j = 0; diff --git a/src/node.h b/src/node.h @@ -3,6 +3,8 @@ #define POLYADVENT_NODE_H #include "resource.h" +#include "mat4.h" +#include <assert.h> #define MAX_NODE_CHILDREN 32 @@ -43,20 +45,47 @@ void node_forward_dir(struct node *node, float *orientation, float *p); void node_rotate(struct node *node, float *p); void node_scale(struct node *node, float val); int node_count(struct node *node); -float *node_world(struct node *node); int node_set_label(struct node *node, const char *label); -struct node *get_node(struct node_id *); -struct node *new_node(struct node_id *); void destroy_node(struct node_id *); void init_node_manager(); - extern struct resource_manager node_manager; static inline struct node *static_nodes() { - return (struct node*)node_manager.resources; + return (struct node*)node_manager.resources; +} + +static inline struct node *new_uninitialized_node(struct node_id *id) +{ + return new_resource(&node_manager, &id->id); +} + +static inline struct node *new_node(struct node_id *id) +{ + struct node *n = node_init(new_uninitialized_node(id)); + assert((int64_t)id->id.uuid != -1); + /* debug("new node %llu\n", id->uuid); */ + return n; +} + +static inline struct node *get_node(struct node_id *id) +{ + return get_resource(&node_manager, &id->id); +} + + +static inline int node_needs_recalc(struct node *node) +{ + struct node *parent = get_node(&node->parent_id); + return (parent && parent->needs_recalc) || node->needs_recalc; } +static inline float *node_world(struct node *node) +{ + return &node->mat[M_X]; +} + + #endif /* POLYADVENT_NODE_H */ diff --git a/src/orbit_util.c b/src/orbit_util.c @@ -4,27 +4,25 @@ #include "debug.h" #include "entity.h" +// entity geom offset +//V3(0.0, 0.0, player_geom->max[2]) + void orbit_update_from_mouse(struct orbit *camera, struct input *input, - float mouse_sens, struct entity *player, - float dt) + float mouse_sens, struct node_id *target_id, + vec3 *offset, float dt) { float target[3]; - struct node *target_node = get_node(&player->node_id); - struct node *cam_node = get_node(&camera->node_id); - struct model *pmodel = get_model(&player->model_id); - assert(pmodel); - struct geometry *player_geom = get_geometry(&pmodel->geom_id); - assert(player_geom); + struct node *cam_node = get_node(&camera->node_id); + assert(cam_node); + struct node *target_node = get_node(target_id); assert(target_node); - assert(cam_node); node_recalc(target_node); vec3_copy(node_world(target_node), target); - //assert(player_geom->max[2] != 0); - vec3_add(target, V3(0.0, 0.0, player_geom->max[2]), target); - /* vec3_add(target, V3(0.0, 0.0, 10.0), target); */ + if (offset) + vec3_add(target, offset, target); float mx = 0.0, my = 0.0; if (input_is_dragging(input, SDL_BUTTON_LEFT) || diff --git a/src/resource.c b/src/resource.c @@ -31,18 +31,6 @@ void *get_all_resources(struct resource_manager *r, u32 *count, struct resource_ return r->resources; } -void init_id(struct resource_id *id) { - id->index = -1; - id->generation = -1; - id->uuid = -1; -} - -void null_id(struct resource_id *id) -{ - id->generation = 0; - id->uuid = -1; - id->index = -1; -} void init_resource_manager(struct resource_manager *r, u32 elem_size, u32 initial_elements, u32 max_elements, diff --git a/src/resource.h b/src/resource.h @@ -29,26 +29,40 @@ struct resource_manager { const char *name; }; +static inline void init_id_(struct resource_id *id) { + id->index = -1; + id->generation = -1; + id->uuid = -1; +} + +static inline void null_id_(struct resource_id *id) +{ + id->generation = 0; + id->uuid = -1; + id->index = -1; +} + +static inline int is_null_id_(struct resource_id *id) +{ + return id->generation == 0; +} + + #define ideq(a, b) ((a)->id.uuid == (b)->id.uuid) +#define init_id(nt) init_id_(&((nt)->id)) +#define null_id(nt) null_id_(&((nt)->id)) +#define is_null_id(nt) is_null_id_(&((nt)->id)) -void init_id(struct resource_id *id); void *get_resource(struct resource_manager *r, struct resource_id *id); void *get_all_resources(struct resource_manager *, u32 *count, struct resource_id **ids); void destroy_resource(struct resource_manager *, struct resource_id *id); void destroy_resource_manager(struct resource_manager *); void *new_resource(struct resource_manager *, struct resource_id *id); void print_id(struct resource_id *, int nl); -void null_id(struct resource_id *id); int is_resource_destroyed(struct resource_manager *r, struct resource_id *id); void init_resource_manager(struct resource_manager *r, u32 elem_size, u32 initial_elements, u32 max_elements, const char *name); -static inline int is_null_id(struct resource_id *id) -{ - return id->generation == 0; -} - - #endif /* RESOURCE_H */ diff --git a/src/shader.h b/src/shader.h @@ -46,6 +46,7 @@ struct gpu_program { int n_shaders; int num_uniforms; int active_attributes; + int id; // index in gpu programs array GLuint handle; const char *name; }; diff --git a/src/skybox.c b/src/skybox.c @@ -50,7 +50,7 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) { (void)ok; node_init(&skybox->node); - init_id(&skybox->model_id.id); + init_id(&skybox->model_id); skybox->program = program; @@ -65,7 +65,7 @@ void create_skybox(struct skybox *skybox, struct gpu_program *program) { mkgeom.num_verts = ARRAY_SIZE(skybox_vertices); mkgeom.num_indices = ARRAY_SIZE(skybox_indices); - struct model *model = new_model(&skybox->model_id); assert(model); + struct model *model = new_model(&skybox->model_id, "skybox"); assert(model); struct geometry *geom = get_geometry(&model->geom_id); assert(geom); model->name = "skybox"; diff --git a/src/terrain.c b/src/terrain.c @@ -58,7 +58,7 @@ void init_terrain(struct terrain *terrain, float size) { struct entity *ent = new_entity(&terrain->entity_id); struct node *node = get_node(&ent->node_id); - struct model *model = new_model(&ent->model_id); assert(model); + struct model *model = new_model(&ent->model_id, "terrain"); assert(model); /* struct model *model = init_model(&ent->model_id); assert(model); */ model->shader = 1; @@ -159,7 +159,7 @@ static void insert_grid_vertex(struct terrain_cell *cell, int ind) static int terrain_cell_debug(struct terrain *terrain, struct terrain_cell *cell, int ind, vec3 *pos) { int ok = 0; - entity_id *ent_id = &cell->debug_ent[ind]; + struct entity_id *ent_id = &cell->debug_ent[ind]; if (is_null_id(ent_id)) { assert(cell->verts_index[ind] < terrain->n_verts); diff --git a/src/terrain.h b/src/terrain.h @@ -26,7 +26,7 @@ struct perlin_settings { struct tri { #ifdef DEBUG - entity_id debug_id; + struct entity_id debug_id; #endif int vert_indices[3]; }; @@ -40,12 +40,12 @@ struct terrain_cell { u8 vert_count; int verts_index[MAX_CELL_VERTS]; #ifdef DEBUG - entity_id debug_ent[MAX_CELL_VERTS]; + struct entity_id debug_ent[MAX_CELL_VERTS]; #endif }; struct terrain { - entity_id entity_id; + struct entity_id entity_id; struct terrain_cell *grid; struct vert_tris *vtris; int n_cells; // all cells = grid_cells^2 diff --git a/src/test_game.c b/src/test_game.c @@ -78,7 +78,7 @@ static void resize_fbos(struct entity *player, struct fbo *shadow_buffer, const float far = 50.0; // default ortho screenspace projection - mat4_ortho(left, right, bottom, top, near, far, m4_ortho); + //mat4_ortho(left, right, bottom, top, near, far, m4_ortho); create_fbo(shadow_buffer, width, height ); //fbo_attach_renderbuffer(shadow_buffer, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL_ATTACHMENT); @@ -700,7 +700,7 @@ void init_test_game(struct engine *engine, struct test_game *game) assert(pnode); player->model_id = get_model_by_name("pirate_officer", NULL); - assert(!is_null_id(&player->model_id.id)); + assert(!is_null_id(&player->model_id)); node_set_label(pnode, "player"); @@ -764,10 +764,10 @@ void default_scene(struct test_game *game) { void entity_test_scene(struct terrain *terrain) { struct model_id rock_model; - init_id(&rock_model.id); + init_id(&rock_model); /* model_id rock_model = get_static_model(model_tower, NULL); */ - struct model *pmodel = new_model(&rock_model); assert(pmodel); + struct model *pmodel = new_model(&rock_model, "procrock"); assert(pmodel); struct geometry *geom = get_geometry(&pmodel->geom_id); assert(geom); proc_sphere(geom); diff --git a/src/update.h b/src/update.h @@ -4,7 +4,7 @@ #include "engine.h" -void update(struct game * game); +void update(struct engine *game); void gravity(struct entity *ent, float dt); void resize_fbos(struct entity *player, struct fbo *shadow_buffer, float *mat4_ortho, int width, int height);