• Advertisement
Sign in to follow this  

Ray Tracer Phong Help

This topic is 3085 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

I am currently working on ray tracer with spheres. I currently use diffuse shading, but having trouble with the Phong shading aspect. I've looked at many tutorials and I've noticed that what Im doing is Im just adding the Phong aspect to the equation. Here is what I have for diffuse shading. Can someone help me with adding the Phong aspect? package ray.shader; import java.util.List; import ray.math.*; import ray.surface.Surface; import ray.*; /** * A Lambertian material scatters light equally in all directions. * * @author ags */ public class Lambertian implements Shader { // These fields are read in from the input file. /** The color of the surface. */ protected final Color diffuseColor = new Color(1, 1, 1); public void setDiffuseColor (Color inDiffuseColor) { diffuseColor.set(inDiffuseColor); } public Color shade (Ray ray, Scene scene, Surface surface) { double[] tValues = surface.intersects(ray); double iSect = 0.0; if (tValues == null) return new Color(); else if (tValues.length > 2) return diffuseColor; else if (tValues.length == 2) iSect = Math.min(tValues[0], tValues[1]); else iSect = tValues[0]; Point3 hitPoint = ray.evaluate(iSect); Vector3 normal = surface.getNormal(hitPoint); List<Light> lights = scene.getLights(); double scale = 0.0; for (int i=0;i<lights.size();i++) { Light currLight = lights.get(i); Vector3 lightRay = new Vector3(); lightRay.sub(currLight.position, hitPoint); lightRay.normalize(); scale += Math.max(0.0, normal.dot(lightRay)); } diffuseColor.scale(scale); return diffuseColor; } }

Share this post


Link to post
Share on other sites
Advertisement
The phong component is (R.dot(V))^n, where
  • V is the vector from the point on the surface to the viewer
  • R is the light vector (lightRay in your code) reflected across the normal
  • n is a parameter you can tweak, generally regarded as a "shininess" or "roughness" exponent. I believe as n gets larger, the specular highlight gets smaller.

You can just add this term to your diffuse term, possibly weighting one more than the other if you'd like. You could also weight on a per-object basis. Did I miss your question completely, or did that help? :)

[Edited by - aryx on September 9, 2009 12:23:45 AM]

Share this post


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

  • Advertisement