I’m trying to make a function for displaying the vertices of a custom b2Body shape (I am using Box2D and am trying to display the Box2D colliders) and this is the function I’m using. It renders lines and points to denote where the vertices in the b2Body shape are. But it doesn’t render vertices in the “interior” of the shape, meaning that if a vertex is closer to the center than two other vertices then it only renders a line between those two vertices and not the vertex which is “inside”. So, how do I render the interior of the shape? If you’re wondering, my line vertex shader is just three mat4s, and fragment shader is just one color, very simple.
<code>#include <box2d/box2d.h>
std::vector<glm::vec2> vertices;
// Iterate over all fixtures in the object's Box2D body and put the collider vertices in the vertices vector
for (b2Fixture* f = object.body->GetFixtureList(); f; f = f->GetNext()) {
if (f->GetType() == b2Shape::e_polygon) {
b2PolygonShape* shape = (b2PolygonShape*)f->GetShape();
for (int i = 0; i < shape->m_count; ++i) {
b2Vec2 vertex = object.body->GetWorldPoint(shape->m_vertices[i]);
vertices.push_back(glm::vec2(vertex.x, vertex.y));
// Create and bind VAO and VBO
glGenVertexArrays(1, &VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), vertices.data(), GL_STATIC_DRAW);
// Define vertex attribute
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void*)0);
glEnableVertexAttribArray(0);
// Use the shader program
shader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setVec4("lineColor", glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
glm::mat4 model = glm::mat4(1.0f);
shader.setMat4("model", model);
// Draw the polygon outline
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
// Draw points to highlight the vertices
glDrawArrays(GL_POINTS, 0, vertices.size());
// Unbind and delete VAO and VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
<code>#include <box2d/box2d.h>
#include <glm/glm.hpp>
#include <vector>
#include <glad/glad.h>
std::vector<glm::vec2> vertices;
// Iterate over all fixtures in the object's Box2D body and put the collider vertices in the vertices vector
for (b2Fixture* f = object.body->GetFixtureList(); f; f = f->GetNext()) {
if (f->GetType() == b2Shape::e_polygon) {
b2PolygonShape* shape = (b2PolygonShape*)f->GetShape();
for (int i = 0; i < shape->m_count; ++i) {
b2Vec2 vertex = object.body->GetWorldPoint(shape->m_vertices[i]);
vertices.push_back(glm::vec2(vertex.x, vertex.y));
}
}
}
// Create and bind VAO and VBO
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), vertices.data(), GL_STATIC_DRAW);
// Define vertex attribute
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void*)0);
glEnableVertexAttribArray(0);
// Use the shader program
shader.use();
// Set shader uniforms
shader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setVec4("lineColor", glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
glm::mat4 model = glm::mat4(1.0f);
shader.setMat4("model", model);
// Draw the polygon outline
glLineWidth(2.0f);
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
// Draw points to highlight the vertices
glPointSize(10.0f);
glDrawArrays(GL_POINTS, 0, vertices.size());
// Unbind and delete VAO and VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
</code>
#include <box2d/box2d.h>
#include <glm/glm.hpp>
#include <vector>
#include <glad/glad.h>
std::vector<glm::vec2> vertices;
// Iterate over all fixtures in the object's Box2D body and put the collider vertices in the vertices vector
for (b2Fixture* f = object.body->GetFixtureList(); f; f = f->GetNext()) {
if (f->GetType() == b2Shape::e_polygon) {
b2PolygonShape* shape = (b2PolygonShape*)f->GetShape();
for (int i = 0; i < shape->m_count; ++i) {
b2Vec2 vertex = object.body->GetWorldPoint(shape->m_vertices[i]);
vertices.push_back(glm::vec2(vertex.x, vertex.y));
}
}
}
// Create and bind VAO and VBO
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), vertices.data(), GL_STATIC_DRAW);
// Define vertex attribute
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void*)0);
glEnableVertexAttribArray(0);
// Use the shader program
shader.use();
// Set shader uniforms
shader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setVec4("lineColor", glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
glm::mat4 model = glm::mat4(1.0f);
shader.setMat4("model", model);
// Draw the polygon outline
glLineWidth(2.0f);
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
// Draw points to highlight the vertices
glPointSize(10.0f);
glDrawArrays(GL_POINTS, 0, vertices.size());
// Unbind and delete VAO and VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);