<p>I am going to assume that you have the normals available for each point and that your vertex pass don't do this for you. For every pixel you will have an intersection point <span class="math-container">$p$</span> that exists on the nearest triangle surface defined by 3 points <span class="math-container">$p_i$</span>, <span class="math-container">$p_j$</span> and <span class="math-container">$p_k$</span> with corresponding normals <span class="math-container">$n_i$</span>, <span class="math-container">$n_j$</span> and <span class="math-container">$n_k$</span>. Phong shading is the normalized interpolated normal, which can be found by:</p>
<ol>
<li><p>Computing the barycentric coordinates of <span class="math-container">$p$</span>, (look at (<a href="https://github.com/Jojendersie/gpugi/blob/5d18526c864bbf09baca02bfab6bcec97b7e1210/gpugi/shader/intersectiontests.glsl#L63" rel="nofollow noreferrer">https://github.com/Jojendersie/gpugi/blob/5d18526c864bbf09baca02bfab6bcec97b7e1210/gpugi/shader/intersectiontests.glsl#L63</a>) in the function &quot;IntersectTriangle&quot;). Note that the order is important such that your coordinate values <span class="math-container">$u$</span>, <span class="math-container">$v$</span> and <span class="math-container">$w$</span> has the same order as your points.</p>
</li>
<li><p>Interpolating and normalizing: <span class="math-container">$\frac{un_i + vn_j + wn_k}{|| un_i + vn_j + wn_k ||}$</span>.</p>
</li>
<li><p>???</p>
</li>
<li><p>Profit!</p>
</li>
</ol>

CC BY-SA 4.0
2023-02-12T10:26:06.740Z
2023-02-12T10:26:06.740Z
Mathis
<p>EDIT: I misunderstood your question, so please disregard this answer.</p>

<p>The Phong shading model assumes a surface normal <span class="math-container">$\mathbf{n}$</span> at the shading point <span class="math-container">$\mathbf{x}$</span> but it is independent of its <a href="https://computergraphics.stackexchange.com/questions/5486/whats-the-difference-between-geometric-surface-normal-and-shading-surface-norma">type</a>, so either the shading normal or the geometric normal will work. Moreover, this reflectance model requires an incoming and outgoing directions. In that sense, you can't just "interpolate the normals" to get the desired effect since you need to take into account the positions of the camera and the light, as well as the roughness parameters.</p>

<p>The (normalized) Phong BRDF is a sum of a diffuse and specular term:</p>

<p><span class="math-container">$$
f_r(\mathbf{x},\omega_i,\omega_o) = \frac{\rho_d}{\pi} + \rho_s \frac{n+2}{2\pi}\max(0,\cos^n\alpha),
$$</span>
where</p>

<ul>
<li><span class="math-container">$\rho_d$</span> is the <em>diffuse albedo</em>, i.e. the fraction of the incoming energy that is reflected diffusely,</li>
<li><span class="math-container">$\rho_s$</span> is the <em>specular albedo</em>, i.e. the fraction of the incoming energy that is reflected specularly,</li>
<li><span class="math-container">$n$</span> is the <em>Phong exponent</em>; higher values yield more mirror-like specular reflection, and</li>
<li><span class="math-container">$\alpha$</span> is the <em>angle</em> between the perfect specular reflection direction <span class="math-container">$\omega_r$</span> and the outgoing direction <span class="math-container">$\omega_o$</span>.</li>
</ul>

<p>You can compute <span class="math-container">$\omega_r$</span> from the surface normal <span class="math-container">$\mathbf{n}$</span> and the lighting direction <span class="math-container">$\omega_i$</span>, which is just</p>

<p><span class="math-container">$$
\omega_r = 2\mathbf{n}(\mathbf{n}\cdot\omega_i) - \omega_i.
$$</span></p>

<p>Typically you pick the albedos such that <span class="math-container">$\rho_d + \rho_s \leqslant 1$</span> to conserve energy.</p>

CC BY-SA 4.0
2019-09-30T08:05:59.597Z
I don't see how this formula makes creased edges appear to smoothly blend into its neighbours, aka phong normal interpolation (I'm not confused with gouraud interpolation)
AnnoyinC
CC BY-SA 4.0
2019-09-30T09:16:54.557Z
He is talking about Phon shading, not the Phong brdf: https://en.m.wikipedia.org/wiki/Phong_shading
Consider editing your answer as it is currently misleading.
lightxbulb
CC BY-SA 4.0
2019-09-30T02:06:06.230Z
2019-09-30T22:52:14.493Z
2019-09-30T22:52:14.493Z
Hubble
<p>Let's suppose we have an object consisting of only 3d points, and triangle faces that each take a subset of these 3d points. How can I interpolate the normal vectors to get that Phong smooth shading? The actual algorithm seems impossible to find yet everybody is doing it.</p>

<p>There is one post on here, but I still have no idea how to implement it: <a href="https://computergraphics.stackexchange.com/questions/5097/normal-interpolation-for-phong-shading">Normal Interpolation for Phong shading</a></p>

<p>Also, it seems wrong to me that this supposed formula only takes in account two normals. What if you have a triangle surrounded by 3 other triangles? Then how do you blend the normals?</p>

CC BY-SA 4.0
2019-09-30T09:23:03.297Z
Refer to: "Weights for Computing Vertex Normals from Facet Normals". Generally you walk over all triangles that share a vertex and use all of the adjacent triangle  normals to compute the normal at that vertex. To achieve this you can walk over all triangles and add their normals to their 3 adjacent vertices (with a weight). More generally you can create a structure which for each vertex lets you iterate over the triangles that share it.
lightxbulb
CC BY-SA 4.0
2019-09-30T16:07:38.807Z
@lightxbulb exactly what I was looking for, thank you! I can't upvote comments yet but otherwise I would have.
AnnoyinC
CC BY-SA 4.0
2020-02-28T04:44:56.270Z
Also worth pointing out the interpolated normals need renormalising per pixel.
PaulHK
<shading>
What is the algorithm used for phong shading normal interpolation?
AnnoyinC
# What is the algorithm used for phong shading normal interpolation?

(Asked by: username_0 on 2019-09-29)

Let's suppose we have an object consisting of only 3d points, and triangle faces that each take a subset of these 3d points. How can I interpolate the normal vectors to get that Phong smooth shading? The actual algorithm seems impossible to find yet everybody is doing it.

There is one post on here, but I still have no idea how to implement it: [Normal Interpolation for Phong shading](https://computergraphics.stackexchange.com/questions/5097/normal-interpolation-for-phong-shading)

Also, it seems wrong to me that this supposed formula only takes in account two normals. What if you have a triangle surrounded by 3 other triangles? Then how do you blend the normals?

**Tags:** shading

> **Comments:**
> **username_1**: Refer to: "Weights for Computing Vertex Normals from Facet Normals". Generally you walk over all triangles that share a vertex and use all of the adjacent triangle  normals to compute the normal at that vertex. To achieve this you can walk over all triangles and add their normals to their 3 adjacent vertices (with a weight). More generally you can create a structure which for each vertex lets you iterate over the triangles that share it.  
> **username_0**: @username_1 exactly what I was looking for, thank you! I can't upvote comments yet but otherwise I would have.  
> **username_2**: Also worth pointing out the interpolated normals need renormalising per pixel.  

---

## 2 answers

### Answer by username_3 on 2023-02-12. Score: 1

I am going to assume that you have the normals available for each point and that your vertex pass don't do this for you. For every pixel you will have an intersection point $p$ that exists on the nearest triangle surface defined by 3 points $p\_i$, $p\_j$ and $p\_k$ with corresponding normals $n\_i$, $n\_j$ and $n\_k$. Phong shading is the normalized interpolated normal, which can be found by:

1. Computing the barycentric coordinates of $p$, (look at (<https://github.com/Jojendersie/gpugi/blob/5d18526c864bbf09baca02bfab6bcec97b7e1210/gpugi/shader/intersectiontests.glsl#L63>) in the function "IntersectTriangle"). Note that the order is important such that your coordinate values $u$, $v$ and $w$ has the same order as your points.
2. Interpolating and normalizing: $\frac{un\_i + vn\_j + wn\_k}{|| un\_i + vn\_j + wn\_k ||}$.
3. ???
4. Profit!


---

### Answer by username_4 on 2019-09-30. Score: 0

EDIT: I misunderstood your question, so please disregard this answer.

The Phong shading model assumes a surface normal $\mathbf{n}$ at the shading point $\mathbf{x}$ but it is independent of its [type](https://computergraphics.stackexchange.com/questions/5486/whats-the-difference-between-geometric-surface-normal-and-shading-surface-norma), so either the shading normal or the geometric normal will work. Moreover, this reflectance model requires an incoming and outgoing directions. In that sense, you can't just "interpolate the normals" to get the desired effect since you need to take into account the positions of the camera and the light, as well as the roughness parameters.

The (normalized) Phong BRDF is a sum of a diffuse and specular term:

$$
f\_r(\mathbf{x},\omega\_i,\omega\_o) = \frac{\rho\_d}{\pi} + \rho\_s \frac{n+2}{2\pi}\max(0,\cos^n\alpha),
$$
where

* $\rho\_d$ is the *diffuse albedo*, i.e. the fraction of the incoming energy that is reflected diffusely,
* $\rho\_s$ is the *specular albedo*, i.e. the fraction of the incoming energy that is reflected specularly,
* $n$ is the *Phong exponent*; higher values yield more mirror-like specular reflection, and
* $\alpha$ is the *angle* between the perfect specular reflection direction $\omega\_r$ and the outgoing direction $\omega\_o$.

You can compute $\omega\_r$ from the surface normal $\mathbf{n}$ and the lighting direction $\omega\_i$, which is just

$$
\omega\_r = 2\mathbf{n}(\mathbf{n}\cdot\omega\_i) - \omega\_i.
$$

Typically you pick the albedos such that $\rho\_d + \rho\_s \leqslant 1$ to conserve energy.

> **Comments:**
> **username_0**: I don't see how this formula makes creased edges appear to smoothly blend into its neighbours, aka phong normal interpolation (I'm not confused with gouraud interpolation)  
> **username_1**: He is talking about Phon shading, not the Phong brdf: https://en.m.wikipedia.org/wiki/Phong_shading Consider editing your answer as it is currently misleading.  

---

