1
1
use crate :: base:: { Context , SimpleMesh , Triangle } ;
2
+ use termion:: { color} ;
2
3
use nalgebra:: { Matrix4 , Vector4 } ;
3
- use termion:: color;
4
4
5
5
pub fn default_shader ( shade : f32 ) -> String {
6
6
let string = if shade <= 0.20 {
@@ -37,44 +37,25 @@ fn orient_triangle(triangle: &Triangle) -> f32 {
37
37
}
38
38
39
39
// Writes multiple meshes to context
40
- pub fn draw_mesh < F > ( context : & mut Context , mesh : & SimpleMesh , transform : Matrix4 < f32 > , shader : F )
41
- where
42
- F : Fn ( f32 ) -> String ,
43
- {
40
+ pub fn draw_mesh < F > ( context : & mut Context , mesh : & SimpleMesh , transform : Matrix4 < f32 > , shader : F ) where F : Fn ( f32 ) -> String {
44
41
for triangle in & mesh. triangles {
45
42
draw_triangle ( context, & triangle, transform, & shader) ;
46
43
}
47
44
}
48
45
49
46
fn bg ( src : String , bg : ( u8 , u8 , u8 ) ) -> String {
50
- format ! (
51
- "{}{}{}{}" ,
52
- color:: Fg ( color:: Rgb ( bg. 0 , bg. 1 , bg. 2 ) ) ,
53
- color:: Bg ( color:: Rgb ( 25 , 25 , 25 ) ) ,
54
- src,
55
- color:: Fg ( color:: Reset )
56
- )
47
+ format ! ( "{}{}{}{}" , color:: Fg ( color:: Rgb ( bg. 0 , bg. 1 , bg. 2 ) ) ,
48
+ color:: Bg ( color:: Rgb ( 25 , 25 , 25 ) ) ,
49
+ src,
50
+ color:: Fg ( color:: Reset ) )
57
51
}
58
52
59
- pub fn draw_triangle < F > (
60
- context : & mut Context ,
61
- triangle : & Triangle ,
62
- transform : Matrix4 < f32 > ,
63
- shader : F ,
64
- ) where
65
- F : Fn ( f32 ) -> String ,
66
- {
53
+ pub fn draw_triangle < F > ( context : & mut Context , triangle : & Triangle , transform : Matrix4 < f32 > , shader : F ) where F : Fn ( f32 ) -> String {
67
54
let mut dist_triangle = triangle. clone ( ) ;
68
55
dist_triangle. mul ( context. utransform * transform) ;
69
56
let aabb = dist_triangle. to_aabb ( ) ; // Calculate triangle bounds
70
- let mins: ( usize , usize ) = (
71
- aabb. min [ 0 ] . max ( 1.0 ) . ceil ( ) as usize ,
72
- aabb. min [ 1 ] . max ( 1.0 ) . ceil ( ) as usize ,
73
- ) ;
74
- let maxs: ( usize , usize ) = (
75
- ( aabb. max [ 0 ] * 2.0 ) . min ( ( context. width - 1 ) as f32 ) . ceil ( ) as usize ,
76
- aabb. max [ 1 ] . min ( ( context. height - 1 ) as f32 ) . ceil ( ) as usize ,
77
- ) ;
57
+ let mins: ( usize , usize ) = ( aabb. min [ 0 ] . max ( 1.0 ) . ceil ( ) as usize , aabb. min [ 1 ] . max ( 1.0 ) . ceil ( ) as usize ) ;
58
+ let maxs: ( usize , usize ) = ( ( aabb. max [ 0 ] * 2.0 ) . min ( ( context. width - 1 ) as f32 ) . ceil ( ) as usize , aabb. max [ 1 ] . min ( ( context. height - 1 ) as f32 ) . ceil ( ) as usize ) ;
78
59
let a = 1.0 / orient_triangle ( & dist_triangle) ;
79
60
80
61
for y in mins. 1 ..maxs. 1 {
@@ -87,13 +68,13 @@ pub fn draw_triangle<F>(
87
68
let pixel_shade = dist_triangle. normal ( ) . z * a * ( w0 + w1 + w2) ;
88
69
let z = dist_triangle. v1 [ 2 ]
89
70
+ a * ( w1 * ( dist_triangle. v2 [ 2 ] - dist_triangle. v1 [ 2 ] )
90
- + w2 * ( dist_triangle. v3 [ 2 ] - dist_triangle. v1 [ 2 ] ) ) ;
71
+ + w2 * ( dist_triangle. v3 [ 2 ] - dist_triangle. v1 [ 2 ] ) ) ;
91
72
let id = y * context. width + x * 2 ;
92
73
if z < context. z_buffer [ id] {
93
74
context. z_buffer [ id] = z;
94
75
let pixel = bg ( shader ( pixel_shade) , dist_triangle. color ) ;
95
76
context. frame_buffer [ id] = ( & pixel) . to_string ( ) ;
96
- context. frame_buffer [ id + 1 ] = ( & pixel) . to_string ( ) ;
77
+ context. frame_buffer [ id+ 1 ] = ( & pixel) . to_string ( ) ;
97
78
}
98
79
}
99
80
}
0 commit comments