Compare commits
8
Commits
50eabecfe9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8aeb643e21 | ||
|
|
4f762c7df9 | ||
|
|
1b94f8ca7a | ||
|
|
3eca1b1f7b | ||
|
|
5a23f49eae | ||
|
|
3ea1a7c5aa | ||
|
|
65bb4d4897 | ||
|
|
36c16d053e |
+33
-17
@@ -1,36 +1,52 @@
|
|||||||
use crate::math::{Point3, Vector3};
|
use crate::math::{Point3, Vector3};
|
||||||
|
|
||||||
pub struct Plane {
|
pub struct Plane<T> {
|
||||||
point: Point3,
|
point: Point3<T>,
|
||||||
normal: Vector3,
|
normal: Vector3<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Plane {
|
impl<T> Plane<T> {
|
||||||
pub fn new(point: Point3, normal: Vector3) -> Plane {
|
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
|
||||||
Plane { point, normal }
|
Plane { point, normal }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Line {
|
pub struct Line<T> {
|
||||||
point: Point3,
|
point: Point3<T>,
|
||||||
direction: Vector3,
|
direction: Vector3<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Sphere {
|
pub struct Sphere<T> {
|
||||||
point: Point3,
|
point: Point3<T>,
|
||||||
radius: f64,
|
radius: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sphere {
|
impl<T> Sphere<T> {
|
||||||
pub fn new(point: Point3, radius: f64) -> Sphere {
|
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
|
||||||
Sphere { point, radius }
|
Sphere { point, radius }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Geometry {
|
pub enum Geometry<T> {
|
||||||
Sphere(Sphere),
|
Sphere(Sphere<T>),
|
||||||
Plane(Plane),
|
Plane(Plane<T>),
|
||||||
Line(Line),
|
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)]
|
#[cfg(test)]
|
||||||
|
|||||||
+64
-17
@@ -1,31 +1,31 @@
|
|||||||
use hello::geometry::{self, Geometry, Sphere};
|
use hello::geometry::{Geometry, Line, Plane, Sphere};
|
||||||
use hello::math::{Mat4x4, Point3};
|
use hello::math::{Mat4x4, Point3, Vector3};
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct RGB {
|
struct RGB<T> {
|
||||||
red: f64,
|
red: T,
|
||||||
green: f64,
|
green: T,
|
||||||
blue: f64,
|
blue: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Texture {
|
struct Texture<T> {
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
pixel: Vec<RGB>,
|
pixel: Vec<RGB<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Texture {
|
impl Texture<f64> {
|
||||||
fn load_texture() -> Texture {
|
fn load_texture() -> Texture<f64> {
|
||||||
let width = 640;
|
let width = 640;
|
||||||
let height = 480;
|
let height = 480;
|
||||||
|
|
||||||
let mut pixel = Vec::new();
|
let mut pixel = Vec::new();
|
||||||
|
|
||||||
for x in 0..width {
|
for _ in 0..width {
|
||||||
for y in 0..height {
|
for _ in 0..height {
|
||||||
pixel.push(RGB {
|
pixel.push(RGB {
|
||||||
red: 1.0,
|
red: 1.0,
|
||||||
green: 1.0,
|
green: 1.0,
|
||||||
@@ -42,14 +42,18 @@ impl Texture {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RenderableGeometry {
|
struct RenderableGeometry<T> {
|
||||||
transform: Mat4x4,
|
transform: Mat4x4,
|
||||||
geometry: Geometry,
|
geometry: Geometry<T>,
|
||||||
texture: Rc<Texture>,
|
texture: Rc<Texture<f64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderableGeometry {
|
impl<T> RenderableGeometry<T> {
|
||||||
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture>) -> RenderableGeometry {
|
fn new(
|
||||||
|
transform: Mat4x4,
|
||||||
|
geometry: Geometry<T>,
|
||||||
|
texture: Rc<Texture<f64>>,
|
||||||
|
) -> RenderableGeometry<T> {
|
||||||
RenderableGeometry {
|
RenderableGeometry {
|
||||||
transform,
|
transform,
|
||||||
geometry,
|
geometry,
|
||||||
@@ -58,9 +62,49 @@ impl RenderableGeometry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
fn main() {
|
||||||
let tex = Rc::new(Texture::load_texture());
|
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(
|
let geo1 = RenderableGeometry::new(
|
||||||
Mat4x4::ident(),
|
Mat4x4::ident(),
|
||||||
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
|
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
|
||||||
@@ -80,4 +124,7 @@ fn main() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
println!("Reference counter = {}", Rc::strong_count(&tex));
|
println!("Reference counter = {}", Rc::strong_count(&tex));
|
||||||
|
|
||||||
|
let a = Vector3::new(2.0, 3.0, 1.0);
|
||||||
|
let b = 10.0 * a;
|
||||||
}
|
}
|
||||||
|
|||||||
+71
-37
@@ -1,91 +1,125 @@
|
|||||||
use std::ops::{Add, Div, Mul, Sub};
|
use std::ops::{Add, Div, Mul, Sub};
|
||||||
|
|
||||||
|
macro_rules! create_vector {
|
||||||
|
($name:ident [$($elem:ident),+] ) => {
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub struct Vector3 {
|
pub struct $name<T> {
|
||||||
x: f64,
|
$($elem: T,)*
|
||||||
y: f64,
|
|
||||||
z: f64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector3 {
|
impl<T> $name<T> {
|
||||||
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
pub fn new($($elem : T,)*) -> $name<T> {
|
||||||
Vector3 { x, y, z }
|
$name { $($elem,)* }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl Add for Vector3 {
|
create_vector! { Vector4 [x, y, z, w] }
|
||||||
type Output = Vector3;
|
create_vector! { Vector3 [x, y, z] }
|
||||||
|
create_vector! { Vector2 [x, y] }
|
||||||
|
|
||||||
fn add(self, rhs: Vector3) -> Self::Output {
|
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)
|
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sub for Vector3 {
|
impl<T> Sub for Vector3<T>
|
||||||
type Output = Vector3;
|
where
|
||||||
|
T: Sub<Output = T>,
|
||||||
|
{
|
||||||
|
type Output = Vector3<T>;
|
||||||
|
|
||||||
fn sub(self, rhs: Vector3) -> Self::Output {
|
fn sub(self, rhs: Vector3<T>) -> Self::Output {
|
||||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul<f64> for Vector3 {
|
impl<T> Mul<T> for Vector3<T>
|
||||||
type Output = Vector3;
|
where
|
||||||
|
T: Mul<Output = T> + Clone + Copy,
|
||||||
|
{
|
||||||
|
type Output = Vector3<T>;
|
||||||
|
|
||||||
fn mul(self, rhs: f64) -> Self::Output {
|
fn mul(self, rhs: T) -> Self::Output {
|
||||||
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul<Vector3> for f64 {
|
macro_rules! impl_mul_for_vector3 {
|
||||||
type Output = Vector3;
|
($($type:ty)+) => {
|
||||||
|
$(
|
||||||
|
impl Mul<Vector3<$type>> for $type {
|
||||||
|
type Output = Vector3<$type>;
|
||||||
|
|
||||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
fn mul(self, rhs: Vector3<$type>) -> Self::Output {
|
||||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||||
}
|
}
|
||||||
|
})*
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul for Vector3 {
|
impl_mul_for_vector3! { i32 f32 f64 }
|
||||||
type Output = f64;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
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
|
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Div<f64> for Vector3 {
|
impl<T> Div<T> for Vector3<T>
|
||||||
type Output = Vector3;
|
where
|
||||||
|
T: Div<Output = T> + Copy + Clone,
|
||||||
|
{
|
||||||
|
type Output = Vector3<T>;
|
||||||
|
|
||||||
fn div(self, rhs: f64) -> Self::Output {
|
fn div(self, rhs: T) -> Self::Output {
|
||||||
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub struct Point3 {
|
pub struct Point3<T> {
|
||||||
x: f64,
|
x: T,
|
||||||
y: f64,
|
y: T,
|
||||||
z: f64,
|
z: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Point3 {
|
impl<T> Point3<T> {
|
||||||
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
|
pub fn new(x: T, y: T, z: T) -> Point3<T> {
|
||||||
Point3 { x, y, z }
|
Point3 { x, y, z }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add<Vector3> for Point3 {
|
impl<T> Add<Vector3<T>> for Point3<T>
|
||||||
type Output = Point3;
|
where
|
||||||
|
T: Add<Output = T>,
|
||||||
|
{
|
||||||
|
type Output = Point3<T>;
|
||||||
|
|
||||||
fn add(self, rhs: Vector3) -> Self::Output {
|
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sub for Point3 {
|
impl<T> Sub for Point3<T>
|
||||||
type Output = Vector3;
|
where
|
||||||
|
T: Sub<Output = T>,
|
||||||
|
{
|
||||||
|
type Output = Vector3<T>;
|
||||||
|
|
||||||
fn sub(self, rhs: Point3) -> Self::Output {
|
fn sub(self, rhs: Point3<T>) -> Self::Output {
|
||||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user