Advertisement
Guest User

Untitled

a guest
Sep 5th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.95 KB | None | 0 0
  1.  
  2. use black::device::{ FragmentShader, VertexShader, Varying, Triangle, Target };
  3. use black::math::{Matrix, Vector2, Vector3, Vector4};
  4. use black::scene::{ Geometry, Vertex };
  5.  
  6. pub struct Buffer;
  7. impl Target for Buffer {
  8.     fn width(&self) -> i32 { 100 }
  9.     fn height(&self) -> i32 { 100 }
  10.     fn set(&mut self, x: i32, y: i32, color: Vector4) {
  11.         println!("{} {}: {}", x, y, color)
  12.     }
  13. }
  14.  
  15. pub struct StandardUniform {
  16.     pub matrix:     Matrix,
  17.     pub view:       Matrix,
  18.     pub projection: Matrix,
  19. }
  20. struct StandardVertexShader;
  21. struct StandardFragmentShader;
  22. struct StandardVarying {
  23.     pub position: Vector4,
  24.     pub normal:   Vector3,
  25.     pub uv:       Vector2,
  26. }
  27. impl Varying for StandardVarying {
  28.     fn new() -> StandardVarying {
  29.         StandardVarying {
  30.             position: Vector4::zero(),
  31.             normal:   Vector3::zero(),
  32.             uv:       Vector2::zero(),
  33.         }
  34.     }
  35.     fn interpolate(
  36.         v0: &StandardVarying,
  37.         v1: &StandardVarying,
  38.         v2: &StandardVarying,
  39.         w0:  &f32,
  40.         w1:  &f32,
  41.         w2:  &f32
  42.     ) -> StandardVarying {
  43.         StandardVarying::new()
  44.     }
  45. }
  46. impl VertexShader for StandardVertexShader {
  47.     type Uniform = StandardUniform;
  48.     type Vertex  = Vertex;
  49.     type Varying = StandardVarying;
  50.     fn main(&self, uniform:  &StandardUniform, input: &Vertex, varying: &mut StandardVarying) -> Vector4 {
  51.         varying.position = input.position;
  52.         varying.normal   = input.normal;
  53.         varying.uv       = input.uv;
  54.         let vp  = Matrix::mul(&uniform.view, &uniform.projection);
  55.         let mvp = Matrix::mul(&uniform.matrix, &vp);
  56.         Vector4::transform(&input.position, &mvp)
  57.     }
  58. }
  59. impl FragmentShader for StandardFragmentShader {
  60.     type Uniform = StandardUniform;
  61.     type Varying = StandardVarying;
  62.     fn main(&self, uniform: &Self::Uniform, varying: &Self::Varying) -> Vector4 {
  63.         Vector4::new(1.0, 1.0, 1.0, 1.0)
  64.     }
  65. }
  66.  
  67.  
  68. fn main() {
  69.  
  70.     let vs = StandardVertexShader;
  71.     let fs = StandardFragmentShader;
  72.     let un = StandardUniform {
  73.         matrix: Matrix::identity(),
  74.         projection: Matrix::perspective_fov(90.0, 1.0, 0.1, 1000.0),
  75.         view: Matrix::look_at(
  76.             &Vector3::new(0.0, 0.0, -2.0),
  77.             &Vector3::new(0.0, 0.0, 0.0),
  78.             &Vector3::new(0.0, 1f32, 0.0)
  79.         )
  80.     };
  81.  
  82.     let mut buffer = Buffer;
  83.     let geometry   = Geometry::obj("./monkey.obj").unwrap();
  84.     let geometry   = geometry.data.lock().unwrap();
  85.     for n in (0..geometry.indices.len()).step_by(3) {
  86.         let i0 = geometry.indices[n+0];
  87.         let i1 = geometry.indices[n+1];
  88.         let i2 = geometry.indices[n+2];
  89.         let v0 = &geometry.vertices[i0];
  90.         let v1 = &geometry.vertices[i1];
  91.         let v2 = &geometry.vertices[i2];
  92.         Triangle::triangle(&vs, &fs, &un,
  93.             v0,
  94.             v1,
  95.             v2,
  96.             &mut buffer
  97.         );
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement