OpenGL: Incorrect Texture Mapping on Cylindrical Geometry

I’m working on rendering a cylinder in OpenGL, and I’m facing an issue with texture mapping. I’m trying to map a texture onto the curved surface of the cylinder, but the texture appears distorted where it connects.

Here’s how I’m generating the vertices and setting up the attribute pointers:

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);

        // Texture coordinates
        // Texture coordinates
        float u1 = static_cast<float>(i) / numSegments; // Left side of the quad
        float u2 = static_cast<float>(i + 1) / numSegments; // Right side of the quad

        // Scale the texture coordinates to fit the entire texture onto the cylinder
        u1 *= 2.0f * M_PI * bottomRadius; 
        u2 *= 2.0f * M_PI * topRadius;


        // Vertices for the first triangle of the side quad
        vertices.push_back(x1_bottom);  // Position
        vertices.push_back(-height / 2.0f);
        vertices.push_back(z1_bottom);
        vertices.push_back(u1);         // Texture coordinates
        vertices.push_back(0.0f);

        vertices.push_back(x1_top);
        vertices.push_back(height / 2.0f);
        vertices.push_back(z1_top);
        vertices.push_back(u1);
        vertices.push_back(1.0f);

        vertices.push_back(x2_bottom);
        vertices.push_back(-height / 2.0f);
        vertices.push_back(z2_bottom);
        vertices.push_back(u2);
        vertices.push_back(0.0f);

        // 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(u2);
        vertices.push_back(0.0f);

        vertices.push_back(x1_top);
        vertices.push_back(height / 2.0f);
        vertices.push_back(z1_top);
        vertices.push_back(u1);
        vertices.push_back(1.0f);

        vertices.push_back(x2_top);
        vertices.push_back(height / 2.0f);
        vertices.push_back(z2_top);
        vertices.push_back(u2);
        vertices.push_back(1.0f);
    }

    return vertices;
}

VAO1.Bind();
VBO VBO1(cylinderVertices.data(), cylinderVertices.size() * sizeof(float));
// Position attribute
VAO1.LinkAttrib(VBO1, 0, 3, GL_FLOAT, 5 * sizeof(float), (void*)0);
// Color attribute
VAO1.LinkAttrib(VBO1, 1, 3, GL_FLOAT, 5 * sizeof(float), (void*)0);
// Texture coordinate attribute
VAO1.LinkAttrib(VBO1, 2, 3, GL_FLOAT, 5 * sizeof(float), (void*)0);
// Light attribute
VAO1.LinkAttrib(VBO1, 3, 3, GL_FLOAT, 5 * sizeof(float), (void*)0);
VAO1.Unbind();
VBO1.Unbind();

I’ve tried adjusting the texture coordinates to scale based on the circumference of the cylinder, as shown in the code snippet above. Additionally, I’ve double-checked the vertex data layout and the attribute setup to ensure they are correct.

I was expecting the texture to be mapped onto the curved surface of the cylinder without distortion or enlargement. Specifically, I wanted the texture to wrap around the cylinder smoothly, following its curvature accurately.

Can someone help me understand what I’m doing wrong? How can I properly map the texture onto the cylinder without distortion or enlargement?

Additional Information:

I’m using GLFW and GLEW for window and context management.
The vertex data layout contains position, color, texture coordinate, and light data for each vertex.

Problem

New contributor

Alexander Rhoades 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