PDA

View Full Version : How to smooth terrain with extra triangles



Akez42
05-26-2009, 06:43 AM
Hi
I've been at this for ages trying to figure out how to get this to work.
Here's my problem, I have been able to create a terrain using float values stored in a text file (I prefer this method to height map).
http://img40.imageshack.us/img40/9776/terrain.png

I wish to seperate each of those triangles by a further amount (changable within the program) to allow for a more smoother look.
I have attempted filling the triangle with smaller triangles to make the terrain look less sharp at each point but the method I was attempting did not seem to work.

http://img40.imageshack.us/img40/5752/terrainsegment.png
That is what happens if I use my method to create a triangle for the terrain.

All I am looking for is the theory behind how to create each triangle based on 2 main y positions for each vertex.
I appologise if I did not explain too well as I found explaining this subject quite hard.

Thanks in advance

zeoverlord
05-26-2009, 12:03 PM
well generating the triangles is the easy part, for each triangle just generate the mid points by averaging them together then build 4 new triangles from those and put the result in a new array.

The hard part is smoothing it, you could use the normals to judge how much the midpoints should be displaced.
Using a heightmap is preferred here though as the height values can be automatically smoothed.

Akez42
05-26-2009, 05:00 PM
Thanks for the theory. i have managed to create the terrain using the 4 extra triangles.
The problem I have now is calculating the normals.
I have searched countless websites looking for information on normals but I can't seem to understand any of it!
I have attempted to assign the normals per vertex but this is my result:
Screenshot (http://img40.imageshack.us/img40/1683/terrainf.png)
I'm not sure if the vertices have to be created in a specific order or if there is some other theory behind it?
Any help would be appreciated as I'm beginning to lose my mind!

zeoverlord
05-29-2009, 05:30 AM
practically what you do is for each polygon you calcylate the polygons normal using all 3 vertics, it's pretty simple and you probably already have the code for that by the looks of things but here is a good one.
http://www.flipcode.com/archives/Calculating_Normals.shtml

next you cycle trough all the vertics and average in all the normals from the polygons that touch it.
Couldn't be easier

Akez42
05-29-2009, 05:49 PM
hi,
I took a look at the website you provided and made an attempt at implementing it but it still doesn't seem to like it.
The code i used is:

tVector3 CalculateNormal(float x1,float y1,float z1,float x2,float y2,float z2,float x3,float y3,float z3)
{
tVector3 vector;

float v1x = x1 - x2;
float v1y = y1 - y2;
float v1z = z1 - z2;

float v2x = x2 - x3;
float v2y = y2 - y3;
float v2z = z2 - z3;

vector.z = (v1y * v2z) - (v2y * v1z);
vector.x = (v1z * v2x) - (v2z * v1x);
vector.y = (v1x * v2y) - (v2x * v1y);

float vectorLength = sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);

vector.x /= vectorLength;
vector.y /= vectorLength;
vector.z /= vectorLength;
return vector;
}
I'm not sure if there are any errors in there? I later call this function and assign its result to a tVector3 variable (struct which stores x,y,z values in) and then use the x,y,z values within the glNormal3f() function.
Is what i do correct or am i doing something i shouldnt be?
Thanks.

ZbuffeR
05-29-2009, 11:59 PM
You got your cross product wrong.
It should be :

vector.x = (v1y * v2z) - (v2y * v1z);
vector.y = (v1z * v2x) - (v2z * v1x);
vector.z = (v1x * v2y) - (v2x * v1y);

Akez42
05-31-2009, 06:43 AM
oh yes. thanks, i never noticed that!

I made that change and changed the order in which each point is created and processed to a clockwise order. Everything seems to look better but there still seems to be a problem.

Screenshot (http://img268.imageshack.us/img268/8643/terraint.png)

The code used to process each triangle along with their texture and normals can be downloaded Here (http://www.mediafire.com/?t5lzmn2ejhy). That code is located within 2 for loops, each for 'xm' and 'zm'. 'height' is the starting bottom left vertex, 'heightOpp' is the opposite, 'heightZ' is half way between 'height' and 'heightfZ' which is the next position on the z axis.
Im not sure why i didn't just include that with the code. I guess i just forgot :D

The CalculateNormals function can be seen above along with the change suggested by ZbuffeR.

Any help would be apreciated as i have no idea what the problem is!
Thanks in advance

awhig
06-25-2009, 08:45 AM
Instead of going for subdivision algorithms, why not to go for bump mapping. You need to tweak normals and let the surface remain untouched. After all, a surface interacts to light for its final appearance.

I am in favor of bump maps because computing normals become tough if you have curved triangles.

Makinis
06-25-2009, 08:56 AM
I don't know if you are still looking for any ideas and I don't realy know if this is going to have the effect that you want but maybe you should try to, for each triangle get the medium z value of all the 3 z values from each point and then create a triangle fan from those 4 points... I think that will make your terrain smoother but I'm not sure if it is going to work... Try it ;)