Discuss Scratch

CodingBio
Scratcher
37 posts

Flat Shading in 3D Rendering

Hello.

I have created a 3D rendering engine, and as you can probably tell it is missing lighting.

The rendering engine can be found here.

I am trying to implement flat shading, but lack the mathematical skill. If anyone can remix the project and implement it, find a good example project I can use, or create an example project I can use, I will be very grateful. You will be credited if the lighting is implemented.

Thank you!
awesome-llama
Scratcher
1000+ posts

Flat Shading in 3D Rendering

Note: I haven't made a 3D rendering engine myself, but I do have experience in 3D software and rendering that is relevant.

First, how do you want to light the model?
If we are not considering shadows being cast on to other objects, then it shouldn't be too hard. Maybe you can test out simple methods first and then try and tackle some of the more complicated things.

The simplest way is to just make the entire ceiling the light source. You could determine the direction a face's normal is and depending on how much it faces the ceiling, set it to light up or not. The faces that don't get light should be rendered darker.

Example in Blender


Now, how do we calculate it?

The first step is to get the face normal as a unit vector (vector of length 1).
The lightest faces should be directly up. That would be a vector directly up (e.g. 0,1,0 if you are using Y up coordinates).

With these two vectors, we find the dot product (basically multiply them together). The single value you get out of it is how lit up the face should be. Since we are using unit vectors, the highest output possible is 1. If the calculation returns around 0, then the face is facing sideways. Negative and it is underneath.
CodingBio
Scratcher
37 posts

Flat Shading in 3D Rendering

awesome-llama wrote:

The first step is to get the face normal as a unit vector (vector of length 1).

How do I calculate the face normal?

Last edited by CodingBio (Feb. 26, 2021 16:40:10)

Math-Coding-Scratch
Scratcher
7 posts

Flat Shading in 3D Rendering

This may help? It is in openGL though. I guess you could still convert it. https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal
hiPeeps124816
Scratcher
500+ posts

Flat Shading in 3D Rendering

if u make the whole ceiling the light source u might wanna make a skybox so it doesnt look weird

Last edited by hiPeeps124816 (Feb. 26, 2021 19:32:40)

hiPeeps124816
Scratcher
500+ posts

Flat Shading in 3D Rendering

Math-Coding-Scratch wrote:

This may help? It is in openGL though. I guess you could still convert it. https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal
this might b a start for converting it
define CalculateSurfaceNormal (Input Triangle) Returns Vector
Set [Vector U v] to ((Triangle.p2) - (Triangle.p1))
Set [Vector V v] to ((Triangle.p3) -(Triangle.p1))
Set [Normal.x v] to (((U.y) * (V.z)) - ((U.z) * (V.y)
Set [Normal.y v] to (((U.z) * (V.x)) - ((U.x) * (V.z))
Set [Normal.z v] to (((U.x) * (V.y)) - ((U.y) * (V.x)))
CodingBio
Scratcher
37 posts

Flat Shading in 3D Rendering

Thanks guys, I implemented it.

In a different engine, that is.

Last edited by CodingBio (Feb. 27, 2021 14:39:50)

Powered by DjangoBB