Compare commits
4 Commits
36c16d053e
...
3eca1b1f7b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3eca1b1f7b | ||
|
|
5a23f49eae | ||
|
|
3ea1a7c5aa | ||
|
|
65bb4d4897 |
@ -34,16 +34,16 @@ pub enum Geometry {
|
||||
}
|
||||
|
||||
impl Geometry {
|
||||
pub fn greet(&self) {
|
||||
pub fn draw(&self) {
|
||||
match self {
|
||||
Geometry::Sphere(_) => {
|
||||
println!("Sphere!");
|
||||
println!("Drawing Sphere!");
|
||||
}
|
||||
Geometry::Line(_) => {
|
||||
println!("Line!");
|
||||
println!("Drawing Line!");
|
||||
}
|
||||
Geometry::Plane(_) => {
|
||||
println!("Plane!");
|
||||
println!("Drawing Plane!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
67
src/main.rs
67
src/main.rs
@ -1,31 +1,31 @@
|
||||
use hello::geometry::{self, Geometry, Sphere};
|
||||
use hello::math::{Mat4x4, Point3};
|
||||
use hello::geometry::{Geometry, Line, Plane, Sphere};
|
||||
use hello::math::{Mat4x4, Point3, Vector3};
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RGB {
|
||||
red: f64,
|
||||
green: f64,
|
||||
blue: f64,
|
||||
struct RGB<T> {
|
||||
red: T,
|
||||
green: T,
|
||||
blue: T,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Texture {
|
||||
struct Texture<T> {
|
||||
width: usize,
|
||||
height: usize,
|
||||
pixel: Vec<RGB>,
|
||||
pixel: Vec<RGB<T>>,
|
||||
}
|
||||
|
||||
impl Texture {
|
||||
fn load_texture() -> Texture {
|
||||
impl Texture<f64> {
|
||||
fn load_texture() -> Texture<f64> {
|
||||
let width = 640;
|
||||
let height = 480;
|
||||
|
||||
let mut pixel = Vec::new();
|
||||
|
||||
for x in 0..width {
|
||||
for y in 0..height {
|
||||
for _ in 0..width {
|
||||
for _ in 0..height {
|
||||
pixel.push(RGB {
|
||||
red: 1.0,
|
||||
green: 1.0,
|
||||
@ -45,11 +45,11 @@ impl Texture {
|
||||
struct RenderableGeometry {
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry,
|
||||
texture: Rc<Texture>,
|
||||
texture: Rc<Texture<f64>>,
|
||||
}
|
||||
|
||||
impl RenderableGeometry {
|
||||
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture>) -> RenderableGeometry {
|
||||
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture<f64>>) -> RenderableGeometry {
|
||||
RenderableGeometry {
|
||||
transform,
|
||||
geometry,
|
||||
@ -58,11 +58,48 @@ impl RenderableGeometry {
|
||||
}
|
||||
}
|
||||
|
||||
trait Renderable {
|
||||
fn render(&self);
|
||||
}
|
||||
|
||||
impl Renderable for Sphere {
|
||||
fn render(&self) {
|
||||
println!("Rendering Sphere!");
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for Plane {
|
||||
fn render(&self) {
|
||||
println!("Rendering Plane!");
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for Line {
|
||||
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));
|
||||
sphere.greet();
|
||||
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(),
|
||||
|
||||
72
src/math.rs
72
src/math.rs
@ -1,62 +1,78 @@
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct Vector3 {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
pub struct Vector3<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
z: T,
|
||||
}
|
||||
|
||||
impl Vector3 {
|
||||
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
||||
impl<T> Vector3<T> {
|
||||
pub fn new(x: T, y: T, z: T) -> Vector3<T> {
|
||||
Vector3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vector3 {
|
||||
type Output = Vector3;
|
||||
impl<T> Add for Vector3<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn add(self, rhs: Vector3) -> Self::Output {
|
||||
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Vector3 {
|
||||
type Output = Vector3;
|
||||
impl<T> Sub for Vector3<T>
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f64> for Vector3 {
|
||||
type Output = Vector3;
|
||||
impl<T> Mul<T> for Vector3<T>
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vector3> for f64 {
|
||||
type Output = Vector3;
|
||||
/*
|
||||
impl<T> Mul<Vector3<T>> for f64 {
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||
fn mul(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
impl Mul for Vector3 {
|
||||
type Output = f64;
|
||||
impl<T> Mul for Vector3<T>
|
||||
where
|
||||
T: Mul<Output = T> + Add<Output = T>,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||
fn mul(self, rhs: Vector3<T>) -> Self::Output {
|
||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<f64> for Vector3 {
|
||||
type Output = Vector3;
|
||||
impl<T> Div<T> for Vector3<T>
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -74,16 +90,16 @@ impl Point3 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Vector3> for Point3 {
|
||||
impl Add<Vector3<T>> for Point3 {
|
||||
type Output = Point3;
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Point3 {
|
||||
type Output = Vector3;
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn sub(self, rhs: Point3) -> Self::Output {
|
||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user