1 #define SOLID_MAX_SIDES 512
3 typedef struct vmf_solid vmf_solid
;
4 typedef struct vmf_vert vmf_vert
;
5 typedef struct vmf_mat vmf_mat
;
6 typedef struct vmf_face vmf_face
;
7 typedef struct vmf_userdata vmf_userdata
;
8 typedef struct vmf_map vmf_map
;
10 typedef enum ESolidResult ESolidResult
;
15 k_ESolidResult_maxsides
,
16 k_ESolidResult_invalid
,
17 k_ESolidResult_errnomem
,
18 k_ESolidResult_corrupt
,
19 k_ESolidResult_degenerate
76 void solidgen_ctx_init( vmf_solid
*ctx
)
78 const u32 init_size
= 128;
80 ctx
->verts
= csr_sb_reserve( NULL
, init_size
, sizeof(vmf_vert
) );
81 ctx
->indices
= csr_sb_reserve( NULL
, init_size
, sizeof(u32
) );
84 void solidgen_ctx_free( vmf_solid
*ctx
)
86 csr_sb_free( ctx
->verts
);
87 csr_sb_free( ctx
->indices
);
90 // Compute bounds of solid gen ctx
91 void solidgen_bounds( vmf_solid
*ctx
, u32 start
, u32 end
, v3f min
, v3f max
)
93 v3f mine
= { INFINITY
, INFINITY
, INFINITY
};
94 v3f maxe
= {-INFINITY
,-INFINITY
,-INFINITY
};
96 for( int i
= start
; i
< end
; i
++ )
98 vmf_vert
*vert
= ctx
->verts
+ i
;
101 mine
[0] = fminf( mine
[0], co
[0] );
102 mine
[1] = fminf( mine
[1], co
[1] );
103 mine
[2] = fminf( mine
[2], co
[2] );
105 maxe
[0] = fmaxf( maxe
[0], co
[0] );
106 maxe
[1] = fmaxf( maxe
[1], co
[1] );
107 maxe
[2] = fmaxf( maxe
[2], co
[2] );
110 v3_copy( mine
, min
);
111 v3_copy( maxe
, max
);
118 double planes
[ SOLID_MAX_SIDES
*4 ];
123 // put an extra plane into the planes list
124 void vmf_addbisector( double p
[4] )
126 double *plane
= vmf_api
.planes
+ vmf_api
.bisectors
* 4;
133 vmf_api
.bisectors
++;
136 void vmf_clearbisectors( void )
138 vmf_api
.bisectors
= 0;
141 void vmf_ignore_mat( const char *material
)
143 vmf_api
.blacklist
= csr_sb_reserve( vmf_api
.blacklist
, 1, sizeof( vmf_mat
) );
144 vmf_mat
*mat
= (vmf_mat
*)csr_sb_use( vmf_api
.blacklist
);
146 mat
->str
= csr_malloc( strlen( material
) + 1 );
147 strcpy( mat
->str
, material
);
149 mat
->hash
= djb2( ( const unsigned char * )material
);
152 void vmf_clearignore( void )
154 for( int i
= 0; i
< csr_sb_count( vmf_api
.blacklist
); i
++ )
156 free( vmf_api
.blacklist
[ i
].str
);
159 csr_sb_free( vmf_api
.blacklist
);
160 vmf_api
.blacklist
= NULL
;
163 int mat_blacklisted( const char *material
)
165 u32 hash
= djb2((const u8
*)material
);
167 for( int j
= 0; j
< csr_sb_count( vmf_api
.blacklist
); j
++ )
169 if( vmf_api
.blacklist
[ j
].hash
== hash
)
171 if( !strcmp( material
, vmf_api
.blacklist
[ j
].str
) )
181 void sort_coplanar( double p
[4], vmf_vert
*points
, u32
*indices
, u32 count
)
183 v3f center
= {0.f
, 0.f
, 0.f
};
186 v3_normalize( norm
);
188 for( int i
= 0; i
< count
; i
++ )
190 v3_add( points
[ indices
[i
] ].co
, center
, center
);
192 v3_divs( center
, count
, center
);
195 v3_sub( points
[ indices
[0] ].co
, center
, ref
);
197 // Calc angles compared to ref
198 float *angles
= (float*)alloca( sizeof(float)*count
);
199 for( int i
= 0; i
< count
; i
++ )
204 v3_sub( points
[ indices
[i
] ].co
, center
, diff
);
205 v3_cross( diff
, ref
, c
);
208 atan2f( v3_length(c
), v3_dot( diff
, ref
) )
209 * (v3_dot( c
, norm
) < 0.f
? -1.f
: 1.f
);
212 // Temporary local indexes
213 u32
*temp_indices
= (u32
*)alloca( sizeof(u32
)*count
);
214 for( u32 i
= 0; i
< count
; i
++ ) temp_indices
[i
] = i
;
216 // Slow sort on large vertex counts
221 for( int i
= 0; i
< count
-1; i
++ )
223 int s0
= i
; int s1
= i
+ 1;
225 if( angles
[temp_indices
[s0
]] > angles
[temp_indices
[s1
]] )
227 // swap indices and mirror on local
228 u32 temp
= indices
[s1
];
229 indices
[s1
] = indices
[s0
];
232 temp
= temp_indices
[s1
];
233 temp_indices
[s1
] = temp_indices
[s0
];
234 temp_indices
[s0
] = temp
;
241 if( !modified
) break;
245 int solid_has_displacement( vdf_node
*node
)
250 while( (pSide
= vdf_next(node
, "side", &it
)) )
252 if( vdf_next( pSide
, "dispinfo", NULL
) )
260 void solid_disp_tri( vmf_solid
*ctx
, u32 a
, u32 b
, u32 c
)
262 *((u32
*)csr_sb_use( ctx
->indices
)) = a
;
263 *((u32
*)csr_sb_use( ctx
->indices
)) = b
;
264 *((u32
*)csr_sb_use( ctx
->indices
)) = c
;
267 void face_add_indice( vmf_face
*f
, u32 idx
)
269 f
->indices
= csr_sb_reserve( f
->indices
, 1, sizeof( u32
) );
270 *((u32
*)csr_sb_use( f
->indices
)) = idx
;
273 ESolidResult
solidgen_push( vmf_solid
*ctx
, vdf_node
*node
)
275 ESolidResult flag
= k_ESolidResult_valid
;
277 vmf_face faces
[ SOLID_MAX_SIDES
];
279 int is_displacement
= 0;
282 // TODO: What is this for again? surely it should be the other way around... i think...
283 if( solid_has_displacement( node
) )
285 printf( "solid_has_displacement\n" );
286 num_planes
= vmf_api
.bisectors
;
288 // Add dummy stuff for globals
290 for( int k
= 0; k
< vmf_api
.bisectors
; k
++ )
292 vmf_face
*dummy
= faces
+ k
;
293 dummy
->indices
= NULL
;
294 dummy
->dispinfo
= NULL
;
295 dummy
->material
= NULL
;
303 while( (pSide
= vdf_next(node
, "side", &it
)) )
305 if( num_planes
>= SOLID_MAX_SIDES
)
307 flag
= k_ESolidResult_maxsides
;
308 fprintf( stderr
, "Solid over maxsides limit (%i)\n", SOLID_MAX_SIDES
);
314 vmf_face
*face
= faces
+ num_planes
;
315 face
->indices
= NULL
;
316 face
->dispinfo
= vdf_next( pSide
, "dispinfo", NULL
);
317 face
->material
= kv_get( pSide
, "material", "" );
318 face
->blacklisted
= mat_blacklisted( face
->material
);
320 kv_double_array( pSide
, "plane", 9, points
);
322 tri_to_plane( points
+6, points
+3, points
+0, vmf_api
.planes
+ num_planes
* 4 );
326 // Compute plane intersections
328 csr_comb_init( 3, i
);
330 v3f center
= { 0.f
, 0.f
, 0.f
};
332 u32 vert_start
= csr_sb_count( ctx
->verts
);
336 // DO something with i j k
339 if( (faces
[ i
[0] ].blacklisted
&& faces
[ i
[1] ].blacklisted
&& faces
[ i
[2] ].blacklisted
) )
342 if( !plane_intersect( vmf_api
.planes
+i
[0]*4, vmf_api
.planes
+i
[1]*4, vmf_api
.planes
+i
[2]*4, p
) )
345 // Check for illegal verts (eg: got clipped by bisectors)
347 for( int m
= 0; m
< num_planes
; m
++ )
349 if( plane_polarity( vmf_api
.planes
+m
*4, p
) > 1e-6f
)
358 ctx
->verts
= csr_sb_reserve( ctx
->verts
, 3, sizeof( vmf_vert
) );
360 // Take the vertex position and add it for centering base on average
362 v3_add( (v3f
){ p
[0], p
[1], p
[2] }, center
, center
);
364 // Store point / respecive normal for each plane that triggered the collision
365 for( int k
= 0; k
< 3; k
++ )
367 if( !faces
[ i
[k
] ].blacklisted
)
369 u32 c
= csr_sb_count( ctx
->verts
);
371 face_add_indice( faces
+ i
[k
], c
);
373 v3d_v3f( p
, ctx
->verts
[ c
].co
);
374 v3d_v3f( vmf_api
.planes
+i
[k
]*4, ctx
->verts
[ c
].nrm
);
376 csr_sb_inc( ctx
->verts
, 1 );
381 while( csr_comb( 3, num_planes
, i
) );
383 // Retrospectively set the center for each point
384 v3_divs( center
, (float)numpoints
, center
);
385 for( ; vert_start
< csr_sb_count( ctx
->verts
); vert_start
++ )
387 v2_copy( center
, ctx
->verts
[ vert_start
].xy
);
390 // Sort each faces and trianglulalate them
391 for( int k
= vmf_api
.bisectors
; k
< num_planes
; k
++ )
393 vmf_face
*face
= faces
+ k
;
395 if( face
->blacklisted
) continue;
397 if( csr_sb_count( face
->indices
) < 3 )
399 if( !vmf_api
.bisectors
)
401 flag
= k_ESolidResult_degenerate
;
402 fprintf( stderr
, "Skipping degenerate face\n" );
407 // Sort only if there is no displacements, or if this side is
408 if( !is_displacement
|| ( is_displacement
&& face
->dispinfo
) )
410 sort_coplanar( vmf_api
.planes
+k
*4, ctx
->verts
, face
->indices
, csr_sb_count( face
->indices
) );
413 if( is_displacement
)
415 // Compute displacement
418 if( csr_sb_count( face
->indices
) != 4 )
420 // Mute error if we have global planes cause they
421 // are of course gonna fuck things up here
422 if( !vmf_api
.bisectors
)
424 flag
= k_ESolidResult_degenerate
;
425 fprintf( stderr
, "Skipping degenerate displacement\n" );
430 // Match starting position
433 float dmin
= 999999.f
;
435 vdf_node
*dispinfo
= face
->dispinfo
;
436 vdf_node
*vdf_normals
= vdf_next( dispinfo
, "normals", NULL
);
437 vdf_node
*vdf_distances
= vdf_next( dispinfo
, "distances", NULL
);
439 kv_float_array( dispinfo
, "startposition", 3, start
);
441 for( int j
= 0; j
< csr_sb_count( face
->indices
); j
++ )
443 float d2
= v3_dist2( start
, ctx
->verts
[ face
->indices
[ j
] ].co
);
451 // Get corners of displacement
452 float *SW
= ctx
->verts
[ face
->indices
[ sw
] ].co
;
453 float *NW
= ctx
->verts
[ face
->indices
[ (sw
+1) % 4] ].co
;
454 float *NE
= ctx
->verts
[ face
->indices
[ (sw
+2) % 4] ].co
;
455 float *SE
= ctx
->verts
[ face
->indices
[ (sw
+3) % 4] ].co
;
457 // Can be either 5, 9, 17
458 numpoints
= pow( 2, kv_get_int( dispinfo
, "power", 2 ) ) + 1;
459 u32 reqverts
= numpoints
*numpoints
;
460 u32 reqidx
= (numpoints
-1)*(numpoints
-1)*6;
462 ctx
->verts
= csr_sb_reserve( ctx
->verts
, reqverts
, sizeof( vmf_vert
) );
463 ctx
->indices
= csr_sb_reserve( ctx
->indices
, reqidx
, sizeof( u32
) );
465 float normals
[ 17*3 ];
466 float distances
[ 17 ];
468 // Calculate displacement positions
469 for( int j
= 0; j
< numpoints
; j
++ )
472 sprintf( key
, "row%i", j
);
474 kv_float_array( vdf_normals
, key
, 17*3, normals
);
475 kv_float_array( vdf_distances
, key
, 17, distances
);
477 float dx
= (float)j
/ (float)(numpoints
- 1); //Time values for linear interpolation
479 for( int m
= 0; m
< numpoints
; m
++ )
481 vmf_vert
*vert
= &ctx
->verts
[ csr_sb_count( ctx
->verts
) + j
*numpoints
+ m
];
483 float dy
= (float)m
/ (float)(numpoints
- 1);
487 v3_lerp( SW
, SE
, dx
, lwr
);
488 v3_lerp( NW
, NE
, dx
, upr
);
489 v3_lerp( lwr
, upr
, dy
, vert
->co
);
491 v3_muladds( vert
->co
, normals
+ m
* 3, distances
[ m
], vert
->co
);
493 // Todo, put correct normal
494 v3_copy( (v3f
){ 0.f
, 0.f
, 1.f
}, vert
->nrm
);
498 // Build displacement indices
500 for( int row
= 0; row
< numpoints
- 1; row
++ )
502 for( int col
= 0; col
< numpoints
- 1; col
++ )
504 u32 c
= csr_sb_count( ctx
->verts
);
506 u32 idxsw
= c
+ ( row
+ 0 ) * numpoints
+ col
+ 0 ;
507 u32 idxse
= c
+ ( row
+ 0 ) * numpoints
+ col
+ 1 ;
508 u32 idxnw
= c
+ ( row
+ 1 ) * numpoints
+ col
+ 0 ;
509 u32 idxne
= c
+ ( row
+ 1 ) * numpoints
+ col
+ 1 ;
511 if( (condition
++) % 2 == 0 )
513 solid_disp_tri( ctx
, idxne
, idxnw
, idxsw
);
514 solid_disp_tri( ctx
, idxse
, idxne
, idxsw
);
518 solid_disp_tri( ctx
, idxse
, idxnw
, idxsw
);
519 solid_disp_tri( ctx
, idxse
, idxne
, idxnw
);
525 csr_sb_inc( ctx
->verts
, numpoints
*numpoints
);
530 u32 tris
= csr_sb_count( face
->indices
) -2;
531 ctx
->indices
= csr_sb_reserve( ctx
->indices
, tris
*3, sizeof( u32
) );
533 u32 c
= csr_sb_count( ctx
->indices
);
535 for( int j
= 0; j
< tris
; j
++ )
537 ctx
->indices
[ c
+j
*3 +0 ] = face
->indices
[ 0 ];
538 ctx
->indices
[ c
+j
*3 +1 ] = face
->indices
[ j
+1 ];
539 ctx
->indices
[ c
+j
*3 +2 ] = face
->indices
[ j
+2 ];
546 csr_sb_inc( ctx
->indices
, tris
*3 );
550 // Free temp polyon buffers
551 for( int j
= 0; j
< num_planes
; j
++ )
553 csr_sb_free( faces
[ j
].indices
);
559 u32
vmf_get_mdl( vmf_map
*map
, const char *mdl
)
561 u32 hash
= djb2( (const unsigned char *)mdl
);
563 for( u32 i
= 0; i
< csr_sb_count( map
->models
); i
++ )
565 if( hash
== map
->models
[i
].hash
&& !strcmp( map
->models
[i
].str
, mdl
) )
574 void vmf_populate_models( vdf_node
*vmf
, vmf_map
*map
)
576 vdf_foreach( vmf
, "entity", ent
)
578 // Use any class name with prop_
579 if( !strncmp( kv_get( ent
, "classname", "" ), "prop_", 5 ))
581 // Check if it exists
582 const char *model_path
= kv_get( ent
, "model", "" );
583 u32 mdl_id
= vmf_get_mdl( map
, model_path
);
587 map
->models
= csr_sb_reserve( map
->models
, 1, sizeof( struct vmf_model
));
589 struct vmf_model
*entry
= &map
->models
[ csr_sb_count( map
->models
) ];
590 entry
->str
= csr_malloc( strlen( model_path
) +1 );
591 strcpy( entry
->str
, model_path
);
592 entry
->hash
= djb2( (const unsigned char *)model_path
);
594 mdl_id
= csr_sb_count( map
->models
);
595 csr_sb_use( map
->models
);
598 // Assign prop-ID for later use
605 void vmf_load_models( vmf_map
*map
)
607 printf( "Loading all models\n" );
609 // Error model. TODO: Maybe don't have this be junk data.
610 map
->models
= csr_sb_reserve( map
->models
, 1, sizeof( struct vmf_model
));
611 csr_sb_use( map
->models
);
612 mdl_error( &map
->models
[0].mdl
);
614 // Create listings for each model
615 vmf_populate_models( map
->root
, map
);
617 for( int i
= 0; i
< csr_sb_count( map
->cache
); i
++ )
619 vmf_populate_models( map
->cache
[i
].root
, map
);
622 printf( "Indexed (%u) models\n", csr_sb_count( map
->models
) );
627 // TODO: Make nice loading bar
628 for( int i
= 1; i
< csr_sb_count( map
->models
); i
++ )
630 printf( "Load model (%d)\n", i
);
632 struct vmf_model
*mdl
= &map
->models
[i
];
634 if( mdl_from_find_files( mdl
->str
, &mdl
->mdl
) )
640 printf( "Done (%u of %u loaded)\n", num_success
, csr_sb_count( map
->models
) );
643 void vmf_init_subvmf( vmf_map
*map
, const char *subvmf
);
645 void vmf_load_all_instances( vmf_map
*map
, vdf_node
*vmf
)
647 vdf_foreach( vmf
, "entity", ent
)
649 if( !strcmp( kv_get( ent
, "classname", "" ), "func_instance" ))
651 // Entity is in use if file is specified, if not just ignore the entity.
652 const char *path
= kv_get( ent
, "file", "" );
653 if( strcmp( path
, "" ) )
655 vmf_init_subvmf( map
, path
);
661 void vmf_init_subvmf( vmf_map
*map
, const char *subvmf
)
663 printf( "Loading subvmf: %s\n", subvmf
);
665 u32 hash
= djb2( (const unsigned char *)subvmf
);
668 for( int i
= 0; i
< csr_sb_count( map
->cache
); i
++ )
670 if( hash
== map
->cache
[i
].hash
)
672 if( !strcmp( map
->cache
[i
].name
, subvmf
) )
679 map
->cache
= csr_sb_reserve( map
->cache
, 1, sizeof( struct vmf_instance
));
680 struct vmf_instance
*inst
= &map
->cache
[ csr_sb_count( map
->cache
) ];
682 if( (inst
->root
= vdf_open_file( subvmf
)) )
684 csr_sb_use( map
->cache
);
687 inst
->name
= csr_malloc( strlen( subvmf
)+1 );
688 strcpy( inst
->name
, subvmf
);
690 // Recursive load other instances
691 vmf_load_all_instances( map
, inst
->root
);
695 // TODO: Don't die here?
696 fprintf( stderr
, "Failed to load instance file\n" );
701 vmf_map
*vmf_init( const char *path
, int load_models
)
703 vmf_map
*map
= csr_calloc( sizeof( vmf_map
) );
704 map
->root
= vdf_open_file( path
);
707 vmf_load_all_instances( map
, map
->root
);
712 vmf_load_models( map
);
718 void vmf_free( vmf_map
*map
)
720 for( int i
= 0; i
< csr_sb_count( map
->cache
); i
++ )
722 vdf_free_r( map
->cache
[i
].root
);
723 free( map
->cache
[i
].name
);
726 for( int i
= 1; i
< csr_sb_count( map
->models
); i
++ )
728 free( map
->models
[i
].str
);
729 mdl_free( &map
->models
[i
].mdl
);
732 vdf_free_r( map
->root
);
733 csr_sb_free( map
->models
);
734 csr_sb_free( map
->cache
);
738 void solidgen_to_obj( vmf_solid
*ctx
, const char *path
)
740 FILE *fp
= fopen( path
, "w" );
744 fprintf( fp
, "o vmf_export\n" );
748 // Write vertex block
749 for( int i
= 0; i
< csr_sb_count( ctx
->verts
); i
++ )
751 vert
= &ctx
->verts
[i
];
752 fprintf( fp
, "v %f %f %f\n", vert
->co
[0], vert
->co
[1], vert
->co
[2] );
755 // Write normals block
756 for( int i
= 0; i
< csr_sb_count( ctx
->verts
); i
++ )
758 vert
= &ctx
->verts
[i
];
759 fprintf( fp
, "vn %f %f %f\n", vert
->nrm
[0], vert
->nrm
[1], vert
->nrm
[2] );
762 fprintf( fp
, "s off\n" );
765 for( int i
= 0; i
< csr_sb_count( ctx
->indices
)/3; i
++ )
767 u32
* base
= ctx
->indices
+ i
*3;
768 fprintf( fp
, "f %u//%u %u//%u %u//%u\n",
769 base
[2]+1, base
[2]+1,
770 base
[1]+1, base
[1]+1,
779 fprintf( stderr
, "Could not open %s for writing\n", path
);