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
Alexander Rhoades is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.