4 A GNU/Linux-first Source1 Hammer replacement
5 built with Blender, for mapmakers
7 Copyright (C) 2022 Harry Godden (hgn)
9 LICENSE: GPLv3.0, please see COPYING and LICENSE for more information
12 - Brush decomposition into convex pieces for well defined geometry
13 - Freely form displacements without limits
14 - Build your entire map in Blender
15 - Compile models and model groups easily
16 - It runs at an ok speed!
17 - Light patch BSP files; remove unwanted realtime effects
18 - Bestest VTF compressor (thanks to Richgel999 and stb)
19 - Pack content automatically
23 File/folder Lang Purpose
25 __init__.py Python Blender plugin interface
28 cxr.h C Heavy lifting; brush decomp, mesh processing
29 cxr_math.h C Vector maths and other handy things
30 cxr_mem.h C Automatic resizing buffers
31 libcxr.c C Compile as SO
34 nbvtf.h C VTF processing interface
35 librgcx.h C++ Rich Geldreich's DXT1/DXT5 compressors
36 stb/ C Sean Barrets image I/O
51 #define CXR_EPSILON 0.001
52 #define CXR_PLANE_SIMILARITY_MAX 0.998
53 #define CXR_BIG_NUMBER 1e300
54 #define CXR_INTERIOR_ANGLE_MAX 0.998
57 #define CXR_IMPLEMENTATION
65 #include "cxr_types.h"
71 #include "cxr_valve_bin.h"
74 typedef struct cxr_world cxr_world
;
75 typedef struct cxr_solid cxr_solid
;
77 typedef struct cxr_mesh cxr_mesh
;
78 typedef struct cxr_edge cxr_edge
;
79 typedef struct cxr_polygon cxr_polygon
;
80 typedef struct cxr_static_mesh cxr_static_mesh
;
81 typedef struct cxr_loop cxr_loop
;
82 typedef struct cxr_static_loop cxr_static_loop
;
83 typedef struct cxr_material cxr_material
;
84 typedef struct cxr_tri_mesh cxr_tri_mesh
;
86 #ifdef CXR_VALVE_MAP_FILE
87 typedef struct cxr_vdf cxr_vdf
;
88 typedef struct cxr_texinfo cxr_texinfo
;
89 typedef struct cxr_visgroup cxr_visgroup
;
90 typedef struct cxr_vmf_context cxr_vmf_context
;
91 #endif /* CXR_VALVE_MAP_FILE */
97 /* Main convexer algorithms */
98 /* Convex decomp from mesh */
99 CXR_API cxr_world
*cxr_decompose( cxr_static_mesh
*src
, i32
*perrcode
);
100 CXR_API
void cxr_free_world( cxr_world
*world
);
101 CXR_API cxr_tri_mesh
*cxr_world_preview( cxr_world
*world
);
102 CXR_API
void cxr_free_tri_mesh( cxr_tri_mesh
*mesh
);
104 #ifdef CXR_VALVE_MAP_FILE
106 CXR_API
void cxr_begin_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
107 CXR_API
void cxr_vmf_begin_entities( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
108 CXR_API
void cxr_push_world_vmf(
109 cxr_world
*world
, cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
110 CXR_API
void cxr_end_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
113 CXR_API cxr_vdf
*cxr_vdf_open( const char *path
);
114 CXR_API
void cxr_vdf_close( cxr_vdf
*vdf
);
115 CXR_API
void cxr_vdf_put( cxr_vdf
*vdf
, const char *str
);
116 CXR_API
void cxr_vdf_node( cxr_vdf
*vdf
, const char *str
);
117 CXR_API
void cxr_vdf_edon( cxr_vdf
*vdf
);
118 CXR_API
void cxr_vdf_kv( cxr_vdf
*vdf
, const char *strk
, const char *strv
);
121 CXR_API
int cxr_lightpatch_bsp( const char *path
);
122 #endif /* CXR_VALVE_MAP_FILE */
126 CXR_API
void cxr_set_log_function( void (*func
)(const char *str
) );
127 CXR_API
void cxr_set_line_function( void (*func
)(v3f p0
, v3f p1
, v4f colour
) );
128 CXR_API
void cxr_write_test_data( cxr_static_mesh
*src
);
129 #endif /* CXR_DEBUG */
131 struct cxr_static_mesh
138 i32 freestyle
, sharp
;
142 struct cxr_static_loop
153 i32 loop_start
, loop_total
;
156 i32 material_id
; /* -1: interior material (nodraw) */
196 cxr_material
*materials
;
207 *p_abverts
; /* This data is stored externally because the data is often
208 shared between solids. */
210 /* Valid when update() is called on this mesh,
211 * Invalid when data is appended to them */
212 struct cxr_edge
*edges
;
213 struct cxr_polygon
*polys
;
214 struct cxr_loop
*loops
;
217 /* Simple mesh type mainly for debugging */
230 #ifdef CXR_VALVE_MAP_FILE
244 * Simplified VDF writing interface. No allocations or nodes, just write to file
252 struct cxr_vmf_context
260 cxr_visgroup
*visgroups
;
263 /* Transform settings */
274 #endif /* CXR_VALVE_MAP_FILE */
279 k_soliderr_non_manifold
,
280 k_soliderr_bad_manifold
,
281 k_soliderr_no_solids
,
282 k_soliderr_degenerate_implicit
,
283 k_soliderr_non_coplanar_vertices
,
284 k_soliderr_non_convex_poly
,
285 k_soliderr_bad_result
,
286 k_soliderr_invalid_input
291 * -----------------------------------------------------------------------------
293 #ifdef CXR_IMPLEMENTATION
295 const char *cxr_build_time
= __DATE__
" @" __TIME__
;
301 #warning 32 bit is not supported in blender 3.0
305 static int cxr_range(int x
, int bound
)
308 x
+= bound
* (x
/bound
+ 1);
314 * This should be called after appending any data to those buffers
316 static void cxr_mesh_update( cxr_mesh
*mesh
)
318 mesh
->edges
= cxr_ab_ptr(&mesh
->abedges
, 0);
319 mesh
->polys
= cxr_ab_ptr(&mesh
->abpolys
, 0);
320 mesh
->loops
= cxr_ab_ptr(&mesh
->abloops
, 0);
323 static v4f colours_random
[] =
325 { 0.863, 0.078, 0.235, 0.4 },
326 { 0.000, 0.980, 0.604, 0.4 },
327 { 0.118, 0.565, 1.000, 0.4 },
328 { 0.855, 0.439, 0.839, 0.4 },
329 { 0.824, 0.412, 0.118, 0.4 },
330 { 0.125, 0.698, 0.667, 0.4 },
331 { 0.541, 0.169, 0.886, 0.4 },
332 { 1.000, 0.843, 0.000, 0.4 }
335 static v4f colours_solids
[] =
337 { 100, 143, 255, 200 },
338 { 120, 94, 240, 200 },
339 { 220, 38, 127, 200 },
344 static v4f colour_entity
= { 37, 241, 122, 255 };
345 static v4f colour_displacement_solid
= { 146, 146, 146, 120 };
346 static v4f colour_error
= { 1.0f
, 0.0f
, 0.0f
, 1.0f
};
347 static v4f colour_face_graph
= { 1.0f
, 1.0f
, 1.0f
, 0.03f
};
348 static v4f colour_success
= { 0.0f
, 1.0f
, 0.0f
, 1.0f
};
350 static void value_random(int n
, v4f colour
)
352 double val
= cxr_range(n
,8);
356 v3_muls( colour
, val
, colour
);
359 static void colour_random_brush(int n
, v4f colour
)
363 int colour_n
= cxr_range( n
, 5 );
364 v4_muls( colours_solids
[ colour_n
], 1.0/255.0, colour
);
365 value_random( value_n
, colour
);
367 int colour_n
= cxr_range( n
, 8 );
368 v4_copy( colours_random
[ colour_n
], colour
);
373 * Debugging and diagnostic utilities
374 * -----------------------------------------------------------------------------
379 static void cxr_debug_line( v3f p0
, v3f p1
, v4f colour
)
382 cxr_line_func( p0
, p1
, colour
);
385 static void cxr_debug_box( v3f p0
, double sz
, v4f colour
)
389 v3_add(p0
, (v3f
){-sz
,-sz
,-sz
}, a
);
390 v3_add(p0
, (v3f
){-sz
, sz
,-sz
}, b
);
391 v3_add(p0
, (v3f
){ sz
, sz
,-sz
}, c
);
392 v3_add(p0
, (v3f
){ sz
,-sz
,-sz
}, d
);
393 v3_add(p0
, (v3f
){-sz
,-sz
,sz
}, a1
);
394 v3_add(p0
, (v3f
){-sz
, sz
,sz
}, b1
);
395 v3_add(p0
, (v3f
){ sz
, sz
,sz
}, c1
);
396 v3_add(p0
, (v3f
){ sz
,-sz
,sz
}, d1
);
398 cxr_debug_line( a
,b
, colour
);
399 cxr_debug_line( b
,c
, colour
);
400 cxr_debug_line( c
,d
, colour
);
401 cxr_debug_line( d
,a
, colour
);
402 cxr_debug_line( a1
,b1
, colour
);
403 cxr_debug_line( b1
,c1
, colour
);
404 cxr_debug_line( c1
,d1
, colour
);
405 cxr_debug_line( d1
,a1
, colour
);
406 cxr_debug_line( a
,a1
, colour
);
407 cxr_debug_line( b
,b1
, colour
);
408 cxr_debug_line( c
,c1
, colour
);
409 cxr_debug_line( d
,d1
, colour
);
413 * Draw arrow with the tips oriented along normal
415 static void cxr_debug_arrow( v3f p0
, v3f p1
, v3f normal
, double sz
, v4f colour
)
417 v3f dir
, tan
, p2
, p3
;
421 v3_cross(dir
,normal
,tan
);
422 v3_muladds( p1
,dir
, -sz
, p2
);
423 v3_muladds( p2
,tan
,sz
,p3
);
424 cxr_debug_line( p1
, p3
, colour
);
425 v3_muladds( p2
,tan
,-sz
,p3
);
426 cxr_debug_line( p1
, p3
, colour
);
427 cxr_debug_line( p0
, p1
, colour
);
431 * Draw arrows CCW around polygon, draw normal vector from center
433 static void cxr_debug_poly( cxr_mesh
*mesh
, cxr_polygon
*poly
, v4f colour
)
435 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
437 for( int i
=0; i
<poly
->loop_total
; i
++ )
439 int lp0
= poly
->loop_start
+i
,
440 lp1
= poly
->loop_start
+cxr_range(i
+1,poly
->loop_total
);
442 int i0
= mesh
->loops
[ lp0
].index
,
443 i1
= mesh
->loops
[ lp1
].index
;
447 v3_lerp( verts
[i0
], poly
->center
, 0.0075, p0
);
448 v3_lerp( verts
[i1
], poly
->center
, 0.0075, p1
);
449 v3_muladds( p0
, poly
->normal
, 0.01, p0
);
450 v3_muladds( p1
, poly
->normal
, 0.01, p1
);
452 cxr_debug_arrow( p0
, p1
, poly
->normal
, 0.05, colour
);
456 v3_muladds( poly
->center
, poly
->normal
, 0.3, nrm0
);
458 cxr_debug_line( poly
->center
, nrm0
, colour
);
461 static void cxr_debug_mesh(cxr_mesh
*mesh
, v4f colour
)
463 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
465 cxr_polygon
*poly
= &mesh
->polys
[i
];
466 cxr_debug_poly( mesh
, poly
, colour
);
470 CXR_API
void cxr_write_test_data( cxr_static_mesh
*src
)
473 "/home/harry/Documents/blender_addons_remote/addons/convexer/cxr/solid.h",
476 fprintf( fp
, "v3f test_verts[] = {\n" );
477 for( int i
=0; i
<src
->vertex_count
; i
++ )
479 fprintf( fp
, " { %f, %f, %f },\n",
482 src
->vertices
[i
][2] );
484 fprintf( fp
, "};\n" );
486 fprintf( fp
, "cxr_static_loop test_loops[] = {\n" );
487 for( int i
=0; i
<src
->loop_count
; i
++ )
489 fprintf( fp
, " {%d, %d, {%f, %f}, %f},\n",
491 src
->loops
[i
].edge_index
,
494 src
->loops
[i
].alpha
);
496 fprintf( fp
, "};\n" );
498 fprintf( fp
, "cxr_polygon test_polys[] = {\n" );
499 for( int i
=0; i
<src
->poly_count
; i
++ )
501 fprintf( fp
, " {%d, %d, {%f, %f, %f}, {%f, %f, %f}},\n",
502 src
->polys
[i
].loop_start
,
503 src
->polys
[i
].loop_total
,
504 src
->polys
[i
].normal
[0],
505 src
->polys
[i
].normal
[1],
506 src
->polys
[i
].normal
[2],
507 src
->polys
[i
].center
[0],
508 src
->polys
[i
].center
[1],
509 src
->polys
[i
].center
[2] );
511 fprintf( fp
, "};\n" );
513 fprintf( fp
, "cxr_edge test_edges[] = {\n" );
514 for( int i
=0; i
<src
->edge_count
; i
++ )
516 fprintf( fp
, " {%d, %d, %d, %d},\n",
519 src
->edges
[i
].freestyle
,
523 fprintf( fp
, "};\n" );
525 fprintf( fp
, "cxr_static_mesh test_mesh = {\n" );
526 fprintf( fp
, " .vertices = test_verts,\n" );
527 fprintf( fp
, " .loops = test_loops,\n" );
528 fprintf( fp
, " .edges = test_edges,\n" );
529 fprintf( fp
, " .polys = test_polys,\n" );
530 fprintf( fp
, " .poly_count=%d,\n", src
->poly_count
);
531 fprintf( fp
, " .vertex_count=%d,\n", src
->vertex_count
);
532 fprintf( fp
, " .edge_count=%d,\n",src
->edge_count
);
533 fprintf( fp
, " .loop_count=%d\n", src
->loop_count
);
534 fprintf( fp
, "};\n" );
539 CXR_API
void cxr_set_log_function( void (*func
)(const char *str
) )
544 CXR_API
void cxr_set_line_function( void (*func
)(v3f p0
, v3f p1
, v4f colour
) )
546 cxr_line_func
= func
;
549 #endif /* CXR_DEBUG */
553 * abverts is a pointer to an existing vertex buffer
555 static cxr_mesh
*cxr_alloc_mesh( int edge_count
, int loop_count
, int poly_count
,
558 cxr_mesh
*mesh
= malloc(sizeof(cxr_mesh
));
559 cxr_ab_init(&mesh
->abedges
, sizeof(cxr_edge
), edge_count
);
560 cxr_ab_init(&mesh
->abloops
, sizeof(cxr_loop
), loop_count
);
561 cxr_ab_init(&mesh
->abpolys
, sizeof(cxr_polygon
), poly_count
);
562 mesh
->p_abverts
= abverts
;
564 cxr_mesh_update( mesh
);
569 static void cxr_free_mesh( cxr_mesh
*mesh
)
571 cxr_ab_free(&mesh
->abedges
);
572 cxr_ab_free(&mesh
->abloops
);
573 cxr_ab_free(&mesh
->abpolys
);
578 * Rebuilds edge data for mesh (useful to get rid of orphaned edges)
580 static void cxr_mesh_clean_edges( cxr_mesh
*mesh
)
582 cxr_abuffer new_edges
;
583 cxr_ab_init( &new_edges
, sizeof(cxr_edge
), mesh
->abedges
.count
);
585 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
587 cxr_polygon
*poly
= &mesh
->polys
[i
];
588 for( int j
=0; j
<poly
->loop_total
; j
++ )
591 *lp0
= &mesh
->loops
[poly
->loop_start
+j
],
592 *lp1
= &mesh
->loops
[poly
->loop_start
+cxr_range(j
+1,poly
->loop_total
)];
594 int i0
= cxr_min(lp0
->index
, lp1
->index
),
595 i1
= cxr_max(lp0
->index
, lp1
->index
);
597 /* Check if edge exists before adding */
598 for( int k
=0; k
<new_edges
.count
; k
++ )
600 cxr_edge
*edge
= cxr_ab_ptr(&new_edges
,k
);
602 if( edge
->i0
== i0
&& edge
->i1
== i1
)
605 goto IL_EDGE_CREATED
;
609 int orig_edge_id
= lp0
->edge_index
;
610 lp0
->edge_index
= new_edges
.count
;
612 cxr_edge edge
= { i0
, i1
};
615 * Copy extra information from original edges
618 if( orig_edge_id
< mesh
->abedges
.count
)
620 cxr_edge
*orig_edge
= &mesh
->edges
[ orig_edge_id
];
621 edge
.freestyle
= orig_edge
->freestyle
;
622 edge
.sharp
= orig_edge
->sharp
;
630 cxr_ab_push( &new_edges
, &edge
);
636 cxr_ab_free( &mesh
->abedges
);
637 mesh
->abedges
= new_edges
;
639 cxr_mesh_update( mesh
);
643 * Remove 0-length faces from mesh (we mark them light that for deletion
644 * Remove all unused loops as a result of removing those faces
646 static void cxr_mesh_clean_faces( cxr_mesh
*mesh
)
648 cxr_abuffer loops_new
;
649 cxr_ab_init( &loops_new
, sizeof(cxr_loop
), mesh
->abloops
.count
);
652 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
654 cxr_polygon
*src
= &mesh
->polys
[i
],
655 *dst
= &mesh
->polys
[new_length
];
657 if( src
->loop_total
> 0 )
659 int src_start
= src
->loop_start
,
660 src_total
= src
->loop_total
;
663 dst
->loop_start
= loops_new
.count
;
665 for( int j
=0; j
<src_total
; j
++ )
667 cxr_loop
*loop
= &mesh
->loops
[src_start
+j
],
668 *ldst
= cxr_ab_ptr(&loops_new
,dst
->loop_start
+j
);
670 ldst
->poly_left
= new_length
;
673 loops_new
.count
+= src_total
;
678 cxr_ab_free( &mesh
->abloops
);
679 mesh
->abloops
= loops_new
;
680 mesh
->abpolys
.count
= new_length
;
682 cxr_mesh_update( mesh
);
686 * Links loop's poly_left and poly_right
687 * Does not support more than 2 polys to one edge
689 * Returns 0 if there is non-manifold geomtry (aka: not watertight)
691 static int cxr_mesh_link_loops( cxr_mesh
*mesh
)
693 i32
*polygon_edge_map
= malloc(mesh
->abedges
.count
*2 *sizeof(i32
));
695 for( int i
= 0; i
< mesh
->abedges
.count
*2; i
++ )
696 polygon_edge_map
[i
] = -1;
698 for( int i
= 0; i
< mesh
->abpolys
.count
; i
++ )
700 cxr_polygon
*poly
= &mesh
->polys
[i
];
702 for( int j
= 0; j
< poly
->loop_total
; j
++ )
704 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
707 for( int k
= 0; k
< 2; k
++ )
709 i32
*edge
= &polygon_edge_map
[loop
->edge_index
*2+k
];
718 /* Overflowed edge mapping... Duplicated faces. */
719 free( polygon_edge_map
);
725 for( int i
= 0; i
< mesh
->abpolys
.count
; i
++ )
727 cxr_polygon
*poly
= &mesh
->polys
[i
];
729 for( int j
= 0; j
< poly
->loop_total
; j
++ )
731 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
733 i32
*face_map
= &polygon_edge_map
[ loop
->edge_index
*2 ];
735 if( face_map
[0] == loop
->poly_left
) loop
->poly_right
= face_map
[1];
736 else loop
->poly_right
= face_map
[0];
741 for( int i
=0; i
<mesh
->abedges
.count
*2; i
++ )
743 if( polygon_edge_map
[i
] == -1 )
745 free( polygon_edge_map
);
750 free( polygon_edge_map
);
755 * Create new empty polygon with known loop count
756 * Must be filled and completed by the following functions!
758 static int cxr_create_poly( cxr_mesh
*mesh
, int loop_count
)
760 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
765 cxr_log( "tried to add new poly with length %d!\n", loop_count
);
770 cxr_ab_reserve( &mesh
->abpolys
, 1 );
771 cxr_ab_reserve( &mesh
->abloops
, loop_count
);
772 cxr_mesh_update( mesh
);
774 cxr_polygon
*poly
= &mesh
->polys
[ mesh
->abpolys
.count
];
776 poly
->loop_start
= mesh
->abloops
.count
;
777 poly
->loop_total
= 0;
778 poly
->material_id
= -1;
779 v3_zero( poly
->center
);
785 * Add one index to the polygon created by the above function
787 static void cxr_poly_push_index( cxr_mesh
*mesh
, int id
)
789 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
791 int nface_id
= mesh
->abpolys
.count
;
792 cxr_polygon
*poly
= &mesh
->polys
[ nface_id
];
794 cxr_loop
*new_loop
= &mesh
->loops
[ poly
->loop_start
+ poly
->loop_total
];
796 new_loop
->poly_left
= nface_id
;
797 new_loop
->poly_right
= -1;
798 new_loop
->index
= id
;
799 new_loop
->edge_index
= 0;
800 v2_zero(new_loop
->uv
);
802 v3_add( poly
->center
, verts
[new_loop
->index
], poly
->center
);
805 mesh
->abloops
.count
++;
809 * Finalize and commit polygon into mesh
811 static void cxr_poly_finish( cxr_mesh
*mesh
)
813 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
815 int nface_id
= mesh
->abpolys
.count
;
816 cxr_polygon
*poly
= &mesh
->polys
[nface_id
];
818 /* Average center and calc normal */
820 v3_divs( poly
->center
, poly
->loop_total
, poly
->center
);
821 cxr_loop
*lp0
= &mesh
->loops
[ poly
->loop_start
],
822 *lp1
= &mesh
->loops
[ poly
->loop_start
+1 ],
823 *lp2
= &mesh
->loops
[ poly
->loop_start
+2 ];
826 verts
[lp0
->index
], verts
[lp1
->index
], verts
[lp2
->index
], poly
->normal
);
828 mesh
->abpolys
.count
++;
832 * Extract the next island from mesh
834 * Returns NULL if mesh is one contigous object
836 static cxr_mesh
*cxr_pull_island( cxr_mesh
*mesh
)
838 cxr_mesh_link_loops(mesh
);
840 int *island_current
= malloc(mesh
->abpolys
.count
*sizeof(int)),
845 island_current
[0] = 0;
848 last_count
= island_len
;
850 for( int i
=0; i
<island_len
; i
++ )
852 cxr_polygon
*poly
= &mesh
->polys
[ island_current
[i
] ];
854 for( int j
=0; j
<poly
->loop_total
; j
++ )
856 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
858 if( loop
->poly_right
!= -1 )
860 int face_present
= 0;
862 for( int k
=0; k
<island_len
; k
++ )
864 if( island_current
[k
] == loop
->poly_right
)
872 island_current
[ island_len
++ ] = loop
->poly_right
;
877 if( island_len
> last_count
)
880 /* Check for complete object */
881 if( island_len
== mesh
->abpolys
.count
)
883 free( island_current
);
887 for( int i
=0; i
<island_len
; i
++ )
889 cxr_polygon
*poly
= &mesh
->polys
[ island_current
[i
] ];
890 loop_count
+= poly
->loop_total
;
893 /* Create and update meshes */
894 cxr_mesh
*newmesh
= cxr_alloc_mesh( mesh
->abedges
.count
,
899 for( int i
=0; i
<island_len
; i
++ )
901 cxr_polygon
*src
= &mesh
->polys
[ island_current
[i
] ];
902 cxr_polygon
*dst
= cxr_ab_ptr(&newmesh
->abpolys
, i
);
905 dst
->loop_start
= newmesh
->abloops
.count
;
907 for( int j
=0; j
<src
->loop_total
; j
++ )
910 *lsrc
= &mesh
->loops
[ src
->loop_start
+j
],
911 *ldst
= cxr_ab_ptr(&newmesh
->abloops
, dst
->loop_start
+j
);
915 ldst
->poly_right
= -1;
918 newmesh
->abloops
.count
+= src
->loop_total
;
919 src
->loop_total
= -1;
922 newmesh
->abpolys
.count
= island_len
;
923 newmesh
->abedges
.count
= mesh
->abedges
.count
;
924 memcpy( cxr_ab_ptr(&newmesh
->abedges
,0),
926 mesh
->abedges
.count
* sizeof(cxr_edge
));
928 cxr_mesh_clean_faces(mesh
);
929 cxr_mesh_clean_edges(mesh
);
930 cxr_mesh_clean_edges(newmesh
);
932 free( island_current
);
937 * Invalid solid is when there are vertices that are coplanar to a face, but are
938 * outside the polygons edges.
940 static int cxr_valid_solid( cxr_mesh
*mesh
, int *solid
, int len
)
942 v3f
*verts
= cxr_ab_ptr(mesh
->p_abverts
, 0);
944 for( int i
=0; i
<len
; i
++ )
946 cxr_polygon
*polyi
= &mesh
->polys
[ solid
[i
] ];
949 normal_to_plane(polyi
->normal
, polyi
->center
, plane
);
951 for( int j
=0; j
<len
; j
++ )
955 cxr_polygon
*polyj
= &mesh
->polys
[ solid
[j
] ];
957 for( int k
=0; k
<polyj
->loop_total
; k
++ )
959 cxr_loop
*lpj
= &mesh
->loops
[ polyj
->loop_start
+k
];
961 /* Test if the vertex is not referenced by the polygon */
962 for( int l
=0; l
<polyi
->loop_total
; l
++ )
964 cxr_loop
*lpi
= &mesh
->loops
[ polyi
->loop_start
+l
];
966 if( lpi
->index
== lpj
->index
)
970 if( fabs(plane_polarity(plane
, verts
[lpj
->index
])) < 0.001 )
982 * Use when iterating the loops array, to get a unique set of edges
983 * Better than using the edges array and doing many more checks
985 static int cxr_loop_unique_edge( cxr_loop
*lp
)
987 if( lp
->poly_left
> lp
->poly_right
)
994 * Identify edges in the mesh where the two connected face's normals
995 * are opposing eachother (or close to identical)
997 static int *cxr_mesh_reflex_edges( cxr_mesh
*mesh
)
999 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1000 int *edge_tagged
= malloc( mesh
->abedges
.count
* sizeof(int) );
1002 for( int i
=0; i
<mesh
->abloops
.count
; i
++ )
1004 cxr_loop
*lp
= &mesh
->loops
[i
];
1005 if( !cxr_loop_unique_edge( lp
) ) continue;
1007 edge_tagged
[lp
->edge_index
] = 0;
1009 cxr_polygon
*polya
= &mesh
->polys
[ lp
->poly_left
],
1010 *polyb
= &mesh
->polys
[ lp
->poly_right
];
1013 normal_to_plane(polyb
->normal
, polyb
->center
, planeb
);
1015 for( int j
=0; j
<polya
->loop_total
; j
++ )
1017 cxr_loop
*lp1
= &mesh
->loops
[ polya
->loop_start
+j
];
1019 if(( plane_polarity( planeb
, verts
[lp1
->index
] ) > 0.001 ) ||
1020 ( v3_dot(polya
->normal
,polyb
->normal
) > CXR_PLANE_SIMILARITY_MAX
))
1022 edge_tagged
[lp
->edge_index
] = 1;
1032 * Same logic as above function except it will apply it to each vertex
1034 static int *cxr_mesh_reflex_vertices( cxr_mesh
*mesh
)
1036 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1038 int *vertex_tagged
= malloc( mesh
->p_abverts
->count
*sizeof(int) );
1039 int *connected_planes
= malloc( mesh
->abpolys
.count
*sizeof(int) );
1041 for( int i
=0; i
<mesh
->p_abverts
->count
; i
++ )
1044 int num_connected
= 0;
1046 /* Create a list of polygons that refer to this vertex */
1047 for( int j
=0; j
<mesh
->abpolys
.count
; j
++ )
1049 cxr_polygon
*poly
= &mesh
->polys
[j
];
1050 for( int k
=0; k
<poly
->loop_total
; k
++ )
1052 cxr_loop
*loop
= &mesh
->loops
[poly
->loop_start
+k
];
1053 if( loop
->index
== i
)
1055 connected_planes
[num_connected
++] = j
;
1061 /* Check all combinations for a similar normal */
1062 for( int j
=0; j
<num_connected
-1; j
++ )
1064 for( int k
=j
+1; k
<num_connected
; k
++ )
1066 cxr_polygon
*polyj
= &mesh
->polys
[connected_planes
[j
]],
1067 *polyk
= &mesh
->polys
[connected_planes
[k
]];
1069 if( v3_dot(polyj
->normal
,polyk
->normal
) > CXR_PLANE_SIMILARITY_MAX
)
1075 * Check if all connected planes either:
1077 * - Coplanar with it
1079 for( int j
=0; j
<num_connected
; j
++ )
1081 for( int k
=j
+1; k
<num_connected
; k
++ )
1083 cxr_polygon
*jpoly
= &mesh
->polys
[ connected_planes
[j
] ],
1084 *kpoly
= &mesh
->polys
[ connected_planes
[k
] ];
1087 normal_to_plane( kpoly
->normal
, kpoly
->center
, plane
);
1088 for( int l
=0; l
<jpoly
->loop_total
; l
++ )
1090 cxr_loop
*lp
= &mesh
->loops
[ jpoly
->loop_start
+l
];
1092 if( plane_polarity( plane
, verts
[lp
->index
] ) > 0.001 )
1100 vertex_tagged
[i
] = 1;
1103 free( connected_planes
);
1104 return vertex_tagged
;
1108 * Detect if potential future edges create a collision with any of the
1109 * existing edges in the mesh
1111 static int cxr_solid_overlap( cxr_mesh
*mesh
,
1114 int common_edge_index
1116 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1117 cxr_edge
*common_edge
= &mesh
->edges
[common_edge_index
];
1119 int unique_a
= pa
->loop_total
-2,
1120 unique_b
= pb
->loop_total
-2;
1122 int *unique_verts
= malloc( (unique_a
+unique_b
)*sizeof(int) );
1123 int unique_total
= 0;
1125 for( int j
=0; j
<2; j
++ )
1127 cxr_polygon
*poly
= (cxr_polygon
*[2]){pa
,pb
}[j
];
1129 for( int i
=0; i
<poly
->loop_total
; i
++ )
1131 cxr_loop
*lp
= &mesh
->loops
[poly
->loop_start
+i
];
1133 if( lp
->index
== common_edge
->i0
|| lp
->index
== common_edge
->i1
)
1136 unique_verts
[ unique_total
++ ] = lp
->index
;
1142 for( int i
=0; i
<unique_a
; i
++ )
1144 for( int j
=unique_a
; j
<unique_total
; j
++ )
1146 int i0
= unique_verts
[i
],
1147 i1
= unique_verts
[j
];
1149 for( int k
=0; k
<mesh
->abedges
.count
; k
++ )
1151 cxr_edge
*edge
= &mesh
->edges
[k
];
1153 if( edge
->i0
== i0
|| edge
->i0
== i1
||
1154 edge
->i1
== i0
|| edge
->i1
== i1
) continue;
1156 double *a0
= verts
[i0
],
1158 *b0
= verts
[edge
->i0
],
1159 *b1
= verts
[edge
->i1
];
1161 double dist
= segment_segment_dist( a0
, a1
, b0
, b1
, ca
, cb
);
1165 free( unique_verts
);
1172 free( unique_verts
);
1177 * Creates the 'maximal' solid that originates from this faceid
1179 * Returns the number of faces used
1181 static int cxr_buildsolid(
1188 faces_tagged
[faceid
] = faceid
;
1191 solid
[solid_len
++] = faceid
;
1193 int search_start
= 0;
1198 for( int j
=search_start
; j
<solid_len
; j
++ )
1200 cxr_polygon
*poly
= &mesh
->polys
[ solid
[j
] ];
1202 for( int k
=0; k
<poly
->loop_total
; k
++ )
1204 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+k
];
1205 cxr_edge
*edge
= &mesh
->edges
[ loop
->edge_index
];
1207 if( faces_tagged
[ loop
->poly_right
] == -1 )
1209 if( !reflex_edges
[loop
->edge_index
] )
1211 /* Check for dodgy edges */
1212 cxr_polygon
*newpoly
= &mesh
->polys
[loop
->poly_right
];
1214 if( cxr_solid_overlap(mesh
,poly
,newpoly
,loop
->edge_index
))
1217 /* Looking ahead by one step gives us an early out for invalid
1218 * configurations. This might just all be handled by the new
1219 * edge overlap detector, though.
1221 for( int l
=0; l
< newpoly
->loop_total
; l
++ )
1223 cxr_loop
*lp1
= &mesh
->loops
[ newpoly
->loop_start
+l
];
1224 cxr_polygon
*future_face
= &mesh
->polys
[ lp1
->poly_right
];
1226 if( reflex_edges
[ lp1
->edge_index
]
1227 || lp1
->poly_right
== loop
->poly_right
)
1230 for( int m
=0; m
<solid_len
; m
++ )
1231 if( solid
[m
] == lp1
->poly_right
)
1234 for( int m
=0; m
<solid_len
; m
++ )
1236 cxr_polygon
*polym
= &mesh
->polys
[solid
[m
]];
1237 double pdist
= v3_dot( polym
->normal
,future_face
->normal
);
1239 if( pdist
> CXR_PLANE_SIMILARITY_MAX
)
1246 /* Check for vertices in the new polygon that exist on a current
1247 * plane. This condition is invalid */
1248 solid
[ solid_len
] = loop
->poly_right
;
1250 if( cxr_valid_solid(mesh
,solid
,solid_len
+1 ) )
1252 faces_tagged
[ loop
->poly_right
] = faceid
;
1262 search_start
= solid_len
;
1264 goto search_iterate
;
1271 int start
, count
, edge_count
;
1275 struct temp_manifold
1277 struct manifold_loop
1287 enum manifold_status
1291 k_manifold_fragmented
,
1292 k_manifold_complete
,
1298 * Create polygon from entire manifold structure.
1300 * Must be completely co-planar
1302 static void cxr_create_poly_full( cxr_mesh
*mesh
, struct temp_manifold
*src
)
1304 if( cxr_create_poly( mesh
, src
->loop_count
) )
1306 for( int l
=0; l
<src
->loop_count
; l
++ )
1307 cxr_poly_push_index( mesh
, src
->loops
[ l
].loop
.index
);
1309 cxr_poly_finish( mesh
);
1314 * Links up all edges into a potential new manifold
1316 * The return status can be:
1317 * (err): Critical programming error
1318 * none: No manifold to create
1319 * fragmented: Multiple sections exist, not just one
1320 * complete: Optimial manifold was created
1322 static void cxr_link_manifold(
1324 struct csolid
*solid
,
1326 struct temp_manifold
*manifold
1328 cxr_loop
**edge_list
= malloc( sizeof(*edge_list
) * solid
->edge_count
);
1329 int *temp_solid
= malloc( solid
->count
*sizeof(int) );
1330 int temp_solid_len
= 0;
1332 int init_reverse
= 0;
1333 int unique_edge_count
= 0;
1335 /* Try remove splitting faces first */
1337 int split_total
= 0;
1338 for( int j
=0; j
<solid
->count
; j
++ )
1340 cxr_polygon
*poly
= &mesh
->polys
[ solid_buffer
[solid
->start
+j
] ];
1341 int interior_count
= 0;
1343 for( int k
=0; k
<poly
->loop_total
; k
++ )
1345 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+k
];
1347 for( int l
=0; l
<solid
->count
; l
++ )
1348 if( loop
->poly_right
== solid_buffer
[solid
->start
+l
] )
1357 if( interior_count
< poly
->loop_total
-1 )
1363 temp_solid
[ temp_solid_len
++ ] = solid_buffer
[solid
->start
+j
];
1366 if( temp_solid_len
< 3 || (split_total
& 0x2) /* unkown reasons */ )
1371 /* Overwrite original solid */
1372 for( int j
=0; j
<temp_solid_len
; j
++ )
1373 solid_buffer
[ solid
->start
+j
] = temp_solid
[ j
];
1375 solid
->count
= temp_solid_len
;
1381 for( int j
=0; j
<solid
->count
; j
++ )
1383 cxr_polygon
*poly
= &mesh
->polys
[ solid_buffer
[solid
->start
+j
] ];
1385 /* when discarding, if a face has only one loop that points outwards,
1389 for( int k
=0; k
<poly
->loop_total
; k
++ )
1391 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+k
];
1393 for( int l
=0; l
<unique_edge_count
; l
++ )
1394 if( edge_list
[l
]->edge_index
== loop
->edge_index
)
1397 for( int l
=0; l
<solid
->count
; l
++ )
1398 if( loop
->poly_right
== solid_buffer
[solid
->start
+l
] )
1401 edge_list
[ unique_edge_count
] = loop
;
1403 if( unique_edge_count
== 0 )
1405 cxr_edge
*edgeptr
= &mesh
->edges
[ loop
->edge_index
];
1406 if( edgeptr
->i1
== loop
->index
)
1410 unique_edge_count
++;
1415 if( unique_edge_count
== 0 )
1418 manifold
->status
= k_manifold_none
;
1422 /* Link edges together to form manifold */
1423 manifold
->loops
= malloc( solid
->edge_count
*sizeof(struct manifold_loop
));
1424 manifold
->split_count
= 0;
1425 manifold
->loop_count
= 0;
1427 cxr_edge
*current
= &mesh
->edges
[ edge_list
[0]->edge_index
];
1429 int endpt
= (!init_reverse
)? current
->i0
: current
->i1
,
1431 curface
= edge_list
[0]->poly_left
;
1434 for( int j
=0; j
<unique_edge_count
; j
++ )
1436 cxr_edge
*other
= &mesh
->edges
[ edge_list
[j
]->edge_index
];
1437 if( other
== current
)
1440 if( other
->i0
== endpt
|| other
->i1
== endpt
)
1445 if( other
->i0
== endpt
) endpt
= current
->i1
;
1446 else endpt
= current
->i0
;
1448 struct manifold_loop
*ml
= &manifold
->loops
[ manifold
->loop_count
++ ];
1450 if( curface
==edge_list
[j
]->poly_left
)
1453 manifold
->split_count
++;
1458 ml
->loop
.edge_index
= edge_list
[j
]->edge_index
;
1459 ml
->loop
.poly_left
= edge_list
[j
]->poly_left
;
1460 ml
->loop
.index
= lastpt
;
1461 ml
->loop
.poly_right
= edge_list
[j
]->poly_right
;
1463 curface
= edge_list
[j
]->poly_left
;
1467 if( manifold
->loop_count
< unique_edge_count
)
1468 manifold
->status
= k_manifold_fragmented
;
1470 manifold
->status
= k_manifold_complete
;
1472 goto manifold_complete
;
1475 goto manifold_continue
;
1479 /* Incomplete links */
1480 manifold
->status
= k_manifold_err
;
1489 * Reconstruct implied internal geometry where the manifold doesn't have
1490 * enough information (vertices) to create a full result.
1492 static int cxr_build_implicit_geo( cxr_mesh
*mesh
, int new_polys
, int start
)
1494 for( int i
=0; i
<new_polys
-2; i
++ )
1496 for( int j
=i
+1; j
<new_polys
-1; j
++ )
1498 for( int k
=j
+1; k
<new_polys
; k
++ )
1500 cxr_polygon
*ptri
= &mesh
->polys
[ start
+i
],
1501 *ptrj
= &mesh
->polys
[ start
+j
],
1502 *ptrk
= &mesh
->polys
[ start
+k
];
1504 v4f planei
, planej
, planek
;
1505 normal_to_plane(ptri
->normal
,ptri
->center
,planei
);
1506 normal_to_plane(ptrj
->normal
,ptrj
->center
,planej
);
1507 normal_to_plane(ptrk
->normal
,ptrk
->center
,planek
);
1511 if( plane_intersect(planei
,planej
,planek
,intersect
) )
1513 /* Make sure the point is inside the convex region */
1515 int point_valid
= 1;
1516 for( int l
=0; l
<mesh
->abpolys
.count
; l
++ )
1518 cxr_polygon
*ptrl
= &mesh
->polys
[l
];
1521 normal_to_plane(ptrl
->normal
, ptrl
->center
, planel
);
1523 if( plane_polarity( planel
, intersect
) > 0.01 )
1526 cxr_log( "degen vert, planes %d, %d, %d [max:%d]\n",
1529 cxr_debug_poly( mesh
, ptri
, colours_random
[3] );
1530 cxr_debug_poly( mesh
, ptrj
, colours_random
[1] );
1531 cxr_debug_poly( mesh
, ptrk
, colours_random
[2] );
1538 /* Extend faces to include this vert */
1540 int nvertid
= mesh
->p_abverts
->count
;
1541 cxr_ab_push( mesh
->p_abverts
, intersect
);
1543 ptrj
->loop_start
+= 1;
1544 ptrk
->loop_start
+= 2;
1546 cxr_ab_reserve( &mesh
->abloops
, 3);
1548 int newi
= ptri
->loop_start
+ptri
->loop_total
,
1549 newj
= ptrj
->loop_start
+ptrj
->loop_total
,
1550 newk
= ptrk
->loop_start
+ptrk
->loop_total
;
1553 *lloopi
= cxr_ab_empty_at(&mesh
->abloops
, newi
),
1554 *lloopj
= cxr_ab_empty_at(&mesh
->abloops
, newj
),
1555 *lloopk
= cxr_ab_empty_at(&mesh
->abloops
, newk
);
1557 lloopi
->index
= nvertid
;
1558 lloopi
->edge_index
= 0;
1559 lloopi
->poly_left
= start
+ i
;
1560 lloopi
->poly_right
= -1;
1562 lloopj
->index
= nvertid
;
1563 lloopj
->poly_left
= start
+ j
;
1564 lloopj
->edge_index
= 0;
1565 lloopj
->poly_right
= -1;
1567 lloopk
->index
= nvertid
;
1568 lloopk
->edge_index
= 0;
1569 lloopk
->poly_left
= start
+ k
;
1570 lloopk
->poly_right
= -1;
1572 v2_zero(lloopi
->uv
);
1573 v2_zero(lloopj
->uv
);
1574 v2_zero(lloopk
->uv
);
1576 ptri
->loop_total
++;
1577 ptrj
->loop_total
++;
1578 ptrk
->loop_total
++;
1580 double qi
= 1.0/(double)ptri
->loop_total
,
1581 qj
= 1.0/(double)ptrj
->loop_total
,
1582 qk
= 1.0/(double)ptrk
->loop_total
;
1584 /* Adjust centers of faces */
1585 v3_lerp( ptri
->center
, intersect
, qi
, ptri
->center
);
1586 v3_lerp( ptrj
->center
, intersect
, qj
, ptrj
->center
);
1587 v3_lerp( ptrk
->center
, intersect
, qk
, ptrk
->center
);
1596 static int cxr_reflex_err( cxr_mesh
*mesh
)
1599 int *reflex_check
= cxr_mesh_reflex_edges( mesh
);
1601 v3f
*temp
= cxr_ab_ptr(mesh
->p_abverts
, 0);
1603 for( int i
=0; i
<mesh
->abedges
.count
; i
++ )
1605 if( reflex_check
[i
] )
1607 cxr_debug_line( temp
[mesh
->edges
[i
].i0
],
1608 temp
[mesh
->edges
[i
].i1
],
1614 free( reflex_check
);
1618 static int cxr_non_manifold_err( cxr_mesh
*mesh
)
1620 if( !cxr_mesh_link_loops(mesh
) )
1623 cxr_log( "non-manifold edges are in the mesh: "
1624 "implicit internal geometry does not have full support\n" );
1626 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1628 for( int i
=0; i
<mesh
->abloops
.count
; i
++ )
1630 cxr_loop
*lp
= &mesh
->loops
[i
];
1631 cxr_edge
*edge
= &mesh
->edges
[lp
->edge_index
];
1632 cxr_debug_line( verts
[edge
->i0
], verts
[edge
->i1
], colours_random
[1] );
1634 if( lp
->poly_left
== -1 || lp
->poly_right
== -1 )
1636 cxr_debug_line( verts
[edge
->i0
], verts
[edge
->i1
], colour_error
);
1647 * Convexer's main algorithm
1649 * Return the best availible convex solid from mesh, and patch the existing mesh
1650 * to fill the gap where the new mesh left it.
1652 * Returns NULL if shape is already convex or empty.
1653 * This function will not preserve edge data such as freestyle, sharp etc.
1655 static cxr_mesh
*cxr_pull_best_solid(
1657 int preserve_more_edges
,
1658 enum cxr_soliderr
*err
)
1660 *err
= k_soliderr_none
;
1662 if( cxr_non_manifold_err( mesh
) )
1664 *err
= k_soliderr_non_manifold
;
1668 int *edge_tagged
= cxr_mesh_reflex_edges( mesh
);
1669 int *vertex_tagged
= cxr_mesh_reflex_vertices( mesh
);
1672 * Connect all marked vertices that share an edge
1675 int *edge_important
= malloc(mesh
->abedges
.count
*sizeof(int));
1676 for( int i
=0; i
< mesh
->abedges
.count
; i
++ )
1677 edge_important
[i
] = 0;
1679 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
1681 cxr_polygon
*poly
= &mesh
->polys
[i
];
1682 int not_tagged
= -1,
1685 for( int j
=0; j
<poly
->loop_total
; j
++ )
1687 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
1689 if( !edge_tagged
[ loop
->edge_index
] )
1691 if( not_tagged
== -1 )
1692 not_tagged
= loop
->edge_index
;
1694 goto edge_unimportant
;
1698 if( not_tagged
!= -1 )
1699 edge_important
[not_tagged
]=1;
1705 * Connect edges where both vertices are reflex, only if we are not
1708 for( int i
=0; i
<mesh
->abedges
.count
; i
++ )
1710 if( edge_important
[i
] && preserve_more_edges
) continue;
1712 cxr_edge
*edge
= &mesh
->edges
[i
];
1713 if( vertex_tagged
[edge
->i0
] && vertex_tagged
[edge
->i1
] )
1717 free( edge_important
);
1719 int *faces_tagged
= malloc(mesh
->abpolys
.count
*sizeof(int));
1720 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
1721 faces_tagged
[i
] = -1;
1723 struct csolid
*candidates
;
1724 int *solid_buffer
= malloc( mesh
->abpolys
.count
*sizeof(int) ),
1725 solid_buffer_len
= 0,
1726 candidate_count
= 0;
1728 candidates
= malloc( mesh
->abpolys
.count
*sizeof(struct csolid
) );
1731 * Create a valid, non-overlapping solid for every face present in the mesh
1733 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
1735 if( faces_tagged
[i
] != -1 ) continue;
1736 faces_tagged
[i
] = i
;
1738 int *solid
= &solid_buffer
[ solid_buffer_len
];
1739 int len
= cxr_buildsolid( mesh
, i
, solid
, edge_tagged
, faces_tagged
);
1742 struct csolid
*csolid
= &candidates
[candidate_count
++];
1743 csolid
->start
= solid_buffer_len
;
1744 csolid
->count
= len
;
1745 csolid
->edge_count
= 0;
1747 v3_zero( csolid
->center
);
1748 for( int j
=0; j
<len
; j
++ )
1750 cxr_polygon
*polyj
= &mesh
->polys
[ solid
[j
] ];
1751 v3_add( polyj
->center
, csolid
->center
, csolid
->center
);
1752 csolid
->edge_count
+= polyj
->loop_total
;
1754 v3_divs( csolid
->center
, len
, csolid
->center
);
1755 solid_buffer_len
+= len
;
1758 free( edge_tagged
);
1759 free( vertex_tagged
);
1760 free( faces_tagged
);
1763 * Choosing the best solid: most defined manifold
1765 struct csolid
*best_solid
= NULL
;
1766 int fewest_manifold_splits
= INT32_MAX
;
1768 struct temp_manifold best_manifold
= { .loops
= NULL
, .loop_count
= 0 };
1769 int max_solid_faces
= 0;
1771 for( int i
=0; i
<candidate_count
; i
++ )
1773 struct csolid
*solid
= &candidates
[i
];
1774 max_solid_faces
= cxr_max(max_solid_faces
,solid
->count
);
1776 if( solid
->count
<= 2 )
1779 struct temp_manifold manifold
;
1780 cxr_link_manifold( mesh
, solid
, solid_buffer
, &manifold
);
1782 if( manifold
.status
== k_manifold_err
)
1784 *err
= k_soliderr_bad_manifold
;
1788 free(manifold
.loops
);
1789 free(best_manifold
.loops
);
1793 if( manifold
.status
== k_manifold_complete
)
1795 if( manifold
.split_count
< fewest_manifold_splits
)
1797 fewest_manifold_splits
= manifold
.split_count
;
1800 free( best_manifold
.loops
);
1801 best_manifold
= manifold
;
1806 if( manifold
.status
!= k_manifold_none
)
1807 free( manifold
.loops
);
1810 if( max_solid_faces
< 2 )
1812 *err
= k_soliderr_no_solids
;
1815 free(best_manifold
.loops
);
1819 if( best_solid
!= NULL
)
1821 cxr_mesh
*pullmesh
= cxr_alloc_mesh( best_solid
->edge_count
,
1822 best_solid
->edge_count
,
1826 for( int i
=0; i
<best_solid
->count
; i
++ )
1828 int nface_id
= pullmesh
->abpolys
.count
;
1829 int exist_plane_id
= solid_buffer
[best_solid
->start
+i
];
1831 cxr_polygon
*exist_face
= &mesh
->polys
[ exist_plane_id
],
1832 *new_face
= cxr_ab_empty( &pullmesh
->abpolys
);
1834 *new_face
= *exist_face
;
1835 new_face
->loop_start
= pullmesh
->abloops
.count
;
1837 for( int j
=0; j
<exist_face
->loop_total
; j
++ )
1839 cxr_loop
*exist_loop
= &mesh
->loops
[ exist_face
->loop_start
+j
],
1840 *new_loop
= cxr_ab_empty(&pullmesh
->abloops
);
1842 new_loop
->index
= exist_loop
->index
;
1843 new_loop
->poly_left
= nface_id
;
1844 new_loop
->poly_right
= -1;
1845 new_loop
->edge_index
= 0;
1846 v2_copy( exist_loop
->uv
, new_loop
->uv
);
1849 exist_face
->loop_total
= -1;
1853 int pullmesh_new_start
= pullmesh
->abpolys
.count
;
1855 if( fewest_manifold_splits
!= 0 )
1857 /* Unusual observation:
1858 * If the split count is odd, the manifold can be created easily
1860 * If it is even, implicit internal geometry is needed to be
1861 * constructed. So the manifold gets folded as we create it segment
1864 * I'm not sure if this is a well defined rule of geometry, but seems
1865 * to apply to the data we care about.
1867 int collapse_used_segments
= (u32
)fewest_manifold_splits
& 0x1? 0: 1;
1871 for( int j
=0; j
< best_manifold
.loop_count
; j
++ )
1873 if( !best_manifold
.loops
[j
].split
) continue;
1875 cxr_loop
*loop
= &best_manifold
.loops
[j
].loop
;
1877 for( int k
=1; k
< best_manifold
.loop_count
; k
++ )
1879 int index1
= cxr_range(j
+k
, best_manifold
.loop_count
);
1880 cxr_loop
*loop1
= &best_manifold
.loops
[index1
].loop
;
1882 if( best_manifold
.loops
[index1
].split
)
1889 if( new_polys
> best_manifold
.loop_count
)
1892 cxr_log( "Programming error: Too many new polys!\n" );
1897 if( cxr_create_poly( pullmesh
, k
+1 ) )
1899 for( int l
=0; l
<k
+1; l
++ )
1901 int i0
= cxr_range(j
+l
, best_manifold
.loop_count
),
1902 index
= best_manifold
.loops
[ i0
].loop
.index
;
1904 cxr_poly_push_index( pullmesh
, index
);
1906 cxr_poly_finish( pullmesh
);
1909 /* Collapse down manifold */
1910 if( collapse_used_segments
)
1912 best_manifold
.loops
[j
].split
= 0;
1913 best_manifold
.loops
[index1
].split
= 0;
1915 int new_length
= (best_manifold
.loop_count
-(k
-1));
1917 struct temp_manifold new_manifold
= {
1918 .loop_count
= new_length
1920 new_manifold
.loops
=
1921 malloc( new_length
*sizeof(*new_manifold
.loops
) );
1923 for( int l
=0; l
<new_length
; l
++ )
1925 int i_src
= cxr_range( j
+k
+l
, best_manifold
.loop_count
);
1926 new_manifold
.loops
[l
] = best_manifold
.loops
[i_src
];
1929 free( best_manifold
.loops
);
1930 best_manifold
= new_manifold
;
1932 goto manifold_repeat
;
1941 if( best_manifold
.loop_count
&& collapse_used_segments
)
1943 cxr_create_poly_full( pullmesh
, &best_manifold
);
1949 cxr_create_poly_full( pullmesh
, &best_manifold
);
1953 if( new_polys
>= 3 )
1955 if( !cxr_build_implicit_geo( pullmesh
, new_polys
, pullmesh_new_start
))
1959 free(best_manifold
.loops
);
1961 cxr_free_mesh( pullmesh
);
1962 *err
= k_soliderr_degenerate_implicit
;
1968 * Copy faces from the pullmesh into original, to patch up where there
1969 * would be gaps created
1971 for( int i
=0; i
<new_polys
; i
++ )
1973 int rface_id
= mesh
->abpolys
.count
;
1974 cxr_polygon
*pface
= &pullmesh
->polys
[pullmesh_new_start
+i
],
1975 *rip_face
= cxr_ab_empty(&mesh
->abpolys
);
1977 rip_face
->loop_start
= mesh
->abloops
.count
;
1978 rip_face
->loop_total
= pface
->loop_total
;
1979 rip_face
->material_id
= -1;
1981 for( int j
=0; j
<rip_face
->loop_total
; j
++ )
1984 &pullmesh
->loops
[ pface
->loop_start
+pface
->loop_total
-j
-1 ],
1985 *rloop
= cxr_ab_empty(&mesh
->abloops
);
1987 rloop
->index
= ploop
->index
;
1988 rloop
->poly_left
= rface_id
;
1989 rloop
->poly_right
= -1;
1990 rloop
->edge_index
= 0;
1991 v2_copy( ploop
->uv
, rloop
->uv
);
1994 v3_copy( pface
->center
, rip_face
->center
);
1995 v3_negate( pface
->normal
, rip_face
->normal
);
1998 cxr_mesh_update( mesh
);
1999 cxr_mesh_update( pullmesh
);
2001 cxr_mesh_clean_faces( mesh
);
2002 cxr_mesh_clean_edges( mesh
);
2003 cxr_mesh_clean_faces( pullmesh
);
2004 cxr_mesh_clean_edges( pullmesh
);
2008 free(best_manifold
.loops
);
2011 * Do final checks on the mesh to make sure we diddn't introduce any
2014 if( cxr_non_manifold_err( pullmesh
) || cxr_reflex_err( pullmesh
) )
2016 *err
= k_soliderr_bad_result
;
2025 free(best_manifold
.loops
);
2027 if( cxr_non_manifold_err( mesh
) || cxr_reflex_err( mesh
) )
2028 *err
= k_soliderr_bad_result
;
2034 * Convert from the format we recieve from blender into our internal format
2035 * with auto buffers.
2037 static cxr_mesh
*cxr_to_internal_format(
2038 cxr_static_mesh
*src
,
2039 cxr_abuffer
*abverts
2041 cxr_mesh
*mesh
= cxr_alloc_mesh( src
->edge_count
, src
->loop_count
,
2042 src
->poly_count
, abverts
);
2044 cxr_ab_init( abverts
, sizeof(v3f
), src
->vertex_count
);
2046 memcpy( mesh
->abedges
.arr
, src
->edges
, src
->edge_count
*sizeof(cxr_edge
));
2047 memcpy( mesh
->abpolys
.arr
, src
->polys
, src
->poly_count
*sizeof(cxr_polygon
));
2048 memcpy( abverts
->arr
, src
->vertices
, src
->vertex_count
*sizeof(v3f
));
2049 mesh
->abedges
.count
= src
->edge_count
;
2050 mesh
->abloops
.count
= src
->loop_count
;
2051 mesh
->abpolys
.count
= src
->poly_count
;
2053 cxr_mesh_update( mesh
);
2055 for( int i
=0; i
<src
->loop_count
; i
++ )
2057 cxr_loop
*lp
= &mesh
->loops
[i
];
2059 lp
->index
= src
->loops
[i
].index
;
2060 lp
->edge_index
= src
->loops
[i
].edge_index
;
2061 v2_copy( src
->loops
[i
].uv
, lp
->uv
);
2062 lp
->alpha
= src
->loops
[i
].alpha
;
2065 abverts
->count
= src
->vertex_count
;
2069 static int cxr_poly_convex( cxr_mesh
*mesh
, cxr_polygon
*poly
)
2071 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2073 for( int i
=0; i
<poly
->loop_total
; i
++ )
2075 int li0
= poly
->loop_start
+ i
,
2076 li1
= poly
->loop_start
+ cxr_range( i
+1, poly
->loop_total
),
2077 li2
= poly
->loop_start
+ cxr_range( i
+2, poly
->loop_total
);
2078 int i0
= mesh
->loops
[li0
].index
,
2079 i1
= mesh
->loops
[li1
].index
,
2080 i2
= mesh
->loops
[li2
].index
;
2084 v3_sub( verts
[i1
], verts
[i0
], v0
);
2085 v3_sub( verts
[i2
], verts
[i1
], v1
);
2087 v3_cross( v0
, v1
, c
);
2088 if( v3_dot( c
, poly
->normal
) <= 0.0 )
2091 cxr_debug_line( verts
[i0
], verts
[i1
], colour_error
);
2092 cxr_debug_box( verts
[i1
], 0.1, colour_error
);
2093 cxr_debug_line( verts
[i1
], verts
[i2
], colour_error
);
2094 cxr_debug_line( verts
[i1
], poly
->center
, colour_error
);
2103 static int cxr_solid_checkerr( cxr_mesh
*mesh
)
2105 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2108 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
2112 cxr_polygon
*poly
= &mesh
->polys
[i
];
2115 normal_to_plane( poly
->normal
, poly
->center
, plane
);
2117 for( int j
=0; j
<poly
->loop_total
; j
++ )
2119 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
2120 double *vert
= verts
[ loop
->index
];
2122 if( fabs(plane_polarity(plane
,vert
)) > 0.0025 )
2128 plane_project_point( plane
, vert
, ref
);
2131 cxr_debug_line( ref
, vert
, colour_error
);
2132 cxr_debug_box( vert
, 0.1, colour_error
);
2139 cxr_debug_poly( mesh
, poly
, colour_error
);
2146 CXR_API
void cxr_free_world( cxr_world
*world
)
2148 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2150 cxr_solid
*solid
= cxr_ab_ptr( &world
->absolids
, i
);
2151 cxr_free_mesh( solid
->pmesh
);
2154 cxr_ab_free( &world
->abverts
);
2155 cxr_ab_free( &world
->absolids
);
2157 if( world
->materials
)
2159 for( int i
=0; i
<world
->material_count
; i
++ )
2160 free( world
->materials
[i
].name
);
2162 free( world
->materials
);
2167 CXR_API cxr_tri_mesh
*cxr_world_preview( cxr_world
*world
)
2169 cxr_tri_mesh
*out
= malloc( sizeof(cxr_tri_mesh
) );
2170 out
->vertex_count
= 0;
2171 out
->indices_count
= 0;
2173 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2175 cxr_solid
*solid
= cxr_ab_ptr( &world
->absolids
, i
);
2176 cxr_mesh
*mesh
= solid
->pmesh
;
2178 for( int j
=0; j
<mesh
->abpolys
.count
; j
++ )
2180 cxr_polygon
*poly
= &mesh
->polys
[j
];
2182 out
->vertex_count
+= poly
->loop_total
* 3; /* Polygon, edge strip */
2183 out
->indices_count
+= (poly
->loop_total
-2) * 3; /* Polygon */
2184 out
->indices_count
+= poly
->loop_total
* 2 * 3; /* Edge strip */
2188 out
->colours
= malloc( sizeof(v4f
)*out
->vertex_count
);
2189 out
->vertices
= malloc( sizeof(v3f
)*out
->vertex_count
);
2190 out
->indices
= malloc( sizeof(i32
)*out
->indices_count
);
2192 out
->normals
= NULL
;
2194 v3f
*overts
= out
->vertices
;
2195 v4f
*colours
= out
->colours
;
2196 i32
*indices
= out
->indices
;
2201 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2203 cxr_solid
*solid
= cxr_ab_ptr( &world
->absolids
, i
);
2204 cxr_mesh
*mesh
= solid
->pmesh
;
2206 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2209 colour_random_brush( i
, colour
);
2211 for( int j
=0; j
<mesh
->abpolys
.count
; j
++ )
2213 cxr_polygon
*poly
= &mesh
->polys
[j
];
2217 for( int k
=0; k
<poly
->loop_total
-2; k
++ )
2223 indices
[ ii
++ ] = istart
+i0
;
2224 indices
[ ii
++ ] = istart
+i1
;
2225 indices
[ ii
++ ] = istart
+i2
;
2228 for( int k
=0; k
<poly
->loop_total
; k
++ )
2230 cxr_loop
*lp
= &mesh
->loops
[poly
->loop_start
+k
];
2233 i1r
= cxr_range(k
+1,poly
->loop_total
)*3+1,
2235 i1i
= cxr_range(k
+1,poly
->loop_total
)*3+2;
2237 indices
[ ii
++ ] = istart
+i0i
;
2238 indices
[ ii
++ ] = istart
+i1i
;
2239 indices
[ ii
++ ] = istart
+i1r
;
2241 indices
[ ii
++ ] = istart
+i0i
;
2242 indices
[ ii
++ ] = istart
+i1r
;
2243 indices
[ ii
++ ] = istart
+i0r
;
2246 v3_muladds( verts
[lp
->index
], poly
->normal
, 0.02, overts
[vi
] );
2247 v4_copy( colour
, colours
[ vi
] );
2252 v3_lerp( verts
[lp
->index
], poly
->center
, 0.2, inner
);
2253 v3_muladds( inner
, poly
->normal
, 0.015, overts
[ vi
] );
2254 v4_copy( colour
, colours
[ vi
] );
2255 v4_copy( (v4f
){ 0.0, 0.0, 0.0, 0.0 }, colours
[vi
] );
2258 v3_muladds(verts
[lp
->index
], poly
->normal
, 0.0, overts
[ vi
] );
2259 v4_copy( colour
, colours
[ vi
] );
2260 v4_copy( (v4f
){ 1.0, 1.0, 1.0, 0.125 }, colours
[vi
] );
2269 CXR_API
void cxr_free_tri_mesh( cxr_tri_mesh
*mesh
)
2271 free( mesh
->colours
);
2272 free( mesh
->indices
);
2273 free( mesh
->vertices
);
2274 free( mesh
->normals
);
2279 CXR_API cxr_world
*cxr_decompose( cxr_static_mesh
*src
, i32
*perrcode
)
2281 /* Make sure data is in the mesh and isn't empty */
2282 if( !src
->edge_count
|| !src
->loop_count
|| !src
->poly_count
)
2284 cxr_log( "Error %d\n", k_soliderr_invalid_input
);
2286 *perrcode
= k_soliderr_invalid_input
;
2292 cxr_world
*world
= malloc( sizeof(*world
) );
2294 /* Copy data to internal formats */
2295 cxr_mesh
*main_mesh
= cxr_to_internal_format( src
, &world
->abverts
);
2296 cxr_ab_init( &world
->absolids
, sizeof(cxr_solid
), 2 );
2298 if( src
->material_count
)
2300 size_t dsize
= sizeof(cxr_material
) * src
->material_count
;
2301 world
->materials
= malloc( dsize
);
2302 memcpy( world
->materials
, src
->materials
, dsize
);
2304 for( int i
=0; i
<src
->material_count
; i
++ )
2306 world
->materials
[i
].name
= malloc(strlen(src
->materials
[i
].name
) +1);
2307 strcpy( world
->materials
[i
].name
, src
->materials
[i
].name
);
2309 world
->material_count
= src
->material_count
;
2311 else world
->materials
= NULL
;
2313 int invalid_count
= 0;
2316 * Preprocessor 1: Island seperation
2320 cxr_mesh
*res
= cxr_pull_island( main_mesh
);
2323 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ res
, 0, 0 });
2327 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ main_mesh
, 0, 0 } );
2330 * Preprocessor 2: Displacement processing & error checks
2332 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2334 cxr_solid
*pinf
= cxr_ab_ptr(&world
->absolids
,i
);
2336 for( int j
=0; j
<pinf
->pmesh
->abpolys
.count
; j
++ )
2338 cxr_polygon
*poly
= &pinf
->pmesh
->polys
[ j
];
2340 for( int k
=0; k
<poly
->loop_total
; k
++ )
2342 cxr_loop
*lp
= &pinf
->pmesh
->loops
[ poly
->loop_start
+k
];
2343 cxr_edge
*edge
= &pinf
->pmesh
->edges
[ lp
->edge_index
];
2345 if( edge
->freestyle
)
2349 if( !cxr_poly_convex( pinf
->pmesh
, poly
) )
2353 error
= k_soliderr_non_convex_poly
;
2357 if( cxr_solid_checkerr( pinf
->pmesh
) )
2361 error
= k_soliderr_non_coplanar_vertices
;
2367 pinf
->displacement
= 1;
2371 * Main convex decomp algorithm
2373 int sources_count
= world
->absolids
.count
;
2378 for( int i
=0; i
<sources_count
; i
++ )
2380 cxr_solid pinf
= *(cxr_solid
*)cxr_ab_ptr(&world
->absolids
, i
);
2382 if( pinf
.displacement
|| pinf
.invalid
)
2387 cxr_mesh
*res
= cxr_pull_best_solid( pinf
.pmesh
, 0, &error
);
2391 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ res
, 0, 0 } );
2395 if( error
== k_soliderr_no_solids
)
2397 /* Retry if non-critical error, with extra edges */
2398 res
= cxr_pull_best_solid(pinf
.pmesh
, 1, &error
);
2401 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ res
, 0, 0 } );
2417 cxr_log( "Error %d\n", error
);
2418 cxr_free_world( world
);
2427 * format specific functions: vdf, vmf, (v)bsp
2428 * ----------------------------------------------------------------------------
2430 #ifdef CXR_VALVE_MAP_FILE
2432 CXR_API cxr_vdf
*cxr_vdf_open(const char *path
)
2434 cxr_vdf
*vdf
= malloc(sizeof(cxr_vdf
));
2437 vdf
->fp
= fopen( path
, "w" );
2448 CXR_API
void cxr_vdf_close(cxr_vdf
*vdf
)
2454 CXR_API
void cxr_vdf_put(cxr_vdf
*vdf
, const char *str
)
2456 for( int i
=0; i
<vdf
->level
; i
++ )
2457 fputs( " ", vdf
->fp
);
2459 fputs( str
, vdf
->fp
);
2462 static void cxr_vdf_printf( cxr_vdf
*vdf
, const char *fmt
, ... )
2464 cxr_vdf_put(vdf
,"");
2467 va_start( args
, fmt
);
2468 vfprintf( vdf
->fp
, fmt
, args
);
2472 CXR_API
void cxr_vdf_node(cxr_vdf
*vdf
, const char *str
)
2474 cxr_vdf_put( vdf
, str
);
2475 putc( (u8
)'\n', vdf
->fp
);
2476 cxr_vdf_put( vdf
, "{\n" );
2481 CXR_API
void cxr_vdf_edon( cxr_vdf
*vdf
)
2484 cxr_vdf_put( vdf
, "}\n" );
2487 CXR_API
void cxr_vdf_kv( cxr_vdf
*vdf
, const char *strk
, const char *strv
)
2489 cxr_vdf_printf( vdf
, "\"%s\" \"%s\"\n", strk
, strv
);
2493 * Data-type specific Keyvalues
2495 static void cxr_vdf_ki32( cxr_vdf
*vdf
, const char *strk
, i32 val
)
2497 cxr_vdf_printf( vdf
, "\"%s\" \"%d\"\n", strk
, val
);
2500 static void cxr_vdf_kdouble( cxr_vdf
*vdf
, const char *strk
, double val
)
2502 cxr_vdf_printf( vdf
, "\"%s\" \"%f\"\n", strk
, val
);
2505 static void cxr_vdf_kaxis( cxr_vdf
*vdf
, const char *strk
,
2506 v3f normal
, double offset
, double scale
2508 cxr_vdf_printf( vdf
, "\"%s\" \"[%f %f %f %f] %f\"\n",
2509 strk
, normal
[0], normal
[1],normal
[2], offset
, scale
);
2512 static void cxr_vdf_kv3f( cxr_vdf
*vdf
, const char *strk
, v3f v
)
2514 cxr_vdf_printf( vdf
, "\"%s\" \"[%f %f %f]\"\n", strk
, v
[0], v
[1], v
[2] );
2517 static void cxr_vdf_karrdouble( cxr_vdf
*vdf
, const char *strk
,
2518 int id
, double *doubles
, int count
2520 cxr_vdf_put(vdf
,"");
2521 fprintf( vdf
->fp
, "\"%s%d\" \"", strk
, id
);
2522 for( int i
=0; i
<count
; i
++ )
2524 if( i
== count
-1 ) fprintf( vdf
->fp
, "%f", doubles
[i
] );
2525 else fprintf( vdf
->fp
, "%f ", doubles
[i
] );
2527 fprintf( vdf
->fp
, "\"\n" );
2530 static void cxr_vdf_karrv3f( cxr_vdf
*vdf
, const char *strk
,
2531 int id
, v3f
*vecs
, int count
2533 cxr_vdf_put(vdf
,"");
2534 fprintf( vdf
->fp
, "\"%s%d\" \"", strk
, id
);
2535 for( int i
=0; i
<count
; i
++ )
2537 const char *format
= i
== count
-1? "%f %f %f": "%f %f %f ";
2538 fprintf( vdf
->fp
, format
, vecs
[i
][0], vecs
[i
][1], vecs
[i
][2] );
2540 fprintf( vdf
->fp
, "\"\n" );
2543 static void cxr_vdf_plane( cxr_vdf
*vdf
, const char *strk
, v3f a
, v3f b
, v3f c
)
2545 cxr_vdf_printf( vdf
, "\"%s\" \"(%f %f %f) (%f %f %f) (%f %f %f)\"\n",
2546 strk
, a
[0], a
[1], a
[2], b
[0], b
[1], b
[2], c
[0], c
[1], c
[2] );
2549 static void cxr_vdf_colour255(cxr_vdf
*vdf
, const char *strk
, v4f colour
)
2552 v4_muls( colour
, 255.0, scale
);
2553 cxr_vdf_printf( vdf
, "\"%s\" \"%d %d %d %d\"\n",
2554 strk
,(int)scale
[0], (int)scale
[1], (int)scale
[2], (int)scale
[3]);
2557 static struct cxr_material cxr_nodraw
=
2559 .res
= { 512, 512 },
2560 .name
= "tools/toolsnodraw"
2564 * Find most extreme point along a given direction
2566 static double support_distance( v3f verts
[3], v3f dir
, double coef
)
2570 coef
* v3_dot( verts
[0], dir
),
2573 coef
* v3_dot( verts
[1], dir
),
2574 coef
* v3_dot( verts
[2], dir
)
2580 * Convert regular UV'd triangle int Source's u/vaxis vectors
2582 * This supports affine move, scale, rotation, parallel skewing
2584 static void cxr_calculate_axis( cxr_texinfo
*transform
, v3f verts
[3],
2585 v2f uvs
[3], v2f texture_res
2587 v2f tT
, bT
; /* Tangent/bitangent pairs for UV space and world */
2590 v2_sub( uvs
[0], uvs
[1], tT
);
2591 v2_sub( uvs
[2], uvs
[1], bT
);
2592 v3_sub( verts
[0], verts
[1], tW
);
2593 v3_sub( verts
[2], verts
[1], bW
);
2595 /* Use arbitrary projection if there is no UV */
2596 if( v2_length( tT
) < 0.0001 || v2_length( bT
) < 0.0001 )
2598 v3f uaxis
, normal
, vaxis
;
2600 v3_copy( tW
, uaxis
);
2601 v3_normalize( uaxis
);
2603 v3_cross( tW
, bW
, normal
);
2604 v3_cross( normal
, uaxis
, vaxis
);
2605 v3_normalize( vaxis
);
2607 v3_copy( uaxis
, transform
->uaxis
);
2608 v3_copy( vaxis
, transform
->vaxis
);
2609 v2_zero( transform
->offset
);
2611 v2_div( (v2f
){128.0, 128.0}, texture_res
, transform
->scale
);
2612 transform
->winding
= 1.0;
2616 /* Detect if UV is reversed */
2617 double winding
= v2_cross( tT
, bT
) >= 0.0f
? 1.0f
: -1.0f
;
2619 /* UV projection reference */
2621 v2_muls((v2f
){1,0}, winding
, vX
);
2622 v2_muls((v2f
){0,1}, winding
, vY
);
2624 /* Reproject reference into world space, including skew */
2627 v3_muls( tW
, v2_cross(vX
,bT
) / v2_cross(bT
,tT
), uaxis1
);
2628 v3_muladds( uaxis1
, bW
, v2_cross(vX
, tT
) / v2_cross(tT
,bT
), uaxis1
);
2630 v3_muls( tW
, v2_cross(vY
,bT
) / v2_cross(bT
,tT
), vaxis1
);
2631 v3_muladds( vaxis1
, bW
, v2_cross(vY
,tT
) / v2_cross(tT
,bT
), vaxis1
);
2633 v3_normalize( uaxis1
);
2634 v3_normalize( vaxis1
);
2636 /* Apply source transform to axis (yes, they also need to be swapped) */
2637 v3f norm
, uaxis
, vaxis
;
2639 v3_cross( bW
, tW
, norm
);
2641 v3_cross( vaxis1
, norm
, uaxis
);
2642 v3_cross( uaxis1
, norm
, vaxis
);
2645 v2f uvmin
, uvmax
, uvdelta
;
2646 v2_minv( uvs
[0], uvs
[1], uvmin
);
2647 v2_minv( uvmin
, uvs
[2], uvmin
);
2648 v2_maxv( uvs
[0], uvs
[1], uvmax
);
2649 v2_maxv( uvmax
, uvs
[2], uvmax
);
2651 v2_sub( uvmax
, uvmin
, uvdelta
);
2653 /* world-uv scale */
2654 v2f uvminw
, uvmaxw
, uvdeltaw
;
2655 uvminw
[0] = -support_distance( verts
, uaxis
, -1.0f
);
2656 uvmaxw
[0] = support_distance( verts
, uaxis
, 1.0f
);
2657 uvminw
[1] = -support_distance( verts
, vaxis
, -1.0f
);
2658 uvmaxw
[1] = support_distance( verts
, vaxis
, 1.0f
);
2660 v2_sub( uvmaxw
, uvminw
, uvdeltaw
);
2664 v2_div( uvdeltaw
, uvdelta
, uv_scale
);
2665 v2_div( uv_scale
, texture_res
, uv_scale
);
2667 /* Find offset via 'natural' point */
2668 v2f target_uv
, natural_uv
, tex_offset
;
2669 v2_mul( uvs
[0], texture_res
, target_uv
);
2671 natural_uv
[0] = v3_dot( uaxis
, verts
[0] );
2672 natural_uv
[1] = -v3_dot( vaxis
, verts
[0] );
2673 v2_div( natural_uv
, uv_scale
, natural_uv
);
2675 tex_offset
[0] = target_uv
[0]-natural_uv
[0];
2676 tex_offset
[1] = -(target_uv
[1]-natural_uv
[1]);
2678 /* Copy everything into output */
2679 v3_copy( uaxis
, transform
->uaxis
);
2680 v3_copy( vaxis
, transform
->vaxis
);
2681 v2_copy( tex_offset
, transform
->offset
);
2682 v2_copy( uv_scale
, transform
->scale
);
2683 transform
->winding
= winding
;
2687 * Get the maximal direction of a vector, while also ignoring an axis
2690 static int cxr_cardinal( v3f a
, int ignore
)
2693 double component_max
= -CXR_BIG_NUMBER
;
2695 for( int i
=0; i
<3; i
++ )
2697 if( i
== ignore
) continue;
2699 if( fabs(a
[i
]) > component_max
)
2701 component_max
= fabs(a
[i
]);
2705 double d
= a
[component
] >= 0.0? 1.0: -1.0;
2713 * Convert contiguous mesh to displacement patch
2715 static int cxr_write_disp( cxr_mesh
*mesh
, cxr_world
*world
,
2716 cxr_vmf_context
*ctx
, cxr_vdf
*output
2718 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2722 int con_start
, con_count
;
2730 *vertinfo
= malloc( sizeof(struct vertinfo
)*mesh
->p_abverts
->count
);
2731 int *graph
= malloc( sizeof(int) * mesh
->abedges
.count
*2 );
2734 for( int i
=0; i
<mesh
->p_abverts
->count
; i
++ )
2736 struct vertinfo
*info
= &vertinfo
[i
];
2737 info
->con_start
= con_pos
;
2738 info
->con_count
= 0;
2745 for( int j
=0; j
<mesh
->abedges
.count
; j
++ )
2747 cxr_edge
*edge
= &mesh
->edges
[j
];
2749 if( edge
->i0
== i
|| edge
->i1
== i
)
2751 graph
[ con_pos
++ ] = edge
->i0
== i
? edge
->i1
: edge
->i0
;
2754 if( edge
->freestyle
)
2760 /* Collect alphas from loops. This discards hard blend information */
2761 for( int i
=0; i
<mesh
->abloops
.count
; i
++ )
2763 cxr_loop
*loop
= &mesh
->loops
[i
];
2764 vertinfo
[loop
->index
].alpha
= loop
->alpha
* 255.0;
2767 v3f refv
, refu
, refn
;
2768 v3_zero(refv
); v3_zero(refu
); v3_zero(refn
);
2771 * Approximately match the area of the result brush faces to the actual
2774 * Necessary for accuracy and even lightmap texel allocation
2777 double uv_area
= 0.0, face_area
= 0.0, sf
;
2778 v2f uvboundmin
, uvboundmax
;
2779 v3f faceboundmin
, faceboundmax
;
2783 v2_fill( uvboundmin
, CXR_BIG_NUMBER
);
2784 v2_fill( uvboundmax
, -CXR_BIG_NUMBER
);
2785 v3_fill( faceboundmin
, CXR_BIG_NUMBER
);
2786 v3_fill( faceboundmax
, -CXR_BIG_NUMBER
);
2788 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
2790 cxr_polygon
*poly
= &mesh
->polys
[i
];
2792 for( int j
=0; j
<poly
->loop_total
; j
++ )
2794 cxr_loop
*lp0
= &mesh
->loops
[ poly
->loop_start
+j
];
2795 v2_minv( lp0
->uv
, uvboundmin
, uvboundmin
);
2796 v2_maxv( lp0
->uv
, uvboundmax
, uvboundmax
);
2797 v3_minv( verts
[lp0
->index
], faceboundmin
, faceboundmin
);
2798 v3_maxv( verts
[lp0
->index
], faceboundmax
, faceboundmax
);
2801 for( int j
=0; j
<poly
->loop_total
-2; j
++ )
2803 cxr_loop
*lp0
= &mesh
->loops
[poly
->loop_start
],
2804 *lp1
= &mesh
->loops
[poly
->loop_start
+j
+1],
2805 *lp2
= &mesh
->loops
[poly
->loop_start
+j
+2];
2808 v3_sub( verts
[lp1
->index
], verts
[lp0
->index
], va
);
2809 v3_sub( verts
[lp2
->index
], verts
[lp0
->index
], vb
);
2810 v3_cross( va
, vb
, orth
);
2812 face_area
+= v3_length( orth
) / 2.0;
2815 v2_sub( lp1
->uv
, lp0
->uv
, uva
);
2816 v2_sub( lp2
->uv
, lp0
->uv
, uvb
);
2818 uv_area
+= fabs(v2_cross( uva
, uvb
)) / 2.0;
2822 v3_add( faceboundmax
, faceboundmin
, face_center
);
2823 v3_muls( face_center
, 0.5, face_center
);
2824 v2_add( uvboundmin
, uvboundmax
, uv_center
);
2825 v2_muls( uv_center
, 0.5, uv_center
);
2827 sf
= sqrt( face_area
/ uv_area
);
2828 int corner_count
= 0;
2831 * Vertex classification
2832 * boundary vertices: they exist on a freestyle edge
2833 * corners: only connected to other boundaries
2835 for( int i
=0; i
<mesh
->p_abverts
->count
; i
++ )
2837 struct vertinfo
*info
= &vertinfo
[i
];
2838 if( !info
->boundary
) continue;
2843 for( int j
=0; j
<info
->con_count
; j
++ )
2845 int con
= graph
[info
->con_start
+j
];
2847 if( vertinfo
[con
].boundary
)
2853 if( count
> 2 || non_manifold
)
2861 * TODO(harry): This currently only supports power 2 displacements
2862 * its quite straightforward to upgrade it.
2864 * TODO(harry): Error checking is needed here for bad input data
2872 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
2874 cxr_polygon
*basepoly
= &mesh
->polys
[i
];
2876 for( int h
=0; h
<basepoly
->loop_total
; h
++ )
2879 i1
= cxr_range(h
+1,basepoly
->loop_total
);
2881 cxr_loop
*l0
= &mesh
->loops
[ basepoly
->loop_start
+i0
],
2882 *l1
= &mesh
->loops
[ basepoly
->loop_start
+i1
];
2883 struct vertinfo
*info
= &vertinfo
[ l0
->index
];
2888 int corner_count
= 1;
2890 cxr_material
*matptr
=
2891 basepoly
->material_id
< 0 || !world
->materials
?
2893 &world
->materials
[ basepoly
->material_id
];
2896 dispedge
[0] = l0
->index
;
2897 dispedge
[1] = l1
->index
;
2898 v2_copy( l0
->uv
, corner_uvs
[0] );
2900 /* Consume (use) face from orignal mesh */
2901 basepoly
->loop_total
= -1;
2903 while( dispedge_count
< 17 )
2905 struct vertinfo
*edge_head
=
2906 &vertinfo
[dispedge
[dispedge_count
-1]];
2910 if( edge_head
->corner
)
2912 /* Find polygon that has edge C-1 -> C */
2913 for( int j
=0; j
<mesh
->abpolys
.count
&& !newvert
; j
++ )
2915 cxr_polygon
*poly
= &mesh
->polys
[j
];
2917 for( int k
=0; k
<poly
->loop_total
; k
++ )
2920 i1
= cxr_range(k
+1,poly
->loop_total
);
2922 cxr_loop
*l0
= &mesh
->loops
[ poly
->loop_start
+i0
],
2923 *l1
= &mesh
->loops
[ poly
->loop_start
+i1
];
2925 if( l0
->index
== dispedge
[dispedge_count
-2] &&
2926 l1
->index
== dispedge
[dispedge_count
-1] )
2928 /* Take the next edge */
2929 v2_copy( l1
->uv
, corner_uvs
[corner_count
++] );
2931 int i2
= cxr_range(i1
+1,poly
->loop_total
);
2932 cxr_loop
*l2
= &mesh
->loops
[ poly
->loop_start
+i2
];
2934 dispedge
[dispedge_count
++] = l2
->index
;
2936 poly
->loop_total
= -1;
2944 for( int j
=0; j
<edge_head
->con_count
; j
++ )
2946 int con
= graph
[edge_head
->con_start
+j
];
2951 if( dispedge_count
> 1 )
2952 if( con
== dispedge
[dispedge_count
-2] )
2955 struct vertinfo
*coninfo
= &vertinfo
[con
];
2957 if( !coninfo
->boundary
)
2960 dispedge
[ dispedge_count
++ ] = con
;
2975 /* All edges collected */
2978 v2_sub( corner_uvs
[1], corner_uvs
[0], va
);
2979 v2_sub( corner_uvs
[2], corner_uvs
[0], vb
);
2981 /* Connect up the grid
2989 * Example: a := common unused vertex that is connected to
2990 * by 1 and 15. Or y-1, and x-1 on the grid.
2991 * g := c and f common vert ^
2996 for( int j
=0; j
<5; j
++ ) grid
[j
] = dispedge
[j
];
2997 for( int j
=1; j
<5; j
++ ) grid
[j
*5+4] = dispedge
[j
+4];
2998 for( int j
=0; j
<4; j
++ ) grid
[4*5+3-j
] = dispedge
[j
+9];
2999 for( int j
=1; j
<4; j
++ ) grid
[j
*5] = dispedge
[16-j
];
3002 for( int j
=1; j
<4; j
++ )
3004 for( int k
=1; k
<4; k
++ )
3006 int s0
= grid
[(j
-1)*5+k
],
3009 struct vertinfo
*va
= &vertinfo
[s0
],
3010 *vb
= &vertinfo
[s1
];
3012 /* Find common non-used vertex */
3013 for( int l
=0; l
<va
->con_count
; l
++ )
3015 for( int m
=0; m
<vb
->con_count
; m
++ )
3017 int cona
= graph
[va
->con_start
+l
],
3018 conb
= graph
[vb
->con_start
+m
];
3022 if( vertinfo
[cona
].used
|| vertinfo
[cona
].boundary
)
3025 grid
[ j
*5+k
] = cona
;
3026 vertinfo
[cona
].used
= 1;
3042 * Create V reference based on first displacement.
3043 * TODO(harry): This is not the moststable selection method!
3044 * faces can come in any order, so the first disp will of
3045 * course always vary. Additionaly the triangle can be oriented
3048 * Improvement can be made by selecting a first disp/triangle
3049 * based on deterministic factors.
3051 if( disp_count
== 0 )
3055 v3_copy( verts
[dispedge
[0]], tri_ref
[0] );
3056 v3_copy( verts
[dispedge
[4]], tri_ref
[1] );
3057 v3_copy( verts
[dispedge
[8]], tri_ref
[2] );
3058 cxr_calculate_axis( &tx
, tri_ref
, corner_uvs
, (v2f
){512,512} );
3060 v3_muls( tx
.vaxis
, -1.0, refv
);
3061 int v_cardinal
= cxr_cardinal( refv
, -1 );
3063 v3_cross( tx
.vaxis
, tx
.uaxis
, refn
);
3064 v3_muls( refn
, -tx
.winding
, refn
);
3066 /* Computing new reference vectors */
3067 int n1_cardinal
= cxr_cardinal( refn
, v_cardinal
);
3071 for( int j
=0; j
<2; j
++ )
3072 if( u_cardinal
== n1_cardinal
|| u_cardinal
== v_cardinal
)
3076 refu
[u_cardinal
] = tx
.uaxis
[u_cardinal
] > 0.0? 1.0: -1.0;
3080 v3_copy( face_center
, p0
);
3081 v3_muladds( face_center
, refn
, 1.5, pn
);
3082 v3_muladds( face_center
, refv
, 1.5, pv
);
3083 v3_muladds( face_center
, refu
, 1.5, pu
);
3085 v3_muladds( face_center
, refn
, 2.0, face_center
);
3088 /* Create world coordinates */
3089 v3f world_corners
[8];
3092 for( int j
=0; j
<4; j
++ )
3095 v2_sub( corner_uvs
[j
], uv_center
, local_uv
);
3096 v2_copy( corner_uvs
[j
], world_uv
[j
] );
3097 v2_muls( local_uv
, sf
, local_uv
);
3099 v3_muls( refu
, local_uv
[0], world_corners
[j
] );
3100 v3_muladds( world_corners
[j
],refv
,local_uv
[1],world_corners
[j
] );
3101 v3_add( face_center
, world_corners
[j
], world_corners
[j
] );
3104 double *colour
= colours_random
[cxr_range(disp_count
,8)];
3106 for( int j
=0; j
<4; j
++ )
3107 v3_muladds( world_corners
[j
], refn
, -1.0, world_corners
[j
+4] );
3109 /* Apply world transform */
3110 for( int j
=0; j
<8; j
++ )
3112 double *p0
= world_corners
[j
];
3113 v3_muls( p0
, ctx
->scale
, p0
);
3114 v3_add( p0
, ctx
->offset
, p0
);
3117 cxr_texinfo texinfo_shared
;
3118 cxr_calculate_axis( &texinfo_shared
, world_corners
, world_uv
,
3119 (v2f
){ matptr
->res
[0], matptr
->res
[1] } );
3122 cxr_vdf_node( output
, "solid" );
3123 cxr_vdf_ki32( output
, "id", ++ ctx
->brush_count
);
3134 double distances
[25];
3137 v3f lside0
, lside1
, lref
, vdelta
, vworld
;
3140 for( int j
=0; j
<5; j
++ )
3142 ty
= (double)j
/(double)(5-1);
3144 v3_lerp( world_corners
[0], world_corners
[3], ty
, lside0
);
3145 v3_lerp( world_corners
[1], world_corners
[2], ty
, lside1
);
3147 for( int k
=0; k
<5; k
++ )
3151 tx
= (double)k
/(double)(5-1);
3152 v3_lerp( lside0
, lside1
, tx
, lref
);
3153 v3_muls( verts
[grid
[index
]], ctx
->scale
, vworld
);
3154 v3_add( ctx
->offset
, vworld
, vworld
);
3156 v3_sub( vworld
, lref
, vdelta
);
3157 v3_copy( vdelta
, normals
[index
] );
3158 v3_normalize( normals
[index
] );
3159 distances
[index
] = v3_dot( vdelta
, normals
[index
] );
3161 alphas
[index
] = vertinfo
[grid
[index
]].alpha
;
3165 for( int j
=0; j
<6; j
++ )
3167 int *side
= sides
[j
];
3169 cxr_vdf_node( output
, "side" );
3170 cxr_vdf_ki32( output
, "id", ++ ctx
->face_count
);
3171 cxr_vdf_plane( output
, "plane", world_corners
[side
[2]],
3172 world_corners
[side
[1]],
3173 world_corners
[side
[0]] );
3175 cxr_vdf_kv( output
, "material", matptr
->name
);
3176 cxr_vdf_kaxis( output
, "uaxis",
3177 texinfo_shared
.uaxis
,
3178 texinfo_shared
.offset
[0],
3179 texinfo_shared
.scale
[0] );
3180 cxr_vdf_kaxis( output
, "vaxis",
3181 texinfo_shared
.vaxis
,
3182 texinfo_shared
.offset
[1],
3183 texinfo_shared
.scale
[1] );
3185 cxr_vdf_kdouble( output
, "rotation", 0.0 );
3186 cxr_vdf_ki32( output
, "lightmapscale", ctx
->lightmap_scale
);
3187 cxr_vdf_ki32( output
, "smoothing_groups", 0 );
3191 cxr_vdf_node( output
, "dispinfo" );
3192 cxr_vdf_ki32( output
, "power", 2 );
3193 cxr_vdf_kv3f( output
, "startposition", world_corners
[0] );
3194 cxr_vdf_ki32( output
, "flags", 0 );
3195 cxr_vdf_kdouble( output
, "elevation", 0.0 );
3196 cxr_vdf_ki32( output
, "subdiv", 0 );
3198 cxr_vdf_node( output
, "normals" );
3199 for( int k
=0; k
<5; k
++ )
3200 cxr_vdf_karrv3f( output
, "row", k
, &normals
[k
*5], 5 );
3201 cxr_vdf_edon( output
);
3203 cxr_vdf_node( output
, "distances" );
3204 for( int k
=0; k
<5; k
++ )
3205 cxr_vdf_karrdouble( output
, "row", k
, &distances
[k
*5], 5 );
3206 cxr_vdf_edon( output
);
3208 cxr_vdf_node( output
, "alphas" );
3209 for( int k
=0; k
<5; k
++ )
3210 cxr_vdf_karrdouble( output
, "row", k
, &alphas
[k
*5], 5 );
3211 cxr_vdf_edon( output
);
3214 * TODO: This might be needed for the compilers. Opens fine in
3219 cxr_vdf_node( output, "offsets" );
3220 for( int k=0; k<5; k++ )
3221 cxr_vdf_printf( output,
3222 "\"row%d\" \"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\"\n", k );
3223 cxr_vdf_edon( output );
3225 cxr_vdf_node( output, "offset_normals" );
3226 for( int k=0; k<5; k++ )
3227 cxr_vdf_printf( output,
3228 "\"row%d\" \"0 0 1 0 0 1 0 0 1 0 0 1 0 0 1\"\n", k );
3229 cxr_vdf_edon( output );
3231 cxr_vdf_node( output, "triangle_tags" );
3232 for( int k=0; k<5-1; k++ )
3233 cxr_vdf_printf( output,
3234 "\"row%d\" \"9 9 9 9 9 9 9 9\"\n", k );
3235 cxr_vdf_edon( output );
3237 cxr_vdf_node( output, "allowed_verts" );
3238 cxr_vdf_printf( output,
3239 "\"10\" \"-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\"\n" );
3240 cxr_vdf_edon( output );
3243 cxr_vdf_edon( output
);
3246 cxr_vdf_edon( output
);
3249 cxr_vdf_node( output
, "editor");
3250 cxr_vdf_colour255( output
, "color",
3251 colours_random
[cxr_range(ctx
->brush_count
,8)]);
3253 cxr_vdf_ki32( output
, "visgroupid", ctx
->visgroupid
);
3254 cxr_vdf_ki32( output
, "visgroupshown",1);
3255 cxr_vdf_ki32( output
, "visgroupautoshown",1);
3256 cxr_vdf_edon( output
);
3258 cxr_vdf_edon( output
);
3270 * Write header information for a vmf to vdf
3272 CXR_API
void cxr_begin_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*output
)
3274 cxr_vdf_node( output
, "versioninfo" );
3275 cxr_vdf_ki32( output
, "editorversion", 400 );
3276 cxr_vdf_ki32( output
, "editorbuild", 8456 );
3277 cxr_vdf_ki32( output
, "mapversion", ctx
->mapversion
);
3278 cxr_vdf_ki32( output
, "formatversion", 100 );
3279 cxr_vdf_ki32( output
, "prefab", 0 );
3280 cxr_vdf_edon( output
);
3282 cxr_vdf_node( output
, "visgroups" );
3284 for( int i
=0; i
<ctx
->visgroup_count
; i
++ )
3286 cxr_vdf_node( output
, "visgroup" );
3287 cxr_vdf_kv( output
, "name", ctx
->visgroups
[i
].name
);
3288 cxr_vdf_ki32( output
, "visgroupid", i
+1 );
3289 cxr_vdf_edon( output
);
3292 cxr_vdf_edon( output
);
3294 cxr_vdf_node( output
, "viewsettings" );
3295 cxr_vdf_ki32( output
, "bSnapToGrid", 1 );
3296 cxr_vdf_ki32( output
, "bShowGrid", 1 );
3297 cxr_vdf_ki32( output
, "bShowLogicalGrid", 0 );
3298 cxr_vdf_ki32( output
, "nGridSpacing", 64 );
3299 cxr_vdf_ki32( output
, "bShow3DGrid", 0 );
3300 cxr_vdf_edon( output
);
3302 cxr_vdf_node( output
, "world" );
3303 cxr_vdf_ki32( output
, "id", 1 );
3304 cxr_vdf_ki32( output
, "mapversion", 1 ); /* ?? */
3305 cxr_vdf_kv( output
, "classname", "worldspawn" );
3306 cxr_vdf_kv( output
, "skyname", ctx
->skyname
);
3307 cxr_vdf_ki32( output
, "maxpropscreenwidth", -1 );
3308 cxr_vdf_kv( output
, "detailvbsp", ctx
->detailvbsp
);
3309 cxr_vdf_kv( output
, "detailmaterial", ctx
->detailmaterial
);
3312 /* Fairly useless but might need in the future */
3313 CXR_API
void cxr_vmf_begin_entities( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
)
3315 cxr_vdf_edon( vdf
);
3318 CXR_API
void cxr_end_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
)
3323 * Write solids (and displacements) to VMF file
3325 CXR_API
void cxr_push_world_vmf( cxr_world
*world
, cxr_vmf_context
*ctx
,
3328 v3f
*verts
= cxr_ab_ptr( &world
->abverts
, 0 );
3330 /* Write all solids as VMF brushes */
3331 for( int i
=0; i
<world
->absolids
.count
; i
++ )
3333 cxr_solid
*solid
= cxr_ab_ptr(&world
->absolids
,i
);
3335 if( solid
->displacement
)
3337 if( !cxr_write_disp( solid
->pmesh
, world
, ctx
, output
) )
3339 cxr_log( "Warning: Invalid displacement\n" );
3344 cxr_vdf_node( output
, "solid" );
3345 cxr_vdf_ki32( output
, "id", ++ ctx
->brush_count
);
3347 for( int j
=0; j
<solid
->pmesh
->abpolys
.count
; j
++ )
3349 cxr_polygon
*poly
= &solid
->pmesh
->polys
[j
];
3350 cxr_loop
*ploops
= &solid
->pmesh
->loops
[poly
->loop_start
];
3352 cxr_material
*matptr
=
3353 poly
->material_id
< 0 || !world
->materials
?
3355 &world
->materials
[ poly
->material_id
];
3357 cxr_vdf_node( output
, "side" );
3358 cxr_vdf_ki32( output
, "id", ++ ctx
->face_count
);
3360 v3f tri
[3]; v2f uvs
[3];
3362 int i0
= ploops
[0].index
,
3363 i1
= ploops
[1].index
,
3364 i2
= ploops
[2].index
;
3366 v3_muls( verts
[i0
], ctx
->scale
, tri
[0] );
3367 v3_muls( verts
[i1
], ctx
->scale
, tri
[1] );
3368 v3_muls( verts
[i2
], ctx
->scale
, tri
[2] );
3370 v3_add( ctx
->offset
, tri
[0], tri
[0] );
3371 v3_add( ctx
->offset
, tri
[1], tri
[1] );
3372 v3_add( ctx
->offset
, tri
[2], tri
[2] );
3374 v2_copy( ploops
[0].uv
, uvs
[0] );
3375 v2_copy( ploops
[1].uv
, uvs
[1] );
3376 v2_copy( ploops
[2].uv
, uvs
[2] );
3378 cxr_vdf_plane( output
, "plane", tri
[2], tri
[1], tri
[0] );
3379 cxr_vdf_kv( output
, "material", matptr
->name
);
3382 cxr_calculate_axis( &tx
, tri
, uvs
,
3383 (double[2]){ matptr
->res
[0], matptr
->res
[1] });
3385 cxr_vdf_kaxis( output
, "uaxis", tx
.uaxis
, tx
.offset
[0], tx
.scale
[0]);
3386 cxr_vdf_kaxis( output
, "vaxis", tx
.vaxis
, tx
.offset
[1], tx
.scale
[1]);
3388 cxr_vdf_kdouble( output
, "rotation", 0.0 );
3389 cxr_vdf_ki32( output
, "lightmapscale", ctx
->lightmap_scale
);
3390 cxr_vdf_ki32( output
, "smoothing_groups", 0);
3392 cxr_vdf_edon( output
);
3395 cxr_vdf_node( output
, "editor" );
3396 cxr_vdf_colour255( output
, "color",
3397 colours_random
[cxr_range(ctx
->brush_count
,8)]);
3399 cxr_vdf_ki32( output
, "visgroupid", ctx
->visgroupid
);
3400 cxr_vdf_ki32( output
, "visgroupshown", 1 );
3401 cxr_vdf_ki32( output
, "visgroupautoshown", 1 );
3402 cxr_vdf_edon( output
);
3404 cxr_vdf_edon( output
);
3409 * Valve Source SDK 2015 CS:GO
3411 #define HEADER_LUMPS 64
3412 #define LUMP_WORLDLIGHTS 54
3414 #pragma pack(push,1)
3423 int fileofs
, filelen
;
3428 lumps
[ HEADER_LUMPS
];
3438 float shadow_cast_offset
[3];
3446 float constant_attn
;
3448 float quadratic_attn
;
3456 * Utility for patching BSP tools to remove -1 distance lights (we set them
3457 * like that, because we want these lights to go away)
3459 * Yes, there is no way to do this in hammer
3460 * Yes, the distance KV is unused but still gets compiled to this lump
3461 * No, Entities only compile will not do this for you
3463 CXR_API
int cxr_lightpatch_bsp( const char *path
)
3465 printf( "Lightpatch: %s\n", path
);
3467 FILE *fp
= fopen( path
, "r+b" );
3472 cxr_log( "Could not open BSP file for editing (r+b)\n" );
3478 struct header header
;
3479 fread( &header
, sizeof(struct header
), 1, fp
);
3480 struct lump
*lump
= &header
.lumps
[ LUMP_WORLDLIGHTS
];
3482 /* Read worldlight array */
3483 struct worldlight
*lights
= malloc( lump
->filelen
);
3484 fseek( fp
, lump
->fileofs
, SEEK_SET
);
3485 fread( lights
, lump
->filelen
, 1, fp
);
3487 /* Remove all marked lights */
3488 int light_count
= lump
->filelen
/ sizeof(struct worldlight
);
3491 for( int i
= 0; i
< light_count
; i
++ )
3492 if( lights
[i
].radius
>= 0.0f
)
3493 lights
[new_count
++] = lights
[i
];
3495 lump
->filelen
= new_count
*sizeof(struct worldlight
);
3497 /* Write changes back to file */
3498 fseek( fp
, lump
->fileofs
, SEEK_SET
);
3499 fwrite( lights
, lump
->filelen
, 1, fp
);
3500 fseek( fp
, 0, SEEK_SET
);
3501 fwrite( &header
, sizeof(struct header
), 1, fp
);
3504 cxr_log( "removed %d marked lights\n", light_count
-new_count
);
3513 #endif /* CXR_VALVE_MAP_FILE */
3514 #endif /* CXR_IMPLEMENTATION */