DEV Community

Cover image for Rasterizer Project - Part: BASIC_END
NoticeableSmeh
NoticeableSmeh

Posted on

Rasterizer Project - Part: BASIC_END

Hello folks lets continue working on this rasterizer, lets get the rasterization going and then were putting this project on hold for a while. I will be making the DoingOpenGL series after this part were I will get a graphics demo going for OpenGL stay tuned for that but heres what were doing today.

So to avoid going too into detail and just making this just completely unreadable I will be trying to keep my explanations more concise and quicker with fewer images just so we can get through this stuff together folks

Here is our triangle fill function, this essentially takes coordinates for 3 different points and fills the area between them. This is finally were barycentric coordinates finally get more useful but more on that in just a second.

Anyways we stard by defining our max and minimum on our 2D plain view for both the X and Y coordinate, this is simply just to ensure that we never paint outside the actually triangle.

So to figure out if a pixel should be painted or not (in other words if its inside the triangle) You use barycentric weights. These are essentially three numbers that symbolize how close that pixel is to each vertex of the triangle hence why we have three numbers. Inside the triangle those numbers always stay between 0 and 1 so we are essentially just making sure that none one of them are negative and all of them added together sum up to 1.
That is what this if statement checks here

For the more meaty math lets take a look more here

denom here is jus tone big number that represents the triangles size and if its zero that means the points are all on one line, nothing to fill essentially. The way to calculate it is from the differences between the triangles points, how far apart they are on each respective axis. Were counting the area here folks essentially we just dont care about dividing it with 2 because it doesnt really serve a purpose in knowing the exact area size, we care about ratios.

So in our code we define our direction of calculating the side that is inside (good side) or outside (bad side) from clockwise direction, which is different from how a lot of other program use it. This however is still fine but this means that if we were to orient our Edges upright it would mean the right side is the side that is inside the triangle and the left side is the one that is outside.

Phew, I am not very good at math but thats my best attempt at explaining this for you as simple as I could.

Time to break down this function. This function has my favorite type of for loop (yay) and it loops through our face count (our amount of triangles inside of our model.

Each face in our model has three vertices, of course, and we then project those vertices into a 2D view, since we’re currently rendering in 2D space. Although the model itself exists in 3D, we’re essentially flattening it onto the screen, turning it into a 2D representation we can draw.

We retrieve each vertex based on the current face index in our loop (since faceCount gives us how many total faces we have). From that index, we can access the first vertex (our A coordinate), then the second (B), and finally the third (C).

Note: Remember that in programming, we start counting at 0, not 1!

Then that backface culled skips triangles that are facing away from the triangle, more on that in a second. And the color of the triangle each gets a random RGB value so via a modulo variation that makes it always unique thanks to our facecount index. I say random in the sense that I have no clue what colour it will be when you run it but really it will always be the same colour on compile.

Alright so this function essentially checks if the triangle is facing toward or away from the camerea. So the first part abx and acx we make towo edge vertex, vertex from a to b and a vertex from a to c. These vectors describe the direction of the triangle in 2d space. Then we are computing the 2D cross product via area2. If the tirangle area is zero or facing the wrong direction dont draw it! Simple. Anyways lets load this cow! That I found on the internet.
https://www.cs.cmu.edu/~kmcrane/Projects/ModelRepository/

So we declare the model and the name of the file as we did in the previous part and then we call our function drwa model filled and pass in the model and our framebuffer and voila

There we go folks,

Thank you for reading and here are some useful links aswell as sources

https://haqr.eu/tinyrenderer/
https://www.cs.cmu.edu/~kmcrane/Projects/ModelRepository/
@article{crane2013robust,
title={Robust fairing via conformal curvature flow},
author={Crane, Keenan and Pinkall, Ulrich and Schr{\"o}der, Peter},
journal={ACM Transactions on Graphics (TOG)},
volume={32},
number={4},
pages={1--10},
year={2013},
publisher={ACM New York, NY, USA}
}

https://3d.si.edu/object/3d/triceratops-horridus-marsh-1889:d8c623be-4ebc-11ea-b77f-2e728ce88125

https://github.com/nothings/stb

The git repository for this project is also avalible at: https://github.com/NoticeableSmeh/Rasterizer_Project

Top comments (0)