<p>It sounds to me like you're slightly mixing up several concepts: how lighting due to point-light sources (Phong model) and lighting due to other objects in the scene (recursively raytraced reflection or transmission) combine with each other, as compared to how specular lighting combines with diffuse lighting and/or transmission (using the Fresnel equations).</p>

<p>A useful way to think about specular vs diffuse is that specular happens at the <em>surface</em> of the material while diffuse, transmissive, and refractive properties are associated with the <em>bulk</em> (the interior) of the material. So you can think of it as two layers composited together. Specular is on top: it reflects a portion of the incoming light, defined by the Fresnel equations, and any remaining light proceeds into the material to light the bulk.</p>

<p>The bulk will have some combination of scattering and absorption properties. Low scattering makes a transmissive material and high scattering makes a diffuse material (the diffuse color is due to scattering on a scale too small to see). Some materials have an intermediate degree of scattering too, but we can ignore that for simplicity and just treat it as a boolean: opaque (Lambert diffuse) or transmissive.</p>

<p>Meanwhile, each of these potential material components (specular, diffuse, and transmissive) has to deal with light arriving both from point light sources and from the rest of the scene. Physically, all light sources are just emissive objects that are part of the scene (area lights) but again for simplicity we often approximate by using point lights.</p>

<p>So, putting it all together, when lighting a surface:</p>

<ul>
<li>For each point light

<ol>
<li>Calculate Fresnel factor based on light vector</li>
<li>Accumulate Phong specular weighted by Fresnel reflectance</li>
<li>[if opaque] Accumulate Lambert diffuse weighted by Fresnel transmittance</li>
<li>[if transparent] Nothing; light goes through and isn't scattered into the camera.</li>
</ol></li>
<li>For reflecting the rest of the scene

<ol>
<li>Calculate Fresnel factor based on view vector</li>
<li>Cast a reflection ray and accumulate it weighted by Fresnel reflectance</li>
<li>[if opaque] Nothing. (Ideally, accumulate Lambert diffuse due to all objects visible in the normal hemisphere of the surface, i.e. indirect diffuse. However, for a basic recursive raytracer this isn't feasible; it requires path tracing, photon mapping, or suchlike.)</li>
<li>[if transparent] Cast a transmission ray and accumulate it weighted by Fresnel transmittance</li>
</ol></li>
</ul>

<p>Also, a note about your material properties: physically, not all of those properties are actually independent of each other. Ks, reflectivity, and refractive index are all the same thing. Ks is the same thing as reflectivity (literally the same value) because both control specular reflections; Ks is for point lights and reflectivity is for reflected environment rays, but it's the same physical mechanism for both. And Ks is determined in turn by the Fresnel equations based on refractive index (which can vary with wavelength).</p>

<p>So, strictly speaking, you should pick one of those variables and let it determine the others. For opaque materials where you don't really care about refractive index, you might let users set Ks (at normal incidence) and backsolve the Fresnel equations to get the appropriate refractive index from that. For transparent materials like glass you can let users set the refractive index and then let the Fresnel equations do their thing.</p>

<p>BTW, to get some physics background that will hopefully help clarify all this, I highly recommend Naty Hoffman's "Physics and Math of Shading" talk from the <a href="http://blog.selfshadow.com/publications/s2015-shading-course/" rel="nofollow">SIGGRAPH 2015 Physically-Based Shading Course</a>. It gives a quick and not-too-mathematical intro to the physical basis of things like diffuse and specular lighting, BRDFs and so on.</p>

CC BY-SA 3.0
2016-08-26T13:33:37.437Z
I have questions. **1.** In the "putting it all together" part you have a condition on whether an object is opaque or transparent. How do I determine this? Do I just add an `opacity` (or `transparency`) boolean value to my materials? **2.** If `ks` is for point lights and `reflectivity` for reflected rays, how do I deduct one from the other as you suggest? There seems to be an equation taking refractive indices and calculating ks from that. (I'm still having a hard time really understand your complete answer. Bear with me, please. Thank you. :)
kleinfreund
CC BY-SA 3.0
2016-08-27T07:20:57.477Z
@kleinfreund 1. Right, I'm suggesting a simplified material model where everything is either opaque (diffuse) or translucent, so you could use a boolean flag for this. 2. I'm saying `ks` and `reflectivity` are equal—literally the same value.
Nathan Reed
CC BY-SA 3.0
2016-08-30T15:25:04.603Z
I see. A `isTransparent` boolean attribute was added to our materials and I'm starting to replace the use of `reflectivity` with `ks`. The fresnel factor based on the light vector is calculated just as the one that is based on the view vector, correct? Instead of `fresnel_reflectance(v, n, rI1, rI2)` where `rI` are the refractive indices, I would do `fresnel_reflectance(l, n, rI1, rI2)` with the first parameter being the light vector instead of the view vector assuming the function is implemented correctly? (based on [this paper, page 6](http://stanford.io/1zco8xk))
kleinfreund
CC BY-SA 3.0
2016-08-30T16:17:38.717Z
Ah! For opaque objects which I don't have a *real* refractive index for, I can determine that by using `ks` a.k.a. `reflectivity` as the solution for `fresnel_reflectance(l, n, rI1, rI2)` to get `rI1`. As for your section *For reflecting the rest of the scene* I would just use `ks` instead of calculating the Fresnel factor based on the view vector?
kleinfreund
CC BY-SA 3.0
2016-08-30T16:35:07.997Z
Sorry for the confusion. I my last comment, instead of *For reflecting the rest of the scene* I meant *For each point light* and also the question concerns the light vector, not the view vector.
kleinfreund
CC BY-SA 3.0
2016-08-30T17:48:46.767Z
I will ask my questions in more detail in a [chat room](http://chat.stackexchange.com/rooms/44693/re-controlling-reflection-and-refraction-with-material-properties-in-ray-tracing) I created. If you want, you can join me there and help me out. This is not the right place for the comments.
kleinfreund
CC BY-SA 3.0
2016-08-23T00:39:27.270Z
2016-09-01T21:58:19.270Z
2016-09-01T21:58:19.270Z
Nathan Reed
<p>As tutors for this year’s programming languages course during our Computer Science studies we’re preparing for the students final project which is the implementation of a ray tracer. We’re implementing one on our own.</p>

<p>I’m having trouble understanding how the <a href="https://en.m.wikipedia.org/wiki/Phong_reflection_model" rel="nofollow noreferrer">phong reflection model</a> and reflection/refraction (<a href="http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf" rel="nofollow noreferrer">Reflections and Refractions in Ray Tracing</a>, Bram de Greve, 2006) can be used in conjunction.</p>

<p>Our materials currently have these properties:</p>

<ul>
<li>ka, kd, ks – ambient, diffuse and specular terms defined as RGB colors</li>
<li>shininess – Controlling specular highlights (0 ≤ shininess)</li>
<li>reflectivity – Controls degree of reflectivity (0 ≤ shininess ≤ 1, 0 means not reflective)</li>
<li>refractive index – Defines the refractive index of the material (0 ≤ refractive index)</li>
</ul>

<p>How I understand it, ray tracing as per phong reflection model with reflection and refraction works roughly like this:</p>

<ol>
<li>The material is (semi)reflective and (semi)transparent (is this a word). In this case reflectivity and transmittance can be calculated with Fresnel equations. However then the reflectivity as specified in the material is ignored. The final color is <code>reflectivity * trace(reflected_ray, depth + 1) + transmittance * trace(refracted_ray, depth + 1)</code>.</li>
<li>The material is (semi)reflective but not transparent. The reflective component is determined by further ray tracing. The other component is calculated via the equation from the Phong reflection model. Our ray tracer currently adds both components as in <code>(1 - reflectivity) * color + reflectivity * trace(reflected_ray, depth + 1)</code> where <code>color</code> is determined by the Phong equation.</li>
<li>The material is not reflective and not transparent. The Phong equation is used solely.</li>
</ol>

<p>How do I know whether a material is (semi)transparent? Do I add a boolean property which I use to determine whether I’m in case 1 or 2?</p>

CC BY-SA 3.0
2016-08-22T01:25:20.833Z
This isn't enough info to be a full answer obviously but the scattering function of the surface (aka BSDF) is what describes how light acts on a surface.  The rendering equation includes the BSDF and shows how this stuff all fits together.  I recommend checking out graphicscodex.com. It is 13 chapters long, where each chapter is only a few pages, but after you read through it (shouldn't take long) you'll have a deep understanding of this stuff.  A deeper read would be anything on "physically based rendering" specifically the book at pbrt.com.
Alan Wolfe
CC BY-SA 3.0
2016-08-22T01:37:08.940Z
Apologies, pbrt.org is the deeper read I mean.
Alan Wolfe
<raytracing>
Controlling reflection and refraction with material properties in ray tracing
kleinfreund
# Controlling reflection and refraction with material properties in ray tracing

(Asked by: username_0 on 2016-08-21)

As tutors for this year’s programming languages course during our Computer Science studies we’re preparing for the students final project which is the implementation of a ray tracer. We’re implementing one on our own.

I’m having trouble understanding how the [phong reflection model](https://en.m.wikipedia.org/wiki/Phong_reflection_model) and reflection/refraction ([Reflections and Refractions in Ray Tracing](http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf), Bram de Greve, 2006) can be used in conjunction.

Our materials currently have these properties:

* ka, kd, ks – ambient, diffuse and specular terms defined as RGB colors
* shininess – Controlling specular highlights (0 ≤ shininess)
* reflectivity – Controls degree of reflectivity (0 ≤ shininess ≤ 1, 0 means not reflective)
* refractive index – Defines the refractive index of the material (0 ≤ refractive index)

How I understand it, ray tracing as per phong reflection model with reflection and refraction works roughly like this:

1. The material is (semi)reflective and (semi)transparent (is this a word). In this case reflectivity and transmittance can be calculated with Fresnel equations. However then the reflectivity as specified in the material is ignored. The final color is `reflectivity * trace(reflected_ray, depth + 1) + transmittance * trace(refracted_ray, depth + 1)`.
2. The material is (semi)reflective but not transparent. The reflective component is determined by further ray tracing. The other component is calculated via the equation from the Phong reflection model. Our ray tracer currently adds both components as in `(1 - reflectivity) * color + reflectivity * trace(reflected_ray, depth + 1)` where `color` is determined by the Phong equation.
3. The material is not reflective and not transparent. The Phong equation is used solely.

How do I know whether a material is (semi)transparent? Do I add a boolean property which I use to determine whether I’m in case 1 or 2?

**Tags:** raytracing

> **Comments:**
> **username_1**: This isn't enough info to be a full answer obviously but the scattering function of the surface (aka BSDF) is what describes how light acts on a surface.  The rendering equation includes the BSDF and shows how this stuff all fits together.  I recommend checking out graphicscodex.com. It is 13 chapters long, where each chapter is only a few pages, but after you read through it (shouldn't take long) you'll have a deep understanding of this stuff.  A deeper read would be anything on "physically based rendering" specifically the book at pbrt.com.  
> **username_1**: Apologies, pbrt.org is the deeper read I mean.  

---

## 1 answers

### Answer by username_2 on 2016-08-23. Score: 4 | Accepted answer

It sounds to me like you're slightly mixing up several concepts: how lighting due to point-light sources (Phong model) and lighting due to other objects in the scene (recursively raytraced reflection or transmission) combine with each other, as compared to how specular lighting combines with diffuse lighting and/or transmission (using the Fresnel equations).

A useful way to think about specular vs diffuse is that specular happens at the *surface* of the material while diffuse, transmissive, and refractive properties are associated with the *bulk* (the interior) of the material. So you can think of it as two layers composited together. Specular is on top: it reflects a portion of the incoming light, defined by the Fresnel equations, and any remaining light proceeds into the material to light the bulk.

The bulk will have some combination of scattering and absorption properties. Low scattering makes a transmissive material and high scattering makes a diffuse material (the diffuse color is due to scattering on a scale too small to see). Some materials have an intermediate degree of scattering too, but we can ignore that for simplicity and just treat it as a boolean: opaque (Lambert diffuse) or transmissive.

Meanwhile, each of these potential material components (specular, diffuse, and transmissive) has to deal with light arriving both from point light sources and from the rest of the scene. Physically, all light sources are just emissive objects that are part of the scene (area lights) but again for simplicity we often approximate by using point lights.

So, putting it all together, when lighting a surface:

* For each point light
  1. Calculate Fresnel factor based on light vector
  2. Accumulate Phong specular weighted by Fresnel reflectance
  3. [if opaque] Accumulate Lambert diffuse weighted by Fresnel transmittance
  4. [if transparent] Nothing; light goes through and isn't scattered into the camera.
* For reflecting the rest of the scene
  1. Calculate Fresnel factor based on view vector
  2. Cast a reflection ray and accumulate it weighted by Fresnel reflectance
  3. [if opaque] Nothing. (Ideally, accumulate Lambert diffuse due to all objects visible in the normal hemisphere of the surface, i.e. indirect diffuse. However, for a basic recursive raytracer this isn't feasible; it requires path tracing, photon mapping, or suchlike.)
  4. [if transparent] Cast a transmission ray and accumulate it weighted by Fresnel transmittance

Also, a note about your material properties: physically, not all of those properties are actually independent of each other. Ks, reflectivity, and refractive index are all the same thing. Ks is the same thing as reflectivity (literally the same value) because both control specular reflections; Ks is for point lights and reflectivity is for reflected environment rays, but it's the same physical mechanism for both. And Ks is determined in turn by the Fresnel equations based on refractive index (which can vary with wavelength).

So, strictly speaking, you should pick one of those variables and let it determine the others. For opaque materials where you don't really care about refractive index, you might let users set Ks (at normal incidence) and backsolve the Fresnel equations to get the appropriate refractive index from that. For transparent materials like glass you can let users set the refractive index and then let the Fresnel equations do their thing.

BTW, to get some physics background that will hopefully help clarify all this, I highly recommend Naty Hoffman's "Physics and Math of Shading" talk from the [SIGGRAPH 2015 Physically-Based Shading Course](http://blog.selfshadow.com/publications/s2015-shading-course/). It gives a quick and not-too-mathematical intro to the physical basis of things like diffuse and specular lighting, BRDFs and so on.

> **Comments:**
> **username_0**: I have questions. **1.** In the "putting it all together" part you have a condition on whether an object is opaque or transparent. How do I determine this? Do I just add an `opacity` (or `transparency`) boolean value to my materials? **2.** If `ks` is for point lights and `reflectivity` for reflected rays, how do I deduct one from the other as you suggest? There seems to be an equation taking refractive indices and calculating ks from that. (I'm still having a hard time really understand your complete answer. Bear with me, please. Thank you. :)  
> **username_2**: @username_0 1. Right, I'm suggesting a simplified material model where everything is either opaque (diffuse) or translucent, so you could use a boolean flag for this. 2. I'm saying `ks` and `reflectivity` are equal—literally the same value.  
> **username_0**: I see. A `isTransparent` boolean attribute was added to our materials and I'm starting to replace the use of `reflectivity` with `ks`. The fresnel factor based on the light vector is calculated just as the one that is based on the view vector, correct? Instead of `fresnel_reflectance(v, n, rI1, rI2)` where `rI` are the refractive indices, I would do `fresnel_reflectance(l, n, rI1, rI2)` with the first parameter being the light vector instead of the view vector assuming the function is implemented correctly? (based on [this paper, page 6](http://stanford.io/1zco8xk))  
> **username_0**: Ah! For opaque objects which I don't have a *real* refractive index for, I can determine that by using `ks` a.k.a. `reflectivity` as the solution for `fresnel_reflectance(l, n, rI1, rI2)` to get `rI1`. As for your section *For reflecting the rest of the scene* I would just use `ks` instead of calculating the Fresnel factor based on the view vector?  
> **username_0**: Sorry for the confusion. I my last comment, instead of *For reflecting the rest of the scene* I meant *For each point light* and also the question concerns the light vector, not the view vector.  
> **username_0**: I will ask my questions in more detail in a [chat room](http://chat.stackexchange.com/rooms/44693/re-controlling-reflection-and-refraction-with-material-properties-in-ray-tracing) I created. If you want, you can join me there and help me out. This is not the right place for the comments.  

---

