tiling terrain seam

Started by
6 comments, last by szecs 11 years, 9 months ago
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...

seamproblem.jpg

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?
Advertisement
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?

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.
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.size(); ++j ) {
vertices.normal += normal_buffer[j];
}

vertices.normal /= normal_buffer.size();
D3DXVec3Normalize(&vertices.normal, &vertices.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?
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.
You should really use 2049x2049 textures, with a one pixel overlap on adjacent edges. This would solve this and other problems.
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.

[attachment=9661:terrain.zip]
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)

This topic is closed to new replies.

Advertisement