Compare commits
45
Commits
04520ffa1e
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8aeb643e21 | ||
|
|
4f762c7df9 | ||
|
|
1b94f8ca7a | ||
|
|
3eca1b1f7b | ||
|
|
5a23f49eae | ||
|
|
3ea1a7c5aa | ||
|
|
65bb4d4897 | ||
|
|
36c16d053e | ||
|
|
50eabecfe9 | ||
|
|
585af00665 | ||
|
|
5065596f96 | ||
|
|
8cd294c44a | ||
|
|
58a9c36d0d | ||
|
|
c4a794c4f2 | ||
|
|
2649558ecd | ||
|
|
6adb2e143f | ||
|
|
29f39a9163 | ||
|
|
0a692c01de | ||
|
|
41eb9a9dc0 | ||
|
|
5b4c205234 | ||
|
|
09ff36dff8 | ||
|
|
45cad795a7 | ||
|
|
5daaf9e1bc | ||
|
|
bb45906753 | ||
|
|
b7a1f52360 | ||
|
|
b1963c7e6a | ||
|
|
9ef8e08936 | ||
|
|
31e224f9b0 | ||
|
|
07b2fc28d8 | ||
|
|
31c9bda5a2 | ||
|
|
794228dfce | ||
|
|
28df45da67 | ||
|
|
81205e5b29 | ||
|
|
07d7855f47 | ||
|
|
352085f8d9 | ||
|
|
b0e998fb28 | ||
|
|
7a1b0975d2 | ||
|
|
12a54c42da | ||
|
|
bd260f1769 | ||
|
|
823ec0ad00 | ||
|
|
019fbcf095 | ||
|
|
20c675ef91 | ||
|
|
6e0e031b12 | ||
|
|
d5427c96ca | ||
|
|
092e5693b7 |
@@ -0,0 +1,72 @@
|
||||
use crate::math::{Point3, Vector3};
|
||||
|
||||
pub struct Plane<T> {
|
||||
point: Point3<T>,
|
||||
normal: Vector3<T>,
|
||||
}
|
||||
|
||||
impl<T> Plane<T> {
|
||||
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
|
||||
Plane { point, normal }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Line<T> {
|
||||
point: Point3<T>,
|
||||
direction: Vector3<T>,
|
||||
}
|
||||
|
||||
pub struct Sphere<T> {
|
||||
point: Point3<T>,
|
||||
radius: T,
|
||||
}
|
||||
|
||||
impl<T> Sphere<T> {
|
||||
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
|
||||
Sphere { point, radius }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Geometry<T> {
|
||||
Sphere(Sphere<T>),
|
||||
Plane(Plane<T>),
|
||||
Line(Line<T>),
|
||||
}
|
||||
|
||||
impl<T> Geometry<T> {
|
||||
pub fn draw(&self) {
|
||||
match self {
|
||||
Geometry::Sphere(_) => {
|
||||
println!("Drawing Sphere!");
|
||||
}
|
||||
Geometry::Line(_) => {
|
||||
println!("Drawing Line!");
|
||||
}
|
||||
Geometry::Plane(_) => {
|
||||
println!("Drawing Plane!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new_plane() {
|
||||
let p = Point3::new(1.0, 2.0, 3.0);
|
||||
let n = Vector3::new(4.0, 5.0, 6.0);
|
||||
|
||||
let plane = Plane::new(p, n);
|
||||
|
||||
assert_eq!(plane.point, p);
|
||||
assert_eq!(plane.normal, n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn foo() {
|
||||
panic!("This code panics!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod geometry;
|
||||
pub mod math;
|
||||
+128
-3
@@ -1,5 +1,130 @@
|
||||
fn main() {
|
||||
for value in (-10..10).rev() {
|
||||
println!("Value: {}", value);
|
||||
use hello::geometry::{Geometry, Line, Plane, Sphere};
|
||||
use hello::math::{Mat4x4, Point3, Vector3};
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RGB<T> {
|
||||
red: T,
|
||||
green: T,
|
||||
blue: T,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Texture<T> {
|
||||
width: usize,
|
||||
height: usize,
|
||||
pixel: Vec<RGB<T>>,
|
||||
}
|
||||
|
||||
impl Texture<f64> {
|
||||
fn load_texture() -> Texture<f64> {
|
||||
let width = 640;
|
||||
let height = 480;
|
||||
|
||||
let mut pixel = Vec::new();
|
||||
|
||||
for _ in 0..width {
|
||||
for _ in 0..height {
|
||||
pixel.push(RGB {
|
||||
red: 1.0,
|
||||
green: 1.0,
|
||||
blue: 1.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Texture {
|
||||
width,
|
||||
height,
|
||||
pixel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RenderableGeometry<T> {
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry<T>,
|
||||
texture: Rc<Texture<f64>>,
|
||||
}
|
||||
|
||||
impl<T> RenderableGeometry<T> {
|
||||
fn new(
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry<T>,
|
||||
texture: Rc<Texture<f64>>,
|
||||
) -> RenderableGeometry<T> {
|
||||
RenderableGeometry {
|
||||
transform,
|
||||
geometry,
|
||||
texture,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait Renderable {
|
||||
fn render(&self);
|
||||
}
|
||||
|
||||
impl<T> Renderable for Sphere<T> {
|
||||
fn render(&self) {
|
||||
println!("Rendering Sphere!");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Renderable for Plane<T> {
|
||||
fn render(&self) {
|
||||
println!("Rendering Plane!");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Renderable for Line<T> {
|
||||
fn render(&self) {
|
||||
println!("Rendering Line!");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let tex = Rc::new(Texture::load_texture());
|
||||
|
||||
let sphere = Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0));
|
||||
let geometries = vec![sphere];
|
||||
|
||||
for g in geometries {
|
||||
g.draw();
|
||||
}
|
||||
|
||||
let mut renderables: Vec<Box<dyn Renderable>> = Vec::new();
|
||||
renderables.push(Box::new(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)));
|
||||
renderables.push(Box::new(Plane::new(
|
||||
Point3::new(0.0, 0.0, 0.0),
|
||||
Vector3::new(0.0, 1.0, 0.0),
|
||||
)));
|
||||
|
||||
for r in renderables {
|
||||
r.render();
|
||||
}
|
||||
|
||||
let geo1 = RenderableGeometry::new(
|
||||
Mat4x4::ident(),
|
||||
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
|
||||
tex.clone(),
|
||||
);
|
||||
|
||||
let geo2 = RenderableGeometry::new(
|
||||
Mat4x4::ident(),
|
||||
Geometry::Sphere(Sphere::new(Point3::new(-2.0, 0.0, 0.0), 1.0)),
|
||||
tex.clone(),
|
||||
);
|
||||
|
||||
let geo3 = RenderableGeometry::new(
|
||||
Mat4x4::ident(),
|
||||
Geometry::Sphere(Sphere::new(Point3::new(2.0, 0.0, 0.0), 1.0)),
|
||||
tex.clone(),
|
||||
);
|
||||
|
||||
println!("Reference counter = {}", Rc::strong_count(&tex));
|
||||
|
||||
let a = Vector3::new(2.0, 3.0, 1.0);
|
||||
let b = 10.0 * a;
|
||||
}
|
||||
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
||||
macro_rules! create_vector {
|
||||
($name:ident [$($elem:ident),+] ) => {
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct $name<T> {
|
||||
$($elem: T,)*
|
||||
}
|
||||
|
||||
impl<T> $name<T> {
|
||||
pub fn new($($elem : T,)*) -> $name<T> {
|
||||
$name { $($elem,)* }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
create_vector! { Vector4 [x, y, z, w] }
|
||||
create_vector! { Vector3 [x, y, z] }
|
||||
create_vector! { Vector2 [x, y] }
|
||||
|
||||
impl<T> Add for Vector3<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sub for Vector3<T>
|
||||
where
|
||||
T: Sub<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn sub(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Mul<T> for Vector3<T>
|
||||
where
|
||||
T: Mul<Output = T> + Clone + Copy,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn mul(self, rhs: T) -> Self::Output {
|
||||
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_mul_for_vector3 {
|
||||
($($type:ty)+) => {
|
||||
$(
|
||||
impl Mul<Vector3<$type>> for $type {
|
||||
type Output = Vector3<$type>;
|
||||
|
||||
fn mul(self, rhs: Vector3<$type>) -> Self::Output {
|
||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||
}
|
||||
})*
|
||||
};
|
||||
}
|
||||
|
||||
impl_mul_for_vector3! { i32 f32 f64 }
|
||||
|
||||
impl<T> Mul for Vector3<T>
|
||||
where
|
||||
T: Mul<Output = T> + Add<Output = T>,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn mul(self, rhs: Vector3<T>) -> Self::Output {
|
||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Div<T> for Vector3<T>
|
||||
where
|
||||
T: Div<Output = T> + Copy + Clone,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn div(self, rhs: T) -> Self::Output {
|
||||
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct Point3<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
z: T,
|
||||
}
|
||||
|
||||
impl<T> Point3<T> {
|
||||
pub fn new(x: T, y: T, z: T) -> Point3<T> {
|
||||
Point3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add<Vector3<T>> for Point3<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Point3<T>;
|
||||
|
||||
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sub for Point3<T>
|
||||
where
|
||||
T: Sub<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn sub(self, rhs: Point3<T>) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Mat4x4 {
|
||||
v: f64,
|
||||
}
|
||||
|
||||
impl Mat4x4 {
|
||||
pub fn ident() -> Mat4x4 {
|
||||
Mat4x4 { v: 0.0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new_vector3() {
|
||||
let v = Vector3::new(1.0, 2.0, 3.0);
|
||||
assert_eq!(v.x, 1.0);
|
||||
assert_eq!(v.y, 2.0);
|
||||
assert_eq!(v.z, 3.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_vector3_to_vector3() {
|
||||
let v1 = Vector3::new(1.0, 2.0, 3.0);
|
||||
let v2 = Vector3::new(4.0, 5.0, 6.0);
|
||||
|
||||
let r = v1 + v2;
|
||||
|
||||
assert_eq!(r.x, 5.0);
|
||||
assert_eq!(r.y, 7.0);
|
||||
assert_eq!(r.z, 9.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_point3() {
|
||||
let p = Point3::new(1.0, 2.0, 3.0);
|
||||
assert_eq!(p.x, 1.0);
|
||||
assert_eq!(p.y, 2.0);
|
||||
assert_eq!(p.z, 3.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#[test]
|
||||
fn very_long_running_test() {}
|
||||
Reference in New Issue
Block a user