gate frame fix
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.h
1 #ifndef WORLD_RENDER_H
2 #define WORLD_RENDER_H
3
4 #include "world.h"
5
6 vg_tex2d tex_terrain_colours = { .path = "textures/gradients.qoi",
7 .flags = VG_TEXTURE_CLAMP|VG_TEXTURE_NEAREST };
8
9 vg_tex2d tex_terrain_noise = { .path = "textures/garbage.qoi",
10 .flags = VG_TEXTURE_NEAREST };
11
12 vg_tex2d tex_alphatest = { .path = "textures/alphatest.qoi",
13 .flags = VG_TEXTURE_NEAREST };
14
15 vg_tex2d tex_graffiti = { .path = "textures/graffitibox.qoi",
16 .flags = VG_TEXTURE_NEAREST };
17
18 static void world_render_init(void)
19 {
20 vg_tex2d_init( (vg_tex2d *[]){ &tex_terrain_colours,
21 &tex_terrain_noise,
22 &tex_alphatest,
23 &tex_graffiti }, 4 );
24 }
25
26
27
28 static void render_world_depth( m4x4f projection, m4x3f camera );
29
30
31
32
33 /*
34 * Rendering
35 */
36
37 static void bind_terrain_textures(void)
38 {
39 vg_tex2d_bind( &tex_terrain_noise, 0 );
40 vg_tex2d_bind( &tex_terrain_colours, 1 );
41 }
42
43 static void render_world_vb( m4x4f projection, v3f camera )
44 {
45 m4x3f identity_matrix;
46 m4x3_identity( identity_matrix );
47
48 shader_vblend_use();
49 shader_vblend_uTexGarbage(0);
50 shader_vblend_uTexGradients(1);
51 shader_link_standard_ub( _shader_vblend.id, 2 );
52 bind_terrain_textures();
53
54 shader_vblend_uPv( projection );
55 shader_vblend_uMdl( identity_matrix );
56 shader_vblend_uCamera( camera );
57
58 scene_bind( &world.geo );
59 mdl_draw_submesh( &world.sm_geo_vb );
60
61 mesh_bind( &world.cars );
62
63 #if 0
64 for( int i=0; i<vg_list_size(world.van_man); i++ )
65 {
66 shader_vblend_uMdl( world.van_man[i].transform );
67 mdl_draw_submesh( &world.car_holden );
68 }
69 #endif
70 }
71
72 static void render_world_alphatest( m4x4f projection, v3f camera )
73 {
74 m4x3f identity_matrix;
75 m4x3_identity( identity_matrix );
76
77 shader_alphatest_use();
78 shader_alphatest_uTexGarbage(0);
79 shader_alphatest_uTexMain(1);
80 shader_link_standard_ub( _shader_alphatest.id, 2 );
81
82 vg_tex2d_bind( &tex_terrain_noise, 0 );
83 vg_tex2d_bind( &tex_alphatest, 1 );
84
85 shader_alphatest_uPv( projection );
86 shader_alphatest_uMdl( identity_matrix );
87 shader_alphatest_uCamera( camera );
88
89 glDisable(GL_CULL_FACE);
90 scene_bind( &world.foliage );
91 mdl_draw_submesh( &world.sm_foliage_alphatest );
92
93 vg_tex2d_bind( &tex_graffiti, 1 );
94 mdl_draw_submesh( &world.sm_graffiti );
95
96 glEnable(GL_CULL_FACE);
97 }
98
99 static void render_terrain( m4x4f projection, v3f camera )
100 {
101 m4x3f identity_matrix;
102 m4x3_identity( identity_matrix );
103
104 shader_terrain_use();
105 shader_terrain_uTexGarbage(0);
106 shader_terrain_uTexGradients(1);
107 shader_link_standard_ub( _shader_terrain.id, 2 );
108 bind_terrain_textures();
109
110 shader_terrain_uPv( projection );
111 shader_terrain_uMdl( identity_matrix );
112 shader_terrain_uCamera( camera );
113
114 scene_bind( &world.geo );
115 mdl_draw_submesh( &world.sm_terrain );
116 mdl_draw_submesh( &world.sm_geo_std_oob );
117 mdl_draw_submesh( &world.sm_geo_std );
118 mdl_draw_submesh( &world.sm_subworld );
119
120 /* TODO: Dont draw in reflection */
121 glDisable(GL_CULL_FACE);
122 scene_bind( &world.foliage );
123 mdl_draw_submesh( &world.sm_foliage_main );
124 glEnable(GL_CULL_FACE);
125 }
126
127 static void render_lowerdome( m4x3f camera )
128 {
129 m4x4f projection, full;
130 pipeline_projection( projection, 0.4f, 1000.0f );
131
132 m4x3f inverse;
133 m3x3_transpose( camera, inverse );
134 v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
135 m4x3_expand( inverse, full );
136 m4x4_mul( projection, full, full );
137
138 m4x3f identity_matrix;
139 m4x3_identity( identity_matrix );
140
141 shader_planeinf_use();
142 shader_planeinf_uMdl(identity_matrix);
143 shader_planeinf_uPv(full);
144 shader_planeinf_uCamera(camera[3]);
145 shader_planeinf_uPlane( (v4f){0.0f,1.0f,0.0f,0.0f} );
146
147 mdl_draw_submesh( &world.dome_lower );
148 }
149
150 static void render_sky(m4x3f camera)
151 {
152 m4x4f projection, full;
153 pipeline_projection( projection, 0.4f, 1000.0f );
154
155 m4x3f inverse;
156 m3x3_transpose( camera, inverse );
157 v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
158 m4x3_expand( inverse, full );
159 m4x4_mul( projection, full, full );
160
161 m4x3f identity_matrix;
162 m4x3_identity( identity_matrix );
163
164 shader_sky_use();
165 shader_sky_uMdl(identity_matrix);
166 shader_sky_uPv(full);
167 shader_sky_uTexGarbage(0);
168 shader_sky_uTime( vg_time );
169
170 vg_tex2d_bind( &tex_terrain_noise, 0 );
171
172 glDepthMask( GL_FALSE );
173 glDisable( GL_DEPTH_TEST );
174
175 mesh_bind( &world.skydome );
176 mdl_draw_submesh( &world.dome_upper );
177
178 glEnable( GL_DEPTH_TEST );
179 glDepthMask( GL_TRUE );
180 }
181
182 static void render_world_gates( m4x4f projection, v3f playerco, m4x3f camera )
183 {
184 float closest = INFINITY;
185 int id = 0;
186
187 for( int i=0; i<world.routes.gate_count; i++ )
188 {
189 struct route_gate *rg = &world.routes.gates[i];
190 float dist = v3_dist2( rg->gate.co[0], camera[3] );
191
192 if( dist < closest )
193 {
194 closest = dist;
195 id = i;
196 }
197 }
198
199 render_gate( &world.routes.gates[id].gate, playerco, camera );
200 v3_lerp( world.render_gate_pos,
201 world.routes.gates[id].gate.co[0],
202 1.0f,
203 world.render_gate_pos );
204 }
205
206 static void render_world( m4x4f projection, m4x3f camera )
207 {
208 render_sky( camera );
209 render_world_routes( projection, camera[3] );
210 render_world_vb( projection, camera[3] );
211 render_world_alphatest( projection, camera[3] );
212 render_terrain( projection, camera[3] );
213
214 int closest = 0;
215 float min_dist = INFINITY;
216
217 for( int i=0; i<world.routes.route_count; i++ )
218 {
219 float dist = v3_dist2( world.routes.routes[i].scoreboard_transform[3],
220 camera[3] );
221
222 if( dist < min_dist )
223 {
224 min_dist = dist;
225 closest = i;
226 }
227 }
228
229 sfd_render( &world.sfd.tester, projection, camera[3],
230 world.routes.routes[closest].scoreboard_transform );
231 }
232
233 static void render_world_depth( m4x4f projection, m4x3f camera )
234 {
235 m4x3f identity_matrix;
236 m4x3_identity( identity_matrix );
237
238 shader_gpos_use();
239 shader_gpos_uCamera( camera[3] );
240 shader_gpos_uPv( projection );
241 shader_gpos_uMdl( identity_matrix );
242
243 scene_bind( &world.geo );
244 scene_draw( &world.geo );
245
246 #if 0
247 glDisable(GL_CULL_FACE);
248 scene_bind( &world.foliage );
249 scene_draw( &world.foliage );
250 glEnable(GL_CULL_FACE);
251 #endif
252 }
253
254 #endif /* WORLD_RENDER_H */