mirror of
https://github.com/stickynotememo/wolf_renderer.git
synced 2026-07-30 06:46:03 +10:00
Update Object structure to enforce edge endpoints corresponding to vertices
This commit is contained in:
+7
-12
@@ -49,23 +49,18 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.expect("Couldn't resize surface");
|
.expect("Couldn't resize surface");
|
||||||
|
|
||||||
let mut scene = vec![Object {
|
let mut scene = vec![Object::new(
|
||||||
vertices: vec![
|
vec![
|
||||||
Point3D{x: 0.0, y: f64::MAX / 2.0, z: 0.0},
|
Point3D{x: 0.0, y: f64::MAX / 2.0, z: 0.0},
|
||||||
Point3D{x: f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0},
|
Point3D{x: f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0},
|
||||||
Point3D{x: -f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0},
|
Point3D{x: -f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0},
|
||||||
],
|
],
|
||||||
edges: vec![
|
vec![
|
||||||
(Point3D{x: 0.0, y: f64::MAX / 2.0, z: 0.0}, Point3D{x: f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0}),
|
(0, 1), (1, 2), (2, 0)
|
||||||
(Point3D{x: f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0}, Point3D{x: -f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0}),
|
|
||||||
(Point3D{x: -f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0}, Point3D{x: 0.0, y: f64::MAX / 2.0, z: 0.0})
|
|
||||||
],
|
],
|
||||||
faces: vec![
|
vec![
|
||||||
(Point3D{x: 0.0, y: f64::MAX / 2.0, z: 0.0},
|
(0, 1, 2)
|
||||||
Point3D{x: f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0},
|
])];
|
||||||
Point3D{x: -f64::MAX / 2.0, y: -f64::MAX / 2.0, z: 0.0})
|
|
||||||
]
|
|
||||||
}];
|
|
||||||
|
|
||||||
let mut dt = Duration::ZERO;
|
let mut dt = Duration::ZERO;
|
||||||
|
|
||||||
|
|||||||
+68
-16
@@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::consts::*;
|
use crate::consts::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@@ -6,26 +8,60 @@ pub struct Point2D {
|
|||||||
pub y: f64
|
pub y: f64
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Point2D {
|
|
||||||
pub fn to_canvas_coordinates(&self) -> Point3D {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn distance(self, p2: &Point2D) -> f64 {
|
|
||||||
let p1 = self;
|
|
||||||
// Pythagorean formula
|
|
||||||
// TODO: use hypot function
|
|
||||||
f64::sqrt(
|
|
||||||
(f64::powf(p2.x.max(p1.x) - p1.x.min(p2.x), 2.0)
|
|
||||||
+ f64::powf(p2.y.max(p1.y) - p1.y.min(p2.y), 2.0)) as 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Object {
|
||||||
|
// Invariants:
|
||||||
|
// Every vertex in edges must have a corresponding vertex in vertices
|
||||||
|
pub vertices: Vec<Point3D>,
|
||||||
|
pub edges: Vec<(Point3D, Point3D)>,
|
||||||
|
pub edge_indices: Vec<(usize, usize)>,
|
||||||
|
pub faces: Vec<(Point3D, Point3D, Point3D)>,
|
||||||
|
pub face_indices: Vec<(usize, usize, usize)>,
|
||||||
|
// the update() function
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Object {
|
||||||
|
/// Takes a vector of vertices and a vector of indices for each of the `edge_indices` and
|
||||||
|
/// `face_indices` parameters. These indices index the `vertices` array and represent the
|
||||||
|
/// endpoints of the line segment (in the case of an edge) or the vertices of the triangle
|
||||||
|
/// (in the case of a face)
|
||||||
|
pub fn new(vertices: Vec<Point3D>,
|
||||||
|
edge_indices: Vec<(usize, usize)>,
|
||||||
|
face_indices: Vec<(usize, usize, usize)>) -> Self {
|
||||||
|
|
||||||
|
let mut ret = Self {vertices, edges: vec![], edge_indices, faces: vec![], face_indices};
|
||||||
|
for (i_start, i_end) in ret.edge_indices.iter() {
|
||||||
|
ret.edges.push((ret.vertices[*i_start].clone(), ret.vertices[*i_end].clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i_one, i_two, i_three /* The three vertices of the triangle */) in ret.face_indices.iter() {
|
||||||
|
ret.faces.push((ret.vertices[*i_one].clone(), ret.vertices[*i_two].clone(), ret.vertices[*i_three].clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// Updates the `edges` and `faces` to correspond to updated vertices.
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
self.edges = vec![];
|
||||||
|
for (i_start, i_end) in self.edge_indices.iter() {
|
||||||
|
self.edges.push((self.vertices[*i_start].clone(), self.vertices[*i_end].clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.faces = vec![];
|
||||||
|
for (i_one, i_two, i_three /* The three vertices of the triangle */) in self.face_indices.iter() {
|
||||||
|
self.faces.push((self.vertices[*i_one].clone(), self.vertices[*i_two].clone(), self.vertices[*i_three].clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Scene = Vec<Object>;
|
||||||
|
|
||||||
impl Point3D {
|
impl Point3D {
|
||||||
pub fn to_screen_coordinates(&self) -> Point2D {
|
pub fn to_screen_coordinates(&self) -> Point2D {
|
||||||
let Point3D {x, y, z} = *self;
|
let Point3D {x, y, z} = *self;
|
||||||
@@ -68,3 +104,19 @@ impl Point3D {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Point2D {
|
||||||
|
pub fn to_canvas_coordinates(&self) -> Point3D {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn distance(self, p2: &Point2D) -> f64 {
|
||||||
|
let p1 = self;
|
||||||
|
// Pythagorean formula
|
||||||
|
// TODO: use hypot function
|
||||||
|
f64::sqrt(
|
||||||
|
(f64::powf(p2.x.max(p1.x) - p1.x.min(p2.x), 2.0)
|
||||||
|
+ f64::powf(p2.y.max(p1.y) - p1.y.min(p2.y), 2.0)) as f64,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
use crate::consts::*;
|
use crate::consts::*;
|
||||||
use crate::points::Point2D;
|
use crate::points::Point2D;
|
||||||
use crate::scene::Scene;
|
use crate::Scene;
|
||||||
|
|
||||||
type Pixel = u32;
|
type Pixel = u32;
|
||||||
type Subpixels = (u8, u8, u8);
|
type Subpixels = (u8, u8, u8);
|
||||||
@@ -62,5 +62,9 @@ pub fn render(scene: &Scene, buf: &mut [u32]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for faces in &obj.faces {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-15
@@ -1,17 +1,7 @@
|
|||||||
use core::f64;
|
use core::f64;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use crate::points::Point3D;
|
use crate::points::Point3D;
|
||||||
|
use crate::points::Scene;
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Object {
|
|
||||||
// Invariants:
|
|
||||||
// Every vertex in edges must have a corresponding vertex in vertices
|
|
||||||
pub vertices: Vec<Point3D>,
|
|
||||||
pub edges: Vec<(Point3D, Point3D)>,
|
|
||||||
pub faces: Vec<(Point3D, Point3D, Point3D)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Scene = Vec<Object>;
|
|
||||||
|
|
||||||
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 {
|
||||||
@@ -19,9 +9,6 @@ pub fn set_scene(dt: Duration, scene: &mut Scene) {
|
|||||||
vertex.z += 1.0;
|
vertex.z += 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for edge in obj.edges.iter_mut() {
|
obj.update();
|
||||||
edge.0.z += 1.0;
|
|
||||||
edge.1.z += 1.0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
mod foo {
|
||||||
|
pub type FooType = u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use foo::FooType; // Removing this line does not cause the bug
|
||||||
|
mod bar {
|
||||||
|
pub fn bar_func() {
|
||||||
|
let val: FooType = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
bar::bar_func();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user