vpk loading/models
[csRadar.git] / csrDraw.h
1 typedef struct csr_frag csr_frag;
2
3 struct csr_frag
4 {
5 u32 id; // Triangle index
6 float qa, qb; // Quantities
7
8 float depth; // 'depth testing'
9 };
10
11 void clear_depth( csr_frag fragments[], u32 x, u32 y )
12 {
13 for( u32 i = 0; i < x*y; i ++ )
14 {
15 fragments[ i ].depth = INFINITY;
16 }
17 }
18
19 void simple_raster( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert tri[3], int id )
20 {
21 // Very simplified tracing algorithm
22
23 v2f bmin = { 0.f, 0.f };
24 v2f bmax = { x, y };
25
26 v2_minv( tri[0].co, tri[1].co, bmin );
27 v2_minv( tri[2].co, bmin, bmin );
28
29 v2_maxv( tri[0].co, tri[1].co, bmax );
30 v2_maxv( tri[2].co, bmax, bmax );
31
32 float range_x = (cam_bounds[2]-cam_bounds[0])/(float)x;
33 float range_y = (cam_bounds[3]-cam_bounds[1])/(float)y;
34
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 ));
39
40 v3f trace_dir = { 0.f, 0.f, -1.f };
41 v3f trace_origin = { 0.f, 0.f, 16385.f };
42
43 for( u32 py = start_y; py < end_y; py ++ )
44 {
45 trace_origin[1] = csr_lerpf( cam_bounds[1], cam_bounds[3], (float)py/(float)y );
46
47 for( u32 px = start_x; px < end_x; px ++ )
48 {
49 trace_origin[0] = csr_lerpf( cam_bounds[0], cam_bounds[2], (float)px/(float)x );
50
51 csr_frag *frag = &fragments[ py*y + px ];
52
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 );
55
56 if( tdepth < frag->depth )
57 {
58 frag->depth = tdepth;
59 frag->id = id;
60 frag->qa = tqa;
61 frag->qb = tqb;
62 }
63 }
64 }
65 }
66
67 // First pass 'fragmentize'
68 void draw_buffers( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert *triangles, u32 triangle_count )
69 {
70 for( u32 i = 0; i < triangle_count; i ++ )
71 {
72 vmf_vert *triangle = triangles + i*3;
73 simple_raster( fragments, x, y, cam_bounds, triangle, i );
74 }
75 }