2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 vg_tex2d tex_terrain_noise
= { .path
= "textures/garbage.qoi",
11 .flags
= VG_TEXTURE_NEAREST
};
13 VG_STATIC
void world_render_init(void)
15 vg_info( "Loading default world textures\n" );
17 vg_acquire_thread_sync();
19 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_noise
}, 1 );
21 vg_release_thread_sync();
24 VG_STATIC
void render_world_depth( m4x4f projection
, m4x3f camera
);
30 VG_STATIC
void bind_terrain_noise(void)
32 vg_tex2d_bind( &tex_terrain_noise
, 0 );
35 VG_STATIC
void world_render_if( enum mdl_shader shader
,
36 enum geo_type geo_type
,
37 void (*bind_point
)(struct world_material
*mat
))
40 for( int i
=0; i
<world
.material_count
; i
++ )
42 struct world_material
*mat
= &world
.materials
[i
];
44 if( mat
->info
.shader
== shader
)
48 if( geo_type
== k_geo_type_solid
)
51 sm
= &mat
->sm_no_collide
;
53 if( !sm
->indice_count
)
57 mdl_draw_submesh( sm
);
62 VG_STATIC
void world_render_both_stages( enum mdl_shader shader
,
63 void (*bind_point
)(struct world_material
*mat
))
65 mesh_bind( &world
.mesh_geo
);
66 world_render_if( shader
, k_geo_type_solid
, bind_point
);
67 mesh_bind( &world
.mesh_no_collide
);
68 world_render_if( shader
, k_geo_type_nonsolid
, bind_point
);
71 VG_STATIC
void bindpoint_diffuse_texture1( struct world_material
*mat
)
73 glActiveTexture( GL_TEXTURE1
);
74 glBindTexture( GL_TEXTURE_2D
, world
.textures
[ mat
->info
.tex_diffuse
] );
77 VG_STATIC
void render_world_vb( m4x4f projection
, v3f camera
)
79 m4x3f identity_matrix
;
80 m4x3_identity( identity_matrix
);
83 shader_vblend_uTexGarbage(0);
84 shader_vblend_uTexGradients(1);
85 shader_link_standard_ub( _shader_vblend
.id
, 2 );
86 vg_tex2d_bind( &tex_terrain_noise
, 0 );
88 shader_vblend_uPv( projection
);
89 shader_vblend_uMdl( identity_matrix
);
90 shader_vblend_uCamera( camera
);
92 world_render_both_stages( k_shader_standard_vertex_blend
,
93 bindpoint_diffuse_texture1
);
96 VG_STATIC
void render_world_standard( m4x4f projection
, v3f camera
)
98 m4x3f identity_matrix
;
99 m4x3_identity( identity_matrix
);
101 shader_standard_use();
102 shader_standard_uTexGarbage(0);
103 shader_standard_uTexMain(1);
104 shader_link_standard_ub( _shader_standard
.id
, 2 );
105 bind_terrain_noise();
107 shader_standard_uPv( projection
);
108 shader_standard_uMdl( identity_matrix
);
109 shader_standard_uCamera( camera
);
111 world_render_both_stages( k_shader_standard
,
112 bindpoint_diffuse_texture1
);
115 VG_STATIC
void render_world_alphatest( m4x4f projection
, v3f camera
)
117 m4x3f identity_matrix
;
118 m4x3_identity( identity_matrix
);
120 shader_alphatest_use();
121 shader_alphatest_uTexGarbage(0);
122 shader_alphatest_uTexMain(1);
123 shader_link_standard_ub( _shader_alphatest
.id
, 2 );
124 bind_terrain_noise();
126 shader_alphatest_uPv( projection
);
127 shader_alphatest_uMdl( identity_matrix
);
128 shader_alphatest_uCamera( camera
);
130 glDisable(GL_CULL_FACE
);
132 world_render_both_stages( k_shader_standard_cutout
,
133 bindpoint_diffuse_texture1
);
135 glEnable(GL_CULL_FACE
);
138 VG_STATIC
void bindpoint_terrain( struct world_material
*mat
)
140 glActiveTexture( GL_TEXTURE1
);
141 glBindTexture( GL_TEXTURE_2D
, world
.textures
[ mat
->info
.tex_diffuse
] );
143 shader_terrain_uSandColour( mat
->info
.colour
);
144 shader_terrain_uBlendOffset( mat
->info
.colour1
);
147 VG_STATIC
void render_terrain( m4x4f projection
, v3f camera
)
149 m4x3f identity_matrix
;
150 m4x3_identity( identity_matrix
);
152 shader_terrain_use();
153 shader_terrain_uTexGarbage(0);
154 shader_terrain_uTexGradients(1);
155 shader_link_standard_ub( _shader_terrain
.id
, 2 );
157 vg_tex2d_bind( &tex_terrain_noise
, 0 );
159 shader_terrain_uPv( projection
);
160 shader_terrain_uMdl( identity_matrix
);
161 shader_terrain_uCamera( camera
);
163 world_render_both_stages( k_shader_terrain_blend
, bindpoint_terrain
);
166 VG_STATIC
void render_lowerdome( m4x3f camera
)
168 m4x4f projection
, full
;
169 pipeline_projection( projection
, 0.4f
, 1000.0f
);
172 m3x3_transpose( camera
, inverse
);
173 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
174 m4x3_expand( inverse
, full
);
175 m4x4_mul( projection
, full
, full
);
177 m4x3f identity_matrix
;
178 m4x3_identity( identity_matrix
);
180 shader_planeinf_use();
181 shader_planeinf_uMdl(identity_matrix
);
182 shader_planeinf_uPv(full
);
183 shader_planeinf_uCamera(camera
[3]);
184 shader_planeinf_uPlane( (v4f
){0.0f
,1.0f
,0.0f
,0.0f
} );
186 mdl_draw_submesh( &world
.dome_lower
);
189 VG_STATIC
void render_sky(m4x3f camera
)
191 m4x4f projection
, full
;
192 pipeline_projection( projection
, 0.4f
, 1000.0f
);
195 m3x3_transpose( camera
, inverse
);
196 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
197 m4x3_expand( inverse
, full
);
198 m4x4_mul( projection
, full
, full
);
200 m4x3f identity_matrix
;
201 m4x3_identity( identity_matrix
);
204 shader_sky_uMdl(identity_matrix
);
205 shader_sky_uPv(full
);
206 shader_sky_uTexGarbage(0);
207 shader_sky_uTime( world
.sky_time
);
209 vg_tex2d_bind( &tex_terrain_noise
, 0 );
211 glDepthMask( GL_FALSE
);
212 glDisable( GL_DEPTH_TEST
);
214 mesh_bind( &world
.skydome
);
215 mdl_draw_submesh( &world
.dome_upper
);
217 glEnable( GL_DEPTH_TEST
);
218 glDepthMask( GL_TRUE
);
221 VG_STATIC
void render_world_gates( m4x4f projection
, v3f playerco
, m4x3f camera
)
223 if( !world
.gate_count
)
226 float closest
= INFINITY
;
229 for( int i
=0; i
<world
.gate_count
; i
++ )
231 struct route_gate
*rg
= &world
.gates
[i
];
232 float dist
= v3_dist2( rg
->gate
.co
[0], camera
[3] );
241 render_gate( &world
.gates
[id
].gate
, playerco
, camera
);
242 v3_lerp( world
.render_gate_pos
,
243 world
.gates
[id
].gate
.co
[0],
245 world
.render_gate_pos
);
248 VG_STATIC
void render_world( m4x4f projection
, m4x3f camera
)
250 render_sky( camera
);
251 render_world_routes( projection
, camera
[3] );
252 render_world_standard( projection
, camera
[3] );
253 render_world_vb( projection
, camera
[3] );
254 render_world_alphatest( projection
, camera
[3] );
255 render_terrain( projection
, camera
[3] );
258 float min_dist
= INFINITY
;
260 if( !world
.route_count
)
263 for( int i
=0; i
<world
.route_count
; i
++ )
265 float dist
= v3_dist2( world
.routes
[i
].scoreboard_transform
[3],
268 if( dist
< min_dist
)
275 sfd_render( projection
, camera
[3],
276 world
.routes
[closest
].scoreboard_transform
);
279 VG_STATIC
void render_world_depth( m4x4f projection
, m4x3f camera
)
281 m4x3f identity_matrix
;
282 m4x3_identity( identity_matrix
);
285 shader_gpos_uCamera( camera
[3] );
286 shader_gpos_uPv( projection
);
287 shader_gpos_uMdl( identity_matrix
);
289 mesh_bind( &world
.mesh_geo
);
290 mesh_draw( &world
.mesh_geo
);
293 glDisable(GL_CULL_FACE
);
294 scene_bind( &world
.foliage
);
295 scene_draw( &world
.foliage
);
296 glEnable(GL_CULL_FACE
);
300 #endif /* WORLD_RENDER_H */