Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Ray Tracing Specular Highlights


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 AF1992   Members   -  Reputation: 106

Like
0Likes
Like

Posted 09 January 2014 - 12:28 PM

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.
 

Attached Thumbnails

  • Untitled.png

Edited by AF1992, 09 January 2014 - 12:50 PM.


Sponsor:

#2 Necrolis   Members   -  Reputation: 1413

Like
0Likes
Like

Posted 10 January 2014 - 03:01 AM

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, 10 January 2014 - 10:46 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS