1 typedef struct csr_frag csr_frag
;
5 u32 id
; // Triangle index
6 float qa
, qb
; // Quantities
8 float depth
; // 'depth testing'
11 void clear_depth( csr_frag fragments
[], u32 x
, u32 y
)
13 for( u32 i
= 0; i
< x
*y
; i
++ )
15 fragments
[ i
].depth
= INFINITY
;
19 void simple_raster( csr_frag fragments
[], u32 x
, u32 y
, v4f cam_bounds
, vmf_vert tri
[3], int id
)
21 // Very simplified tracing algorithm
23 v2f bmin
= { 0.f
, 0.f
};
26 v2_minv( tri
[0].co
, tri
[1].co
, bmin
);
27 v2_minv( tri
[2].co
, bmin
, bmin
);
29 v2_maxv( tri
[0].co
, tri
[1].co
, bmax
);
30 v2_maxv( tri
[2].co
, bmax
, bmax
);
32 float range_x
= (cam_bounds
[2]-cam_bounds
[0])/(float)x
;
33 float range_y
= (cam_bounds
[3]-cam_bounds
[1])/(float)y
;
35 int start_x
= csr_max( 0, floorf( (bmin
[0]-cam_bounds
[0])/range_x
));
36 int end_x
= csr_min( x
, floorf( (bmax
[0]-cam_bounds
[0])/range_x
));
37 int start_y
= csr_max( 0, ceilf( (bmin
[1]-cam_bounds
[1])/range_y
));
38 int end_y
= csr_min( y
, ceilf( (bmax
[1]-cam_bounds
[1])/range_y
));
40 v3f trace_dir
= { 0.f
, 0.f
, -1.f
};
41 v3f trace_origin
= { 0.f
, 0.f
, 16385.f
};
43 for( u32 py
= start_y
; py
< end_y
; py
++ )
45 trace_origin
[1] = csr_lerpf( cam_bounds
[1], cam_bounds
[3], (float)py
/(float)y
);
47 for( u32 px
= start_x
; px
< end_x
; px
++ )
49 trace_origin
[0] = csr_lerpf( cam_bounds
[0], cam_bounds
[2], (float)px
/(float)x
);
51 csr_frag
*frag
= &fragments
[ py
*y
+ px
];
53 float tqa
= 0.f
, tqb
= 0.f
;
54 float tdepth
= csr_ray_tri( trace_origin
, trace_dir
, tri
[0].co
, tri
[1].co
, tri
[2].co
, &tqa
, &tqb
);
56 if( tdepth
< frag
->depth
)
67 // First pass 'fragmentize'
68 void draw_buffers( csr_frag fragments
[], u32 x
, u32 y
, v4f cam_bounds
, vmf_vert
*triangles
, u32 triangle_count
)
70 for( u32 i
= 0; i
< triangle_count
; i
++ )
72 vmf_vert
*triangle
= triangles
+ i
*3;
73 simple_raster( fragments
, x
, y
, cam_bounds
, triangle
, i
);