# Strange Texturing problem

This topic is 2299 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, there i am currently having issues with my terrain texture. What i am trying to accomplish is taking a seamless texture and repeating it in both direction (x and z) as often as i like.
First of all here is the problem i am talking about:

Here i was trying to repeat the texutre 2 times in both direction, but as you can see there is a problem in the spaces between. Its really odd. In the middle of the cross the texture gets displayed aswell, but really small (can't really see it in the picture) and it seems like that it gets interpolated from the edges of this little texture to the edges of the terrain. Here is how im calculating the U/V coordinates:

 // Calculate how much to increment the texture coordinates by. incrementValue = (float)TEXTURE_REPEAT / (float)m_terrainWidth; // Calculate how many times to repeat the texture. incrementCount = m_terrainWidth / TEXTURE_REPEAT; // Initialize the tu and tv coordinate values. tuCoordinate = 0.0f; tvCoordinate = 1.0f; // Initialize the tu and tv coordinate indexes. tuCount = 0; tvCount = 0; // Loop through the entire height map and calculate the tu and tv texture coordinates for each vertex. for(j=0; j<m_terrainHeight; j++) { for(i=0; i<m_terrainWidth; i++) { // Store the texture coordinate in the height map. m_heightMap[(m_terrainHeight * j) + i].texture.x = tuCoordinate; m_heightMap[(m_terrainHeight * j) + i].texture.y = tvCoordinate; // Increment the tu texture coordinate by the increment value and increment the index by one. tuCoordinate += incrementValue; tuCount++; // Check if at the far right end of the texture and if so then start at the beginning again. if(tuCount == incrementCount) { tuCoordinate = 0.0f; tuCount = 0; } } // Increment the tv texture coordinate by the increment value and increment the index by one. tvCoordinate -= incrementValue; tvCount++; // Check if at the top of the texture and if so then start at the bottom again. if(tvCount == incrementCount) { tvCoordinate = 1.0f; tvCount = 0; } } 

I have to adjust those so they fit for every triangle, i do this in my indices calculation as follows:

 int index = 0; for(int j=0; j<(verticesAlongWidth-1); j++) { for(int i=0; i<(verticesAlongLength-1); i++) { index1 = (m_terrainHeight * j) + i; // Bottom left. index2 = (m_terrainHeight * j) + (i+1); // Bottom right. index3 = (m_terrainHeight * (j+1)) + i; // Upper left. index4 = (m_terrainHeight * (j+1)) + (i+1); // Upper right. tv = m_heightMap[index3].texture.y; if(tv == 1.0f) { m_heightMap[index3].texture.y = 0.0f; } tu = m_heightMap[index4].texture.x; tv = m_heightMap[index4].texture.y; if(tu == 0.0f) { m_heightMap[index4].texture.x = 1.0f; } if(tv == 1.0f) { m_heightMap[index4].texture.y = 0.0f; } tu = m_heightMap[index4].texture.x; tv = m_heightMap[index4].texture.y; if(tu == 0.0f) { m_heightMap[index4].texture.x = 1.0f; } if(tv == 1.0f) { m_heightMap[index4].texture.y = 0.0f; } tu = m_heightMap[index2].texture.x; if(tu == 0.0f) { m_heightMap[index2].texture.x = 1.0f; } 

If you need more info please let me know..i am setting at this problem for hours now...it might be pretty simple and i dont see it.

##### Share on other sites
Hi,

I took a quick look and I noticed this:

if(tuCount == incrementCount)
{
tuCoordinate = 0.0f;
tuCount = 0;
}

The problem space between the tiles has the whole texture drawn in that space because your texture coordinates go from 0-1 and then you clamp them back to 0. Logically there is a triangle with starting texture coordinate of 1 and ending coordinate 0. That shrinks the whole texture in that particular region.

Your texture coordinates may go over 1.0. It isn't a problem.

Cheers! Edited by kauna

##### Share on other sites
Im not quite sure if i understand correctly.
I am clamping the value back to 0 because the other tile (texture reapeat) should start. In my mind there shouldn't be a problem with a triangle wich shrinks the texture. You care to elaborate? Maybe with ur suggested solution?

Thanks a lot so far!

##### Share on other sites
My suggestion is : don't clamp. The texture will repeat correctly.

You can make your shader to output the texture coordinates, and you'll notice that there is a triangle which has texture coordinates from the opposite sides, which isn't what you are after.

Cheers!

##### Share on other sites
You mean like this ?:

 for(j=0; j<m_terrainHeight; j++) { for(i=0; i<m_terrainWidth; i++) { // Store the texture coordinate in the height map. m_heightMap[(m_terrainHeight * j) + i].texture.x = tuCoordinate; m_heightMap[(m_terrainHeight * j) + i].texture.y = tvCoordinate; tuCoordinate += incrementValue; } // Increment the tv texture coordinate by the increment value and increment the index by one. tvCoordinate -= incrementValue; } 

If i do it like this it looks like this:

I am sorry if i am not getting your point here :/. I am kinda new to this stuff.

##### Share on other sites
If you can get a copy of Frank Luna's Intro to 3D Game Programming with DirectX he covers terrain creation including setting (u,v) values. Here's an algorithm I use:

 int k = 0; for (float i = 0; i < numVertRows; ++i) { for (float j = 0; j < numVertCols; ++j) { verts[k].pos.x = i * CellWidth; verts[k].pos.z = j * CellHeight; verts[k].pos.y = 0.0f; verts[k].tex0.x = ( i * (1.0f / numCellRows) ); verts[k].tex0.y = 1.0f - ( j * (1.0f / numCellCols) ); k++; // Next vertex } } 

##### Share on other sites
I was able to use ur advice. It is working now. Thanks a lot!

And i will def. take a look at the book.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013567
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!