mirror of
https://github.com/stickynotememo/wolf_renderer.git
synced 2026-07-30 06:46:03 +10:00
Triangle functionality
This commit is contained in:
@@ -7,6 +7,7 @@ pub struct Point2D {
|
|||||||
pub x: f64,
|
pub x: f64,
|
||||||
pub y: f64
|
pub y: f64
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Point3D {
|
pub struct Point3D {
|
||||||
pub x: f64, pub y: f64, pub z: f64
|
pub x: f64, pub y: f64, pub z: f64
|
||||||
@@ -118,4 +119,10 @@ impl Point2D {
|
|||||||
+ f64::powf(p2.y.max(p1.y) - p1.y.min(p2.y), 2.0)) as f64,
|
+ f64::powf(p2.y.max(p1.y) - p1.y.min(p2.y), 2.0)) as f64,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn gradient(self, p2: &Point2D) -> f64 {
|
||||||
|
let p1 = self;
|
||||||
|
|
||||||
|
(p2.y - p1.y) / (p2.x - p1.x)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-2
@@ -1,5 +1,5 @@
|
|||||||
use crate::consts::*;
|
use crate::consts::*;
|
||||||
use crate::points::Point2D;
|
use crate::points::{Point2D, Point3D};
|
||||||
use crate::Scene;
|
use crate::Scene;
|
||||||
|
|
||||||
type Pixel = u32;
|
type Pixel = u32;
|
||||||
@@ -62,7 +62,28 @@ pub fn render(scene: &Scene, buf: &mut [u32]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for faces in &obj.faces {
|
|
||||||
|
for (p0, p1, p2) in &obj.faces {
|
||||||
|
|
||||||
|
let (p0, p1, p2) = (p0.to_screen_coordinates(), p1.to_screen_coordinates(), p2.to_screen_coordinates());
|
||||||
|
// Finds the leftmost and rightmost points on the triangle and only iterates over them.
|
||||||
|
let horizontal_range = p0.x.min(p1.x.min(p2.x)) as usize..=p0.x.max(p1.x.max(p2.x)) as usize;
|
||||||
|
let vertical_range = p0.y.min(p1.y.min(p2.y)) as usize..=p0.y.max(p1.y.max(p2.y)) as usize;
|
||||||
|
|
||||||
|
for x in horizontal_range.clone() {
|
||||||
|
for y in vertical_range.clone() {
|
||||||
|
let p = Point2D{x: x as f64, y: y as f64};
|
||||||
|
|
||||||
|
let area = 0.5 *(-p1.y*p2.x + p0.y*(-p1.x + p2.x) + p0.x*(p1.y - p2.y) + p1.x*p2.y);
|
||||||
|
let s = 1.0/(2.0*area)*(p0.y*p2.x - p0.x*p2.y + (p2.y - p0.y)*p.x + (p0.x - p2.x)*p.y);
|
||||||
|
let t = 1.0/(2.0*area)*(p0.x*p1.y - p0.y*p1.x + (p0.y - p1.y)*p.x + (p1.x - p0.x)*p.y);
|
||||||
|
|
||||||
|
if s > 0.0 && t > 0.0 && 1.0 - s - t > 0.0 {
|
||||||
|
// dbg!(p);
|
||||||
|
*index_buffer(buf, p) = to_rgb((255, 255, 255));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ use crate::points::Scene;
|
|||||||
pub fn set_scene(dt: Duration, scene: &mut Scene) {
|
pub fn set_scene(dt: Duration, scene: &mut Scene) {
|
||||||
for obj in scene {
|
for obj in scene {
|
||||||
for vertex in obj.vertices.iter_mut() {
|
for vertex in obj.vertices.iter_mut() {
|
||||||
vertex.z += 1.0;
|
// vertex.z += 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.update();
|
obj.update();
|
||||||
|
|||||||
Reference in New Issue
Block a user