• Create Account

## tiling terrain seam

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1timothyjlaird  Members

598
Like
0Likes
Like

Posted 24 June 2012 - 10:44 AM

I'm trying to tile some terrain that I generated using height mapping. Basically I make a plane on the x/z axis and then change the 'y' values based on a height map. In order to make the terrain tile I averaged the 'y' values on sides of each block of terrain (there are 4). Problem is that I end up with a seam that actually lines up well but the lighting is all screwed up along the seam as you can see here...

I'm fairly confident this is because I am not calculating the normals quite right along the seam. This is the code I am using the calculate the normals:

//calc normals for smooth shading
for (int counter = 0; counter < indexData.Length; counter += 3)
{
Vector3 v1 = vertexData[indexData[counter + 0]].Position;
Vector3 v2 = vertexData[indexData[counter + 1]].Position;
Vector3 v3 = vertexData[indexData[counter + 2]].Position;
Vector3 vu = v3 - v1;
Vector3 vt = v2 - v1;
Vector3 normal = Vector3.Cross(vu, vt);
normal.Normalize();
vertexData[indexData[counter]].Normal += normal;
vertexData[indexData[counter + 1]].Normal += normal;
vertexData[indexData[counter + 2]].Normal += normal;
}
for (int counter = 0; counter < vertexData.Length; counter++)
{
vertexData[counter].Normal.Normalize();
}


Any ideas on how to resolve this?

### #2gnomgrol  Members

699
Like
0Likes
Like

Posted 25 June 2012 - 07:08 AM

How are the tiles buildet up? Each has its own Vertexbuffer?
The code for your normals seems correct, may you are calling it before you change the y-values of the vertices?
That caused the problem in my case, may its the same for you.

What does the code to average the y-values look like? Can you post it please?
Oh, another question I have, how do you make the textures fit together that perfectly in slope-based texturing?

Edited by gnomgrol, 25 June 2012 - 08:28 AM.

### #3timothyjlaird  Members

598
Like
0Likes
Like

Posted 25 June 2012 - 06:44 PM

How are the tiles buildet up? Each has its own Vertexbuffer?
The code for your normals seems correct, may you are calling it before you change the y-values of the vertices?
That caused the problem in my case, may its the same for you.

What does the code to average the y-values look like? Can you post it please?
Oh, another question I have, how do you make the textures fit together that perfectly in slope-based texturing?

The vertex and index buffers are the same for all 4 blocks of terrain. I simply change effect.World (translation matrix) each time, then draw.

Nope. The last thing I do is calculate the normals.

Here is the code I used for averaging the y-values along the edges:

private float[,] getMirroredVertexDataTwoDim(float[,] yvalues, int size)
{
float leftsidey;
float rightsidey;
float avgleftright;
float topsidey;
float bottomsidey;
float avgtopbottom;
for (int zcounter = 0; zcounter < size; zcounter++)
{
leftsidey = yvalues[0, zcounter];
rightsidey = yvalues[size - 1, zcounter];
avgleftright = (leftsidey + rightsidey) / 2.0f;
yvalues[0, zcounter] = avgleftright;
yvalues[size - 1, zcounter] = avgleftright;
}
for (int xcounter = 0; xcounter < size; xcounter++)
{
topsidey = yvalues[xcounter, 0];
bottomsidey = yvalues[xcounter, size - 1];
avgtopbottom = (topsidey + bottomsidey) / 2.0f;
yvalues[xcounter, 0] = avgtopbottom;
yvalues[xcounter, size - 1] = avgtopbottom;
}
return yvalues;
}


I'm not using slope-based texturing...just one big texture for each tile (2048x2048). A bit crude but effective. I used L3DT to make it for me.

### #4gnomgrol  Members

699
Like
0Likes
Like

Posted 26 June 2012 - 12:17 AM

Im sorry, I cant really figure out whats the cause of your problem.
Here is the code I use to calculate normals (DirectX):

Cant post in sourcetags, buggy somehow:

// GET ALL DA NORMALZ!!!
std::vector<D3DXVECTOR3>* normal_buffer = new std::vector<D3DXVECTOR3>[chunkVnum];

for( int i = 0; i<chunkInum; i += 3 ){
// get the three vertices that make the faces
D3DXVECTOR3 p1 = vertices[indices[i+0]].pos;
D3DXVECTOR3 p2 = vertices[indices[i+1]].pos;
D3DXVECTOR3 p3 = vertices[indices[i+2]].pos;

D3DXVECTOR3 v1 = p2 - p1;
D3DXVECTOR3 v2 = p3 - p1;
D3DXVECTOR3 normal;
D3DXVec3Cross(&normal, &v1, &v2);

D3DXVec3Normalize(&normal, &normal);

// Store the face's normal for each of the vertices that make up the face.
normal_buffer[indices[i+0]].push_back( normal );
normal_buffer[indices[i+1]].push_back( normal );
normal_buffer[indices[i+2]].push_back( normal );
}

// Now loop through each vertex vector, and avarage out all the normals stored.
for( int i = 0; i < chunkVnum; ++i )
{
for( int j = 0; j < normal_buffer[i].size(); ++j ) {
vertices[i].normal += normal_buffer[i][j];
}

vertices[i].normal /= normal_buffer[i].size();
D3DXVec3Normalize(&vertices[i].normal, &vertices[i].normal);
}

May it can help you in some way ;/
How do you load the heightmap on the chunk if you calculate x and z with the effect?

Edited by gnomgrol, 26 June 2012 - 12:34 AM.

### #5szecs  Members

2820
Like
1Likes
Like

Posted 26 June 2012 - 01:35 AM

Without looking into the code in detail, I assume you only use one block's geometry to calculate the normals. But the normals are affected by the neighbor blocks' geometry too.

### #6mark ds  Members

1736
Like
0Likes
Like

Posted 26 June 2012 - 05:16 AM

You should really use 2049x2049 textures, with a one pixel overlap on adjacent edges. This would solve this and other problems.

### #7timothyjlaird  Members

598
Like
0Likes
Like

Posted 26 June 2012 - 11:24 AM

To be more clear...the heightmap texture is only 256x256. The diffuse texture is 2048x2048. I have attached my terrain code (not everything, just terrain)...maybe that will help.

Without looking into the code in detail, I assume you only use one block's geometry to calculate the normals. But the normals are affected by the neighbor blocks' geometry too.

That makes sense. I guess I could just stitch a seam of quads between blocks but then I would not be able to translate them later...in order to give the illusion of infinite terrain.

### #8szecs  Members

2820
Like
0Likes
Like

Posted 26 June 2012 - 12:00 PM

mark ds' suggestion is good, even if (s)he misunderstood you. I think the best thing is to use overlap on the block borders. Better than fetching data from another block (because there is a reason for using blocks instead of one block)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.