assorted crap
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.h
index 32a15baeddd6e0bdf84769f41f2c88b4ec73adc0..ae9399bf8e02d711484159672c0384bd8aaabc9b 100644 (file)
@@ -7,58 +7,74 @@
 
 #include "world.h"
 
-vg_tex2d tex_terrain_colours = { .path = "textures/gradients.qoi",
-                                 .flags = VG_TEXTURE_CLAMP|VG_TEXTURE_NEAREST };
-
 vg_tex2d tex_terrain_noise = { .path = "textures/garbage.qoi",
                                  .flags = VG_TEXTURE_NEAREST };
 
-vg_tex2d tex_alphatest = { .path = "textures/alphatest.qoi",
-                                 .flags = VG_TEXTURE_NEAREST };
-
-vg_tex2d tex_graffiti = { .path = "textures/graffitibox.qoi",
-                                 .flags = VG_TEXTURE_NEAREST };
-
-static void world_render_init(void)
+VG_STATIC void world_render_init(void)
 {
    vg_info( "Loading default world textures\n" );
 
    vg_acquire_thread_sync();
    {
-      vg_tex2d_init( (vg_tex2d *[]){ &tex_terrain_colours, 
-                                     &tex_terrain_noise,
-                                     &tex_alphatest,
-                                     &tex_graffiti }, 4 );
+      vg_tex2d_init( (vg_tex2d *[]){ &tex_terrain_noise }, 1 );
    }
    vg_release_thread_sync();
 }
 
-static void world_render_free(void*_)
+VG_STATIC void render_world_depth( m4x4f projection, m4x3f camera );
+
+/*
+ * Rendering
+ */
+
+VG_STATIC void bind_terrain_noise(void)
 {
-   vg_tex2d_free( (vg_tex2d *[]){ &tex_terrain_colours, 
-                                  &tex_terrain_noise,
-                                  &tex_alphatest,
-                                  &tex_graffiti }, 4 );
+   vg_tex2d_bind( &tex_terrain_noise, 0 );
 }
 
+VG_STATIC void world_render_if( enum mdl_shader shader, 
+                                enum geo_type geo_type,
+                                void (*bind_point)(struct world_material *mat))
+{
+   
+   for( int i=0; i<world.material_count; i++ )
+   {
+      struct world_material *mat = &world.materials[i];
 
+      if( mat->info.shader == shader )
+      {
+         mdl_submesh *sm;
 
-static void render_world_depth( m4x4f projection, m4x3f camera );
-
+         if( geo_type == k_geo_type_solid )
+            sm = &mat->sm_geo;
+         else
+            sm = &mat->sm_no_collide;
 
+         if( !sm->indice_count )
+            continue;
 
+         bind_point( mat );
+         mdl_draw_submesh( sm );
+      }
+   }
+}
 
-/*
- * Rendering
- */
+VG_STATIC void world_render_both_stages( enum mdl_shader shader,
+                                void (*bind_point)(struct world_material *mat))
+{
+   mesh_bind( &world.mesh_geo );
+   world_render_if( shader, k_geo_type_solid, bind_point );
+   mesh_bind( &world.mesh_no_collide );
+   world_render_if( shader, k_geo_type_nonsolid, bind_point );
+}
 
-static void bind_terrain_textures(void)
+VG_STATIC void bindpoint_diffuse_texture1( struct world_material *mat )
 {
-   vg_tex2d_bind( &tex_terrain_noise, 0 );
-   vg_tex2d_bind( &tex_terrain_colours, 1 );
+   glActiveTexture( GL_TEXTURE1 );
+   glBindTexture( GL_TEXTURE_2D, world.textures[ mat->info.tex_diffuse ] );
 }
 
-static void render_world_vb( m4x4f projection, v3f camera )
+VG_STATIC void render_world_vb( m4x4f projection, v3f camera )
 {
    m4x3f identity_matrix;
    m4x3_identity( identity_matrix );
@@ -67,27 +83,36 @@ static void render_world_vb( m4x4f projection, v3f camera )
    shader_vblend_uTexGarbage(0);
    shader_vblend_uTexGradients(1);
    shader_link_standard_ub( _shader_vblend.id, 2 );
-   bind_terrain_textures();
+   vg_tex2d_bind( &tex_terrain_noise, 0 );
 
    shader_vblend_uPv( projection );
    shader_vblend_uMdl( identity_matrix );
    shader_vblend_uCamera( camera );
 
-   scene_bind( &world.geo );
-   mdl_draw_submesh( &world.sm_geo_vb );
+   world_render_both_stages( k_shader_standard_vertex_blend, 
+                             bindpoint_diffuse_texture1 );
+}
 
-   mesh_bind( &world.cars );
+VG_STATIC void render_world_standard( m4x4f projection, v3f camera )
+{
+   m4x3f identity_matrix;
+   m4x3_identity( identity_matrix );
 
-#if 0
-   for( int i=0; i<vg_list_size(world.van_man); i++ )
-   {
-      shader_vblend_uMdl( world.van_man[i].transform );
-      mdl_draw_submesh( &world.car_holden );
-   }
-#endif
+   shader_standard_use();
+   shader_standard_uTexGarbage(0);
+   shader_standard_uTexMain(1);
+   shader_link_standard_ub( _shader_standard.id, 2 );
+   bind_terrain_noise();
+
+   shader_standard_uPv( projection );
+   shader_standard_uMdl( identity_matrix );
+   shader_standard_uCamera( camera );
+   
+   world_render_both_stages( k_shader_standard,
+                             bindpoint_diffuse_texture1 );
 }
 
-static void render_world_alphatest( m4x4f projection, v3f camera )
+VG_STATIC void render_world_alphatest( m4x4f projection, v3f camera )
 {
    m4x3f identity_matrix;
    m4x3_identity( identity_matrix );
@@ -96,25 +121,30 @@ static void render_world_alphatest( m4x4f projection, v3f camera )
    shader_alphatest_uTexGarbage(0);
    shader_alphatest_uTexMain(1);
    shader_link_standard_ub( _shader_alphatest.id, 2 );
-
-   vg_tex2d_bind( &tex_terrain_noise, 0 );
-   vg_tex2d_bind( &tex_alphatest, 1 );
+   bind_terrain_noise();
 
    shader_alphatest_uPv( projection );
    shader_alphatest_uMdl( identity_matrix );
    shader_alphatest_uCamera( camera );
 
    glDisable(GL_CULL_FACE);
-   scene_bind( &world.foliage );
-   mdl_draw_submesh( &world.sm_foliage_alphatest );
-
-   vg_tex2d_bind( &tex_graffiti, 1 );
-   mdl_draw_submesh( &world.sm_graffiti );
+   
+   world_render_both_stages( k_shader_standard_cutout,
+                             bindpoint_diffuse_texture1 );
 
    glEnable(GL_CULL_FACE);
 }
 
-static void render_terrain( m4x4f projection, v3f camera )
+VG_STATIC void bindpoint_terrain( struct world_material *mat )
+{
+   glActiveTexture( GL_TEXTURE1 );
+   glBindTexture( GL_TEXTURE_2D, world.textures[ mat->info.tex_diffuse ] );
+
+   shader_terrain_uSandColour( mat->info.colour );
+   shader_terrain_uBlendOffset( mat->info.colour1 );
+}
+
+VG_STATIC void render_terrain( m4x4f projection, v3f camera )
 {
    m4x3f identity_matrix;
    m4x3_identity( identity_matrix );
@@ -123,26 +153,17 @@ static void render_terrain( m4x4f projection, v3f camera )
    shader_terrain_uTexGarbage(0);
    shader_terrain_uTexGradients(1);
    shader_link_standard_ub( _shader_terrain.id, 2 );
-   bind_terrain_textures();
+
+   vg_tex2d_bind( &tex_terrain_noise, 0 );
 
    shader_terrain_uPv( projection );
    shader_terrain_uMdl( identity_matrix );
    shader_terrain_uCamera( camera );
 
-   scene_bind( &world.geo );
-   mdl_draw_submesh( &world.sm_terrain );
-   mdl_draw_submesh( &world.sm_geo_std_oob );
-   mdl_draw_submesh( &world.sm_geo_std );
-   mdl_draw_submesh( &world.sm_subworld );
-
-   /* TODO: Dont draw in reflection */
-   glDisable(GL_CULL_FACE);
-   scene_bind( &world.foliage );
-   mdl_draw_submesh( &world.sm_foliage_main );
-   glEnable(GL_CULL_FACE);
+   world_render_both_stages( k_shader_terrain_blend, bindpoint_terrain );
 }
 
-static void render_lowerdome( m4x3f camera )
+VG_STATIC void render_lowerdome( m4x3f camera )
 {
    m4x4f projection, full;
    pipeline_projection( projection, 0.4f, 1000.0f );
@@ -165,7 +186,7 @@ static void render_lowerdome( m4x3f camera )
    mdl_draw_submesh( &world.dome_lower );
 }
 
-static void render_sky(m4x3f camera)
+VG_STATIC void render_sky(m4x3f camera)
 {
    m4x4f projection, full;
    pipeline_projection( projection, 0.4f, 1000.0f );
@@ -197,14 +218,17 @@ static void render_sky(m4x3f camera)
    glDepthMask( GL_TRUE );
 }
 
-static void render_world_gates( m4x4f projection, v3f playerco, m4x3f camera )
+VG_STATIC void render_world_gates( m4x4f projection, v3f playerco, m4x3f camera )
 {
+   if( !world.gate_count )
+      return;
+
    float closest = INFINITY;
    int   id = 0;
 
-   for( int i=0; i<world.routes.gate_count; i++ )
+   for( int i=0; i<world.gate_count; i++ )
    {
-      struct route_gate *rg = &world.routes.gates[i];
+      struct route_gate *rg = &world.gates[i];
       float dist = v3_dist2( rg->gate.co[0], camera[3] );
 
       if( dist < closest )
@@ -214,17 +238,18 @@ static void render_world_gates( m4x4f projection, v3f playerco, m4x3f camera )
       }
    }
 
-   render_gate( &world.routes.gates[id].gate, playerco, camera );
+   render_gate( &world.gates[id].gate, playerco, camera );
    v3_lerp( world.render_gate_pos, 
-            world.routes.gates[id].gate.co[0],
+            world.gates[id].gate.co[0],
             1.0f,
             world.render_gate_pos );
 }
 
-static void render_world( m4x4f projection, m4x3f camera )
+VG_STATIC void render_world( m4x4f projection, m4x3f camera )
 {
    render_sky( camera );
    render_world_routes( projection, camera[3] );
+   render_world_standard( projection, camera[3] );
    render_world_vb( projection, camera[3] );
    render_world_alphatest( projection, camera[3] );
    render_terrain( projection, camera[3] );
@@ -232,9 +257,12 @@ static void render_world( m4x4f projection, m4x3f camera )
    int closest = 0;
    float min_dist = INFINITY;
 
-   for( int i=0; i<world.routes.route_count; i++ )
+   if( !world.route_count )
+      return;
+
+   for( int i=0; i<world.route_count; i++ )
    {
-      float dist = v3_dist2( world.routes.routes[i].scoreboard_transform[3],
+      float dist = v3_dist2( world.routes[i].scoreboard_transform[3],
                               camera[3] );
 
       if( dist < min_dist )
@@ -244,11 +272,11 @@ static void render_world( m4x4f projection, m4x3f camera )
       }
    }
 
-   sfd_render( &world.sfd.tester, projection, camera[3], 
-         world.routes.routes[closest].scoreboard_transform );
+   sfd_render( projection, camera[3], 
+               world.routes[closest].scoreboard_transform );
 }
 
-static void render_world_depth( m4x4f projection, m4x3f camera )
+VG_STATIC void render_world_depth( m4x4f projection, m4x3f camera )
 {
    m4x3f identity_matrix;
    m4x3_identity( identity_matrix );
@@ -258,8 +286,8 @@ static void render_world_depth( m4x4f projection, m4x3f camera )
    shader_gpos_uPv( projection );
    shader_gpos_uMdl( identity_matrix );
    
-   scene_bind( &world.geo );
-   scene_draw( &world.geo );
+   mesh_bind( &world.mesh_geo );
+   mesh_draw( &world.mesh_geo );
 
 #if 0
    glDisable(GL_CULL_FACE);