|
Post by mnemonic on Feb 8, 2019 17:14:32 GMT
Hello, I'm having a problem with the specular highlighting of the sphere at the end of chapter 06. I've spent the last 2 hours trying to track down the problem but I'm at a loss as to what is causing it. Any ideas on what might cause such an anomoly ?. The problem that I am experiencing is illustrated below. Regards.
|
|
|
Post by Jamis on Feb 8, 2019 17:48:32 GMT
Well, the good news is that the highlight is in the right place, and appears to be a reasonable size. I suspect the coloration issue is caused by the specular value being too large (perhaps by several magnitudes), so that when it is added to the base color, it causes the color to overflow. Can you share your code somewhere, at least the bit that computes the shading?
|
|
|
Post by mnemonic on Feb 8, 2019 17:55:02 GMT
Okay,
static void Lighting(vec3& clr, const material& m, const vec3& pt, const pointlight& light_source, const vec3& eye, const vec3& n) { vec3 effective_colour; vec3 ambient, diffuse, specular;
float light_dot_normal; vec3 light_dir;
HadamardProduct(effective_colour, m.colour, light_source.colour);
light_dir = light_source.pos - pt; Vec3Normalize(light_dir);
ambient = m.ambient * effective_colour; light_dot_normal = Vec3Dot(light_dir, n);
if(light_dot_normal < 0.0f) { diffuse.r = 0.0f; diffuse.g = 0.0f; diffuse.b = 0.0f; // black for diffuse specular.r = 0.0f; specular.g = 0.0f; specular.b = 0.0f; // black for specular } else { vec3 reflect; float reflect_dot_eye;
diffuse = (light_dot_normal * m.diffuse) * effective_colour; Vec3Reflect(reflect, -light_dir, n); reflect_dot_eye = Vec3Dot(reflect, eye);
if(reflect_dot_eye <= 0.0f) { specular.r = 0.0f; specular.g = 0.0f; specular.b = 0.0f; } else { float factor = powf(reflect_dot_eye, m.shininess); specular = (factor * m.specular) * light_source.colour; } } #ifdef _DEBUG Vec3Print("ambient", ambient); Vec3Print("diffuse", diffuse); Vec3Print("specular", specular); #endif // compute the overall colour at pt... clr = ambient + diffuse + specular; }
|
|
|
Post by Jamis on Feb 8, 2019 18:06:13 GMT
Thank you! Also, what values are you using for ambient, diffuse, and specular for that scene? And what is the output of those debug values when you define _DEBUG?
|
|
|
Post by mnemonic on Feb 8, 2019 20:55:39 GMT
If I am not mistaken these were quoted in the book... page 87
The debug output shows the ambient, diffuse and specular colour components for each pixel in the scene. Unfortunately I can't output that in here (or I don't think I can) as it is a ~5 mb text file. Is there anything specific I can go and look for ?
|
|
|
Post by Jamis on Feb 8, 2019 21:32:00 GMT
Yes, for debugging these kinds of issues, here's a technique that's been invaluable for me.
First, find the coordinates of a pixel in the output image that you want to inspect. In your case, just pick one somewhere in that odd specular highlight. Let's call that pixel (x, y).
Next, fiddle with your render() function so that it only actually renders the pixel at (x, y).
Now, your debug output should be significantly smaller, and should relate only to the production of that specific pixel. This can really help you zero in on issues like this, so you can see what's happening when things go wrong.
|
|
|
Post by mnemonic on Feb 9, 2019 10:59:47 GMT
I managed to find the first offending pixel and it looks like there was a clamping issue (or lack of one) in the routine I was using to set pixels. I noticed that the red and blue components of the input colour wrapped around back to zero when cast to an unsigned char. Putting a clamping routine for colour components outside of the range [0 .. 1] has now fixed the problem.
Thank you for the advice.
|
|
|
Post by Jamis on Feb 9, 2019 14:27:03 GMT
Excellent! I'm glad you were able to figure it out.
|
|
|
Post by andrew on Mar 14, 2019 13:20:45 GMT
Thanks for this! My image turned out exactly like yours, and within 30 seconds here, I found this fix, awesome!
|
|
|
Post by henriquedelima on Jun 7, 2019 11:40:41 GMT
I created this account because i had this exact same issue. Thanks for this post! Im really enjoying this book so far.
|
|
|
Post by comps4food on Jun 25, 2019 5:00:19 GMT
Fist I want to echo what others have said, awesome book!!!!
My issue was the highlight not showing at all
I zeroed in on the lighting function and particular the parts that computed the specular variable. I change the Shininess property, nothing, I swapped the power for the factor variable, adverse effect.
I then back tracked to other supporting variables.
Page 88
reflectv <- reflect(-lightv, normalv) I interrupted that as negate the light vector then compute the reflect related to the normal vector
When I remove the operation to negate the light vector it worked
reflectv <- reflect(lightv, normalv) from
var reflectVector = lightVector.Negate().Reflect(normalVector);
to
var reflectVector = lightVector.Reflect(normalVector);
|
|
|
Post by comps4food on Jun 25, 2019 6:14:34 GMT
Another solution and the test pass, and the highlight shows, but that might because there is no test coverage
on page 89
if (reflect_dot_eye <= 0)
{
specular <- black;
} changed to
if (reflect_dot_eye <= -1)
{
specular <- black;
} Don't know if the issue is with the calculation of reflectv and reflect_dot_eye or the test of the results.
|
|
|
Post by Jamis on Jun 26, 2019 4:07:10 GMT
comps4food, the "reflect_dot_eye" comparison needs to be relative to 0, and not -1, because it could be a fraction negative number (between 0 and 1). Any time that dot product is negative, it means the light vector is being reflected away from the eye, and will therefore not be visible (thus, black). Can you share how you're computing the lightv vector? It should be the light position, minus the point of intersection. If you have that flipped around you would probably be getting the results you're reporting.
|
|
|
Post by comps4food on Jun 26, 2019 20:44:22 GMT
Just finished chapter 7 awesome stuff, great to spend many hours learning and writing test, thinking will this work, then getting to Putting It Together and seeing an image!!! When the comparison relative to zero (<= 0) The logic passes the test, but fails to put a highlight on the image. Now I have tried swapping lightv with the comparison relative to 0. Test failed the image is an inverse, shadow on top and the highlight is missing. If I changed the comparison relative to -1 then I get the inverse image, shadow on top and the highlight on bottom, test don't pass. Which test covers the comparison relative to zero (<= 0) is true? I have tools that tell me if logic has test coverage, I get no coverage for when comparison to zero (<= 0) is true, comparison is always false. Based on what I'm reading I was thinking scenario: Lighting with the eye between light and surface, eye offset 45°. I get zero for specular but not because of the comparison but because of the calculations from computing the specular contribution. With that scenario lightv is (0, 0, -1), light_dot_normal is 1, normalv is (0, 0, -1), reflectv is (0, 0, -1), reflective_dot_eye 0.707106 I guess if I know if the individual variables used are correct and/or what the value should be. The following code is is in a Material class that works and passes test using comparison of reflect_dot_eye <= -1. Material.cspublic RtColor Lighting(PointLight light, RtPoint point, RtVector eyeVector, RtVector normalVector)
{ var effectiveColor = Color * light.Intensity; var lightVector = (light.Position - point).Normalized(); var ambient = effectiveColor * Ambient; var lightDotNormal = lightVector.Dot(normalVector);
RtColor diffuse; RtColor specular;
if (lightDotNormal < 0) { diffuse = RtColor.Black; specular = RtColor.Black; } else { diffuse = effectiveColor * Diffuse * lightDotNormal;
var reflectVector = lightVector.Negate().Reflect(normalVector); var reflectDotEye = reflectVector.Dot(eyeVector);
if (reflectDotEye <= -1) { specular = RtColor.Black; } else { var factor = Math.Pow(reflectDotEye, Shininess); specular = light.Intensity * Specular * factor; } }
var results = ambient + diffuse + specular; return results;
}
|
|
|
Post by comps4food on Jun 27, 2019 15:52:45 GMT
Now I'm totally baffled, I now know how to put images on the board by using GitHub. I was going to post some proofs of what I'm seeing. I changed the code back to comparison relative to 0, and guess what it worked!!!! I will keep hacking away until I find the bug and update later. Although I can't prove it now, I know it was not there, I swear . I will check out some previous commits I have to GitHub and compare code to find the issue. Thank You
|
|