Sign in to follow this  

Texture coords for repeating texture along cylinder tube

Recommended Posts

laylay    310
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?

Share this post

Link to post
Share on other sites
Ashaman73    13715
Here's some pseudo code:
v_texture_coord = 0
scale = 1.0
for i=1...n do
cylinder = cylinder_array[i]
cylinder.start_v = v_texture_coord

v_texture_coord = v_texture_coord + cylinder.length * scale
cylinder.end_v = v_texture_coord

Share this post

Link to post
Share on other sites
laylay    310
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);

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

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. Edited by laylay

Share this post

Link to post
Share on other sites
Brother Bob    10344
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 <=.

Share this post

Link to post
Share on other sites
laylay    310
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this