How to Generate Cylinder Vertices with a Circular Hole in OPENGL?

  softwareengineering

I’m working on a project where I need to generate vertices for a cylinder mesh in a 3D environment using C++. I’ve managed to generate the vertices for the cylinder’s sides, but now I need to create a circular hole on the side face of the cylinder.

I’m currently using a function to generate vertices for the cylinder’s sides based on its bottom radius, top radius, height, and the number of segments. However, I’m unsure how to modify this function to include a circular hole of a specified radius and position on the side face of the cylinder.

Could someone provide guidance on how to modify the existing function to generate vertices for a cylinder with a circular hole? Specifically, I’d like to know how to adjust the function to add vertices for the hole and exclude vertices within the hole’s region.

Here’s the function I’m currently using:

std::vector<float> CylinderVertices(float bottomRadius, float topRadius, float height, int numSegments) {
std::vector<float> vertices;

// Generate vertices for the sides of the cylinder
for (int i = 0; i < numSegments; ++i) {
    float theta1 = 2.0 * M_PI * i / numSegments;
    float theta2 = 2.0 * M_PI * (i + 1) / numSegments;

    float x1_bottom = bottomRadius * cos(theta1);
    float z1_bottom = bottomRadius * sin(theta1);
    float x2_bottom = bottomRadius * cos(theta2);
    float z2_bottom = bottomRadius * sin(theta2);

    float x1_top = topRadius * cos(theta1);
    float z1_top = topRadius * sin(theta1);
    float x2_top = topRadius * cos(theta2);
    float z2_top = topRadius * sin(theta2);

    // Vertices for the first triangle of the side quad
    vertices.push_back(x1_bottom);
    vertices.push_back(-height / 2.0f);
    vertices.push_back(z1_bottom);
    vertices.push_back(x1_top);
    vertices.push_back(height / 2.0f);
    vertices.push_back(z1_top);
    vertices.push_back(x2_bottom);
    vertices.push_back(-height / 2.0f);
    vertices.push_back(z2_bottom);

    // Vertices for the second triangle of the side quad
    vertices.push_back(x2_bottom);
    vertices.push_back(-height / 2.0f);
    vertices.push_back(z2_bottom);
    vertices.push_back(x1_top);
    vertices.push_back(height / 2.0f);
    vertices.push_back(z1_top);
    vertices.push_back(x2_top);
    vertices.push_back(height / 2.0f);
    vertices.push_back(z2_top);
}

    return vertices;
}

VAO VAO1;
VAO1.Bind();
VBO VBO1(cylinderVertices.data(), cylinderVertices.size() * sizeof(float));
VAO1.LinkAttrib(VBO1, 0, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
VAO1.LinkAttrib(VBO1, 1, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
VAO1.LinkAttrib(VBO1, 2, 2, GL_FLOAT, 3 * sizeof(float), (void*)0);
VAO1.LinkAttrib(VBO1, 3, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
VAO1.Unbind();
VBO1.Unbind();


VAO1.Bind();
glDrawArrays(GL_TRIANGLES, 0, cylinderVertices.size() / 3);
VAO1.Unbind();

New contributor

Nathan Budhuu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT