Browse Source

move hexgrid render_group init out of scene_loader

master
cinnaboot 8 years ago
parent
commit
4064b41014
  1. 5
      TODO.md
  2. 33
      src/hexgame.cpp
  3. 21
      src/hexgrid.cpp
  4. 10
      src/hexgrid.h
  5. 25
      src/scene_loader.cpp
  6. 2
      src/scene_loader.h

5
TODO.md

@ -1,6 +1,6 @@
## TODO: ## TODO:
- init HexGrid render groups in hgInit and remove slCreateHexRenderGroups - allocate meMeshGroup->meshes as single chunk of mem instead of p2p
- position entities on grid hexes - position entities on grid hexes
- pathfinding - pathfinding
- assimp animation - assimp animation
@ -35,9 +35,8 @@
- think about combining mesh data and render_group data to save some memory - think about combining mesh data and render_group data to save some memory
- actually don't need to save the vertex/normal buffers after passing to opengl - actually don't need to save the vertex/normal buffers after passing to opengl
- use a storage pool for assimp meshes allowing reuse across entities - use a storage pool for assimp meshes allowing reuse across entities
- also cache textures from loaded meshes for re-use
- remove v2i/v3f... etc from util.h and either use glm everywhere, or write a smaller linear math.h - remove v2i/v3f... etc from util.h and either use glm everywhere, or write a smaller linear math.h
- find a better alternative to g_data_dir in scene_loader.cpp - add an application config file and structure to replace various defines
## LATER TODO: ## LATER TODO:
- add initial opengl constant for INT_MAX, and checks in render_group functions - add initial opengl constant for INT_MAX, and checks in render_group functions

33
src/hexgame.cpp

@ -54,6 +54,23 @@ loadSceneFromJson(game_state* s, render_state* rs)
game_state* gs = g_game_state; game_state* gs = g_game_state;
render_state* rs = g_render_state; render_state* rs = g_render_state;
slParseCamera(sd, rs->cam);
slParseHexGrid(sd, gs->grid, rs->palette_image);
if (gs->grid.gridT == HASH_MAP
&& !slParseGridHashMap(gs->grid, DATA_DIR, HEXMAP_SCHEMA_FILE))
{
ret = false;
}
if (hgInit(gs->grid, rs->filled_hex_render_group, rs->hex_line_render_group, rs->default_shader)) {
// TODO: can remove these once we're using the global color palatte for all textures
rs->filled_hex_render_group->render_objects[0]->tex_id = rs->palette_id;
rs->hex_line_render_group->render_objects[0]->tex_id = rs->palette_id;
} else {
ret = false;
}
if (slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES)) { if (slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES)) {
for (uint i = 0; i < gs->entity_count; i++) { for (uint i = 0; i < gs->entity_count; i++) {
if (!entInit(gs->entities[i], DATA_DIR, rs->default_shader)) { if (!entInit(gs->entities[i], DATA_DIR, rs->default_shader)) {
@ -66,22 +83,6 @@ loadSceneFromJson(game_state* s, render_state* rs)
ret = false; ret = false;
} }
slParseCamera(sd, rs->cam);
slParseHexGrid(sd, gs->grid, rs->palette_image);
if (slCreateHexRenderGroups(gs->grid, rs)) {
if (gs->grid.gridT == HASH_MAP) {
if (!slParseGridHashMap(gs->grid, DATA_DIR, HEXMAP_SCHEMA_FILE))
ret = false;
}
if (!hgInit(gs->grid, rs->filled_hex_render_group, rs->hex_line_render_group))
ret = false;
} else {
LOG(ERROR) << "Error creating hexgrid render groups\n";
ret = false;
}
if (!slParseLights(sd, rs->lights, rs->num_lights, rs->max_lights)) { if (!slParseLights(sd, rs->lights, rs->num_lights, rs->max_lights)) {
LOG(ERROR) << "Error loading lights, exiting\n"; LOG(ERROR) << "Error loading lights, exiting\n";
ret = false; ret = false;

21
src/hexgrid.cpp

@ -3,6 +3,10 @@
#include "hexgrid.h" #include "hexgrid.h"
#define MAX_HEX_COUNT 8192 // NOTE: padding some extra space for hex buffers to grow
#define IDX_PER_HEX_FILLED 54 // NOTE: 6 triangles * 3 vertices per triangle * 3 floats per vertex
#define IDX_PER_HEX_LINES 36 // NOTE: 6 lines * 2 vertices per line * 3 floats per vertex
// forward declarations // forward declarations
hex_info createHexInfo(hexgrid& hg, int q, int r); hex_info createHexInfo(hexgrid& hg, int q, int r);
@ -14,7 +18,11 @@ void populateLineGLBuffers(hexgrid& hg, hex_info& hxi, render_group* rg_lines);
// interface // interface
bool bool
hgInit(hexgrid& hg, render_group* rg_filled, render_group* rg_lines) hgInit(
hexgrid& hg,
render_group*& rg_filled,
render_group*& rg_lines,
rg_shader_program& shader)
{ {
Orientation o = (hg.layout_mode == LAYOUT_FLAT) ? layout_flat : layout_pointy; Orientation o = (hg.layout_mode == LAYOUT_FLAT) ? layout_flat : layout_pointy;
hg.hexlib_layout = Layout(o, Point(hg.hex_size, hg.hex_size), Point(hg.position.x, hg.position.y)); hg.hexlib_layout = Layout(o, Point(hg.hex_size, hg.hex_size), Point(hg.position.x, hg.position.y));
@ -33,6 +41,17 @@ hgInit(hexgrid& hg, render_group* rg_filled, render_group* rg_lines)
uint hex_count = hg.hex_map.size(); uint hex_count = hg.hex_map.size();
assert(hex_count < MAX_HEX_COUNT); assert(hex_count < MAX_HEX_COUNT);
// init render_groups
uint buf_len = MAX_HEX_COUNT * IDX_PER_HEX_FILLED;
rg_filled = rgInitSingle(shader, buf_len, true);
buf_len = MAX_HEX_COUNT * IDX_PER_HEX_LINES;
rg_lines = rgInitSingle(shader, buf_len, true, 0, GL_LINES);
if ((rg_filled == nullptr) || (rg_lines == nullptr)) {
LOG(ERROR) << "Error allocating render_group, exiting\n";
return false;
}
render_object* ro_f = rg_filled->render_objects[0]; render_object* ro_f = rg_filled->render_objects[0];
render_object* ro_l = rg_lines->render_objects[0]; render_object* ro_l = rg_lines->render_objects[0];
ro_f->vertex_buffer.count = hex_count * IDX_PER_HEX_FILLED; ro_f->vertex_buffer.count = hex_count * IDX_PER_HEX_FILLED;

10
src/hexgrid.h

@ -8,10 +8,6 @@
#include "render_group.h" #include "render_group.h"
#include "util.h" #include "util.h"
#define MAX_HEX_COUNT 8192 // NOTE: padding some extra space for hex buffers to grow
#define IDX_PER_HEX_FILLED 54 // NOTE: 6 triangles * 3 vertices per triangle * 3 floats per vertex
#define IDX_PER_HEX_LINES 36 // NOTE: 6 lines * 2 vertices per line * 3 floats per vertex
// TODO: rename to "hex_draw_mode" to fit project style // TODO: rename to "hex_draw_mode" to fit project style
enum HexDrawMode enum HexDrawMode
@ -87,7 +83,11 @@ struct hexgrid
}; };
bool hgInit(hexgrid& hg, render_group* rg_filled, render_group* rg_lines); bool hgInit(
hexgrid& hg,
render_group*& rg_filled,
render_group*& rg_lines,
rg_shader_program& shader);
bool hgAddHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group* rg_lines); bool hgAddHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group* rg_lines);

25
src/scene_loader.cpp

@ -20,8 +20,7 @@ struct slSceneDoc
rapidjson::Document* doc; rapidjson::Document* doc;
}; };
// TODO: find a better way to avoid threading 'data_dir' through gooey // TODO: put in global application config structure with other defines
// this would be a good candidate to go in some global application config structure
static const char* g_data_dir; static const char* g_data_dir;
@ -213,30 +212,10 @@ slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_
return true; return true;
} }
LOG(ERROR) << "Error validation JSON\n";
return false; return false;
} }
bool
slCreateHexRenderGroups(hexgrid& hg, render_state* rs)
{
uint buf_len = MAX_HEX_COUNT * IDX_PER_HEX_FILLED;
rs->filled_hex_render_group = rgInitSingle(rs->default_shader, buf_len, true);
buf_len = MAX_HEX_COUNT * IDX_PER_HEX_LINES;
rs->hex_line_render_group = rgInitSingle(rs->default_shader, buf_len, true, 0, GL_LINES);
if ((rs->filled_hex_render_group == nullptr) || (rs->hex_line_render_group == nullptr)) {
LOG(ERROR) << "Error allocating render_group, exiting\n";
return false;
}
// TODO: can remove these once we're using the global color palatte for all textures
rs->filled_hex_render_group->render_objects[0]->tex_id = rs->palette_id;
rs->hex_line_render_group->render_objects[0]->tex_id = rs->palette_id;
return true;
}
bool bool
slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights) slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights)
{ {

2
src/scene_loader.h

@ -23,8 +23,6 @@ void slParseHexGrid(slSceneDoc* sd, hexgrid& hg, util_image& palette_image);
bool slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_file); bool slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_file);
bool slCreateHexRenderGroups(hexgrid& hg, render_state* rs);
bool slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights); bool slParseLights(slSceneDoc* sd, rg_point_light* lights, uint& num_lights, uint max_lights);
// TODO: this is pretty hacky atm just for testing // TODO: this is pretty hacky atm just for testing

Loading…
Cancel
Save