fixed m4x3 mul, rendering works
[csRadar.git] / csrDraw.h
1 typedef struct csr_frag csr_frag;
2 typedef struct csr_target csr_target;
3
4 struct csr_frag
5 {
6 u32 id; // Triangle index
7 float depth; // 'depth testing'
8
9 v3f co;
10 v3f nrm;
11 };
12
13 struct csr_target
14 {
15 csr_frag *fragments;
16 u32 x, y;
17 v4f bounds;
18 };
19
20 void csr_rt_clear( csr_target *rt )
21 {
22 for( u32 i = 0; i < rt->x*rt->y; i ++ )
23 {
24 rt->fragments[ i ].depth = 0.f;
25 }
26 }
27
28 void simple_raster( csr_target *rt, vmf_vert tri[3], int id )
29 {
30 // Very simplified tracing algorithm
31 float tqa = 0.f, tqb = 0.f;
32
33 v2f bmin = { 0.f, 0.f };
34 v2f bmax = { rt->x, rt->y };
35
36 v2_minv( tri[0].co, tri[1].co, bmin );
37 v2_minv( tri[2].co, bmin, bmin );
38
39 v2_maxv( tri[0].co, tri[1].co, bmax );
40 v2_maxv( tri[2].co, bmax, bmax );
41
42 float range_x = (rt->bounds[2]-rt->bounds[0])/(float)rt->x;
43 float range_y = (rt->bounds[3]-rt->bounds[1])/(float)rt->y;
44
45 int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0])/range_x)));
46 int end_x = csr_max( 0, csr_min( rt->x-1, floorf( (bmax[0]-rt->bounds[0])/range_x )));
47 int start_y = csr_min( rt->y-1, csr_max( 0, ceilf( (bmin[1]-rt->bounds[1])/range_y )));
48 int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[1])/range_y )));
49
50 v3f trace_dir = { 0.f, 0.f, 1.f };
51 v3f trace_origin = { 0.f, 0.f, -16385.f };
52
53 for( u32 py = start_y; py <= end_y; py ++ )
54 {
55 trace_origin[1] = csr_lerpf( rt->bounds[1], rt->bounds[3], (float)py/(float)rt->y );
56
57 for( u32 px = start_x; px <= end_x; px ++ )
58 {
59 csr_frag *frag = &rt->fragments[ py * rt->y + px ];
60
61 trace_origin[0] = csr_lerpf( rt->bounds[0], rt->bounds[2], (float)px/(float)rt->x );
62 float tdepth = csr_ray_tri( trace_origin, trace_dir, tri[0].co, tri[1].co, tri[2].co, &tqa, &tqb );
63
64 if( tdepth > frag->depth )
65 {
66 frag->depth = tdepth;
67
68 v3_muls( tri[1].co, tqa, frag->co );
69 v3_muladds( frag->co, tri[2].co, tqb, frag->co );
70 v3_muladds( frag->co, tri[0].co, 1.f - tqa - tqb, frag->co );
71 }
72 }
73 }
74 }
75
76 void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform )
77 {
78 m3x3f normal;
79 vmf_vert new_tri[3];
80
81 // Derive normal matrix
82 m4x3_to_3x3( transform, normal );
83 m3x3_inv_transpose( normal, normal );
84
85 for( u32 i = 0; i < triangle_count; i ++ )
86 {
87 vmf_vert *triangle = triangles + i*3;
88
89 m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
90 m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
91 m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
92 m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
93 m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
94 m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
95
96 simple_raster( rt, new_tri, 0 );
97 }
98 }
99
100 void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, int const group, m4x3f prev, m4x3f inst )
101 {
102 m4x3f transform = M4X3_IDENTITY;
103 vmf_solid solid;
104 vmf_vert tri[3];
105
106 // Multiply previous transform with instance transform to create basis
107 if( prev )
108 {
109 m4x3_mul( prev, inst, transform );
110 }
111
112 // Draw brushes
113 solidgen_ctx_init( &solid );
114 vdf_node *world = vdf_next( root, "world", NULL );
115
116 vdf_foreach( world, "solid", brush )
117 {
118 solidgen_push( &solid, brush );
119 }
120
121 for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ )
122 {
123 u32 * base = solid.indices + i*3;
124
125 tri[0] = solid.verts[ base[0] ];
126 tri[1] = solid.verts[ base[1] ];
127 tri[2] = solid.verts[ base[2] ];
128
129 csr_draw( rt, tri, 1, transform );
130 }
131
132 solidgen_ctx_reset( &solid );
133
134 // Actual entity loop
135 m4x3f model;
136
137 vdf_foreach( root, "entity", ent )
138 {
139 if( ent->user & VMF_FLAG_IS_PROP )
140 {
141 // Create model transform
142 m4x3_identity( model );
143
144 vmf_entity_transform( ent, model );
145 m4x3_mul( transform, model, model );
146
147 // Draw model
148 mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl;
149 for( int i = 0; i < mdl->num_indices/3; i ++ )
150 {
151 for( int j = 0; j < 3; j ++ )
152 {
153 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ], tri[j].co );
154 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
155 tri[j].xy[0] = 0.f;
156 tri[j].xy[1] = 0.f;
157 }
158
159 csr_draw( rt, tri, 1, model );
160 }
161 }
162 else if( ent->user & VMF_FLAG_IS_INSTANCE )
163 {
164 m4x3_identity( model );
165 vmf_entity_transform( ent, model );
166
167 draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, group, transform, model );
168 }
169 else if( ent->user & VMF_FLAG_BRUSH_ENT )
170 {
171 // ...
172 }
173 }
174
175 solidgen_ctx_free( &solid );
176 }