model view prototype
[convexer.git] / cxr / cxr.h
index e1c1679f405ff2fda3080bcc67ea304acc20b0b7..2a4267f0a152ffa3e0b34fd3eb4c5f4fd8a36db0 100644 (file)
--- a/cxr/cxr.h
+++ b/cxr/cxr.h
@@ -48,7 +48,6 @@ LICENSE: GPLv3.0, please see COPYING and LICENSE for more information
    IMPLEMENTATION
 */
 
-#define CXR_API
 #define CXR_EPSILON 0.001
 #define CXR_PLANE_SIMILARITY_MAX 0.998
 #define CXR_BIG_NUMBER 1e300
@@ -60,31 +59,17 @@ LICENSE: GPLv3.0, please see COPYING and LICENSE for more information
 
 #include <stdio.h>
 #include <math.h>
-#include <stdint.h>
-#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 
-typedef uint8_t        u8;
-typedef uint16_t       u16;
-typedef uint32_t       u32;
-typedef uint64_t       u64;
-typedef int8_t         i8;
-typedef int16_t        i16;
-typedef int32_t        i32;
-typedef int64_t        i64;
-
-typedef unsigned int uint;
-
-typedef double         v2f[2];
-typedef double         v3f[3];
-typedef double         v4f[4];
-typedef v3f                    m3x3f[3];
-typedef v3f                    m4x3f[4];
-typedef v3f                    boxf[2];
-
+#include "cxr_types.h"
 #include "cxr_math.h"
 #include "cxr_mem.h"
+#include "cxr_log.h"
+
+#ifdef CXR_VALVE_BIN
+ #include "cxr_valve_bin.h"
+#endif
 
 typedef struct cxr_world cxr_world;
 typedef struct cxr_solid cxr_solid;
@@ -232,8 +217,11 @@ struct cxr_mesh
 /* Simple mesh type mainly for debugging */
 struct cxr_tri_mesh
 {
-   v3f *vertices;
+   v3f *vertices,
+       *normals;
+   v2f *uvs;
    v4f *colours;
+
    i32 *indices,
         indices_count,
         vertex_count;
@@ -314,9 +302,6 @@ enum cxr_soliderr
 #endif
 #endif
 
-static void (*cxr_log_func)(const char *str);
-static void (*cxr_line_func)( v3f p0, v3f p1, v4f colour );
-
 static int cxr_range(int x, int bound)
 {
    if( x < 0 )
@@ -391,21 +376,6 @@ static void colour_random_brush(int n, v4f colour)
 
 #ifdef CXR_DEBUG
 
-static void cxr_log( const char *fmt, ... )
-{
-   char buf[512];
-
-   va_list args;
-   va_start( args, fmt );
-   vsnprintf( buf, sizeof(buf)-1, fmt, args );
-   va_end(args);
-
-   if( cxr_log_func )
-      cxr_log_func( buf );
-
-   fputs(buf,stdout);
-}
-
 static void cxr_debug_line( v3f p0, v3f p1, v4f colour )
 {
    if( cxr_line_func )
@@ -2218,6 +2188,8 @@ CXR_API cxr_tri_mesh *cxr_world_preview( cxr_world *world )
    out->colours = malloc( sizeof(v4f)*out->vertex_count );
    out->vertices = malloc( sizeof(v3f)*out->vertex_count );
    out->indices = malloc( sizeof(i32)*out->indices_count );
+   out->uvs = NULL;
+   out->normals = NULL;
 
    v3f *overts = out->vertices;
    v4f *colours = out->colours;
@@ -2299,6 +2271,8 @@ CXR_API void cxr_free_tri_mesh( cxr_tri_mesh *mesh )
    free( mesh->colours );
    free( mesh->indices );
    free( mesh->vertices );
+   free( mesh->normals );
+   free( mesh->uvs );
    free( mesh );
 }
 
@@ -3535,5 +3509,144 @@ CXR_API int cxr_lightpatch_bsp( const char *path )
 
    return 1;
 }
+
+#ifdef CXR_VALVE_BIN
+
+CXR_API cxr_tri_mesh *cxr_load_mdl( const char *mdlname )
+{
+       char path[1024];
+       strcpy( path, mdlname );
+       cxr_stripext( path );
+       strcat( path, ".dx90.vtx" );
+       VTXFileHeader_t *pVtxHdr = (VTXFileHeader_t *)cxr_fs_get( path );
+       
+       if( !pVtxHdr )
+               return NULL;
+       
+       /* .VVD */
+       strcpy( path, mdlname );
+       cxr_stripext( path );
+       strcat( path, ".vvd" );
+       vertexFileHeader_t *pVvdHdr = (vertexFileHeader_t *)cxr_fs_get( path );
+       
+       if( !pVvdHdr )
+       {
+               free( pVtxHdr );
+               return 0;
+       }
+       
+       /* .MDL */
+       strcpy( path, mdlname );
+       cxr_stripext( path );
+       strcat( path, ".mdl" );
+       studiohdr_t *pMdl = (studiohdr_t *)cxr_fs_get( path );
+       
+       if( !pMdl )
+       {
+               free( pVtxHdr );
+               free( pVvdHdr );
+               return 0;
+       }
+       
+   cxr_tri_mesh *mesh = malloc( sizeof(cxr_tri_mesh) );
+   mesh->colours = NULL;
+   
+       mesh->indices_count = vtx_count_indices( pVtxHdr, pMdl );
+       mesh->indices = malloc( mesh->indices_count * sizeof( u32 ) );
+
+       mesh->vertex_count = pVvdHdr->numLodVertexes[0];
+       mesh->vertices = malloc( mesh->vertex_count * sizeof(v3f) );
+   mesh->uvs = malloc( mesh->vertex_count * sizeof(v2f) );
+   mesh->normals = malloc( mesh->vertex_count * sizeof(v3f) );
+       
+#if 0
+   mesh->bounds[0][0] = pMdl->hull_min[0];
+   mesh->bounds[0][1] = pMdl->hull_min[1];
+   mesh->bounds[0][2] = pMdl->hull_min[2];
+   mesh->bounds[0][0] = pMdl->hull_max[0];
+   mesh->bounds[0][1] = pMdl->hull_max[1];
+   mesh->bounds[0][2] = pMdl->hull_max[2];
+#endif
+       
+       mesh->indices_count = 0;
+       
+       for( int bodyID = 0; bodyID < pVtxHdr->numBodyParts; ++bodyID )
+       {
+               /* Body parts */
+               VTXBodyPartHeader_t* pVtxBodyPart = pBodyPartVTX( pVtxHdr, bodyID );
+               mstudiobodyparts_t *pBodyPart = studiohdr_pBodypart( pMdl, bodyID );
+               
+               for( int modelID = 0; modelID < pBodyPart->nummodels; ++modelID )
+               {       
+         /* models */
+                       VTXModelHeader_t* pVtxModel = pModelVTX( pVtxBodyPart, modelID );
+                       mstudiomodel_t *pStudioModel = 
+            mstudiobodyparts_pModel( pBodyPart, modelID );
+
+                       int nLod = 0;
+                       VTXModelLODHeader_t *pVtxLOD = pLODVTX( pVtxModel, nLod );
+
+                       for( int nMesh = 0; nMesh < pStudioModel->nummeshes; ++nMesh )
+                       {
+            /* meshes */
+                               VTXMeshHeader_t* pVtxMesh = pMeshVTX( pVtxLOD, nMesh );
+                               mstudiomesh_t* pMesh = studiomodel_pMesh( pStudioModel, nMesh );
+
+                               for ( int nGroup = 0; nGroup < pVtxMesh->numStripGroups; ++nGroup )
+                               {
+               /* groups */
+                                       VTXStripGroupHeader_t* pStripGroup = 
+                  pStripGroupVTX( pVtxMesh, nGroup );
+
+                                       for ( int nStrip = 0; nStrip < pStripGroup->numStrips; nStrip++ )
+                                       {
+                  /* strips */
+                                               VTXStripHeader_t *pStrip = pStripVTX( pStripGroup, nStrip );
+
+                                               if ( pStrip->flags & STRIP_IS_TRILIST )
+                                               {
+                     /* indices */
+                                                       for ( int i = 0; i < pStrip->numIndices; i ++ )
+                                                       {
+                                                               u16 i1 = *pIndexVTX( pStripGroup, 
+                              pStrip->indexOffset + i );                                                               
+
+                                                               mesh->indices[ mesh->indices_count ++ ] = 
+                           pVertexVTX( pStripGroup, i1 )->origMeshVertID + 
+                           pMesh->vertexoffset;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       mstudiovertex_t *vertexData = GetVertexData( pVvdHdr );
+       
+       for( int i = 0; i < mesh->vertex_count; i ++ )
+       {
+               mstudiovertex_t *vert = vertexData + i;
+
+      mesh->vertices[i][0] = vert->pos[0];
+      mesh->vertices[i][1] = vert->pos[1];
+      mesh->vertices[i][2] = vert->pos[2];
+
+      mesh->normals[i][0] = vert->norm[0];
+      mesh->normals[i][1] = vert->norm[1];
+      mesh->normals[i][2] = vert->norm[2];
+
+      mesh->uvs[i][0] = vert->uv[0];
+      mesh->uvs[i][1] = vert->uv[1];
+       }
+       
+       free( pVtxHdr );
+       free( pVvdHdr );
+       free( pMdl );
+       
+       return mesh;
+}
+#endif /* CXR_VALVE_BIN */
+
 #endif /* CXR_VALVE_MAP_FILE */
 #endif /* CXR_IMPLEMENTATION */