PDA

View Full Version : Artifacts with reflect() & specular highlights



MarcusL
06-15-2007, 06:44 AM
Hi,

I'm using tangentspace bumpmapping and am adding some fake specular highlights with a lightmap, with this:

float specular = texture2D(lightMap, reflect(eyeVecTSN, normalTS).xy * .5 + .5).r;

I get texture coordinates in the correct range and they vary pretty well in the large, but there are small artifacts that get worse when I zoom in. See these pictures:

http://yar.nu/macke/bad_specular1.png
http://yar.nu/macke/bad_specular2.png

The normal map seems ok, and the artifacts doesn't appear to be per-texel either. I've normalized everything just to be sure (didn't help much).

As you can see, the coords are correct locally, but there are blocks with different offsets. I've never encountered anything like it. I suspect numerical issues, but I don't know enough about it to figure out if and/or what is wrong.

This is on a GeForce 7800 with vec/float precision (no difference from half).

All help welcome!

k_szczech
06-15-2007, 09:43 AM
Just in case: make sure you have texture filtering enabled.
Blocky artifacts can also occur if you work with floats that are in different range. An example:

float a = 1000.0;
float b = 0.01;
float c = 1000.1;
float d = 0.1;

float x = (a + b) - (c + d);
/* bad results */
/* x = 1000.01 - 1000.2 */
/* x = 100?.?? - 100?.? - precision loss after 3 digits */
/* x = 0.0 */

float x = (a - c) + (b - d);
/* good results */
/* x = -0.1 + (-0.09) */
/* x = -0.100?? + (-0.0900??) - precision loss after 3 digits */
/* x = -0.190 */Review your code for calculating eyeVecTSN and normalTS.

knackered
06-19-2007, 08:53 AM
the eye vector should not be normalised at the vertex level, only at the pixel level.

Lord crc
06-19-2007, 08:19 PM
Might I ask why the eye vector should not be normalized? Is there a good and detailed doc about this somewhere?

MarcusL
06-19-2007, 11:58 PM
I had some wierdness in there, (very different length on tangents vs. normals was one) but that's not all of it.

It's better, but not perfect. Thanks for the help, k_szczech. :)

knackered
06-20-2007, 02:12 AM
Originally posted by Lord crc:
Might I ask why the eye vector should not be normalized? Is there a good and detailed doc about this somewhere? No, just my experience. It's most definitely true though. Try it out. If you normalise at the vertex level you're using a divide, which will chop off some precision, then you're interpolating a less precise value which you re-normalise at pixel level, which chops off more precision.

Komat
06-20-2007, 05:32 AM
Originally posted by Lord crc:
Might I ask why the eye vector should not be normalized?If you normalize the eye vector in vertex shader, you will get incorrect interpolation because important information is lost.

In the following image the A and B are two vertices of polygon (values bellow are corresponding x coordinates). E represents point which is slightly above the polygon (e.g. 0.1) and has x coordinate of zero.


E
A*************************B
-3 0 1If you interpolate between vectors A->E (3,0.1) and B->E (-1,0.1) the interpolated value in place with zero x coordinate (interpolation factor 0.75) will be (0,0.1) so it will correctly point in upwards direction towards the E. If you normalize both vectors before the interpolation you will interpolate from value that is ( 0.99944490,0.0333148) to value that is (-0.99503719,0.099503) and the interpolated value for the same interpolation factor (and thus in the same space on the polygon) will be (-0.4964166675,0.082955) which has certainly a wrong direction because has nonzero x component so it does not point towards the E.

knackered
06-20-2007, 06:48 AM
and it only becomes apparent when the eye point is very close to the polygon. You should get a perfect specular highlight no matter how far you are from it.

MarcusL
06-20-2007, 07:26 AM
Nice. That helped with those artifacts. I still get the jittery stuff though.

Could that be because I have an insufficiently tesselated mesh? (Various reasons for this at the moment)

knackered
06-20-2007, 07:40 AM
when you say jittery stuff, what do you mean?
if you're saying the whole specular reflection moves randomly about as the viewpoint moves, then maybe your object is too big or placed at too great a distance from the origin and you're seeing the limits of single precision floats.

MarcusL
06-20-2007, 07:52 AM
Sorry, I meant that not normalizing the eye vector helped with artifacts when going too close to the polygons. However, that seldom occurs in my app, I merely got that screesnhot to show the artifacts I'm really trying to fix better.

What I did mean was the "checkered" sawtooth artifacts. The highlight texturing in the pictures I've posted above isn't smooth, but rather jagged.

It seems as if the texture coordinates are making jumps at irregular intervals. These jumps are aligned with the UV mapping direction, but they do not align with texel value changes (plus, I do have bilinear enabled).

My object is rougly 0.2 in size, with a near/far at 0.001/0.5. (I'm dealing with a rather small scale here, but it's not that tiny, IMHO.)

knackered
06-20-2007, 08:40 AM
that is *really* small.
try scaling everything up 100 times and see what happens.

MarcusL
06-20-2007, 09:02 AM
Didn't help much. :(

However, all I could do was to put a transform on the top of everything, which essentially means that the clip-space coordinates are equivalent, and since I'm seeing errors at the fragment level, I find it hard to believe it should fix anything.

(I.e. it's floating point, so whether I'm at a scale of 1e2 or 1e-2 shouldn't matter, as long as I have enough significant digits I should get 0..1 when interpolating the polys)

I could also hack the vertex program to scale things up, but that's equivalent to changing the modelview matrix.

knackered
06-20-2007, 09:26 AM
Certainly a weird specular highlight you've got there. You say you've checked your texture filtering parameters?
Other than that, haven't a clue, sorry. Good luck.

MarcusL
06-20-2007, 09:31 AM
Truly. The jaggies don't match texels / texture resolution, so I doubt there's filtering involved.

Ok. Thanks for helping out anyway. :)

Lord crc
06-20-2007, 02:58 PM
Thanks for the normalization explanation.

zed
06-20-2007, 03:40 PM
are u using uncompressed textures?
ie not dxt1/3/5

MarcusL
06-21-2007, 12:18 AM
yup

Humus
06-27-2007, 04:46 PM
Have you tried using 16bit/channel textures? Keep in mind that most hardware interpolates 8bit/ch textures in 8bit, so even if you don't have any more precise data you could get better quality by upscaling it to a 16bit/ch texture.

MarcusL
06-28-2007, 01:56 AM
Hm. I can't get things better by shifting the format (will try harder soon). However, I think you're right in that I'm seeing these interpolation artifacts.

Especially, if I render gl_fragColor = fwidth(texture2d(normalMap, uv)) * 20, I get lines (indicating steps in texture) that match the discontinuities I'm seeing.

(Doing this confirms that I have linear interpolation and no compression, btw.)

Also, if I generate the normal procedurally (sin/cos noise based on uv), I don't get any artifacts.

MarcusL
06-28-2007, 05:20 AM
Here's with the fwidth(), so it's quite obvious that I'm not getting my normal map as nicely interpolated as I would want.

http://yar.nu/macke/bad_specular3.png

It might be that I'm doing the math in a bad way though, so that these problem are exaggerated.

How does one do tangent space specular environment mapping properly anyway? :) (I've tried to find some good examples that use GLSL, most most I find are from the good ole' register-combiner era.)

FWIW, I also tried with a very low-resolution normal map and I get these errors even there, so it's not related to the texel data.

knackered
06-28-2007, 06:16 AM
I've never had specular highlights like that with 8bit precision.

MarcusL
06-28-2007, 06:30 AM
Do you mean that your's look better or that you've never tried them?

Note that my speculars aren't L dot H based, but done via an environment map.

zed
06-29-2007, 01:47 AM
try turning off aniostropic filtering.
or set filtering to GL_NEAREST

zed
07-01-2007, 02:56 PM
FWIW im seeing the same thing in one of my shaders ( i always assumed it was due to the dxt compression but ill investigate further)

zed
07-04-2007, 12:56 AM
http://www.zedzeek.com/KEA_2007-07-02_2.jpg
heres what i have, though its due to the dxt

ZbuffeR
07-17-2007, 03:17 PM
MarcusL, this looks a lot like your problem :
http://www.opengl.org/discussion_boards/ubb/ultimatebb.php?ubb=get_topic;f=3;t=015323;p=1#0000 00

MarcusL
07-18-2007, 02:24 AM
Indeed. But I did try float textures and it didn't help much. I'll give it another try when I get back from vacation. :)