basic npc
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.c
index 2355e20bf204bd942870515167bbe75d2366e502..1b1a9c08399d1d68020c58e37d673e18dc5748a2 100644 (file)
@@ -2,9 +2,6 @@
  * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
  */
 
-#ifndef WORLD_RENDER_C
-#define WORLD_RENDER_C
-
 #include "world.h"
 #include "world_render.h"
 #include "font.h"
 #include "ent_miniworld.h"
 #include "player_remote.h"
 #include "ent_skateshop.h"
+#include "ent_npc.h"
 #include "shaders/model_entity.h"
 
+struct world_render world_render;
+
 static int ccmd_set_time( int argc, const char *argv[] ){
    world_instance *world = world_current_instance();
    if( argc == 1 )
@@ -66,7 +66,8 @@ static void async_world_render_init( void *payload, u32 size ){
    }
 }
 
-static void world_render_init(void){
+void world_render_init(void)
+{
    VG_VAR_F32( k_day_length );
    VG_VAR_I32( k_debug_light_indices );
    VG_VAR_I32( k_debug_light_complexity );
@@ -77,19 +78,6 @@ static void world_render_init(void){
    world_render.sky_rate = 1.0;
    world_render.sky_target_rate = 1.0;
 
-   shader_scene_standard_register();
-   shader_scene_standard_alphatest_register();
-   shader_scene_foliage_register();
-   shader_scene_override_register();
-   shader_scene_cubemapped_register();
-   shader_scene_fxglow_register();
-   shader_scene_vertex_blend_register();
-   shader_scene_terrain_register();
-   shader_scene_depth_register();
-   shader_scene_position_register();
-   shader_model_sky_register();
-   shader_model_sky_space_register();
-
    vg_info( "Loading world resources\n" );
    vg_linear_clear( vg_mem.scratch );
 
@@ -111,35 +99,40 @@ static void world_render_init(void){
  * standard uniform bindings
  * ----------------------------------------------------------------------------
  */
-static void world_link_lighting_ub( world_instance *world, GLuint shader ){
+void world_link_lighting_ub( world_instance *world, GLuint shader )
+{
    GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );   
    glUniformBlockBinding( shader, idx, world->ubo_bind_point );
 }
 
-static void world_bind_position_texture( world_instance *world, 
-                                            GLuint shader, GLuint location,
-                                            int slot ){
+void world_bind_position_texture( world_instance *world, 
+                                  GLuint shader, GLuint location,
+                                  int slot )
+{
    render_fb_bind_texture( &world->heightmap, 0, slot );
    glUniform1i( location, slot );
 }
 
-static void world_bind_light_array( world_instance *world,
-                                       GLuint shader, GLuint location, 
-                                       int slot ){
+void world_bind_light_array( world_instance *world,
+                             GLuint shader, GLuint location, 
+                             int slot )
+{
    glActiveTexture( GL_TEXTURE0 + slot );
    glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
    glUniform1i( location, slot );
 }
 
-static void world_bind_light_index( world_instance *world,
-                                       GLuint shader, GLuint location,
-                                       int slot ){
+void world_bind_light_index( world_instance *world,
+                             GLuint shader, GLuint location,
+                             int slot )
+{
    glActiveTexture( GL_TEXTURE0 + slot );
    glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
    glUniform1i( location, slot );
 }
 
-static void bind_terrain_noise(void){
+void bind_terrain_noise(void)
+{
    glActiveTexture( GL_TEXTURE0 );
    glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
 }
@@ -165,7 +158,7 @@ static void bindpoint_diffuse_texture1( world_instance *world,
  */
 
 struct world_pass{
-   camera *cam;
+   vg_camera *cam;
    enum mdl_shader shader;
    enum world_geo_type geo_type;
 
@@ -176,7 +169,7 @@ struct world_pass{
    void (*fn_set_uNormalMtx)( m3x3f mnorm );
 };
 
-static void render_world_depth( world_instance *world, camera *cam );
+void render_world_depth( world_instance *world, vg_camera *cam );
 
 /*
  * Render a run of submeshes, only of those which match material_id
@@ -293,7 +286,7 @@ static void world_render_both_stages( world_instance *world,
    glEnable( GL_CULL_FACE );
 }
 
-static void render_world_vb( world_instance *world, camera *cam ){
+static void render_world_vb( world_instance *world, vg_camera *cam ){
    shader_scene_vertex_blend_use();
    shader_scene_vertex_blend_uTexGarbage(0);
    shader_scene_vertex_blend_uTexGradients(1);
@@ -316,7 +309,7 @@ static void render_world_vb( world_instance *world, camera *cam ){
    world_render_both_stages( world, &pass );
 }
 
-static void world_shader_standard_bind( world_instance *world, camera *cam ){
+static void world_shader_standard_bind( world_instance *world, vg_camera *cam ){
    shader_scene_standard_use();
    shader_scene_standard_uTexGarbage(0);
    shader_scene_standard_uTexMain(1);
@@ -327,7 +320,7 @@ static void world_shader_standard_bind( world_instance *world, camera *cam ){
    shader_scene_standard_uCamera( cam->transform[3] );
 }
 
-static void render_world_standard( world_instance *world, camera *cam ){
+static void render_world_standard( world_instance *world, vg_camera *cam ){
    world_shader_standard_bind( world, cam );
    struct world_pass pass = {
       .shader = k_shader_standard,
@@ -360,7 +353,7 @@ static void bindpoint_diffuse1_and_cubemap10( world_instance *world,
    shader_scene_cubemapped_uColour( mat->info.colour );
 }
 
-static void render_world_cubemapped( world_instance *world, camera *cam,
+static void render_world_cubemapped( world_instance *world, vg_camera *cam,
                                      int enabled ){
    if( !mdl_arrcount( &world->ent_cubemap ) )
       return;
@@ -402,7 +395,7 @@ static void render_world_cubemapped( world_instance *world, camera *cam,
    }
 }
 
-static void render_world_alphatest( world_instance *world, camera *cam ){
+static void render_world_alphatest( world_instance *world, vg_camera *cam ){
    shader_scene_standard_alphatest_use();
    shader_scene_standard_alphatest_uTexGarbage(0);
    shader_scene_standard_alphatest_uTexMain(1);
@@ -426,7 +419,7 @@ static void render_world_alphatest( world_instance *world, camera *cam ){
    glEnable(GL_CULL_FACE);
 }
 
-static void render_world_foliage( world_instance *world, camera *cam ){
+static void render_world_foliage( world_instance *world, vg_camera *cam ){
    shader_scene_foliage_use();
    shader_scene_foliage_uTexGarbage(0);
    shader_scene_foliage_uTexMain(1);
@@ -613,7 +606,7 @@ static void world_render_challenges( world_instance *world,
 }
 
 static void render_world_fxglow( world_instance *host_world, 
-                                 world_instance *world, camera *cam,
+                                 world_instance *world, vg_camera *cam,
                                  m4x3f world_mmdl,
                                  int generic, int challenges, int regions ){
    shader_scene_fxglow_use();
@@ -700,7 +693,7 @@ static void bindpoint_override( world_instance *world,
    }
 }
 
-static void render_terrain( world_instance *world, camera *cam ){
+static void render_terrain( world_instance *world, vg_camera *cam ){
    shader_scene_terrain_use();
    shader_scene_terrain_uTexGarbage(0);
    shader_scene_terrain_uTexGradients(1);
@@ -723,7 +716,7 @@ static void render_terrain( world_instance *world, camera *cam ){
    world_render_both_stages( world, &pass );
 }
 
-static void render_sky( world_instance *world, camera *cam ){
+static void render_sky( world_instance *world, vg_camera *cam ){
    /* 
     * Modify matrix to remove clipping and view translation
     */
@@ -780,7 +773,7 @@ static void render_sky( world_instance *world, camera *cam ){
       glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
    }
    else {
-      assert(0);
+      vg_fatal_error( "Programming error\n" );
    }
 
    glDepthMask( GL_FALSE );
@@ -793,7 +786,8 @@ static void render_sky( world_instance *world, camera *cam ){
    glDepthMask( GL_TRUE );
 }
 
-static void render_world_gates( world_instance *world, camera *cam ){
+void render_world_gates( world_instance *world, vg_camera *cam )
+{
    float closest = INFINITY;
    struct ent_gate *gate = NULL;
 
@@ -838,7 +832,8 @@ static void render_world_gates( world_instance *world, camera *cam ){
    }
 }
 
-static void world_prerender( world_instance *world ){
+void world_prerender( world_instance *world )
+{
    if( mdl_arrcount( &world->ent_light ) ){
       f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
       world->time += vg.time_frame_delta * (1.0/(rate*60.0));
@@ -892,13 +887,16 @@ static void world_prerender( world_instance *world ){
                     sizeof(struct ub_world_lighting), &world->ub_lighting );
 }
 
-static void render_other_entities( world_instance *world, camera *cam ){
+static void render_other_entities( world_instance *world, vg_camera *cam )
+{
    f32 radius = 40.0f;
    bh_iter it;
    bh_iter_init_range( 0, &it, cam->pos, radius+10.0f );
 
    u32 glider_list[4],
-       glider_count = 0;
+       glider_count = 0,
+       npc_list[4],
+       npc_count = 0;
 
    i32 idx;
    while( bh_next( world->entity_bh, &it, &idx ) ){
@@ -906,20 +904,26 @@ static void render_other_entities( world_instance *world, camera *cam ){
           type  = mdl_entity_id_type( id ),
           index = mdl_entity_id_id( id );
 
-      if( type == k_ent_glider ) {
+      if( type == k_ent_glider ) 
+      {
          if( glider_count < vg_list_size(glider_list) )
             glider_list[ glider_count ++ ] = index;
       }
+      else if( type == k_ent_npc ) 
+      {
+         if( npc_count < vg_list_size(npc_list) )
+            npc_list[ npc_count ++ ] = index;
+      }
    }
 
    shader_model_entity_use();
    shader_model_entity_uTexMain( 0 );
    shader_model_entity_uCamera( cam->transform[3] );
    shader_model_entity_uPv( cam->mtx.pv );
-   
    WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
 
-   for( u32 j=0; j<glider_count; j ++ ){
+   for( u32 j=0; j<glider_count; j ++ )
+   {
       ent_glider *glider = mdl_arritm( &world->ent_glider, glider_list[j] );
 
       if( !(glider->flags & 0x1) )
@@ -934,11 +938,20 @@ static void render_other_entities( world_instance *world, camera *cam ){
 
       render_glider_model( cam, world, mdl, k_board_shader_entity );
    }
+
+   for( u32 j=0; j<npc_count; j ++ )
+   {
+      u32 index = npc_list[j];
+      ent_npc *npc = mdl_arritm( &world->ent_npc, npc_list[j] );
+      npc_update( npc );
+      npc_render( npc, world, cam );
+   }
 }
 
-static void render_world( world_instance *world, camera *cam,
-                          int stenciled, int viewing_from_gate, 
-                          int with_water, int with_cubemaps ){
+void render_world( world_instance *world, vg_camera *cam,
+                   int stenciled, int viewing_from_gate, 
+                   int with_water, int with_cubemaps )
+{
    if( stenciled ){
       glClear( GL_DEPTH_BUFFER_BIT );
       glStencilFunc( GL_EQUAL, 1, 0xFF );
@@ -1068,11 +1081,12 @@ static void render_world_override_pass( world_instance *world,
    }
 }
 
-static void render_world_override( world_instance *world,
-                                   world_instance *lighting_source,
-                                   m4x3f mmdl,
-                                   camera *cam,
-                                   ent_spawn *dest_spawn, v4f map_info ){
+void render_world_override( world_instance *world,
+                            world_instance *lighting_source,
+                            m4x3f mmdl,
+                            vg_camera *cam,
+                            ent_spawn *dest_spawn, v4f map_info )
+{
    struct world_pass pass = {
       .cam = cam,
       .fn_bind_textures = bindpoint_override,
@@ -1136,7 +1150,7 @@ static void render_world_override( world_instance *world,
 
 static void render_cubemap_side( world_instance *world, ent_cubemap *cm, 
                                     u32 side ){
-   camera cam;
+   vg_camera cam;
    glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, cm->texture_id, 0 );
    glClear( GL_DEPTH_BUFFER_BIT );
@@ -1167,20 +1181,21 @@ static void render_cubemap_side( world_instance *world, ent_cubemap *cm,
    v3_copy( cm->co, cam.transform[3] );
    m4x3_invert_affine( cam.transform, cam.transform_inverse );
 
-   camera_update_view( &cam );
+   vg_camera_update_view( &cam );
 
    cam.nearz = 0.1f;
    cam.farz = 1000.0f;
    cam.fov = 90.0f;
    m4x4_copy( cam.mtx.p,  cam.mtx_prev.p );
    m4x4_projection( cam.mtx.p, cam.fov, 1.0f, cam.nearz, cam.farz );
-   camera_finalize( &cam );
-   camera_finalize( &cam );
+   vg_camera_finalize( &cam );
+   vg_camera_finalize( &cam );
 
    render_world( world, &cam, 0, 1, 1, 0 );
 }
 
-static void render_world_cubemaps( world_instance *world ){
+void render_world_cubemaps( world_instance *world )
+{
    if( world->cubemap_cooldown )
       world->cubemap_cooldown --;
    else{
@@ -1205,7 +1220,8 @@ static void render_world_cubemaps( world_instance *world ){
  * ---------------------------------------------
  */
 
-static void render_world_depth( world_instance *world, camera *cam ){
+void render_world_depth( world_instance *world, vg_camera *cam )
+{
    m4x3f identity_matrix;
    m4x3_identity( identity_matrix );
 
@@ -1220,7 +1236,8 @@ static void render_world_depth( world_instance *world, camera *cam ){
    mesh_draw( &world->mesh_geo );
 }
 
-static void render_world_position( world_instance *world, camera *cam ){
+void render_world_position( world_instance *world, vg_camera *cam )
+{
    m4x3f identity_matrix;
    m4x3_identity( identity_matrix );
 
@@ -1254,7 +1271,8 @@ static f32 *skybox_prop_location( world_instance *world, i32 index ){
    }
 }
 
-static void imgui_world_light_edit( world_instance *world ){
+void imgui_world_light_edit( world_instance *world )
+{
    ui_rect panel = { vg.window_x-400, 0, 400, vg.window_y };
    ui_fill( panel, ui_colour( k_ui_bg+1 ) );
    ui_outline( panel, 1, ui_colour( k_ui_bg+7 ), 0 );
@@ -1278,5 +1296,3 @@ static void imgui_world_light_edit( world_instance *world ){
       fclose( fp );
    }
 }
-
-#endif