Hello, I've been working on a Ray Tracer, in OpenGL using C++. I'm fairly new to both of these things so I'm struggling with certain elements of the shading, I feel like the mathematics used are correct in my code and I'm simply not combining the Ambient, specular and Diffuse properly.
My ambient and diffuse shading works well, When specular is added I either get strange rings of color, or no change at all. I had this problem initially with diffuse shading and it was overcome by clamping the color values however I've been stuck with this for a week or two now and made very little progress, I could use the insight of someone more experienced.
//Specular Lighting Calculation-------------------------------------------------------------
//KsSx(R dot V)^n ///// R = Reflectionvector ///// V = view vector
//Values: ///// Ks = specular coefficient ///// Sx = specular color
//------------------------------------------------------------------------------------------
//Code, sorry I'm not sure how to properly format this
TVector V = UnitVector;
float myDot = -V.dotProduct(NormalIntersect);
float myLen = 2.0 * myDot;
TVector tempNormal = NormalIntersect.Multiply(myLen);
TVector R = tempNormal.Addition(V);
R = R.normalize();
float mySpec = max(-R.dotProduct(V),0);
Color Specular(SpecularColor.getcolorRed() * mySpec,
SpecularColor.getcolorGreen() * mySpec,
SpecularColor.getcolorBlue() * mySpec, 0);
//Set Pixel Final Color
Color pixelFinalColor(color.getcolorRed() * (Diffuse + ka + Specular.getcolorRed()),
color.getcolorGreen() * (Diffuse + ka + Specular.getcolorGreen()),
color.getcolorBlue() * (Diffuse + ka + Specular.getcolorBlue()),1);
//Code End---------------------------------------------------------------------------------------------------
The attached picture shows my diffuse shading, and then the problem the addition of specular causes. Thanks in advance to anyone that can provide a reason as to why this does not work correctly, Sorry if I've missed something simple. Thanks for your time.
Ray Tracing Specular Highlights
Your reflect vector looks odd to me (but then again the one I used is inverted from all the documentation I came across, which correctly has r = 2(n dot l)n - l), this is the code I used in one of my ray tracers for diffuse + specular, maybe it gives you some hints:
if(!World->GetGeometry()->Occlude(Ray(i + l * 0.001f,l),light_dist))
{
float nl = n.dot(l);
if(nl > 0)
c += lc * vColor * pMaterial->GetDiffuse() * nl;
Vector r = l - (n * 2 * nl);
float rv = ray.GetDirection().dot(r);
if(rv > 0)
c += lc * vColor * pMaterial->GetSpecular() * std::powf(rv,pMaterial->GetSpecularPower());
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement