Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2015 02:43
Show Gist options
  • Save anonymous/dc0686a9847463812882 to your computer and use it in GitHub Desktop.
Save anonymous/dc0686a9847463812882 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::fmt;
struct Vector3<T>( T, T, T );
struct Vector4<T>( T, T, T, T );
struct Vertex
{
pos : Vector3<f32>,
color : Vector4<f32>,
normal : Vector3<f32>
}
struct Mesh< 'a >
{
verts : &'a mut [Vertex],
triangles : &'a mut [Vector3<usize>],
}
impl<T : fmt::Display> fmt::Display for Vector3<T>{
fn fmt( &self, f : &mut fmt::Formatter ) -> fmt::Result
{
write!( f, "Vec3({}, {}, {})", self.0, self.1, self.2 )
}
}
impl<T : fmt::Display> fmt::Display for Vector4<T>{
fn fmt( &self, f : &mut fmt::Formatter ) -> fmt::Result
{
write!( f, "Vec4({}, {}, {}, {})", self.0, self.1, self.2, self.3 )
}
}
impl fmt::Display for Vertex
{
fn fmt( &self, f : &mut fmt::Formatter ) -> fmt::Result
{
write!( f, "Vertex < pos: {}, loc: {}, norm: {} >", self.pos, self.color, self.normal )
}
}
impl< 'a > Mesh<'a>{
fn print( &self )
{
println!( "Mesh Print" );
for triangle in self.triangles.iter()
{
println!( "Triangle : {}" triangle.);
println!( "{}", self.verts[ triangle.0 ] );
println!( "{}", self.verts[ triangle.1 ] );
println!( "{}", self.verts[ triangle.2 ] );
}
}
}
fn main() {
//let location = Vector3( 1.0, 2.0, 3.5 );
//let triangles = Vector3( 0, 1, 2 );
let vert1 = Vertex{
pos : Vector3( -1.0, 1.0, 0.0 ),
color : Vector4( 1.0, 1.0, 1.0, 1.0),
normal : Vector3( 0.0, 0.0, -1.0 )
};
let vert2 = Vertex{
pos : Vector3( 1.0, 1.0, 0.0 ),
color : Vector4( 1.0, 1.0, 1.0, 1.0),
normal : Vector3( 0.0, 0.0, -1.0 )
};
let vert3 = Vertex{
pos : Vector3( -1.0, -1.0, 0.0 ),
color : Vector4( 1.0, 1.0, 1.0, 1.0),
normal : Vector3( 0.0, 0.0, -1.0 )
};
let vert4 = Vertex{
pos : Vector3( 1.0, -1.0, 0.0 ),
color : Vector4( 1.0, 1.0, 1.0, 1.0),
normal : Vector3( 0.0, 0.0, -1.0 )
};
let mesh = Mesh{
verts : &mut[vert1, vert2, vert3, vert4],
triangles : &mut[ Vector3( 0usize, 1usize, 2usize ), Vector3( 1usize, 2usize, 3usize) ]
};
mesh.print()
//println!( "{}, {}", location, triangles );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment