# OpenGL In-shader normal (dot3) from heightmap in screen-space?

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

## Recommended Posts

Hi, All. I need some help/advice: I am trying to generate a normal vector on the fly from a heightmap only (i.e. no dot3 normalmap, not even 2 components and recreate the 3rd). Specifically, I am using GLSL and the dFdx() and dFdy() functions. The lighting is looking almost correct, but there are weird Moire patterns when you get close, and edge effects (the latter of which I kind of expected). My question is this: has anyone implemented this with good results? (OpenGL or DirectX) I'll post the code below, but it's sort of a hack, because I think the full math would actually require the inverse of a 2x2 matrix: (u & v are texture directions, sx & sy are screen x & y, h = height, d = partial derivative operator). Sorry for my ASCII matrix skills [8^) [ du/dsx dv/dsx ] { dh/du }={ dh/dsx } [ du/dsy dv/dsy ] { dh/dv }.{ dh/dsy } where I'd like to solve for dh/du and dh/dv. Since I don't know how to do that in GLSL, here's my hack:
vec3 dh = vec3 (dFdx (t.r), dFdy (t.r), 0.0) *
(depth * texsize * fract (-fwidth (t.r)));
norm = eyeSpaceNormal + dh;
t = vec4 (normalize(norm), 0.0);

alternatively, I could just do a 3-point sample in texture space and do my own derivatives. Neither the matrix version nor the 3-point version seem like they would be fast or robust...am I missing something? It could also just be the way GL computes the dFdx function (2x2 blocks of pixels, right?) Anyway, below is the badness... Results comparison: dot3 version dFdx & dFdy version [Edited by - lonesock on March 13, 2006 10:07:02 AM]

##### Share on other sites
What I would like to know is how did you invent math to do derivatives on discrete data?

##### Share on other sites
Quote:
 Original post by Anonymous PosterWhat I would like to know is how did you invent math to do derivatives on discrete data?

If you are referring to the dFdx and dFdy operators, OpenGL provides these (since the pixels are processed in parallel, the driver has access to the values at neighboring pixels). For the 3-point sampling I mentioned, I would just sample the height at my pixel's (u,v) location, then at (u+delta,v), then at (u,v+delta). Since the driver is doing linear interpolation of the height values, I can use finite differences to approximate the derivative in both directions.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999991
×