Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use black::device::{ FragmentShader, VertexShader, Varying, Triangle, Target };
- use black::math::{Matrix, Vector2, Vector3, Vector4};
- use black::scene::{ Geometry, Vertex };
- pub struct Buffer;
- impl Target for Buffer {
- fn width(&self) -> i32 { 100 }
- fn height(&self) -> i32 { 100 }
- fn set(&mut self, x: i32, y: i32, color: Vector4) {
- println!("{} {}: {}", x, y, color)
- }
- }
- pub struct StandardUniform {
- pub matrix: Matrix,
- pub view: Matrix,
- pub projection: Matrix,
- }
- struct StandardVertexShader;
- struct StandardFragmentShader;
- struct StandardVarying {
- pub position: Vector4,
- pub normal: Vector3,
- pub uv: Vector2,
- }
- impl Varying for StandardVarying {
- fn new() -> StandardVarying {
- StandardVarying {
- position: Vector4::zero(),
- normal: Vector3::zero(),
- uv: Vector2::zero(),
- }
- }
- fn interpolate(
- v0: &StandardVarying,
- v1: &StandardVarying,
- v2: &StandardVarying,
- w0: &f32,
- w1: &f32,
- w2: &f32
- ) -> StandardVarying {
- StandardVarying::new()
- }
- }
- impl VertexShader for StandardVertexShader {
- type Uniform = StandardUniform;
- type Vertex = Vertex;
- type Varying = StandardVarying;
- fn main(&self, uniform: &StandardUniform, input: &Vertex, varying: &mut StandardVarying) -> Vector4 {
- varying.position = input.position;
- varying.normal = input.normal;
- varying.uv = input.uv;
- let vp = Matrix::mul(&uniform.view, &uniform.projection);
- let mvp = Matrix::mul(&uniform.matrix, &vp);
- Vector4::transform(&input.position, &mvp)
- }
- }
- impl FragmentShader for StandardFragmentShader {
- type Uniform = StandardUniform;
- type Varying = StandardVarying;
- fn main(&self, uniform: &Self::Uniform, varying: &Self::Varying) -> Vector4 {
- Vector4::new(1.0, 1.0, 1.0, 1.0)
- }
- }
- fn main() {
- let vs = StandardVertexShader;
- let fs = StandardFragmentShader;
- let un = StandardUniform {
- matrix: Matrix::identity(),
- projection: Matrix::perspective_fov(90.0, 1.0, 0.1, 1000.0),
- view: Matrix::look_at(
- &Vector3::new(0.0, 0.0, -2.0),
- &Vector3::new(0.0, 0.0, 0.0),
- &Vector3::new(0.0, 1f32, 0.0)
- )
- };
- let mut buffer = Buffer;
- let geometry = Geometry::obj("./monkey.obj").unwrap();
- let geometry = geometry.data.lock().unwrap();
- for n in (0..geometry.indices.len()).step_by(3) {
- let i0 = geometry.indices[n+0];
- let i1 = geometry.indices[n+1];
- let i2 = geometry.indices[n+2];
- let v0 = &geometry.vertices[i0];
- let v1 = &geometry.vertices[i1];
- let v2 = &geometry.vertices[i2];
- Triangle::triangle(&vs, &fs, &un,
- v0,
- v1,
- v2,
- &mut buffer
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement