Skip to content

Commit

Permalink
Fixed drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbeedle committed Feb 23, 2024
1 parent 447a0c6 commit 547464d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
54 changes: 30 additions & 24 deletions testbed/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ struct GLRenderTriangles {
//
struct GLRenderTrees {
enum { e_maxVertices = 16 }; // Number of segments in the circle
static const int NUM_CIRCLE_VERTICES =
e_maxVertices + 2; // Additional for center and duplicate of first vertex
static const int NUM_CIRCLE_VERTICES = e_maxVertices + 2;
float circleVertices[NUM_CIRCLE_VERTICES * 2]; // Each vertex has x and y

std::vector<b2Vec2> treePositions; // Dynamic array to hold tree positions
Expand Down Expand Up @@ -631,7 +630,6 @@ struct GLRenderTrees {
color = f_color;
}
)";

// Initialize circle vertices for a unit circle
circleVertices[0] = 0.0f; // Center of circle
circleVertices[1] = 0.0f;
Expand Down Expand Up @@ -659,17 +657,38 @@ struct GLRenderTrees {
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);

// Instance positions
// // Instance positions
// glBindBuffer(GL_ARRAY_BUFFER, m_buffers[1]);
// glEnableVertexAttribArray(1);
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// glVertexAttribDivisor(1, 1); // Advance once per instance

// // Instance colors
// glBindBuffer(GL_ARRAY_BUFFER, m_buffers[2]);
// glEnableVertexAttribArray(2);
// glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
// glVertexAttribDivisor(2, 1); // Advance once per instance

// glBindVertexArray(0); // Unbind VAO

// sCheckGLError();
}

void setBuffers(const std::vector<b2Vec2>& positions,
const std::vector<b2Color>& colors) {
// Instance positions - Set once since positions are static
glBindBuffer(GL_ARRAY_BUFFER, m_buffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(b2Vec2) * positions.size(),
positions.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glVertexAttribDivisor(1, 1); // Advance once per instance
glVertexAttribDivisor(1, 1);

// Instance colors
// Instance colors - Initially empty, updated dynamically
glBindBuffer(GL_ARRAY_BUFFER, m_buffers[2]);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glVertexAttribDivisor(2, 1); // Advance once per instance
glVertexAttribDivisor(2, 1);

glBindVertexArray(0); // Unbind VAO

Expand All @@ -691,30 +710,20 @@ struct GLRenderTrees {

void UpdateTree(int index, const b2Vec2& position, const b2Color& color) {
// Update only the specified tree
glBindBuffer(GL_ARRAY_BUFFER, m_buffers[1]); // Positions buffer
glBufferSubData(GL_ARRAY_BUFFER, sizeof(b2Vec2) * index, sizeof(b2Vec2),
&position);

glBindBuffer(GL_ARRAY_BUFFER, m_buffers[2]); // Colors buffer
glBufferSubData(GL_ARRAY_BUFFER, sizeof(b2Color) * index, sizeof(b2Color),
&color);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind after update
}

void UpdateTrees(const std::vector<b2Vec2>& positions,
const std::vector<b2Color>& colors) {
m_count = positions.size(); // Assume colors and positions are synchronized

glBindBuffer(GL_ARRAY_BUFFER, m_buffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(b2Vec2) * positions.size(),
positions.data(), GL_DYNAMIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, m_buffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(b2Color) * colors.size(),
colors.data(), GL_DYNAMIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind after update
}

void Flush() {
Expand Down Expand Up @@ -963,16 +972,13 @@ void DebugDraw::DrawTrees(const std::vector<b2Vec2>& positions,
const std::vector<int>& changedIDs) {
// Update the tree data. This could be optimized to only happen if data has
// changed.
for (size_t i = 0; i < changedIDs.size(); ++i) {
int index = changedIDs[i];
m_trees->UpdateTree(index, positions[index], colors[index]);
}
// Draw the trees
m_trees->UpdateTrees(positions, colors);
m_trees->Flush();
}

void DebugDraw::DrawAllTrees(const std::vector<b2Vec2>& positions,
const std::vector<b2Color>& colors) {
m_trees->setBuffers(positions, colors);
m_trees->UpdateTrees(positions, colors);
m_trees->Flush();
}
Expand Down
9 changes: 1 addition & 8 deletions testbed/tests/DroneSwarmTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,7 @@ class DroneSwarmTest : public Test {

// m_world->DebugDraw();
// Draw(m_world, &g_debugDraw);
// SEE HOW WE UPDATE TREES IN USUAL DRAW SETTING TO FIND IF THEY'RE MAPPED
// FOUND TREES ARE CHANGED
// Update Drone position
// Update Drone position and add found trees to list
for (auto &drone : drones) {
drone->update(drones);
for (auto &tree : drone->getFoundDiseasedTrees()) {
Expand All @@ -549,17 +547,12 @@ class DroneSwarmTest : public Test {
// Not first run, only update changed trees
for (Tree *tree : foundTrees) {
int id = tree->getID();
foundIDs.push_back(id);
treePositions[id] = tree->getBody()->GetPosition();
if (tree->isMapped()) {
treeColors[id] = b2Color(0.0f, 1.0f, 0.0f, 0.5f);
} else {
treeColors[id] = b2Color(1.0f, 0.0f, 0.0f, 0.5f);
}
}
for (auto &id : foundIDs) {
std::cout << "Found tree with ID: " << id << std::endl;
}

if (firstRun) {
g_debugDraw.DrawAllTrees(treePositions, treeColors);
Expand Down

0 comments on commit 547464d

Please sign in to comment.