How to find N(gradient of plane) and L(direction to light) for implementing shading in polygon filling in Computer Graphics?

97 Views Asked by At

I have $3$ points $(x_1,y_1,z_1)$, $(x_2,y_2,z_2)$, $(x_3,y_3,z_3)$ of the $3D$ plane and light source $(x_s,y_s,z_s)$.

enter image description here

Now, how can I find out the $N$ vector and $L$ vector?

1

There are 1 best solutions below

3
On

Suppose, $P_1(x_1,y_1,z_1)$, $P_2(x_2,y_2,z_2)$, $P_3(x_3,y_3,z_3)$ and light source $S(x_s,y_s,z_s)$ are give. Then, $N$ is normal vector of cross product of $(P_2-P1)$ and $(P_3-P_1)$.

$(P_2-P1)=(x_2-x_1)\hat{i}+(y_2-y_1)\hat{j}+(z_2-z_1)\hat{k}$

$(P_3-P_1)=(x_3-x_1)\hat{i}+(y_3-y_1)\hat{j}+(z_3-z_1)\hat{k}$

$\begin{vmatrix}\hat{i} & \hat{j} & \hat{k} \\ x_2-x_1 & y_2-y_1 & z_2-z_1 \\x_3-x_1 & y_3-y_1 & z_3-z_1 \end{vmatrix}=N'$

$ \hat{i} \{(y_2-y_1)(z_3-z_1)-(y_3-y_1)(z_2-z_1)\}-\hat{j}\{ (x_2-x_1)(z_3-z_1)-(x_3-x_1)(z_2-z_1)\}+\hat{k}\{(x_2-x_1)(y_3-y_1)-(x_3-x_1)(y_2-y_1)\}=N'$

$ \hat{i} \{(y_2-y_1)(z_3-z_1)-(y_3-y_1)(z_2-z_1)\}+\hat{j}\{ (x_3-x_1)(z_2-z_1)-(x_2-x_1)(z_3-z_1)\}+\hat{k}\{(x_2-x_1)(y_3-y_1)-(x_3-x_1)(y_2-y_1)\}=N'$

$N=\frac{N'}{||N'||}$

Now, if $P_n(x,y,z)$ is a new point on the given plane then,(If you assume that brightness of each pixel of the polygon is same then you can consider this point as center of the polygon)
$L'=(S-P_n)$
$L'=(x_s-x)\hat{i}+(y_s-y)\hat{j}+(z_s-z)\hat{k}$
$L=\frac{L'}{||L'||}$
$N.L=N_xL_x+N_yL_y+N_zL_z$
$N.L=\{(y_2-y_1)(z_3-z_1)-(y_3-y_1)(z_2-z_1)\}(x_s-x)+\{(x_3-x_1)(z_2-z_1)-(x_2-x_1)(z_3-z_1)\}(y_s-y)+\{(x_2-x_1)(y_3-y_1)-(x_3-x_1)(y_2-y_1)\}(z_s-z)$

$N.L$ is needed for shading/brightness of a pixel(e.g. glcolor3f(N.L, N.L, N.L))