Texture coords for repeating texture along cylinder tube

Started by
3 comments, last by laylay 11 years, 5 months ago
I have a bunch of cylinders attached to each other (like a long tube) I want to be able to texture it without any stretching, just a nice repeating texture going along it. I know how to wrap around it but not along it. The problem is that each cylinder segment of the tube aren't the same size so I can't just increase the v coord by 1 each time. How can this be calculated?
Advertisement
Here's some pseudo code:

v_texture_coord = 0
scale = 1.0
for i=1...n do
cylinder = cylinder_array
cylinder.start_v = v_texture_coord

v_texture_coord = v_texture_coord + cylinder.length * scale
cylinder.end_v = v_texture_coord
end
Thanks. So pretty much I can just increase along the length of the segment? What are the start and end v coords for?


It seems to work anyway but I'm having a problem with wrapping around the pipe that I didn't notice. The last face on the cylinder segment is messed up. This is how I'm calculating it.

[source lang="cpp"] // loop through each point on the radius
for (unsigned int i = 0; i < m_points; ++i)
{
// sin and cos displace the vertex in a radius
float x = cos(Math::DegreesToRadians(degrees)) * m_radius;
float y = sin(Math::DegreesToRadians(degrees)) * m_radius;

// offset the vertex up and right to orientate it
Vector3f point = position;
point += right * x;
point += up * y;

// create the vertex
Vertex vertex;
vertex.position = point;
vertex.normal = (point - position).Normalise();
vertex.texCoord = Vector2f((float)i / ((float)m_points - 1.0f), ycoord);
m_vertices.push_back(vertex);

// keep going around the radius
degrees += 360.0f / (float)m_points - 1;
}[/source]

What I think is happening is that the 0th vertex on the ring has a u texcoord of 0 but the last one is 1, that's going to cause problems I think. How should I handle it?

I have a feeling I'll have to have a duplicate vertex at the 0th position because it needs to be 0 for the first face but 1 for the last face. Kind of sucks.
If you want the cylinder to have N points around, then you need N+1 vertices since the first and the last two vertices have different texture coordinates even though they have the same position. In your code, as far as I can see all you need to do it change the loop exit condition from < to <=.
Yeah I thought so, thanks for clarifying that though. There was a bit more work involved with getting it to work but it's wrapping around perfectly now.

This topic is closed to new replies.

Advertisement