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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật