A super simple polygon rasterizer in Rust
Cui:
cargo r
Gui:
cargo r -p gui
This is a project to explore a fast algorithm to fill inside of a polygon using scanline algorithm. Note that we don't want to use OpenGL or WebGL.
Filling inside a polygon sounds an easy task, but if you want to do it as fast as possible, it is not trivial. If it was triangle, it can be a little bit simple (GPUs use triangles), but it would require triangulation (converting a polygon into a series of triangles). I want to try implementing an algorithm without triangulation and see how fast it can be.
We represent each edge of the polygon in a vector equation:
where
The vector equation for the scanline is:
where
Now, we want to find out the intersection of the edge and the scanline to get the starting and ending point along the scanline.
Erasing
Solving these equations, we get this:
These are mapped to Rust functions like these:
fn get_t(y: f64, v: [f64; 2], d: [f64; 2]) -> f64 {
(y - v[1]) / d[1]
}
fn get_s(y: f64, v: [f64; 2], d: [f64; 2]) -> f64 {
(y - v[1]) * d[0] / d[1] + v[0]
}
Be aware that if
if d[0] == 0. {
continue;
}
If the condition
It outputs something like:
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
-----------------------------**---------------------------------
---------------------------*****--------------------------------
--------------------------******--------------------------------
-------------------------********-------------------------------
-----------------------***********------------------------------
----------------------*************-----------------------------
---------------------***************----------------------------
-------------------*****************----------------------------
------------------*******************---------------------------
-----------------*********************--------------------------
---------------************************-------------------------
--------------**************************------------------------
-------------***************************------------------------
-----------******************************-----------------------
----------********************************----------------------
--------------*****************************---------------------
------------------**************************--------------------
----------------------**********************--------------------
--------------------------*******************-------------------
------------------------------****************------------------
----------------------------------*************-----------------
--------------------------------------**********----------------
------------------------------------------******----------------
----------------------------------------------***---------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
Or you can give poly
argument and get:
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
-----------------------------**---------------------------------
---------------------------*****--------------------------------
--------------------------*******-------------------------------
-------------------------*********------------------------------
-----------------------************-----------------------------
----------------------**************----------------------------
---------------------****************---------------------------
-------------------*******************--------------------------
------------------*********************-------------------------
-----------------***********************------------------------
---------------**************************-----------------------
--------------****************************----------------------
-------------******************************---------------------
-----------*********************************--------------------
----------***********************************-------------------
-----------***********************************------------------
-----------************************************-----------------
------------************************************----------------
------------*************************************---------------
-------------*************************************--------------
-------------******************************---------------------
--------------**********************----------------------------
--------------***************-----------------------------------
---------------*******------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------