diff --git a/TODO.md b/TODO.md index ed4cef2..0557d4b 100644 --- a/TODO.md +++ b/TODO.md @@ -36,6 +36,7 @@ - use a storage pool for assimp meshes allowing reuse across entities - remove v2i/v3f... etc from util.h and either use glm everywhere, or write a smaller linear math.h - add an application config file and structure to replace various defines + - be sure to put a max_property_length or similar for c-string properties ## LATER TODO: - add initial opengl constant for INT_MAX, and checks in render_group functions diff --git a/data/hashmap_scene.json b/data/hashmap_scene.json index 16cc330..931931e 100644 --- a/data/hashmap_scene.json +++ b/data/hashmap_scene.json @@ -1,7 +1,7 @@ { "camera" : { - "position" : {"x":400,"y":-275,"z":325}, - "target" : {"x":140,"y":0,"z":0}, + "position" : {"x":200,"y":-150,"z":150}, + "target" : {"x":0,"y":0,"z":0}, "world_up" : {"x":0,"y":0,"z":1} }, @@ -20,32 +20,35 @@ { "name" : "catepillar 1", "model_file" : "catepillar.dae", - "position" : {"x":-140,"y":50,"z":0}, "rotation" : {"x":0,"y":0,"z":0,"w":0}, - "scale" : {"x":10,"y":10,"z":10}, - "grid_pos": {"x":10,"y":10,"z":10} + "scale" : {"x":2,"y":2,"z":2}, + "grid_pos": {"q":0,"r":-5,"s":5}, + "use_grid": true }, { "name" : "catepillar 2", "model_file" : "catepillar.dae", - "position" : {"x" :140,"y":0,"z":0}, "rotation" : {"x":0,"y":0,"z":0,"w":0}, - "scale" : {"x":10,"y":10,"z":10}, - "grid_pos": {"q":10,"r":10,"s":10} + "scale" : {"x":2,"y":2,"z":2}, + "grid_pos": {"q":8,"r":-5,"s":-3}, + "use_grid": true }, { "name" : "box", "model_file" : "block2.dae", - "position" : {"x":140,"y":-100,"z":0}, "rotation" : {"x":0,"y":0,"z":0,"w":0}, - "scale" : {"x":10,"y":10,"z":10} + "scale" : {"x":5,"y":5,"z":5}, + "grid_pos": {"q":-2,"r":-2,"s":-4}, + "use_grid": true }, { "name" : "ground", "model_file" : "level.2.dae", "position" : {"x":0,"y":0,"z":-1}, "rotation" : {"x":0,"y":0,"z":0,"w":0}, - "scale" : {"x":300,"y":300,"z":300} + "scale" : {"x":300,"y":300,"z":300}, + "grid_pos": {"q":0,"r":0,"s":0}, + "use_grid": false } ], diff --git a/src/entity.cpp b/src/entity.cpp index 6eccbcc..43650a5 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -8,7 +8,7 @@ bool convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool bool -entInit(Entity& e, const char* data_dir, rg_shader_program& shader) +entInit(Entity& e, const char* data_dir, rg_shader_program& shader, v3f world_pos) { uint max_len = 256; // TODO: define max_len for property strings char full_path[max_len]; @@ -25,8 +25,9 @@ entInit(Entity& e, const char* data_dir, rg_shader_program& shader) rg->render_objects = UTIL_ALLOC(num_meshes, render_object*); // apply translation/rotation/scaling + glm::vec3 v3_world(world_pos.x, world_pos.y, world_pos.z); e.world_transform = glm::mat4(1.0); - entTranslate(e, e.translation); + entTranslate(e, v3_world); entScale(e, e.scale); for (uint i = 0; i < num_meshes; i++) diff --git a/src/entity.h b/src/entity.h index 7632f36..d9ef74d 100644 --- a/src/entity.h +++ b/src/entity.h @@ -7,6 +7,12 @@ #include "mesh.h" #include "render_group.h" +struct ent_grid_position +{ + int q; + int r; + int s; +}; struct Entity { @@ -14,13 +20,17 @@ struct Entity glm::vec3 scale; glm::vec3 translation; glm::vec4 rotation; + + bool use_grid; // NOTE: determines if entity is position with grid_pos or translation + ent_grid_position grid_pos; + meMeshGroup mesh_group; render_group* ren_group; char model_filename[256]; }; -bool entInit(Entity& e, const char* data_dir, rg_shader_program& shader); +bool entInit(Entity& e, const char* data_dir, rg_shader_program& shader, v3f world_pos); void entFree(Entity& e); diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 7e7eed1..eb13b8e 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -73,7 +73,14 @@ loadSceneFromJson(game_state* s, render_state* rs) if (slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES)) { for (uint i = 0; i < gs->entity_count; i++) { - if (!entInit(gs->entities[i], DATA_DIR, rs->default_shader)) { + Entity& e = gs->entities[i]; + v3f world_pos; + if (e.use_grid) + world_pos = hgGetWorldPostion(gs->grid, e.grid_pos.q, e.grid_pos.r, e.grid_pos.s); + else + world_pos = v3f(e.translation.x, e.translation.y, e.translation.z); + + if (!entInit(e, DATA_DIR, rs->default_shader, world_pos)) { LOG(ERROR) << "Error initilaizing entity\n"; return false; } diff --git a/src/hexgrid.cpp b/src/hexgrid.cpp index e16eae0..ff002ea 100644 --- a/src/hexgrid.cpp +++ b/src/hexgrid.cpp @@ -216,6 +216,13 @@ hgGetSingleHex(hexgrid& hg, real32 x, real32 y) return nullptr; } +v3f +hgGetWorldPostion(hexgrid& hg, int q, int r, int s) +{ + Point p = hex_to_pixel(hg.hexlib_layout, Hex(q,r,s)); + return v3f(p.x, p.y, hg.position.z); // NOTE: this doesn't account for hg.normal +} + void hgResetHexes(hexgrid& hg) { diff --git a/src/hexgrid.h b/src/hexgrid.h index 89e8b73..3c25300 100644 --- a/src/hexgrid.h +++ b/src/hexgrid.h @@ -97,6 +97,8 @@ void hgUpdateUVBuffer(hexgrid& hg, render_group* rg); hex_info* hgGetSingleHex(hexgrid& hg, real32 x, real32 y); +v3f hgGetWorldPostion(hexgrid& hg, int q, int r, int s); + void hgResetHexes(hexgrid& hg); void hgUpdateHexFill(hexgrid& hg, int32 x, int32 y); diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index 74d4620..c5964eb 100644 --- a/src/scene_loader.cpp +++ b/src/scene_loader.cpp @@ -90,7 +90,16 @@ slParseEntities(slSceneDoc* sd, Entity* entity_array, uint& entity_count, uint m for (uint i = 0; i < entities.Size(); i++) { Entity& e = entity_array[i]; e.scale = parseVec3(entities[i]["scale"]); - e.translation = parseVec3(entities[i]["position"]); + e.use_grid = entities[i]["use_grid"].GetBool(); + + if (e.use_grid) { + const rapidjson::Value& grid_pos = entities[i]["grid_pos"]; + e.grid_pos.q = grid_pos["q"].GetInt(); + e.grid_pos.r = grid_pos["r"].GetInt(); + e.grid_pos.s = grid_pos["s"].GetInt(); + } else { + e.translation = parseVec3(entities[i]["position"]); + } uint max_len = 256; // TODO: make define for string property max_len std::string model_file_str(entities[i]["model_file"].GetString()); diff --git a/src/scene_loader.h b/src/scene_loader.h index ff83a5a..5ff6ce9 100644 --- a/src/scene_loader.h +++ b/src/scene_loader.h @@ -25,5 +25,4 @@ bool slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_sc bool slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights); -// TODO: this is pretty hacky atm just for testing bool slSaveGridFile(hexgrid& hg);