Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / finding advanced terrain normals

Author
Message
draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 23rd Aug 2008 19:50
Are there any plugins or code snippets to find the normal of an XZ coordinate on advanced terrain? I tried searching, but the forum search options dont work very well, nor does google. I basically want to stick a grass static to the angle of the ground.
kaedroho
17
Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 23rd Aug 2008 20:15
you can use Sparkys Collision DLL to raycast the bit which you want to find the normal of and use SC_GetCollisionNormalXYZ to get the normal.

draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 23rd Aug 2008 21:36
Thats a possibility, but that means I have to do a raycast for every single grass static (16000 approximately). Isn't there a way to get the normal just by knowing the X and Z coordinates on the terrain?
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Aug 2008 21:54 Edited at: 23rd Aug 2008 21:56
Quote: "Are there any plugins or code snippets to find the normal of an XZ coordinate on advanced terrain? I tried searching, but the forum search options dont work very well, nor does google. I basically want to stick a grass static to the angle of the ground."


You've already got one in my various AT shader demos (which you've seen ).

Check out the Misty Fog demo, you'll find the following in the dba code:



That same demo also shows you how to add normals to the terrain, but I expect you know how yo do that already. The only thing to check is the grid size within each terrain limb - this is needed in the declarations at the beginning where the array that is used in the calculations is declared:



Post back if you're having problems with that code.

Edit:

Quote: "Isn't there a way to get the normal just by knowing the X and Z coordinates on the terrain?"


No, you need the Y coordinate as well - otherwise you wouldn't know whether it was flat or a mountain valley for example.
kaedroho
17
Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 23rd Aug 2008 21:55
I use memblocks to find out the normals on my terrain, but I dont use Advanced terrain so that wont work. You could use get ground height on 3 points around the position and do some complicated maths to find the normal.

draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 23rd Aug 2008 22:44 Edited at: 23rd Aug 2008 22:58
Thanks GG, thats just what I needed! There's more code in the normal generation function than I bargained for but I'll get it working.

Edit: I spoke a little too soon Im quite useless when it comes to the more advanced math. Where exactly are you adding the normals to the terrain?
Xlaydos
21
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 23rd Aug 2008 22:58
You can calcuate the normals, by taking 3 points (A,B,C) on the same polygon and taking the cross product of vectors AB and AC. This will return a vector perpendicular to the polygon but can be pointing "up" (+ve y component) or "down" (-ve y component) you can correct this by checking the y component and multiplying by -1 if necessary.
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Aug 2008 01:51
Quote: "Im quite useless when it comes to the more advanced math. Where exactly are you adding the normals to the terrain?"


Maths has nothing to do with it - it's all in the code:



It should be obvious from that snippet that:

1. I first build the terrain (build terrain, etc, ).

2. I convert the terrain's format to include normals (remember the terrain IS object 1).

3. I then calculate the normals using the function terrainNormalGenerator() (there's a clue in the name of the function I think).

The function terrainNormalGenerator() is then doing the following

4. First find the dimensions of the terrain - max width, distance between neighbouring vertices, etc.

5. Then find the heights of each vertex using the vertexdata commands and store them in an array.

6. Finally calculate the normals at each vertex using the height data from step 5 and the XZ interval data from step 4 (again there's a clue in the use of the command set vertexdata normals ).

Hope this helps.

I can try to explain the maths involved if I must ...

[I haven't attempted to smooth or average the normals by looking at all polys adjacent to a vertex as is sometimes done - it might be possible to improve on my calculation by doing that but, in my demos, the extra effort didn't seem to be worthwhile. Notice that I didn't use the built-in DBPro set object normals command - that gives visible seams between AT limbs when lit at certain angles.]

Login to post a reply

Server time is: 2025-08-09 02:06:14
Your offset time is: 2025-08-09 02:06:14