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?
Texture coords for repeating texture along cylinder tube
Started by laylay, Nov 12 2012 04:35 AM
4 replies to this topic
Sponsor:
#2 Members - Reputation: 4741
Posted 12 November 2012 - 06:49 AM
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 end
Ashaman
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#3 Members - Reputation: 207
Posted 12 November 2012 - 10:34 PM
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.
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.
Edited by laylay, 13 November 2012 - 12:05 AM.
#4 Moderators - Reputation: 4915
Posted 13 November 2012 - 04:30 AM
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 <=.






