From d91e717dfae2943eb5126d87ecf0918267113ed2 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Wed, 16 Jan 2019 14:50:12 -0500 Subject: [PATCH] debugging remove hex function the actual bug was not using a reference iterator when updating the "hexID" --- data/test_scene.json | 2 +- src/gooey.cpp | 29 +++++++++++++++++++---------- src/hexgame.cpp | 20 ++++++++++++-------- src/hexgrid.cpp | 37 ++++++++++++++++++++++--------------- src/hexgrid.h | 3 ++- src/render_group.cpp | 10 +++------- src/render_group.h | 2 +- src/renderer.cpp | 8 +++----- 8 files changed, 63 insertions(+), 48 deletions(-) diff --git a/data/test_scene.json b/data/test_scene.json index 281c929..6d399e7 100644 --- a/data/test_scene.json +++ b/data/test_scene.json @@ -13,7 +13,7 @@ "hex_line_color": 5, "hexlib_orientation" : "layout_flat", "grid_type" : "hexagon", - "hex_radius" : 15 + "hex_radius" : 1 }, "entities" : [ diff --git a/src/gooey.cpp b/src/gooey.cpp index 7f3b965..6234e82 100644 --- a/src/gooey.cpp +++ b/src/gooey.cpp @@ -20,6 +20,7 @@ // forward declarations void renderRendererWindow(render_state* rs, ImGuiWindowFlags window_flags = 0); void renderHexgridWindow(hexgrid& grid, ImGuiWindowFlags window_flags = 0); +void renderHexInfo(hex_info* hxi); bool @@ -133,8 +134,8 @@ renderRendererWindow(render_state* rs, ImGuiWindowFlags window_flags) void renderHexgridWindow(hexgrid& grid, ImGuiWindowFlags window_flags) { - ImGui::SetNextWindowPos(ImVec2(250,20)); - ImGui::SetNextWindowSize(ImVec2(250, 300)); + ImGui::SetNextWindowPos(ImVec2(0,270)); + ImGui::SetNextWindowSize(ImVec2(350, 350)); ImGui::SetNextWindowBgAlpha(0.3f); ImGui::Begin("Hexgrid", nullptr, window_flags); @@ -157,19 +158,27 @@ renderHexgridWindow(hexgrid& grid, ImGuiWindowFlags window_flags) ImGui::SameLine(); ImGui::TextUnformatted(grid.is_selecting ? "true" : "false"); ImGui::Text("Hex Count: %lu", grid.hex_map.size()); + ImGui::Separator(); ImGui::Text("current_hex: "); - if (grid.current_hex) { - Hex ch = grid.current_hex->hex; - ImGui::SameLine(); ImGui::Text("%i, %i, %i", ch.q, ch.r, ch.s); - } + if (grid.current_hex) + renderHexInfo(grid.current_hex); + ImGui::Separator(); ImGui::Text("start_hex: "); - if (grid.start_hex) { - Hex sh = grid.start_hex->hex; - ImGui::SameLine(); ImGui::Text("%i, %i, %i", sh.q, sh.r, sh.s); - } + if (grid.start_hex) + renderHexInfo(grid.start_hex); ImGui::Separator(); ImGui::End(); } + +void +renderHexInfo(hex_info* hxi) +{ + Hex h = hxi->hex; + ImGui::SameLine(); ImGui::Text("%i, %i, %i", h.q, h.r, h.s); + ImGui::SameLine(); ImGui::Text(", selected: %s", (hxi->selected) ? "true" : "false"); + ImGui::Text("hexID: %u", hxi->hexID); + ImGui::Text("world position x,y: %f,%f", hxi->XPos, hxi->YPos); +} diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 3e05bca..e398086 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -55,6 +55,7 @@ loadSceneFromJson(game_state* s, render_state* rs) if (!slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES, DATA_DIR)) { LOG(ERROR) << "Error loading Entities, exiting\n"; + slFreeSceneDoc(sd); return false; } @@ -62,23 +63,26 @@ loadSceneFromJson(game_state* s, render_state* rs) slParseHexGrid(sd, gs->grid, rs->palette_image); if (slCreateHexRenderGroups(gs->grid, rs)) { - if (!hgInit(gs->grid, rs->filled_hex_render_group, rs->hex_line_render_group)) + if (!hgInit(gs->grid, rs->filled_hex_render_group, rs->hex_line_render_group)) { + slFreeSceneDoc(sd); return false; + } } else { LOG(ERROR) << "Error creating hexgrid render groups\n"; + slFreeSceneDoc(sd); return false; } if (!slParseLights(sd, rs->lights, rs->num_lights, rs->max_lights)) { LOG(ERROR) << "Error loading lights, exiting\n"; + slFreeSceneDoc(sd); return false; } - - slFreeSceneDoc(sd); } else { return false; } + slFreeSceneDoc(sd); return true; } @@ -92,7 +96,7 @@ init() // which contains the hashtable. g_game_state->grid.hex_map = std::unordered_map(); // NOTE: testing hex_add - g_game_state->grid.draw_mode = ADD_HEXES; + g_game_state->grid.draw_mode = NONE; // TODO: maybe add this as an option to scene json? g_game_state->grid.normal = v3f(0.f, 0.f, 1.f); @@ -203,11 +207,11 @@ handleMouseDown(SDL_MouseButtonEvent &e) case NONE: default: v2f v = cameraUnproject(rs->cam, coords.x, coords.y, dims.x, dims.y); - hex_info *hex = hgGetSingleHex(gs->grid, v.x, v.y); + hex_info *hxi = hgGetSingleHex(gs->grid, v.x, v.y); - if (hex) { - hex->selected = !hex->selected; - gs->grid.start_hex = gs->grid.current_hex = hex; + if (hxi) { + hxi->selected = !hxi->selected; + gs->grid.start_hex = gs->grid.current_hex = hxi; } break; diff --git a/src/hexgrid.cpp b/src/hexgrid.cpp index 0a9ee16..5a3a0ae 100644 --- a/src/hexgrid.cpp +++ b/src/hexgrid.cpp @@ -91,9 +91,19 @@ hgAddHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group* rg_ Hex h = hex_round(pixel_to_hex(hg.hexlib_layout, Point(hex_coords.x, hex_coords.y))); hex_info new_hex = createHexInfo(hg, h.q, h.r); hg.hex_map.insert({h, new_hex}); +#if 0 + // NOTE: have to repopulate all the buffers, and re-index + uint i = 0; + for (auto it : hg.hex_map) { + it.second.hexID = i; + populateFilledHexGLBuffers(hg, it.second, rg_filled); + populateLineGLBuffers(hg, it.second, rg_lines); + i++; + } +#else populateFilledHexGLBuffers(hg, new_hex, rg_filled); populateLineGLBuffers(hg, new_hex, rg_lines); - +#endif // NOTE: we could send just a part of the buffer if performance ends up being bad here glBindBuffer(GL_ARRAY_BUFFER, vb_f.buffer_id); glBufferSubData(GL_ARRAY_BUFFER, 0, vb_f.count * sizeof(GLfloat), vb_f.buffer); @@ -145,7 +155,7 @@ hgRemoveHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group* // NOTE: have to repopulate all the buffers, and re-index uint i = 0; - for (auto it : hg.hex_map) { + for (auto& it : hg.hex_map) { it.second.hexID = i; populateFilledHexGLBuffers(hg, it.second, rg_filled); populateLineGLBuffers(hg, it.second, rg_lines); @@ -286,9 +296,10 @@ hgUpdateHexConeFill(hexgrid& hg, int32 x, int32 y, float cone_angle, std::vector } void -hgUpdateUVBuffer(hexgrid& hg, float* uv_buffer, uint buf_len) +hgUpdateUVBuffer(hexgrid& hg, render_group* rg) { - assert(buf_len == hg.hex_map.size() * IDX_PER_HEX_FILLED); + gl_buffer& uv_buf = rg->render_objects[0]->uv_buffer; + assert(uv_buf.count == hg.hex_map.size() * IDX_PER_HEX_FILLED); for (auto& it : hg.hex_map) { hex_info hxi = it.second; @@ -296,11 +307,14 @@ hgUpdateUVBuffer(hexgrid& hg, float* uv_buffer, uint buf_len) uint buf_idx = hxi.hexID * IDX_PER_HEX_FILLED; for (uint j = 0; j < IDX_PER_HEX_FILLED; j +=3) { - uv_buffer[buf_idx + j + 0] = uv_coords.x; - uv_buffer[buf_idx + j + 1] = uv_coords.y; - uv_buffer[buf_idx + j + 2] = 0; + uv_buf.buffer[buf_idx + j + 0] = uv_coords.x; + uv_buf.buffer[buf_idx + j + 1] = uv_coords.y; + uv_buf.buffer[buf_idx + j + 2] = 0; } } + + glBindBuffer(GL_ARRAY_BUFFER, uv_buf.buffer_id); + glBufferSubData(GL_ARRAY_BUFFER, 0, uv_buf.count * sizeof(GLfloat), uv_buf.buffer); } // internal @@ -333,14 +347,7 @@ createHexagonGrid(hexgrid& hg) for (int r = r1; r <= r2; r++) { hex_info hxi = createHexInfo(hg, q, r); - - // NOTE: testing sparse grid - if ((q > -2 && q < 2) && (r > -2 && r < 2)) { - // skip - } else { - hg.hex_map.insert({hxi.hex, hxi}); - } - ///////////// + hg.hex_map.insert({hxi.hex, hxi}); } } } diff --git a/src/hexgrid.h b/src/hexgrid.h index fbd53ef..fd0ea93 100644 --- a/src/hexgrid.h +++ b/src/hexgrid.h @@ -91,6 +91,8 @@ bool hgAddHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group bool hgRemoveHex(hexgrid& hg, v3f hex_coords, render_group* rg_filled, render_group* rg_lines); +void hgUpdateUVBuffer(hexgrid& hg, render_group* rg); + // ------------------- hex_info* hgGetSingleHex(hexgrid& hg, real32 x, real32 y); @@ -103,4 +105,3 @@ void hgUpdateHexLineDraw(hexgrid& hg, int32 x, int32 y); void hgUpdateHexConeFill(hexgrid& hg, int32 x, int32 y, float cone_angle, std::vector& debug_vertices); -void hgUpdateUVBuffer(hexgrid& hg, float* uv_buffer, uint buf_len); diff --git a/src/render_group.cpp b/src/render_group.cpp index b97d66a..58b5024 100644 --- a/src/render_group.cpp +++ b/src/render_group.cpp @@ -177,8 +177,7 @@ rgBufferData(gl_buffer* buffer, GLenum usage, GLenum target) void rgDraw(render_group* rg, glm::mat4 model_matrix, glm::mat4 view_matrix, glm::mat4 projection_matrix, - rg_point_light* lights, uint num_lights, - bool update_vertex_data, bool update_color_data) + rg_point_light* lights, uint num_lights, bool update_vertex_data) { for (uint i = 0; i < rg->num_objects; i++) { render_object* ro = rg->render_objects[i]; @@ -230,11 +229,6 @@ rgDraw(render_group* rg, glm::mat4 model_matrix, glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); glBindTexture(GL_TEXTURE_2D, ro->tex_id); glUniform1i(rg->shader.sampler_id, 0); - - if (update_color_data) { - glBufferSubData(GL_ARRAY_BUFFER, 0, ro->uv_buffer.count * sizeof(GLfloat), - ro->uv_buffer.buffer); - } } // draw @@ -321,6 +315,8 @@ freeRenderObject(render_object* ro) utilSafeFree(ro); } +// TODO: might as well move this to mesh.cpp and use render_group commands there +// since we're doing the same with hexgrid now bool convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool use_texture) { diff --git a/src/render_group.h b/src/render_group.h index 2dd64a7..6e5df42 100644 --- a/src/render_group.h +++ b/src/render_group.h @@ -87,7 +87,7 @@ void rgBufferData(gl_buffer* buffer, GLenum usage, GLenum target); void rgDraw(render_group* rg, glm::mat4 model_matrix, glm::mat4 view_matrix, glm::mat4 projection_matrix, rg_point_light* lights, uint num_lights, - bool update_vertex_data = false, bool update_color_data = false); + bool update_vertex_data = false); void rgFree(render_group* rg); diff --git a/src/renderer.cpp b/src/renderer.cpp index f3549f1..5482983 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -183,11 +183,9 @@ renRenderFrame(render_state* rs, hexgrid& hg, Entity* entities, uint32 entity_co // filled hexes // get new colors every frame - render_group* rg = rs->filled_hex_render_group; - gl_buffer& uv_buf = rg->render_objects[0]->uv_buffer; - hgUpdateUVBuffer(hg, uv_buf.buffer, uv_buf.count); - rgDraw(rg, m_model, m_view, m_projection, - rs->lights, rs->num_lights, false, true); + hgUpdateUVBuffer(hg, rs->filled_hex_render_group); + rgDraw(rs->filled_hex_render_group, m_model, m_view, m_projection, + rs->lights, rs->num_lights); // hex lines rgDraw(rs->hex_line_render_group, m_model, m_view, m_projection,