Skip to main content
gamedev.net
🔒 Locked

Texture Coordinates Alignment

Started by Bretttido Oct 5, 2002 at 5:45 PM 1 replies 500+ views
Original Post
Bretttido
Bretttido
Let me explain my scenario briefly. Say you have an area of terrain that is completely flat and contains a single texture repeated across the terrain. Calculating the texture coordinates is a simple process of using the (x, y) coordinates of the terrain (we''re assuming here that z is UP towards the sky). This same process of assigning texture coordinates based on (x, y) can be used if there are varying heights across the terrain. However, this will result in steep triangles that have a stretched texture due to not taking the height into account. If the height is taken into account, though, the texture will avoid the stretching problem but will end up not aligning with eachother. This can be explained if you follow a texture over a steep hill which has a total "x-texture-distance" of 160. Whereas if you simply follow along the bottom of the hill, the "x-texture-distance" could be a much smaller value of 100 or so. End result? Texture coordinates don''t line up. Does anyone know of a way to maintain texture coordinates that take into account height and align evenly across a terrain?
Jim Adams
Jim Adams
If it''s a single texture map you are using, then:

tx = 0.0f;
ty = 0.0f;

// X = terrain width in units
for(DWORD x=0;x<2048.0f;x+=TerrainScale) {
Vertex[Whatever].tx = tx;
Vertex[Whatever].ty = ty;

// Get vectors of this vertex, and next vertex
ThisVertex = Vertex[Whatever].vecPos;
NextVertex = Vertex[Whatever+1].vecPos;

// Get difference and length of vectors
vecDiff = NextVertex - ThisVertex;
float Length = D3DXVec3Length(&vecDiff);

// Offset texture coordinates by length
tx += (Length / TextureScale);
}

The above code will loop through the width of the terrain, calculate the length between vertices, and use the result as the texture coordinates difference. Notice that you need to scale the texture coordinate difference down to a reasonable amount. For example, if the average distance between vertices is 256.0f, then TextureScale = 256.0f;, meaning that the terrain would loop once per average terrain vertex.

The problem with the above is that the looparound values would get very high for the texture, and certain video cards can''t handle that. You should be able to modify it a bit to mod the tx values into a range of 0 to 3 or something lower.


Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
and Focus On: Advanced Animation with DirectX

Bretttido
Bretttido
Hmm.. maybe I''m just having difficulties conceptualizing the problem. The solution you described above is what I initially thought to do. However, take a look at the following ASCII terrain where the height at a particular vertex is 0-9


00000000000
00012421000
00000000000


So if we were to follow along the rows starting with the top row which is completely flat. We would end up with tx being 10.0f at the right side of the terrain. Then at the second row, we would end up with something greater than 10.0f at the right side of the terrain. So it seems to me that the only way that row1 and row2 would be aligned, is if by lucky chance the extra ground distance covered in row2 was an integral amount greater than row1 (ie 11.0f, 12.0f, et cetera). BTW, the tx value for row2 would come out to be about 14.13f.

Please correct me if I''m just looking at this problem incorrectly.

Brett

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.