<p>First of all, it is obvious that the normal vector of the upper surface is <span class="math-container">$(0,1,0)$</span>. But you can calculate it in the following way:</p>
<p>Given three points on a surface that are not aligned, you can calculate the normal direction. A very simple way is to use the cross product
<span class="math-container">$n = |(p1 - p0)$</span> x <span class="math-container">$(p2 - p0)|$</span> . With this formula you have to be careful which points you choose for <span class="math-container">$p0$</span>, <span class="math-container">$p1$</span> and <span class="math-container">$p2$</span>, because the cross product gives the orthogonal direction of the two input vectors according to the right-hand rule.</p>
<p>What is the right hand rule?
<a href="https://i.sstatic.net/RDvch.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/RDvch.png" alt="enter image description here" /></a></p>
<p><strong>Figure 1:</strong> Copied from <a href="https://en.wikipedia.org/wiki/Cross_product" rel="nofollow noreferrer">Wikipedia</a>. Shows how to easily remember the direction of the result of a cross product. (according to the right hand rule (Do <strong>NOT</strong> use the left hand ;P ))</p>
<p>So the result of <span class="math-container">$a$</span> x <span class="math-container">$b$</span> is not the same as <span class="math-container">$b$</span> x <span class="math-container">$a$</span>. So the cross product is not commutative! When calculating the normal vector, this should be taken into account! The length of the result of the cross product must be normalized.</p>
<p>Once we have the normal vectors of all 6 surfaces, we can proceed with the formula.
<span class="math-container">$$I_{RGB}(x) = \frac{2n(x) * I + 1}{3} * (1,0,1) + \frac{1-n(x)*I}{3} * (1,1,1)$$</span>
The question of the professor is not well defined, because the light source direction vary depending on the position of the point on surface... I would expect a well divined question from a university... The question could be defined when saying: the light source is infinity far away, which leeds to a directional light source. Or in case inside the course you only talked about directional light sources, they need to define the light cast direction. But when looking at the answer of the professor we see, that he/she expect you to use the direction of: <span class="math-container">$|lightPosition - centerCube|$</span>. Therefore we know the value of <span class="math-container">$I = (0,1,0)$</span> (where, <span class="math-container">$I$</span>: the <strong>unit</strong> light source direction,) (so this vectors is normalized). So if we substitute <span class="math-container">$I$</span> into the equation, we get:
<span class="math-container">$$I_{RGB}(x) = \frac{2n(x) * (0,1,0) + 1}{3} * (1,0,1) + \frac{1-n(x)*(0,1,0)}{3} * (1,1,1)$$</span></p>
<p><strong>Lets start with the top face:</strong>
<span class="math-container">$$n = (0,1,0)$$</span>
So placing the vector into the formular:
<span class="math-container">$$I_{RGB}(x) = \frac{2(0,1,0) * (0,1,0) + 1}{3} * (1,0,1) + \frac{1-(0,1,0)*(0,1,0)}{3} * (1,1,1)$$</span>
because <span class="math-container">$n(x)*I$</span> is the dot product, we can shorten the equation:
<span class="math-container">$$I_{RGB}(x) = \frac{2 + 1}{3} * (1,0,1) + \frac{1-1}{3} * (1,1,1)$$</span>
<span class="math-container">$$= 1 * (1,0,1) + 0 * (1,1,1)$$</span>
<span class="math-container">$$= (1,0,1)$$</span>
<span class="math-container">$(1,0,1)$</span> as RGB value is pure magenta.</p>
<p><strong>Lets continue with the bottom face:</strong>
<span class="math-container">$$n = (0,-1,0)$$</span>
So placing the vector into the formular:
<span class="math-container">$$I_{RGB}(x) = \frac{2(0,-1,0) * (0,1,0) + 1}{3} * (1,0,1) + \frac{1-(0,-1,0)*(0,1,0)}{3}*(1,1,1)$$</span>
Lets shorten it like above, gives you:
<span class="math-container">$$I_{RGB}(x) = \frac{-2 + 1}{3} * (1,0,1) + \frac{1-(-1)}{3}*(1,1,1)$$</span>
<span class="math-container">$$= (-\frac{1}{3},0,-\frac{1}{3}) + (\frac{2}{3},\frac{2}{3},\frac{2}{3})$$</span>
<span class="math-container">$$= (\frac{1}{3},\frac{2}{3},\frac{1}{3})$$</span></p>
<p><strong>Lets continue with the front face:</strong>
<span class="math-container">$$n = (0,0,1)$$</span>
So placing the vector into the formular:
<span class="math-container">$$I_{RGB}(x) = \frac{2(0,0,1) * (0,1,0) + 1}{3} * (1,0,1) + \frac{1-(0,0,1)*(0,1,0)}{3}*(1,1,1)$$</span>
Lets shorten it like above, gives you:
<span class="math-container">$$I_{RGB}(x) = \frac{0 + 1}{3} * (1,0,1) + \frac{1-0}{3}*(1,1,1)$$</span>
<span class="math-container">$$= (\frac{1}{3},0,\frac{1}{3}) + (\frac{1}{3},\frac{1}{3},\frac{1}{3})$$</span>
<span class="math-container">$$= (\frac{2}{3},\frac{1}{3},\frac{2}{3})$$</span></p>
<p>I'll stop here, for the other 3 faces it works the same way...</p>

CC BY-SA 4.0
2023-05-07T13:34:43.973Z
Excellent explanation, thank you!
LC796
CC BY-SA 4.0
2023-05-07T16:07:22.553Z
You are welcome :)
Thomas
CC BY-SA 4.0
2023-05-06T11:19:02.347Z
2023-05-07T07:34:50.507Z
2023-05-07T07:34:50.507Z
Thomas
<p><img src="https://i.gyazo.com/433790846f85cd7389f6c82e7cb111ea.png" alt="Text" /></p>
<p>The above problem is from a past Computer Graphics exam.</p>
<p>I am particularly confused about the explanation given, firstly for the top and bottom planes (since the direction of normal for top plane seems to be opposite the direction of I)</p>
<p>I tried to calculate the normal vectors, for the top plane for instance, by choosing two arbitrary points on the top plane ([1,1,1] and [0,1,0]), subtracting the two point vectors to get the vector lying across the plane. I then used the fact that the normal vector dot product with the vector lying across the plane equals zero to get a linear equation of the plane in the form Ax + By + Cz = 0, and used that to try to find the normal vector from the A,B,C components to multiply by the unit vector to yield the normal vector. This seems to be incorrect to me since it gave a normal vector defined as (1,0,1), which intuitively seems incorrect.</p>
<p>I do intuitively understand that the normal vector from the top plane will be positive in the y axis, and the normal vector from the bottom plane will be negative in the y axis. I can also see that a vector in the positive y axis would go towards the magenta light source so the plane that defines that normal vector would be illuminated. I do not understand how the specific values were computed.</p>
<p>If possible, an explanation of how they obtained values for the top plane, bottom plane, and 4 other planes would be appreciated. Thanks.</p>

CC BY-SA 4.0
2023-05-06T03:28:12.530Z
Why would you calculate normal vectors? The question tells you that the cube is aligned with the axes. Which means the normals are the standard unit vectors. For top plane, this is simply $(0,1,0)$. The Light source is at $(0,2,0)$ If you take the unit vector for this it's the same as $(0, 1, 0)$.  Just plug this in the formula and you'll get the color for the plane. Repeat the steps for other planes.
gallickgunner
CC BY-SA 4.0
2023-05-07T13:35:32.203Z
Thank you, that makes sense.
LC796
<rendering><mathematics><lighting><color><vectors>
With a light source at (0,2,0) and unit cube (from [-1,1]) using a given shading formula, how is the r,g,b for each surface calculated?
LC796
# With a light source at (0,2,0) and unit cube (from [-1,1]) using a given shading formula, how is the r,g,b for each surface calculated?

(Asked by: username_0 on 2023-05-05)

![Text](https://i.gyazo.com/433790846f85cd7389f6c82e7cb111ea.png)

The above problem is from a past Computer Graphics exam.

I am particularly confused about the explanation given, firstly for the top and bottom planes (since the direction of normal for top plane seems to be opposite the direction of I)

I tried to calculate the normal vectors, for the top plane for instance, by choosing two arbitrary points on the top plane ([1,1,1] and [0,1,0]), subtracting the two point vectors to get the vector lying across the plane. I then used the fact that the normal vector dot product with the vector lying across the plane equals zero to get a linear equation of the plane in the form Ax + By + Cz = 0, and used that to try to find the normal vector from the A,B,C components to multiply by the unit vector to yield the normal vector. This seems to be incorrect to me since it gave a normal vector defined as (1,0,1), which intuitively seems incorrect.

I do intuitively understand that the normal vector from the top plane will be positive in the y axis, and the normal vector from the bottom plane will be negative in the y axis. I can also see that a vector in the positive y axis would go towards the magenta light source so the plane that defines that normal vector would be illuminated. I do not understand how the specific values were computed.

If possible, an explanation of how they obtained values for the top plane, bottom plane, and 4 other planes would be appreciated. Thanks.

**Tags:** rendering, mathematics, lighting, color, vectors

> **Comments:**
> **username_1**: Why would you calculate normal vectors? The question tells you that the cube is aligned with the axes. Which means the normals are the standard unit vectors. For top plane, this is simply $(0,1,0)$. The Light source is at $(0,2,0)$ If you take the unit vector for this it's the same as $(0, 1, 0)$.  Just plug this in the formula and you'll get the color for the plane. Repeat the steps for other planes.  
> **username_0**: Thank you, that makes sense.  

---

## 1 answers

### Answer by username_2 on 2023-05-06. Score: 0 | Accepted answer

First of all, it is obvious that the normal vector of the upper surface is $(0,1,0)$. But you can calculate it in the following way:

Given three points on a surface that are not aligned, you can calculate the normal direction. A very simple way is to use the cross product
$n = |(p1 - p0)$ x $(p2 - p0)|$ . With this formula you have to be careful which points you choose for $p0$, $p1$ and $p2$, because the cross product gives the orthogonal direction of the two input vectors according to the right-hand rule.

What is the right hand rule?
[![enter image description here](https://i.sstatic.net/RDvch.png)](https://i.sstatic.net/RDvch.png)

**Figure 1:** Copied from [Wikipedia](https://en.wikipedia.org/wiki/Cross_product). Shows how to easily remember the direction of the result of a cross product. (according to the right hand rule (Do **NOT** use the left hand ;P ))

So the result of $a$ x $b$ is not the same as $b$ x $a$. So the cross product is not commutative! When calculating the normal vector, this should be taken into account! The length of the result of the cross product must be normalized.

Once we have the normal vectors of all 6 surfaces, we can proceed with the formula.
$$I\_{RGB}(x) = \frac{2n(x) \* I + 1}{3} \* (1,0,1) + \frac{1-n(x)\*I}{3} \* (1,1,1)$$
The question of the professor is not well defined, because the light source direction vary depending on the position of the point on surface... I would expect a well divined question from a university... The question could be defined when saying: the light source is infinity far away, which leeds to a directional light source. Or in case inside the course you only talked about directional light sources, they need to define the light cast direction. But when looking at the answer of the professor we see, that he/she expect you to use the direction of: $|lightPosition - centerCube|$. Therefore we know the value of $I = (0,1,0)$ (where, $I$: the **unit** light source direction,) (so this vectors is normalized). So if we substitute $I$ into the equation, we get:
$$I\_{RGB}(x) = \frac{2n(x) \* (0,1,0) + 1}{3} \* (1,0,1) + \frac{1-n(x)\*(0,1,0)}{3} \* (1,1,1)$$

**Lets start with the top face:**
$$n = (0,1,0)$$
So placing the vector into the formular:
$$I\_{RGB}(x) = \frac{2(0,1,0) \* (0,1,0) + 1}{3} \* (1,0,1) + \frac{1-(0,1,0)\*(0,1,0)}{3} \* (1,1,1)$$
because $n(x)\*I$ is the dot product, we can shorten the equation:
$$I\_{RGB}(x) = \frac{2 + 1}{3} \* (1,0,1) + \frac{1-1}{3} \* (1,1,1)$$
$$= 1 \* (1,0,1) + 0 \* (1,1,1)$$
$$= (1,0,1)$$
$(1,0,1)$ as RGB value is pure magenta.

**Lets continue with the bottom face:**
$$n = (0,-1,0)$$
So placing the vector into the formular:
$$I\_{RGB}(x) = \frac{2(0,-1,0) \* (0,1,0) + 1}{3} \* (1,0,1) + \frac{1-(0,-1,0)\*(0,1,0)}{3}\*(1,1,1)$$
Lets shorten it like above, gives you:
$$I\_{RGB}(x) = \frac{-2 + 1}{3} \* (1,0,1) + \frac{1-(-1)}{3}\*(1,1,1)$$
$$= (-\frac{1}{3},0,-\frac{1}{3}) + (\frac{2}{3},\frac{2}{3},\frac{2}{3})$$
$$= (\frac{1}{3},\frac{2}{3},\frac{1}{3})$$

**Lets continue with the front face:**
$$n = (0,0,1)$$
So placing the vector into the formular:
$$I\_{RGB}(x) = \frac{2(0,0,1) \* (0,1,0) + 1}{3} \* (1,0,1) + \frac{1-(0,0,1)\*(0,1,0)}{3}\*(1,1,1)$$
Lets shorten it like above, gives you:
$$I\_{RGB}(x) = \frac{0 + 1}{3} \* (1,0,1) + \frac{1-0}{3}\*(1,1,1)$$
$$= (\frac{1}{3},0,\frac{1}{3}) + (\frac{1}{3},\frac{1}{3},\frac{1}{3})$$
$$= (\frac{2}{3},\frac{1}{3},\frac{2}{3})$$

I'll stop here, for the other 3 faces it works the same way...

> **Comments:**
> **username_0**: Excellent explanation, thank you!  
> **username_2**: You are welcome :)  

---

