Skip to content

Commit

Permalink
fix drawing in multiple layers (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bb1950328 committed Feb 27, 2021
1 parent 8376347 commit e655309
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/controller.cpp
Expand Up @@ -84,8 +84,8 @@ namespace controller {
const auto enableDebugOutput = config::getBool(config::ENABLE_GL_DEBUG_OUTPUT);
glfwSetErrorCallback(glfwErrorCallback);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, enableDebugOutput?4:3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, enableDebugOutput?3:2);
glfwWindowHint(GLFW_SAMPLES, (int) (config::getInt(config::MSAA_SAMPLES)));
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

Expand Down Expand Up @@ -303,8 +303,8 @@ namespace controller {
spdlog::info("BrickSim started.");
initialize();

//openFile("test_files/mpd_test.mpd");
openFile("~/Downloads/arocs.mpd");
openFile("test_files/bricks_test.ldr");
//openFile("~/Downloads/arocs.mpd");
//openFile("3001.dat");

while (!(glfwWindowShouldClose(window) || userWantsToExit)) {
Expand Down
7 changes: 7 additions & 0 deletions src/gui/window_element_properties.cpp
Expand Up @@ -316,6 +316,13 @@ namespace gui {
}

ImGui::DragScalar(ICON_FA_LAYER_GROUP" Layer", ImGuiDataType_U8, &node->layer, 0.2f, nullptr, nullptr);
static layer_t lastLayer = node->layer;
if (lastSelectedNode != node) {
lastLayer = node->layer;
} else if (lastLayer != node->layer) {
controller::setElementTreeChanged(true);
lastLayer = node->layer;
}

lastSelectedNode = node;
} else {
Expand Down
39 changes: 21 additions & 18 deletions src/mesh.cpp
Expand Up @@ -195,7 +195,7 @@ void Mesh::writeGraphicsData() {
addMinEnclosingBallLines();
}
sortInstancesByLayer();
updateInstanceCountOfLayerAndGreater();
updateInstanceCountOfLayer();

initializeTriangleGraphics();
initializeLineGraphics();
Expand Down Expand Up @@ -291,7 +291,7 @@ void Mesh::rewriteInstanceBuffer() {
std::lock_guard<std::recursive_mutex> lg(controller::getOpenGlMutex());
if (instancesHaveChanged) {
sortInstancesByLayer();
updateInstanceCountOfLayerAndGreater();
updateInstanceCountOfLayer();

//todo just clear buffer data when no instances

Expand Down Expand Up @@ -331,18 +331,21 @@ void Mesh::sortInstancesByLayer() {
});
}

void Mesh::updateInstanceCountOfLayerAndGreater() {
instanceCountOfLayerAndGreater.clear();
void Mesh::updateInstanceCountOfLayer() {
instanceCountOfLayer.clear();
if (!instances.empty()) {
layer_t layerNum = instances.begin()->layer;
unsigned count = 0;
unsigned int count = 0, totalCount = 0;
for (const auto &inst : instances) {
if (inst.layer!=layerNum) {
instanceCountOfLayerAndGreater.emplace(layerNum, count);
instanceCountOfLayer.emplace(layerNum, std::make_pair(count, totalCount));
layerNum = inst.layer;
totalCount += count;
count = 0;
}
count++;
}
instanceCountOfLayerAndGreater.emplace(layerNum, count);
instanceCountOfLayer.emplace(layerNum, std::make_pair(count, totalCount));
}
}

Expand Down Expand Up @@ -439,33 +442,33 @@ void Mesh::initializeOptionalLineGraphics() {
}

void Mesh::drawTriangleGraphics(layer_t layer) {
const auto it = instanceCountOfLayerAndGreater.find(layer);
if (it != instanceCountOfLayerAndGreater.cend()) {
const auto it = instanceCountOfLayer.find(layer);
if (it != instanceCountOfLayer.cend()) {
std::lock_guard<std::recursive_mutex> lg(controller::getOpenGlMutex());
for (const auto &entry: triangleIndices) {
const auto color = entry.first;
const std::vector<unsigned int>& indices = entry.second;
glBindVertexArray(VAOs[color]);
glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr, it->second);
glDrawElementsInstancedBaseInstance(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr, it->second.first, it->second.second);
}
}
}

void Mesh::drawLineGraphics(layer_t layer) {
const auto it = instanceCountOfLayerAndGreater.find(layer);
if (it != instanceCountOfLayerAndGreater.cend()) {
const auto it = instanceCountOfLayer.find(layer);
if (it != instanceCountOfLayer.cend()) {
std::lock_guard<std::recursive_mutex> lg(controller::getOpenGlMutex());
glBindVertexArray(lineVAO);
glDrawElementsInstanced(GL_LINES, lineIndices.size(), GL_UNSIGNED_INT, nullptr, it->second);
glDrawElementsInstancedBaseInstance(GL_LINES, lineIndices.size(), GL_UNSIGNED_INT, nullptr, it->second.first, it->second.second);
}
}

void Mesh::drawOptionalLineGraphics(layer_t layer) {
const auto it = instanceCountOfLayerAndGreater.find(layer);
if (it != instanceCountOfLayerAndGreater.cend()) {
const auto it = instanceCountOfLayer.find(layer);
if (it != instanceCountOfLayer.cend()) {
std::lock_guard<std::recursive_mutex> lg(controller::getOpenGlMutex());
glBindVertexArray(optionalLineVAO);
glDrawElementsInstanced(GL_LINES_ADJACENCY, optionalLineIndices.size(), GL_UNSIGNED_INT, nullptr, it->second);
glDrawElementsInstancedBaseInstance(GL_LINES_ADJACENCY, optionalLineIndices.size(), GL_UNSIGNED_INT, nullptr, it->second.first, it->second.second);
}
}

Expand Down Expand Up @@ -564,9 +567,9 @@ std::pair<glm::vec3, float> Mesh::getMinimalEnclosingBall() {
}

bool MeshInstance::operator==(const MeshInstance &other) const {
return transformation == other.transformation && color.get() == other.color.get() && elementId == other.elementId && selected == other.selected;
return transformation == other.transformation && color.get() == other.color.get() && elementId == other.elementId && selected == other.selected && layer == other.layer;
}

bool MeshInstance::operator!=(const MeshInstance &other) const {
return transformation != other.transformation || color.get() != other.color.get() || elementId != other.elementId || selected != other.selected;
return transformation != other.transformation || color.get() != other.color.get() || elementId != other.elementId || selected != other.selected || layer != other.layer;
}
4 changes: 2 additions & 2 deletions src/mesh.h
Expand Up @@ -68,7 +68,7 @@ class Mesh {

std::vector<MeshInstance> instances;
bool instancesHaveChanged = false;
std::map<layer_t, unsigned int> instanceCountOfLayerAndGreater;//for example element with key 4 is the number of elements in layer 4 and above
std::map<layer_t, std::pair<unsigned int, unsigned int>> instanceCountOfLayer;//value.first is instance count in this layer, value.second is instance count sum of all layers before

std::string name = "?";

Expand Down Expand Up @@ -122,7 +122,7 @@ class Mesh {
void addOptionalLineVertex(const LineVertex &vertex);

void addMinEnclosingBallLines();
void updateInstanceCountOfLayerAndGreater();
void updateInstanceCountOfLayer();
void sortInstancesByLayer();
};

Expand Down
12 changes: 12 additions & 0 deletions test_files/bricks_test.ldr
@@ -0,0 +1,12 @@
0 FILE bricks_test.ldr
0 just some bricks to test software
0 Author: bb1950328
0 Name: bricks_test.ldr
0 !LDRAW_ORG Model
0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt
0 !THEME Technic
1 4 0 0 0 1 0 0 0 1 0 0 0 1 3001.dat
1 5 0 0 45 1 0 0 0 1 0 0 0 1 3001.dat
1 6 0 0 90 1 0 0 0 1 0 0 0 1 3001.dat
1 7 0 0 135 1 0 0 0 1 0 0 0 1 3001.dat
1 8 0 0 180 1 0 0 0 1 0 0 0 1 3001.dat

0 comments on commit e655309

Please sign in to comment.