1 #define SOLID_MAX_SIDES 512
2 #define VMF_FLAG_IS_PROP 0x1
3 #define VMF_FLAG_IS_INSTANCE 0x2
4 #define VMF_FLAG_BRUSH_ENT 0x4
6 typedef struct vmf_solid vmf_solid
;
7 typedef struct vmf_vert vmf_vert
;
8 typedef struct vmf_mat vmf_mat
;
9 typedef struct vmf_face vmf_face
;
10 typedef struct vmf_userdata vmf_userdata
;
11 typedef struct vmf_map vmf_map
;
13 typedef enum ESolidResult ESolidResult
;
18 k_ESolidResult_maxsides
,
19 k_ESolidResult_invalid
,
20 k_ESolidResult_errnomem
,
21 k_ESolidResult_corrupt
,
22 k_ESolidResult_degenerate
83 void solidgen_ctx_reset( vmf_solid
*ctx
)
85 csr_sb_clear( ctx
->verts
);
86 csr_sb_clear( ctx
->indices
);
89 void solidgen_ctx_init( vmf_solid
*ctx
)
91 const u32 init_size
= 128;
93 ctx
->verts
= csr_sb_reserve( NULL
, init_size
, sizeof(vmf_vert
) );
94 ctx
->indices
= csr_sb_reserve( NULL
, init_size
, sizeof(u32
) );
97 void solidgen_ctx_free( vmf_solid
*ctx
)
99 csr_sb_free( ctx
->verts
);
100 csr_sb_free( ctx
->indices
);
103 // Compute bounds of solid gen ctx
104 void solidgen_bounds( vmf_solid
*ctx
, u32 start
, u32 end
, v3f min
, v3f max
)
106 v3f mine
= { INFINITY
, INFINITY
, INFINITY
};
107 v3f maxe
= {-INFINITY
,-INFINITY
,-INFINITY
};
109 for( int i
= start
; i
< end
; i
++ )
111 vmf_vert
*vert
= ctx
->verts
+ i
;
112 float *co
= vert
->co
;
114 mine
[0] = fminf( mine
[0], co
[0] );
115 mine
[1] = fminf( mine
[1], co
[1] );
116 mine
[2] = fminf( mine
[2], co
[2] );
118 maxe
[0] = fmaxf( maxe
[0], co
[0] );
119 maxe
[1] = fmaxf( maxe
[1], co
[1] );
120 maxe
[2] = fmaxf( maxe
[2], co
[2] );
123 v3_copy( mine
, min
);
124 v3_copy( maxe
, max
);
131 double planes
[ SOLID_MAX_SIDES
*4 ];
136 // put an extra plane into the planes list
137 void vmf_addbisector( double p
[4] )
139 double *plane
= vmf_api
.planes
+ vmf_api
.bisectors
* 4;
146 vmf_api
.bisectors
++;
149 void vmf_clearbisectors( void )
151 vmf_api
.bisectors
= 0;
154 void vmf_ignore_mat( const char *material
)
156 vmf_api
.blacklist
= csr_sb_reserve( vmf_api
.blacklist
, 1, sizeof( vmf_mat
) );
157 vmf_mat
*mat
= (vmf_mat
*)csr_sb_use( vmf_api
.blacklist
);
159 mat
->str
= csr_malloc( strlen( material
) + 1 );
160 strcpy( mat
->str
, material
);
162 mat
->hash
= djb2( ( const unsigned char * )material
);
165 void vmf_clearignore( void )
167 for( int i
= 0; i
< csr_sb_count( vmf_api
.blacklist
); i
++ )
169 free( vmf_api
.blacklist
[ i
].str
);
172 csr_sb_free( vmf_api
.blacklist
);
173 vmf_api
.blacklist
= NULL
;
176 int mat_blacklisted( const char *material
)
178 u32 hash
= djb2((const u8
*)material
);
180 for( int j
= 0; j
< csr_sb_count( vmf_api
.blacklist
); j
++ )
182 if( vmf_api
.blacklist
[ j
].hash
== hash
)
184 if( !strcmp( material
, vmf_api
.blacklist
[ j
].str
) )
194 void sort_coplanar( double p
[4], vmf_vert
*points
, u32
*indices
, u32 count
)
196 v3f center
= {0.f
, 0.f
, 0.f
};
199 v3_normalize( norm
);
201 for( int i
= 0; i
< count
; i
++ )
203 v3_add( points
[ indices
[i
] ].co
, center
, center
);
205 v3_divs( center
, count
, center
);
208 v3_sub( points
[ indices
[0] ].co
, center
, ref
);
210 // Calc angles compared to ref
211 float *angles
= (float*)alloca( sizeof(float)*count
);
212 for( int i
= 0; i
< count
; i
++ )
217 v3_sub( points
[ indices
[i
] ].co
, center
, diff
);
218 v3_cross( diff
, ref
, c
);
221 atan2f( v3_length(c
), v3_dot( diff
, ref
) )
222 * (v3_dot( c
, norm
) < 0.f
? -1.f
: 1.f
);
225 // Temporary local indexes
226 u32
*temp_indices
= (u32
*)alloca( sizeof(u32
)*count
);
227 for( u32 i
= 0; i
< count
; i
++ ) temp_indices
[i
] = i
;
229 // Slow sort on large vertex counts
234 for( int i
= 0; i
< count
-1; i
++ )
236 int s0
= i
; int s1
= i
+ 1;
238 if( angles
[temp_indices
[s0
]] > angles
[temp_indices
[s1
]] )
240 // swap indices and mirror on local
241 u32 temp
= indices
[s1
];
242 indices
[s1
] = indices
[s0
];
245 temp
= temp_indices
[s1
];
246 temp_indices
[s1
] = temp_indices
[s0
];
247 temp_indices
[s0
] = temp
;
254 if( !modified
) break;
258 int solid_has_displacement( vdf_node
*node
)
263 while( (pSide
= vdf_next(node
, "side", &it
)) )
265 if( vdf_next( pSide
, "dispinfo", NULL
) )
273 void solid_disp_tri( vmf_solid
*ctx
, u32 a
, u32 b
, u32 c
)
275 *((u32
*)csr_sb_use( ctx
->indices
)) = a
;
276 *((u32
*)csr_sb_use( ctx
->indices
)) = b
;
277 *((u32
*)csr_sb_use( ctx
->indices
)) = c
;
280 void face_add_indice( vmf_face
*f
, u32 idx
)
282 f
->indices
= csr_sb_reserve( f
->indices
, 1, sizeof( u32
) );
283 *((u32
*)csr_sb_use( f
->indices
)) = idx
;
286 ESolidResult
solidgen_push( vmf_solid
*ctx
, vdf_node
*node
)
288 ESolidResult flag
= k_ESolidResult_valid
;
290 vmf_face faces
[ SOLID_MAX_SIDES
];
292 int is_displacement
= 0;
295 // TODO: What is this for again? surely it should be the other way around... i think...
296 if( solid_has_displacement( node
) )
298 printf( "solid_has_displacement\n" );
299 num_planes
= vmf_api
.bisectors
;
301 // Add dummy stuff for globals
303 for( int k
= 0; k
< vmf_api
.bisectors
; k
++ )
305 vmf_face
*dummy
= faces
+ k
;
306 dummy
->indices
= NULL
;
307 dummy
->dispinfo
= NULL
;
308 dummy
->material
= NULL
;
316 while( (pSide
= vdf_next(node
, "side", &it
)) )
318 if( num_planes
>= SOLID_MAX_SIDES
)
320 flag
= k_ESolidResult_maxsides
;
321 fprintf( stderr
, "Solid over maxsides limit (%i)\n", SOLID_MAX_SIDES
);
327 vmf_face
*face
= faces
+ num_planes
;
328 face
->indices
= NULL
;
329 face
->dispinfo
= vdf_next( pSide
, "dispinfo", NULL
);
330 face
->material
= kv_get( pSide
, "material", "" );
331 face
->blacklisted
= mat_blacklisted( face
->material
);
333 kv_double_array( pSide
, "plane", 9, points
);
335 tri_to_plane( points
+6, points
+3, points
+0, vmf_api
.planes
+ num_planes
* 4 );
339 // Compute plane intersections
341 csr_comb_init( 3, i
);
343 v3f center
= { 0.f
, 0.f
, 0.f
};
345 u32 vert_start
= csr_sb_count( ctx
->verts
);
349 // DO something with i j k
352 if( (faces
[ i
[0] ].blacklisted
&& faces
[ i
[1] ].blacklisted
&& faces
[ i
[2] ].blacklisted
) )
355 if( !plane_intersect( vmf_api
.planes
+i
[0]*4, vmf_api
.planes
+i
[1]*4, vmf_api
.planes
+i
[2]*4, p
) )
358 // Check for illegal verts (eg: got clipped by bisectors)
360 for( int m
= 0; m
< num_planes
; m
++ )
362 if( plane_polarity( vmf_api
.planes
+m
*4, p
) > 1e-6f
)
371 ctx
->verts
= csr_sb_reserve( ctx
->verts
, 3, sizeof( vmf_vert
) );
373 // Take the vertex position and add it for centering base on average
375 v3_add( (v3f
){ p
[0], p
[1], p
[2] }, center
, center
);
377 // Store point / respecive normal for each plane that triggered the collision
378 for( int k
= 0; k
< 3; k
++ )
380 if( !faces
[ i
[k
] ].blacklisted
)
382 u32 c
= csr_sb_count( ctx
->verts
);
384 face_add_indice( faces
+ i
[k
], c
);
386 v3d_v3f( p
, ctx
->verts
[ c
].co
);
387 v3d_v3f( vmf_api
.planes
+i
[k
]*4, ctx
->verts
[ c
].nrm
);
389 csr_sb_inc( ctx
->verts
, 1 );
394 while( csr_comb( 3, num_planes
, i
) );
396 // Retrospectively set the center for each point
397 v3_divs( center
, (float)numpoints
, center
);
398 for( ; vert_start
< csr_sb_count( ctx
->verts
); vert_start
++ )
400 v2_copy( center
, ctx
->verts
[ vert_start
].xy
);
403 // Sort each faces and trianglulalate them
404 for( int k
= vmf_api
.bisectors
; k
< num_planes
; k
++ )
406 vmf_face
*face
= faces
+ k
;
408 if( face
->blacklisted
) continue;
410 if( csr_sb_count( face
->indices
) < 3 )
412 if( !vmf_api
.bisectors
)
414 flag
= k_ESolidResult_degenerate
;
415 fprintf( stderr
, "Skipping degenerate face\n" );
420 // Sort only if there is no displacements, or if this side is
421 if( !is_displacement
|| ( is_displacement
&& face
->dispinfo
) )
423 sort_coplanar( vmf_api
.planes
+k
*4, ctx
->verts
, face
->indices
, csr_sb_count( face
->indices
) );
426 if( is_displacement
)
428 // Compute displacement
431 if( csr_sb_count( face
->indices
) != 4 )
433 // Mute error if we have global planes cause they
434 // are of course gonna fuck things up here
435 if( !vmf_api
.bisectors
)
437 flag
= k_ESolidResult_degenerate
;
438 fprintf( stderr
, "Skipping degenerate displacement\n" );
443 // Match starting position
446 float dmin
= 999999.f
;
448 vdf_node
*dispinfo
= face
->dispinfo
;
449 vdf_node
*vdf_normals
= vdf_next( dispinfo
, "normals", NULL
);
450 vdf_node
*vdf_distances
= vdf_next( dispinfo
, "distances", NULL
);
452 kv_float_array( dispinfo
, "startposition", 3, start
);
454 for( int j
= 0; j
< csr_sb_count( face
->indices
); j
++ )
456 float d2
= v3_dist2( start
, ctx
->verts
[ face
->indices
[ j
] ].co
);
464 // Get corners of displacement
465 float *SW
= ctx
->verts
[ face
->indices
[ sw
] ].co
;
466 float *NW
= ctx
->verts
[ face
->indices
[ (sw
+1) % 4] ].co
;
467 float *NE
= ctx
->verts
[ face
->indices
[ (sw
+2) % 4] ].co
;
468 float *SE
= ctx
->verts
[ face
->indices
[ (sw
+3) % 4] ].co
;
470 // Can be either 5, 9, 17
471 numpoints
= pow( 2, kv_get_int( dispinfo
, "power", 2 ) ) + 1;
472 u32 reqverts
= numpoints
*numpoints
;
473 u32 reqidx
= (numpoints
-1)*(numpoints
-1)*6;
475 ctx
->verts
= csr_sb_reserve( ctx
->verts
, reqverts
, sizeof( vmf_vert
) );
476 ctx
->indices
= csr_sb_reserve( ctx
->indices
, reqidx
, sizeof( u32
) );
478 float normals
[ 17*3 ];
479 float distances
[ 17 ];
481 // Calculate displacement positions
482 for( int j
= 0; j
< numpoints
; j
++ )
485 sprintf( key
, "row%i", j
);
487 kv_float_array( vdf_normals
, key
, 17*3, normals
);
488 kv_float_array( vdf_distances
, key
, 17, distances
);
490 float dx
= (float)j
/ (float)(numpoints
- 1); //Time values for linear interpolation
492 for( int m
= 0; m
< numpoints
; m
++ )
494 vmf_vert
*vert
= &ctx
->verts
[ csr_sb_count( ctx
->verts
) + j
*numpoints
+ m
];
496 float dy
= (float)m
/ (float)(numpoints
- 1);
500 v3_lerp( SW
, SE
, dx
, lwr
);
501 v3_lerp( NW
, NE
, dx
, upr
);
502 v3_lerp( lwr
, upr
, dy
, vert
->co
);
504 v3_muladds( vert
->co
, normals
+ m
* 3, distances
[ m
], vert
->co
);
506 // Todo, put correct normal
507 v3_copy( (v3f
){ 0.f
, 0.f
, 1.f
}, vert
->nrm
);
511 // Build displacement indices
513 for( int row
= 0; row
< numpoints
- 1; row
++ )
515 for( int col
= 0; col
< numpoints
- 1; col
++ )
517 u32 c
= csr_sb_count( ctx
->verts
);
519 u32 idxsw
= c
+ ( row
+ 0 ) * numpoints
+ col
+ 0 ;
520 u32 idxse
= c
+ ( row
+ 0 ) * numpoints
+ col
+ 1 ;
521 u32 idxnw
= c
+ ( row
+ 1 ) * numpoints
+ col
+ 0 ;
522 u32 idxne
= c
+ ( row
+ 1 ) * numpoints
+ col
+ 1 ;
524 if( (condition
++) % 2 == 0 )
526 solid_disp_tri( ctx
, idxne
, idxnw
, idxsw
);
527 solid_disp_tri( ctx
, idxse
, idxne
, idxsw
);
531 solid_disp_tri( ctx
, idxse
, idxnw
, idxsw
);
532 solid_disp_tri( ctx
, idxse
, idxne
, idxnw
);
538 csr_sb_inc( ctx
->verts
, numpoints
*numpoints
);
543 u32 tris
= csr_sb_count( face
->indices
) -2;
544 ctx
->indices
= csr_sb_reserve( ctx
->indices
, tris
*3, sizeof( u32
) );
546 u32 c
= csr_sb_count( ctx
->indices
);
548 for( int j
= 0; j
< tris
; j
++ )
550 ctx
->indices
[ c
+j
*3 +0 ] = face
->indices
[ 0 ];
551 ctx
->indices
[ c
+j
*3 +1 ] = face
->indices
[ j
+1 ];
552 ctx
->indices
[ c
+j
*3 +2 ] = face
->indices
[ j
+2 ];
559 csr_sb_inc( ctx
->indices
, tris
*3 );
563 // Free temp polyon buffers
564 for( int j
= 0; j
< num_planes
; j
++ )
566 csr_sb_free( faces
[ j
].indices
);
572 u32
vmf_get_mdl( vmf_map
*map
, const char *mdl
)
574 u32 hash
= djb2( (const unsigned char *)mdl
);
576 for( u32 i
= 0; i
< csr_sb_count( map
->models
); i
++ )
578 if( hash
== map
->models
[i
].hash
&& !strcmp( map
->models
[i
].str
, mdl
) )
587 int vmf_class_is_prop( vdf_node
*ent
)
589 return !strncmp( kv_get( ent
, "classname", "" ), "prop_", 5 );
592 void vmf_populate_models( vdf_node
*vmf
, vmf_map
*map
)
594 vdf_foreach( vmf
, "entity", ent
)
596 // Use any class name with prop_
597 if( vmf_class_is_prop( ent
) )
599 // Check if it exists
600 const char *model_path
= kv_get( ent
, "model", "" );
601 u32 mdl_id
= vmf_get_mdl( map
, model_path
);
605 map
->models
= csr_sb_reserve( map
->models
, 1, sizeof( struct vmf_model
));
607 struct vmf_model
*entry
= &map
->models
[ csr_sb_count( map
->models
) ];
608 entry
->str
= csr_malloc( strlen( model_path
) +1 );
609 strcpy( entry
->str
, model_path
);
610 entry
->hash
= djb2( (const unsigned char *)model_path
);
612 mdl_id
= csr_sb_count( map
->models
);
613 csr_sb_use( map
->models
);
616 // Assign prop-ID for later use
617 ent
->user
= VMF_FLAG_IS_PROP
;
624 void vmf_load_models( vmf_map
*map
)
626 printf( "Loading all models\n" );
628 // Error model. TODO: Maybe don't have this be junk data.
629 map
->models
= csr_sb_reserve( map
->models
, 1, sizeof( struct vmf_model
));
630 csr_sb_use( map
->models
);
631 mdl_error( &map
->models
[0].mdl
);
633 // Create listings for each model
634 vmf_populate_models( map
->root
, map
);
636 for( int i
= 0; i
< csr_sb_count( map
->cache
); i
++ )
638 vmf_populate_models( map
->cache
[i
].root
, map
);
641 printf( "Indexed (%u) models\n", csr_sb_count( map
->models
)-1 );
646 // TODO: Make nice loading bar
647 for( int i
= 1; i
< csr_sb_count( map
->models
); i
++ )
649 struct vmf_model
*mdl
= &map
->models
[i
];
651 if( mdl_from_find_files( mdl
->str
, &mdl
->mdl
) )
657 fprintf( stderr
, "Failed to load model: %s\n", mdl
->str
);
661 printf( "Done (%u of %u loaded)\n", num_success
, csr_sb_count( map
->models
)-1 );
664 u32
vmf_init_subvmf( vmf_map
*map
, const char *subvmf
);
666 void vmf_load_all_instances( vmf_map
*map
, vdf_node
*vmf
)
668 vdf_foreach( vmf
, "entity", ent
)
670 if( !strcmp( kv_get( ent
, "classname", "" ), "func_instance" ))
672 // Entity is in use if file is specified, if not just ignore the entity.
673 const char *path
= kv_get( ent
, "file", "" );
674 if( strcmp( path
, "" ) )
676 if( (ent
->user1
= vmf_init_subvmf( map
, path
)))
679 ent
->user
= VMF_FLAG_IS_INSTANCE
;
686 // TODO: Merge this into above function.. doesnt need to be seperated
687 u32
vmf_init_subvmf( vmf_map
*map
, const char *subvmf
)
690 u32 hash
= djb2( (const unsigned char *)subvmf
);
693 for( u32 i
= 0; i
< csr_sb_count( map
->cache
); i
++ )
695 if( hash
== map
->cache
[i
].hash
)
697 if( !strcmp( map
->cache
[i
].name
, subvmf
) )
704 printf( "Loading subvmf: %s\n", subvmf
);
706 id
= csr_sb_count( map
->cache
);
707 map
->cache
= csr_sb_reserve( map
->cache
, 1, sizeof( struct vmf_instance
));
708 struct vmf_instance
*inst
= &map
->cache
[ id
];
710 if( (inst
->root
= vdf_open_file( subvmf
)) )
712 csr_sb_use( map
->cache
);
715 inst
->name
= csr_malloc( strlen( subvmf
)+1 );
716 strcpy( inst
->name
, subvmf
);
718 // Recursive load other instances
719 vmf_load_all_instances( map
, inst
->root
);
725 fprintf( stderr
, "Failed to load instance file\n" );
730 vmf_map
*vmf_init( const char *path
, int load_models
)
732 vmf_map
*map
= csr_calloc( sizeof( vmf_map
) );
733 map
->root
= vdf_open_file( path
);
736 vmf_load_all_instances( map
, map
->root
);
741 vmf_load_models( map
);
747 void vmf_free( vmf_map
*map
)
749 for( int i
= 0; i
< csr_sb_count( map
->cache
); i
++ )
751 vdf_free_r( map
->cache
[i
].root
);
752 free( map
->cache
[i
].name
);
755 for( int i
= 1; i
< csr_sb_count( map
->models
); i
++ )
757 free( map
->models
[i
].str
);
758 mdl_free( &map
->models
[i
].mdl
);
761 vdf_free_r( map
->root
);
762 csr_sb_free( map
->models
);
763 csr_sb_free( map
->cache
);
767 void solidgen_to_obj( vmf_solid
*ctx
, const char *path
)
769 FILE *fp
= fopen( path
, "w" );
773 fprintf( fp
, "o vmf_export\n" );
777 // Write vertex block
778 for( int i
= 0; i
< csr_sb_count( ctx
->verts
); i
++ )
780 vert
= &ctx
->verts
[i
];
781 fprintf( fp
, "v %f %f %f\n", vert
->co
[0], vert
->co
[1], vert
->co
[2] );
784 // Write normals block
785 for( int i
= 0; i
< csr_sb_count( ctx
->verts
); i
++ )
787 vert
= &ctx
->verts
[i
];
788 fprintf( fp
, "vn %f %f %f\n", vert
->nrm
[0], vert
->nrm
[1], vert
->nrm
[2] );
791 fprintf( fp
, "s off\n" );
794 for( int i
= 0; i
< csr_sb_count( ctx
->indices
)/3; i
++ )
796 u32
* base
= ctx
->indices
+ i
*3;
797 fprintf( fp
, "f %u//%u %u//%u %u//%u\n",
798 base
[2]+1, base
[2]+1,
799 base
[1]+1, base
[1]+1,
808 fprintf( stderr
, "Could not open %s for writing\n", path
);
812 void vmf_entity_transform( vdf_node
*ent
, m4x3f mat
)
814 v3f angles
= {0.f
,0.f
,0.f
};
815 v3f offset
= {0.f
,0.f
,0.f
};
819 scale
= kv_get_float( ent
, "uniformscale", 1.f
);
820 kv_float_array( ent
, "angles", 3, angles
);
821 kv_float_array( ent
, "origin", 3, offset
);
824 m4x3_translate( mat
, offset
);
826 // Make rotation ( Pitch yaw roll // YZX. Source->OpenGL ordering a lil messed up )
827 m4x3_rotate_z( mat
, csr_rad( angles
[1] ) );
828 m4x3_rotate_y( mat
, csr_rad( angles
[0] ) );
829 m4x3_rotate_x( mat
, csr_rad( angles
[2] ) );
832 m4x3_scale( mat
, scale
);