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