Topic Options
Rate This Topic
#257982 - 05/26/09 08:43 AM How to smooth terrain with extra triangles
Akez42 Offline
Newbie

Registered: 05/26/09
Posts: 7
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).


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.


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


Edited by Akez42 (05/26/09 12:20 PM)

Top
#258016 - 05/26/09 02:03 PM Re: How to smooth terrain with extra triangles [Re: Akez42]
zeoverlord Offline
Frequent Contributor
***

Registered: 02/23/06
Posts: 667
Loc: Sweden
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.

Top
#258034 - 05/26/09 07:00 PM Re: How to smooth terrain with extra triangles [Re: zeoverlord]
Akez42 Offline
Newbie

Registered: 05/26/09
Posts: 7
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
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!

Top
#258157 - 05/29/09 07:30 AM Re: How to smooth terrain with extra triangles [Re: Akez42]
zeoverlord Offline
Frequent Contributor
***

Registered: 02/23/06
Posts: 667
Loc: Sweden
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

Top
#258181 - 05/29/09 07:49 PM Re: How to smooth terrain with extra triangles [Re: zeoverlord]
Akez42 Offline
Newbie

Registered: 05/26/09
Posts: 7
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:
 Code:
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.

Top
#258194 - 05/30/09 01:59 AM Re: How to smooth terrain with extra triangles [Re: Akez42]
ZbuffeR Offline
OpenGL Guru
*****

Registered: 12/01/03
Posts: 3752
Loc: Grenoble - France
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);

Top
#258235 - 05/31/09 08:43 AM Re: How to smooth terrain with extra triangles [Re: ZbuffeR]
Akez42 Offline
Newbie

Registered: 05/26/09
Posts: 7
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

The code used to process each triangle along with their texture and normals can be downloaded Here. 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

Top
#259601 - 06/25/09 10:45 AM Re: How to smooth terrain with extra triangles [Re: Akez42]
awhig Offline
Regular Contributor

Registered: 06/16/09
Posts: 133
Loc: FL , USA
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.

Top
#259606 - 06/25/09 10:56 AM Re: How to smooth terrain with extra triangles [Re: awhig]
Makinis Offline
Newbie

Registered: 05/13/09
Posts: 45
Loc: Portugal
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 wink
_________________________
"If you can't be a good example, you'll have to serve as a good warning..."

Top


Moderator:  Khronos_webmaster 
Who's Online
6 registered (dukey, Werty, obirsoy, skynet, AGL_Music, mrmoo), 44 Guests and 85 Spiders online.
Key: Admin, Global Mod, Mod
Newest Members
Nonozor, Maire Nicolas, minakshee, Koter, pixelwrangler
24934 Registered Users
Top Posters (30 Days)
Alfonse Reinheart 152
ZbuffeR 92
Dark Photon 73
marshats 47
Brolingstanz 44
Ilian Dinev 41
Iulian B 38
Stephen A 37
Kip Warner 28
devdept 26
skynet 24
Pierre 23
igorgiv 23
DarkShadow44 23
Yann LE PETITCORPS 22
scratt 21
Abdallah DIB 21
Aleksandar 20
Pierre Boudier 19
mikeynovemberoscar 19
Forum Stats
24934 Members
12 Forums
52392 Topics
271545 Posts

Max Online: 482 @ 08/11/08 06:19 PM