Skip to content

Commit 6586c9e

Browse files
committed
fixed an issue where cones were drawn by mistake
1 parent fcfe663 commit 6586c9e

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/opengl_primitives.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct Object {
4040
GLenum gl_draw_mode = GL_TRIANGLES;
4141
GLint gl_program = 0; // which programm is used to shade this object
4242
std::string name = "default";
43+
bool drawInstanced = false;
4344

4445
size_t elementCount() const { return elements.size(); }
4546
size_t vertexCount() const { return vertices.size(); }
@@ -75,6 +76,7 @@ struct ObjectInfo {
7576
GLenum gl_draw_mode = GL_TRIANGLES;
7677
size_t offset = 0;
7778
std::string name = "default";
79+
bool drawInstanced = false;
7880
bool enabled = true;
7981

8082
ObjectInfo(size_t vert, size_t elem, GLenum draw_mode)
@@ -88,6 +90,7 @@ struct ObjectInfo {
8890
number_vertices = object.vertexCount();
8991
number_instances = object.instanceCount();
9092
name = object.name;
93+
drawInstanced = object.drawInstanced;
9194
}
9295
};
9396

src/opengl_widgets.cpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ void OpenGLWidget::renderScene() {
276276
// for every program (key) a vector with objects is stored
277277
for (const auto& obj_group : object_tree) {
278278
glUseProgram(obj_group.first);
279-
size_t instance_count = 0;
280279

281280
for (const size_t& obj_idx : obj_group.second) {
282281
const ObjectInfo& obj = scene_info[obj_idx];
@@ -296,18 +295,17 @@ void OpenGLWidget::renderScene() {
296295
}
297296

298297
if (obj.enabled) {
299-
if (obj.number_instances > 0) { // instanced rendering
298+
if (obj.drawInstanced) { // instanced rendering
300299
glDrawElementsInstancedBaseVertexBaseInstance(
301300
obj.gl_draw_mode, static_cast<GLint>(obj.number_elements), GL_UNSIGNED_SHORT,
302301
(void*)(obj.offset), static_cast<GLsizei>(obj.number_instances),
303-
static_cast<GLint>(obj.base_index), static_cast<GLint>(instance_count));
302+
static_cast<GLint>(obj.base_index), static_cast<GLint>(obj.base_instance));
304303
} else { // direct rendering with elements
305304
glDrawElementsBaseVertex(obj.gl_draw_mode, static_cast<GLint>(obj.number_elements),
306305
GL_UNSIGNED_SHORT, (void*)(obj.offset),
307306
static_cast<GLint>(obj.base_index));
308307
}
309308
}
310-
instance_count += obj.number_instances;
311309
}
312310
}
313311

@@ -380,13 +378,21 @@ void OpenGLWidget::recalculateOrbitPositions() {
380378
std::vector<glm::mat4> transformations;
381379
transformations.reserve(problem_instance.getSatellites().size() + problem_instance.scheduled_communications.size());
382380
glm::mat4 scale = glm::inverse(glm::scale(glm::vec3(zoom))); // ignore zoom
381+
size_t instance_count = 0;
383382

383+
auto infos = getObjectInfo("satellites");
384+
for (const auto& obj_info : infos)
385+
obj_info->base_instance = instance_count;
384386
for (const Satellite& o : problem_instance.getSatellites()) {
385387
glm::vec3 position = o.cartesian_coordinates(sim_time) / real_world_scale;
386388
glm::mat4 translation = glm::translate(position);
387389
transformations.push_back(translation * scale);
390+
instance_count++;
388391
}
389392

393+
infos = getObjectInfo("communications_arrowhead");
394+
for (const auto& obj_info : infos)
395+
obj_info->base_instance = instance_count;
390396
for (const auto& c : problem_instance.scheduled_communications) {
391397
glm::vec3 sat1 = problem_instance.getSatellites()[c.first].cartesian_coordinates(sim_time) / real_world_scale;
392398
glm::vec3 sat2 = problem_instance.getSatellites()[c.second].cartesian_coordinates(sim_time) / real_world_scale;
@@ -398,8 +404,12 @@ void OpenGLWidget::recalculateOrbitPositions() {
398404
glm::mat4 rotation = glm::rotate(angle, axis);
399405

400406
transformations.push_back(translation * rotation * scale);
407+
instance_count++;
401408
}
402409

410+
infos = getObjectInfo("orientation_arrowhead");
411+
for (const auto& obj_info : infos)
412+
obj_info->base_instance = instance_count;
403413
for (const auto& s : problem_instance.getSatellites()) {
404414
glm::vec3 position = s.cartesian_coordinates(sim_time) / real_world_scale;
405415
TimelineEvent<glm::vec3> last_orientation = satellite_orientations[&s].previousEvent(sim_time, false);
@@ -429,12 +439,13 @@ void OpenGLWidget::recalculateOrbitPositions() {
429439

430440
glm::mat4 translation = glm::translate(glm::vec3(position + direction_vector));
431441
transformations.push_back(translation * scale * rotation);
442+
instance_count++;
432443
}
433444

434445
// push data to VBO
435446
if (transformations.size() != 0) {
436447
size_t size_transformation = sizeof(transformations[0]) * transformations.size();
437-
glBindBuffer(GL_ARRAY_BUFFER, vbo_transformations); // set active
448+
glBindBuffer(GL_ARRAY_BUFFER, vbo_transformations); // set active
438449
glBufferData(GL_ARRAY_BUFFER, size_transformation, &transformations[0], GL_STREAM_DRAW); // push data
439450
}
440451
}
@@ -483,7 +494,7 @@ std::vector<Object> OpenGLWidget::createLines() {
483494
all_lines.push_back(communication_line);
484495
}
485496
edgescene_com_start = problem_instance.islCount();
486-
edgescene_com_end = edgescene_com_start + problem_instance.scheduled_communications.size() - 1;
497+
edgescene_com_end = edgescene_com_start + problem_instance.scheduled_communications.size();
487498

488499
// build satellite orientations
489500
for (auto const& satellite : problem_instance.getSatellites()) {
@@ -652,6 +663,7 @@ void OpenGLWidget::prepareInstanceScene(const PhysicalInstance& instance) {
652663
Object satellites = OpenGLPrimitives::createSatellite();
653664
satellites.name = "satellites";
654665
satellites.gl_program = satellite_prog;
666+
satellites.drawInstanced = true;
655667
// for each satellite in instance we need one copy of the satellite object
656668
for (int i = 0; i < problem_instance.getSatellites().size(); i++) {
657669
satellites.object_transformations.push_back(glm::mat4(1.f));
@@ -668,6 +680,7 @@ void OpenGLWidget::prepareInstanceScene(const PhysicalInstance& instance) {
668680
Object cone = OpenGLPrimitives::createCone(0.006f, 0.03f, glm::vec3(.55f, .1f, 1.f));
669681
cone.name = "communications_arrowhead";
670682
cone.gl_program = satellite_prog;
683+
cone.drawInstanced = true;
671684
for (int i = 0; i < problem_instance.scheduled_communications.size(); i++) {
672685
cone.object_transformations.push_back(glm::mat4(1.f));
673686
}
@@ -677,6 +690,7 @@ void OpenGLWidget::prepareInstanceScene(const PhysicalInstance& instance) {
677690
cone = OpenGLPrimitives::createCone(0.005f, 0.012f, glm::vec3(1.f));
678691
cone.name = "orientation_arrowhead";
679692
cone.gl_program = satellite_prog;
693+
cone.drawInstanced = true;
680694
for (int i = 0; i < problem_instance.getSatellites().size(); i++) {
681695
cone.object_transformations.push_back(glm::mat4(1.f));
682696
}
@@ -916,7 +930,7 @@ void OpenGLWidget::buildGUI() {
916930

917931
static bool hide_comms = false;
918932
if (ImGui::Checkbox("Hide scheduled communications", &hide_comms)) {
919-
for (size_t i = edgescene_com_start; i <= edgescene_com_end; i++) {
933+
for (size_t i = edgescene_com_start; i < edgescene_com_end; i++) {
920934
scene[EDGES_SUBSCENE].setEnabled(i, !hide_comms);
921935
}
922936
auto infos = getObjectInfo("communications_arrowhead");
@@ -927,7 +941,7 @@ void OpenGLWidget::buildGUI() {
927941

928942
static bool hide_orientations = false;
929943
if (ImGui::Checkbox("Hide satellite orientations", &hide_orientations)) {
930-
for (size_t i = edgescene_com_end + 1; i < scene[EDGES_SUBSCENE].objectCount(); i++) {
944+
for (size_t i = edgescene_com_end; i < scene[EDGES_SUBSCENE].objectCount(); i++) {
931945
scene[EDGES_SUBSCENE].setEnabled(i, !hide_orientations);
932946
}
933947
auto infos = getObjectInfo("orientation_arrowhead");

0 commit comments

Comments
 (0)