build system revision
[carveJwlIkooP6JGAAIwe30JlM.git] / model.h
diff --git a/model.h b/model.h
index 34963be69e49ef7e5d67c4c20241c24e7ef6baa0..63e6e7f5ebf84b31a28b5e1e39dd4205fae37092 100644 (file)
--- a/model.h
+++ b/model.h
@@ -1,14 +1,20 @@
 /*
- * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
  */
 
-#ifndef MODEL_H
-#define MODEL_H
+#pragma once
 
 #include "skaterift.h"
 
+#include "vg/vg_io.h"
+#include "vg/vg_async.h"
+#include "vg/vg_tex.h"
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
 #define MDL_VERSION_MIN 101
-#define MDL_VERSION_NR 104
+#define MDL_VERSION_NR 105
 
 enum mdl_shader{
    k_shader_standard                = 0,
@@ -20,15 +26,19 @@ enum mdl_shader{
    k_shader_boundary                = 6,
    k_shader_fxglow                  = 7,
    k_shader_cubemap                 = 8,
+   k_shader_walking                 = 9,
+   k_shader_foliage                 = 10,
    k_shader_override                = 30000
 };
 
 enum mdl_surface_prop{
-   k_surface_prop_concrete          = 0,
-   k_surface_prop_wood              = 1,
-   k_surface_prop_grass             = 2,
-   k_surface_prop_tiles             = 3,
-   k_surface_prop_metal             = 4
+   k_surface_prop_concrete = 0,
+   k_surface_prop_wood     = 1,
+   k_surface_prop_grass    = 2,
+   k_surface_prop_tiles    = 3,
+   k_surface_prop_metal    = 4,
+   k_surface_prop_snow     = 5,
+   k_surface_prop_sand     = 6
 };
 
 enum material_flag{
@@ -310,9 +320,10 @@ static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
          if( l != 1 ) mdl_load_fatal_corrupt( mdl );
       }
       else {
-         assert( stride >= arr->item_size );
          vg_warn( "Applying alignment fixup to array @%p [%u -> %u] x %u\n", 
                   buffer, arr->item_size, stride, arr->item_count );
+         if( stride < arr->item_size )
+            vg_fatal_error( "not safe\n" );
 
          for( u32 i=0; i<arr->item_count; i++ ){
             u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file );
@@ -325,7 +336,10 @@ static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
 static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr, 
                                  mdl_array *arr, void *lin_alloc, u32 stride )
 {
-   assert( stride >= arr->item_size );
+   if( stride < arr->item_size ){
+      vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size );
+      vg_fatal_error( "not safe\n" );
+   }
 
    if( arr->item_count ){
       u32 size = stride*arr->item_count;
@@ -604,8 +618,8 @@ static void async_mdl_load_glmesh( void *payload, u32 size )
                            job->indices, job->indice_count );
 }
 
-static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
-{
+static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh
+                                   u32 *fixup_table ){
    mdl_array *arr_vertices = mdl_find_array( mdl, "mdl_vert" );
    mdl_array *arr_indices = mdl_find_array( mdl, "mdl_indice" );
 
@@ -631,6 +645,16 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
       mdl_load_array_file_buffer( mdl, arr_indices, job->indices, 
                                   sizeof(mdl_indice) );
 
+      if( fixup_table ){
+         for( u32 i=0; i<job->vertex_count; i ++ ){
+            mdl_vert *vert = &job->verts[i];
+            
+            for( u32 j=0; j<4; j++ ){
+               vert->groups[j] = fixup_table[vert->groups[j]];
+            }
+         }
+      }
+
       /*
        * Unpack the indices (if there are meshes)
        * ---------------------------------------------------------
@@ -665,7 +689,7 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
 
 /* uploads the glmesh, and textures. everything is saved into the mdl_context */
 static void mdl_async_full_load_std( mdl_context *mdl ){
-   mdl_async_load_glmesh( mdl, &mdl->mesh );
+   mdl_async_load_glmesh( mdl, &mdl->mesh, NULL );
    
    for( u32 i=0; i<mdl_arrcount( &mdl->textures ); i ++ ){
       vg_linear_clear( vg_mem.scratch );
@@ -678,5 +702,3 @@ static void mdl_async_full_load_std( mdl_context *mdl ){
                                VG_TEX2D_CLAMP|VG_TEX2D_NEAREST, &tex->glname );
    }
 }
-#endif