Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Ray Tracing Specular Highlights

This topic is 2107 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'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.

Edited by AF1992

Share this post

Link to post
Share on other sites

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());
Edited by Necrolis

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!