Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

revearz

Some help with Finite Differences

This topic is 5464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a heightfield (in x-z plane) and want to calculate the normals using finite differences. So far I have: ( ignoring boundary cases ) normal.X = height[x-1][ z ] - height[x+1][ z ] normal.Z = height[ x ][z-1] - height[ x ][z+1] what would the normal.Y value be? - Thanks

Share this post


Link to post
Share on other sites
Advertisement
Once more a post of mine about a topic I have absolutely no experience with but since you got no reply so far...
.
.
.

Why not get two tangent vectors to your surface ....
a = (-2, height[x-1][ z ] - height[x+1][ z ], 0) and
b = (0, height[ x ][z-1] - height[ x ][z+1], -2).

Then compute the normal n by using the vector-product: n = -a x b (the minus sign is because I assume a right-handed coordinate system {x,y,z}).

Share this post


Link to post
Share on other sites
Let f(x,z) be the height field.

Now, construct the partial derivatives using finite differences (denoting spacing between height field as h):

@h/@x = (f[x+1][z]-f[x-1][z])/(2h)
@h/@z = (f[x][z+1]-f[x][z-1])/(2h)

Now construct these into tangent vectors:

tx = (1,@h/@x,0)
tz = (0,@h/@z,1)

Then the normal is n = tx cross tz.

From here on you can easily work your way to simplify the formulas.

- Mikko Kauppila

Share this post


Link to post
Share on other sites
Hi .. thanks for the reply.

Atheist: Why do you use -2 in your computations of a and b?


uutee: What is h? Space between height field? If so:

Position x0 = position at (x-1,z),
x1 = position at (x+1,z),
z0 = position at (x,z-1),
z1 = position at (x,z+1)

h = 2 * Length of ( x0 - x1 ) for @h/@x
h = 2 * Length of ( z0 - z1 ) for @h/@z

Am I right?

Thanks!



[edited by - revearz on September 24, 2003 10:39:10 PM]

Share this post


Link to post
Share on other sites
-2 is because you go two steps (i.e. from [z+1] to [z-1]). The minus because you go backwards (it´s just to stay compatible with what you posted; I did cut&paste).

Utee uses the 2 for the same reason i guess. The h probably should be the distance between two neighbouring mesh-points. I simply assumed it was 1.

Share this post


Link to post
Share on other sites
>Position x0 = position at (x-1,z),
>x1 = position at (x+1,z),
>z0 = position at (x,z-1),
>z1 = position at (x,z+1)

Actually, if you can compute (of course you can) these positions (as 3-dimensional vectors) then the normal computation is even more easy:

t1 = x1-x0 // 3-vector
t2 = z1-z0 // 3-vector
n = t1 cross t2
(normalize n)

Now you don''t need the parameter h in any way.

- Mikko Kauppila

Share this post


Link to post
Share on other sites
I assume this is what you want:

/// @param z1 Z of the +X vertex

/// @param z2 Z of the +Y vertex

/// @param z3 Z of the -X vertex

/// @param z4 Z of the -Y vertex

/// @param scale Ratio of Sxy / Sz, where Sxy is the distance between vertices and Sz scales the heightmap Z

/// values to the actual Z values.


D3DXVECTOR3 * ComputeGridNormal( float z1, float z2, float z3, float z4, float scale, D3DXVECTOR3 * pN )
{
// The 4 adjacent points in a uniform grid: A, B, C, D

//

// B

// |

// C--0--A

// |

// D

//

//

// The ratio of XY-scale to Z-scale: s = Sxy / Sz

// The desired normal: N = cross(A,B) + cross(B,C) + cross(C,D) + cross(D,A), (then normalize)

//

// Simplified:

// Nx = 2 * s * (Cz - Az)

// Ny = 2 * s * (Dz - Bz)

// Nz = 4 * s^2

// N = normalize( N )

//

// Since N is normalized in the end, it can be divided by 2 * s:

//

// Nx = Cz - Az

// Ny = Dz - Bz

// Nz = 2 * s

// N = normalize( N )

//


pN->x = z3 - z1;
pN->y = z4 - z2;
pN->z = 2.0f * scale;

D3DXVec3Normalize( pN, pN );

return pN;
}

Share this post


Link to post
Share on other sites
Hi .. thanks for the replies.

JohnBolton: If I''m not using a uniform grid, what would Sxy be?

uutee: thanks .. this saves me the trouble of calculating h since my grid is non-uniform .. saves a couple of sqrt''s ..

Share this post


Link to post
Share on other sites
quote:
Original post by revearz
JohnBolton: If I''m not using a uniform grid, what would Sxy be?



If the grid is not uniform, you can''t use this.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!