4 A GNU/Linux-first Source1 Hammer replacement
5 built with Blender, for mapmakers
7 Copyright (C) 2022 Harry Godden (hgn)
10 - Brush decomposition into convex pieces for well defined geometry
11 - Freely form displacements without limits
12 - Build your entire map in Blender
13 - Compile models and model groups easily
14 - It runs at an ok speed!
15 - Light patch BSP files; remove unwanted realtime effects
16 - Fastest VTF compressor (thanks to Richgel999 and stb)
20 File/folder Lang Purpose
22 __init__.py Python Blender plugin interface
25 cxr.h C Heavy lifting; brush decomp, mesh processing
26 cxr_math.h C Vector maths and other handy things
27 cxr_mem.h C Automatic resizing buffers
28 libcxr.c C Compile as SO
31 nbvtf.h C VTF processing interface
32 librgcx.h C++ Rich Geldreich's DXT1/DXT5 compressors
33 stb/ C Sean Barrets image I/O
49 #define CXR_EPSILON 0.001
50 #define CXR_PLANE_SIMILARITY_MAX 0.998
51 #define CXR_BIG_NUMBER 1e300
52 #define CXR_INTERIOR_ANGLE_MAX 0.998
55 #define CXR_IMPLEMENTATION
74 typedef unsigned int uint
;
76 typedef double v2f
[2];
77 typedef double v3f
[3];
78 typedef double v4f
[4];
86 typedef struct cxr_world cxr_world
;
87 typedef struct cxr_solid cxr_solid
;
89 typedef struct cxr_mesh cxr_mesh
;
90 typedef struct cxr_edge cxr_edge
;
91 typedef struct cxr_polygon cxr_polygon
;
92 typedef struct cxr_static_mesh cxr_static_mesh
;
93 typedef struct cxr_loop cxr_loop
;
94 typedef struct cxr_static_loop cxr_static_loop
;
95 typedef struct cxr_material cxr_material
;
96 typedef struct cxr_tri_mesh cxr_tri_mesh
;
98 #ifdef CXR_VALVE_MAP_FILE
99 typedef struct cxr_vdf cxr_vdf
;
100 typedef struct cxr_texinfo cxr_texinfo
;
101 typedef struct cxr_vmf_context cxr_vmf_context
;
102 #endif /* CXR_VALVE_MAP_FILE */
108 /* Main convexer algorithms */
109 /* Convex decomp from mesh */
110 CXR_API cxr_world
*cxr_decompose( cxr_static_mesh
*src
, i32
*perrcode
);
111 CXR_API
void cxr_free_world( cxr_world
*world
);
112 CXR_API cxr_tri_mesh
*cxr_world_preview( cxr_world
*world
);
113 CXR_API
void cxr_free_tri_mesh( cxr_tri_mesh
*mesh
);
115 #ifdef CXR_VALVE_MAP_FILE
117 CXR_API
void cxr_begin_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
118 CXR_API
void cxr_vmf_begin_entities( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
119 CXR_API
void cxr_push_world_vmf(
120 cxr_world
*world
, cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
121 CXR_API
void cxr_end_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
);
124 CXR_API cxr_vdf
*cxr_vdf_open( const char *path
);
125 CXR_API
void cxr_vdf_close( cxr_vdf
*vdf
);
126 CXR_API
void cxr_vdf_put( cxr_vdf
*vdf
, const char *str
);
127 CXR_API
void cxr_vdf_node( cxr_vdf
*vdf
, const char *str
);
128 CXR_API
void cxr_vdf_edon( cxr_vdf
*vdf
);
129 CXR_API
void cxr_vdf_kv( cxr_vdf
*vdf
, const char *strk
, const char *strv
);
132 CXR_API
int cxr_lightpatch_bsp( const char *path
);
133 #endif /* CXR_VALVE_MAP_FILE */
137 CXR_API
void cxr_set_log_function( void (*func
)(const char *str
) );
138 CXR_API
void cxr_set_line_function( void (*func
)(v3f p0
, v3f p1
, v4f colour
) );
139 CXR_API
void cxr_write_test_data( cxr_static_mesh
*src
);
140 #endif /* CXR_DEBUG */
142 struct cxr_static_mesh
149 i32 freestyle
, sharp
;
153 struct cxr_static_loop
163 i32 loop_start
, loop_total
;
166 i32 material_id
; /* -1: interior material (nodraw) */
205 cxr_material
*materials
;
216 *p_abverts
; /* This data is stored externally because the data is often
217 shared between solids. */
219 /* Valid when update() is called on this mesh,
220 * Invalid when data is appended to them */
221 struct cxr_edge
*edges
;
222 struct cxr_polygon
*polys
;
223 struct cxr_loop
*loops
;
226 /* Simple mesh type mainly for debugging */
236 #ifdef CXR_VALVE_MAP_FILE
245 * Simplified VDF writing interface. No allocations or nodes, just write to file
253 struct cxr_vmf_context
261 /* Transform settings */
271 #endif /* CXR_VALVE_MAP_FILE */
276 k_soliderr_non_manifold
,
277 k_soliderr_bad_manifold
,
278 k_soliderr_no_solids
,
279 k_soliderr_degenerate_implicit
,
280 k_soliderr_non_coplanar_vertices
,
281 k_soliderr_non_convex_poly
,
282 k_soliderr_bad_result
287 * -----------------------------------------------------------------------------
289 #ifdef CXR_IMPLEMENTATION
291 const char *cxr_build_time
= __DATE__
" @" __TIME__
;
294 static void (*cxr_log_func
)(const char *str
);
295 static void (*cxr_line_func
)( v3f p0
, v3f p1
, v4f colour
);
297 static int cxr_range(int x
, int bound
)
300 x
+= bound
* (x
/bound
+ 1);
306 * This should be called after appending any data to those buffers
308 static void cxr_mesh_update( cxr_mesh
*mesh
)
310 mesh
->edges
= cxr_ab_ptr(&mesh
->abedges
, 0);
311 mesh
->polys
= cxr_ab_ptr(&mesh
->abpolys
, 0);
312 mesh
->loops
= cxr_ab_ptr(&mesh
->abloops
, 0);
315 static v4f colours_random
[] =
317 { 0.863, 0.078, 0.235, 0.4 },
318 { 0.000, 0.980, 0.604, 0.4 },
319 { 0.118, 0.565, 1.000, 0.4 },
320 { 0.855, 0.439, 0.839, 0.4 },
321 { 0.824, 0.412, 0.118, 0.4 },
322 { 0.125, 0.698, 0.667, 0.4 },
323 { 0.541, 0.169, 0.886, 0.4 },
324 { 1.000, 0.843, 0.000, 0.4 }
327 static v4f colours_solids
[] =
329 { 100, 143, 255, 200 },
330 { 120, 94, 240, 200 },
331 { 220, 38, 127, 200 },
336 static v4f colour_entity
= { 37, 241, 122, 255 };
337 static v4f colour_displacement_solid
= { 146, 146, 146, 120 };
338 static v4f colour_error
= { 1.0f
, 0.0f
, 0.0f
, 1.0f
};
339 static v4f colour_face_graph
= { 1.0f
, 1.0f
, 1.0f
, 0.03f
};
340 static v4f colour_success
= { 0.0f
, 1.0f
, 0.0f
, 1.0f
};
342 static void value_random(int n
, v4f colour
)
344 double val
= cxr_range(n
,8);
348 v3_muls( colour
, val
, colour
);
351 static void colour_random_brush(int n
, v4f colour
)
355 int colour_n
= cxr_range( n
, 5 );
356 v4_muls( colours_solids
[ colour_n
], 1.0/255.0, colour
);
357 value_random( value_n
, colour
);
359 int colour_n
= cxr_range( n
, 8 );
360 v4_copy( colours_random
[ colour_n
], colour
);
365 * Debugging and diagnostic utilities
366 * -----------------------------------------------------------------------------
371 static void cxr_log( const char *fmt
, ... )
376 va_start( args
, fmt
);
377 vsnprintf( buf
, sizeof(buf
)-1, fmt
, args
);
386 static void cxr_debug_line( v3f p0
, v3f p1
, v4f colour
)
389 cxr_line_func( p0
, p1
, colour
);
392 static void cxr_debug_box( v3f p0
, double sz
, v4f colour
)
396 v3_add(p0
, (v3f
){-sz
,-sz
,-sz
}, a
);
397 v3_add(p0
, (v3f
){-sz
, sz
,-sz
}, b
);
398 v3_add(p0
, (v3f
){ sz
, sz
,-sz
}, c
);
399 v3_add(p0
, (v3f
){ sz
,-sz
,-sz
}, d
);
400 v3_add(p0
, (v3f
){-sz
,-sz
,sz
}, a1
);
401 v3_add(p0
, (v3f
){-sz
, sz
,sz
}, b1
);
402 v3_add(p0
, (v3f
){ sz
, sz
,sz
}, c1
);
403 v3_add(p0
, (v3f
){ sz
,-sz
,sz
}, d1
);
405 cxr_debug_line( a
,b
, colour
);
406 cxr_debug_line( b
,c
, colour
);
407 cxr_debug_line( c
,d
, colour
);
408 cxr_debug_line( d
,a
, colour
);
409 cxr_debug_line( a1
,b1
, colour
);
410 cxr_debug_line( b1
,c1
, colour
);
411 cxr_debug_line( c1
,d1
, colour
);
412 cxr_debug_line( d1
,a1
, colour
);
413 cxr_debug_line( a
,a1
, colour
);
414 cxr_debug_line( b
,b1
, colour
);
415 cxr_debug_line( c
,c1
, colour
);
416 cxr_debug_line( d
,d1
, colour
);
420 * Draw arrow with the tips oriented along normal
422 static void cxr_debug_arrow( v3f p0
, v3f p1
, v3f normal
, double sz
, v4f colour
)
424 v3f dir
, tan
, p2
, p3
;
428 v3_cross(dir
,normal
,tan
);
429 v3_muladds( p1
,dir
, -sz
, p2
);
430 v3_muladds( p2
,tan
,sz
,p3
);
431 cxr_debug_line( p1
, p3
, colour
);
432 v3_muladds( p2
,tan
,-sz
,p3
);
433 cxr_debug_line( p1
, p3
, colour
);
434 cxr_debug_line( p0
, p1
, colour
);
438 * Draw arrows CCW around polygon, draw normal vector from center
440 static void cxr_debug_poly( cxr_mesh
*mesh
, cxr_polygon
*poly
, v4f colour
)
442 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
444 for( int i
=0; i
<poly
->loop_total
; i
++ )
446 int lp0
= poly
->loop_start
+i
,
447 lp1
= poly
->loop_start
+cxr_range(i
+1,poly
->loop_total
);
449 int i0
= mesh
->loops
[ lp0
].index
,
450 i1
= mesh
->loops
[ lp1
].index
;
454 v3_lerp( verts
[i0
], poly
->center
, 0.0075, p0
);
455 v3_lerp( verts
[i1
], poly
->center
, 0.0075, p1
);
456 v3_muladds( p0
, poly
->normal
, 0.01, p0
);
457 v3_muladds( p1
, poly
->normal
, 0.01, p1
);
459 cxr_debug_arrow( p0
, p1
, poly
->normal
, 0.05, colour
);
463 v3_muladds( poly
->center
, poly
->normal
, 0.3, nrm0
);
465 cxr_debug_line( poly
->center
, nrm0
, colour
);
468 static void cxr_debug_mesh(cxr_mesh
*mesh
, v4f colour
)
470 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
472 cxr_polygon
*poly
= &mesh
->polys
[i
];
473 cxr_debug_poly( mesh
, poly
, colour
);
477 CXR_API
void cxr_write_test_data( cxr_static_mesh
*src
)
480 "/home/harry/Documents/blender_addons_remote/addons/convexer/cxr/solid.h",
483 fprintf( fp
, "v3f test_verts[] = {\n" );
484 for( int i
=0; i
<src
->vertex_count
; i
++ )
486 fprintf( fp
, " { %f, %f, %f },\n",
489 src
->vertices
[i
][2] );
491 fprintf( fp
, "};\n" );
493 fprintf( fp
, "cxr_static_loop test_loops[] = {\n" );
494 for( int i
=0; i
<src
->loop_count
; i
++ )
496 fprintf( fp
, " {%d, %d},\n",
498 src
->loops
[i
].edge_index
);
500 fprintf( fp
, "};\n" );
502 fprintf( fp
, "cxr_polygon test_polys[] = {\n" );
503 for( int i
=0; i
<src
->poly_count
; i
++ )
505 fprintf( fp
, " {%d, %d, {%f, %f, %f}, {%f, %f, %f}},\n",
506 src
->polys
[i
].loop_start
,
507 src
->polys
[i
].loop_total
,
508 src
->polys
[i
].normal
[0],
509 src
->polys
[i
].normal
[1],
510 src
->polys
[i
].normal
[2],
511 src
->polys
[i
].center
[0],
512 src
->polys
[i
].center
[1],
513 src
->polys
[i
].center
[2] );
515 fprintf( fp
, "};\n" );
517 fprintf( fp
, "cxr_edge test_edges[] = {\n" );
518 for( int i
=0; i
<src
->edge_count
; i
++ )
520 fprintf( fp
, " {%d, %d, %d, %d},\n",
523 src
->edges
[i
].freestyle
,
527 fprintf( fp
, "};\n" );
529 fprintf( fp
, "cxr_static_mesh test_mesh = {\n" );
530 fprintf( fp
, " .vertices = test_verts,\n" );
531 fprintf( fp
, " .loops = test_loops,\n" );
532 fprintf( fp
, " .edges = test_edges,\n" );
533 fprintf( fp
, " .polys = test_polys,\n" );
534 fprintf( fp
, " .poly_count=%d,\n", src
->poly_count
);
535 fprintf( fp
, " .vertex_count=%d,\n", src
->vertex_count
);
536 fprintf( fp
, " .edge_count=%d,\n",src
->edge_count
);
537 fprintf( fp
, " .loop_count=%d\n", src
->loop_count
);
538 fprintf( fp
, "};\n" );
543 CXR_API
void cxr_set_log_function( void (*func
)(const char *str
) )
548 CXR_API
void cxr_set_line_function( void (*func
)(v3f p0
, v3f p1
, v4f colour
) )
550 cxr_line_func
= func
;
553 #endif /* CXR_DEBUG */
557 * abverts is a pointer to an existing vertex buffer
559 static cxr_mesh
*cxr_alloc_mesh( int edge_count
, int loop_count
, int poly_count
,
562 cxr_mesh
*mesh
= malloc(sizeof(cxr_mesh
));
563 cxr_ab_init(&mesh
->abedges
, sizeof(cxr_edge
), edge_count
);
564 cxr_ab_init(&mesh
->abloops
, sizeof(cxr_loop
), loop_count
);
565 cxr_ab_init(&mesh
->abpolys
, sizeof(cxr_polygon
), poly_count
);
566 mesh
->p_abverts
= abverts
;
568 cxr_mesh_update( mesh
);
573 static void cxr_free_mesh( cxr_mesh
*mesh
)
575 cxr_ab_free(&mesh
->abedges
);
576 cxr_ab_free(&mesh
->abloops
);
577 cxr_ab_free(&mesh
->abpolys
);
582 * Rebuilds edge data for mesh (useful to get rid of orphaned edges)
584 static void cxr_mesh_clean_edges( cxr_mesh
*mesh
)
586 cxr_abuffer new_edges
;
587 cxr_ab_init( &new_edges
, sizeof(cxr_edge
), mesh
->abedges
.count
);
589 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
591 cxr_polygon
*poly
= &mesh
->polys
[i
];
592 for( int j
=0; j
<poly
->loop_total
; j
++ )
595 *lp0
= &mesh
->loops
[poly
->loop_start
+j
],
596 *lp1
= &mesh
->loops
[poly
->loop_start
+cxr_range(j
+1,poly
->loop_total
)];
598 int i0
= cxr_min(lp0
->index
, lp1
->index
),
599 i1
= cxr_max(lp0
->index
, lp1
->index
);
601 /* Check if edge exists before adding */
602 for( int k
=0; k
<new_edges
.count
; k
++ )
604 cxr_edge
*edge
= cxr_ab_ptr(&new_edges
,k
);
606 if( edge
->i0
== i0
&& edge
->i1
== i1
)
609 goto IL_EDGE_CREATED
;
613 int orig_edge_id
= lp0
->edge_index
;
614 lp0
->edge_index
= new_edges
.count
;
616 cxr_edge edge
= { i0
, i1
};
619 * Copy extra information from original edges
622 if( orig_edge_id
< mesh
->abedges
.count
)
624 cxr_edge
*orig_edge
= &mesh
->edges
[ orig_edge_id
];
625 edge
.freestyle
= orig_edge
->freestyle
;
626 edge
.sharp
= orig_edge
->sharp
;
634 cxr_ab_push( &new_edges
, &edge
);
640 cxr_ab_free( &mesh
->abedges
);
641 mesh
->abedges
= new_edges
;
643 cxr_mesh_update( mesh
);
647 * Remove 0-length faces from mesh (we mark them light that for deletion
648 * Remove all unused loops as a result of removing those faces
650 static void cxr_mesh_clean_faces( cxr_mesh
*mesh
)
652 cxr_abuffer loops_new
;
653 cxr_ab_init( &loops_new
, sizeof(cxr_loop
), mesh
->abloops
.count
);
656 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
658 cxr_polygon
*src
= &mesh
->polys
[i
],
659 *dst
= &mesh
->polys
[new_length
];
661 if( src
->loop_total
> 0 )
663 int src_start
= src
->loop_start
,
664 src_total
= src
->loop_total
;
667 dst
->loop_start
= loops_new
.count
;
669 for( int j
=0; j
<src_total
; j
++ )
671 cxr_loop
*loop
= &mesh
->loops
[src_start
+j
],
672 *ldst
= cxr_ab_ptr(&loops_new
,dst
->loop_start
+j
);
674 ldst
->poly_left
= new_length
;
677 loops_new
.count
+= src_total
;
682 cxr_ab_free( &mesh
->abloops
);
683 mesh
->abloops
= loops_new
;
684 mesh
->abpolys
.count
= new_length
;
686 cxr_mesh_update( mesh
);
690 * Links loop's poly_left and poly_right
691 * Does not support more than 2 polys to one edge
693 * Returns 0 if there is non-manifold geomtry (aka: not watertight)
695 static int cxr_mesh_link_loops( cxr_mesh
*mesh
)
697 i32
*polygon_edge_map
= malloc(mesh
->abedges
.count
*2 *sizeof(i32
));
699 for( int i
= 0; i
< mesh
->abedges
.count
*2; i
++ )
700 polygon_edge_map
[i
] = -1;
702 for( int i
= 0; i
< mesh
->abpolys
.count
; i
++ )
704 cxr_polygon
*poly
= &mesh
->polys
[i
];
706 for( int j
= 0; j
< poly
->loop_total
; j
++ )
708 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
711 for( int k
= 0; k
< 2; k
++ )
713 i32
*edge
= &polygon_edge_map
[loop
->edge_index
*2+k
];
722 /* Overflowed edge mapping... Duplicated faces. */
723 free( polygon_edge_map
);
729 for( int i
= 0; i
< mesh
->abpolys
.count
; i
++ )
731 cxr_polygon
*poly
= &mesh
->polys
[i
];
733 for( int j
= 0; j
< poly
->loop_total
; j
++ )
735 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
737 i32
*face_map
= &polygon_edge_map
[ loop
->edge_index
*2 ];
739 if( face_map
[0] == loop
->poly_left
) loop
->poly_right
= face_map
[1];
740 else loop
->poly_right
= face_map
[0];
745 for( int i
=0; i
<mesh
->abedges
.count
*2; i
++ )
747 if( polygon_edge_map
[i
] == -1 )
749 free( polygon_edge_map
);
754 free( polygon_edge_map
);
759 * Create new empty polygon with known loop count
760 * Must be filled and completed by the following functions!
762 static int cxr_create_poly( cxr_mesh
*mesh
, int loop_count
)
764 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
769 cxr_log( "tried to add new poly with length %d!\n", loop_count
);
774 cxr_ab_reserve( &mesh
->abpolys
, 1 );
775 cxr_ab_reserve( &mesh
->abloops
, loop_count
);
776 cxr_mesh_update( mesh
);
778 cxr_polygon
*poly
= &mesh
->polys
[ mesh
->abpolys
.count
];
780 poly
->loop_start
= mesh
->abloops
.count
;
781 poly
->loop_total
= 0;
782 poly
->material_id
= -1;
783 v3_zero( poly
->center
);
789 * Add one index to the polygon created by the above function
791 static void cxr_poly_push_index( cxr_mesh
*mesh
, int id
)
793 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
795 int nface_id
= mesh
->abpolys
.count
;
796 cxr_polygon
*poly
= &mesh
->polys
[ nface_id
];
798 cxr_loop
*new_loop
= &mesh
->loops
[ poly
->loop_start
+ poly
->loop_total
];
800 new_loop
->poly_left
= nface_id
;
801 new_loop
->poly_right
= -1;
802 new_loop
->index
= id
;
803 new_loop
->edge_index
= 0;
804 v2_zero(new_loop
->uv
);
806 v3_add( poly
->center
, verts
[new_loop
->index
], poly
->center
);
809 mesh
->abloops
.count
++;
813 * Finalize and commit polygon into mesh
815 static void cxr_poly_finish( cxr_mesh
*mesh
)
817 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
819 int nface_id
= mesh
->abpolys
.count
;
820 cxr_polygon
*poly
= &mesh
->polys
[nface_id
];
822 /* Average center and calc normal */
824 v3_divs( poly
->center
, poly
->loop_total
, poly
->center
);
825 cxr_loop
*lp0
= &mesh
->loops
[ poly
->loop_start
],
826 *lp1
= &mesh
->loops
[ poly
->loop_start
+1 ],
827 *lp2
= &mesh
->loops
[ poly
->loop_start
+2 ];
830 verts
[lp0
->index
], verts
[lp1
->index
], verts
[lp2
->index
], poly
->normal
);
832 mesh
->abpolys
.count
++;
836 * Extract the next island from mesh
838 * Returns NULL if mesh is one contigous object
840 static cxr_mesh
*cxr_pull_island( cxr_mesh
*mesh
)
842 cxr_mesh_link_loops(mesh
);
844 int *island_current
= malloc(mesh
->abpolys
.count
*sizeof(int)),
849 island_current
[0] = 0;
852 last_count
= island_len
;
854 for( int i
=0; i
<island_len
; i
++ )
856 cxr_polygon
*poly
= &mesh
->polys
[ island_current
[i
] ];
858 for( int j
=0; j
<poly
->loop_total
; j
++ )
860 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
862 if( loop
->poly_right
!= -1 )
864 int face_present
= 0;
866 for( int k
=0; k
<island_len
; k
++ )
868 if( island_current
[k
] == loop
->poly_right
)
876 island_current
[ island_len
++ ] = loop
->poly_right
;
881 if( island_len
> last_count
)
884 /* Check for complete object */
885 if( island_len
== mesh
->abpolys
.count
)
887 free( island_current
);
891 for( int i
=0; i
<island_len
; i
++ )
893 cxr_polygon
*poly
= &mesh
->polys
[ island_current
[i
] ];
894 loop_count
+= poly
->loop_total
;
897 /* Create and update meshes */
898 cxr_mesh
*newmesh
= cxr_alloc_mesh( mesh
->abedges
.count
,
903 for( int i
=0; i
<island_len
; i
++ )
905 cxr_polygon
*src
= &mesh
->polys
[ island_current
[i
] ];
906 cxr_polygon
*dst
= cxr_ab_ptr(&newmesh
->abpolys
, i
);
909 dst
->loop_start
= newmesh
->abloops
.count
;
911 for( int j
=0; j
<src
->loop_total
; j
++ )
914 *lsrc
= &mesh
->loops
[ src
->loop_start
+j
],
915 *ldst
= cxr_ab_ptr(&newmesh
->abloops
, dst
->loop_start
+j
);
919 ldst
->poly_right
= -1;
922 newmesh
->abloops
.count
+= src
->loop_total
;
923 src
->loop_total
= -1;
926 newmesh
->abpolys
.count
= island_len
;
927 newmesh
->abedges
.count
= mesh
->abedges
.count
;
928 memcpy( cxr_ab_ptr(&newmesh
->abedges
,0),
930 mesh
->abedges
.count
* sizeof(cxr_edge
));
932 cxr_mesh_clean_faces(mesh
);
933 cxr_mesh_clean_edges(mesh
);
934 cxr_mesh_clean_edges(newmesh
);
936 free( island_current
);
941 * Invalid solid is when there are vertices that are coplanar to a face, but are
942 * outside the polygons edges.
944 static int cxr_valid_solid( cxr_mesh
*mesh
, int *solid
, int len
)
946 v3f
*verts
= cxr_ab_ptr(mesh
->p_abverts
, 0);
948 for( int i
=0; i
<len
; i
++ )
950 cxr_polygon
*polyi
= &mesh
->polys
[ solid
[i
] ];
953 normal_to_plane(polyi
->normal
, polyi
->center
, plane
);
955 for( int j
=0; j
<len
; j
++ )
959 cxr_polygon
*polyj
= &mesh
->polys
[ solid
[j
] ];
961 for( int k
=0; k
<polyj
->loop_total
; k
++ )
963 cxr_loop
*lpj
= &mesh
->loops
[ polyj
->loop_start
+k
];
965 /* Test if the vertex is not referenced by the polygon */
966 for( int l
=0; l
<polyi
->loop_total
; l
++ )
968 cxr_loop
*lpi
= &mesh
->loops
[ polyi
->loop_start
+l
];
970 if( lpi
->index
== lpj
->index
)
974 if( fabs(plane_polarity(plane
, verts
[lpj
->index
])) < 0.001 )
986 * Use when iterating the loops array, to get a unique set of edges
987 * Better than using the edges array and doing many more checks
989 static int cxr_loop_unique_edge( cxr_loop
*lp
)
991 if( lp
->poly_left
> lp
->poly_right
)
998 * Identify edges in the mesh where the two connected face's normals
999 * are opposing eachother (or close to identical)
1001 static int *cxr_mesh_reflex_edges( cxr_mesh
*mesh
)
1003 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1004 int *edge_tagged
= malloc( mesh
->abedges
.count
* sizeof(int) );
1006 for( int i
=0; i
<mesh
->abloops
.count
; i
++ )
1008 cxr_loop
*lp
= &mesh
->loops
[i
];
1009 if( !cxr_loop_unique_edge( lp
) ) continue;
1011 edge_tagged
[lp
->edge_index
] = 0;
1013 cxr_polygon
*polya
= &mesh
->polys
[ lp
->poly_left
],
1014 *polyb
= &mesh
->polys
[ lp
->poly_right
];
1017 normal_to_plane(polyb
->normal
, polyb
->center
, planeb
);
1019 for( int j
=0; j
<polya
->loop_total
; j
++ )
1021 cxr_loop
*lp1
= &mesh
->loops
[ polya
->loop_start
+j
];
1023 if(( plane_polarity( planeb
, verts
[lp1
->index
] ) > 0.001 ) ||
1024 ( v3_dot(polya
->normal
,polyb
->normal
) > CXR_PLANE_SIMILARITY_MAX
))
1026 edge_tagged
[lp
->edge_index
] = 1;
1036 * Same logic as above function except it will apply it to each vertex
1038 static int *cxr_mesh_reflex_vertices( cxr_mesh
*mesh
)
1040 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1042 int *vertex_tagged
= malloc( mesh
->p_abverts
->count
*sizeof(int) );
1043 int *connected_planes
= malloc( mesh
->abpolys
.count
*sizeof(int) );
1045 for( int i
=0; i
<mesh
->p_abverts
->count
; i
++ )
1048 int num_connected
= 0;
1050 /* Create a list of polygons that refer to this vertex */
1051 for( int j
=0; j
<mesh
->abpolys
.count
; j
++ )
1053 cxr_polygon
*poly
= &mesh
->polys
[j
];
1054 for( int k
=0; k
<poly
->loop_total
; k
++ )
1056 cxr_loop
*loop
= &mesh
->loops
[poly
->loop_start
+k
];
1057 if( loop
->index
== i
)
1059 connected_planes
[num_connected
++] = j
;
1065 /* Check all combinations for a similar normal */
1066 for( int j
=0; j
<num_connected
-1; j
++ )
1068 for( int k
=j
+1; k
<num_connected
; k
++ )
1070 cxr_polygon
*polyj
= &mesh
->polys
[connected_planes
[j
]],
1071 *polyk
= &mesh
->polys
[connected_planes
[k
]];
1073 if( v3_dot(polyj
->normal
,polyk
->normal
) > CXR_PLANE_SIMILARITY_MAX
)
1079 * Check if all connected planes either:
1081 * - Coplanar with it
1083 for( int j
=0; j
<num_connected
; j
++ )
1085 for( int k
=j
+1; k
<num_connected
; k
++ )
1087 cxr_polygon
*jpoly
= &mesh
->polys
[ connected_planes
[j
] ],
1088 *kpoly
= &mesh
->polys
[ connected_planes
[k
] ];
1091 normal_to_plane( kpoly
->normal
, kpoly
->center
, plane
);
1092 for( int l
=0; l
<jpoly
->loop_total
; l
++ )
1094 cxr_loop
*lp
= &mesh
->loops
[ jpoly
->loop_start
+l
];
1096 if( plane_polarity( plane
, verts
[lp
->index
] ) > 0.001 )
1104 vertex_tagged
[i
] = 1;
1107 free( connected_planes
);
1108 return vertex_tagged
;
1112 * Detect if potential future edges create a collision with any of the
1113 * existing edges in the mesh
1115 static int cxr_solid_overlap( cxr_mesh
*mesh
,
1118 int common_edge_index
1120 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1121 cxr_edge
*common_edge
= &mesh
->edges
[common_edge_index
];
1123 int unique_a
= pa
->loop_total
-2,
1124 unique_b
= pb
->loop_total
-2;
1126 int *unique_verts
= malloc( (unique_a
+unique_b
)*sizeof(int) );
1127 int unique_total
= 0;
1129 for( int j
=0; j
<2; j
++ )
1131 cxr_polygon
*poly
= (cxr_polygon
*[2]){pa
,pb
}[j
];
1133 for( int i
=0; i
<poly
->loop_total
; i
++ )
1135 cxr_loop
*lp
= &mesh
->loops
[poly
->loop_start
+i
];
1137 if( lp
->index
== common_edge
->i0
|| lp
->index
== common_edge
->i1
)
1140 unique_verts
[ unique_total
++ ] = lp
->index
;
1146 for( int i
=0; i
<unique_a
; i
++ )
1148 for( int j
=unique_a
; j
<unique_total
; j
++ )
1150 int i0
= unique_verts
[i
],
1151 i1
= unique_verts
[j
];
1153 for( int k
=0; k
<mesh
->abedges
.count
; k
++ )
1155 cxr_edge
*edge
= &mesh
->edges
[k
];
1157 if( edge
->i0
== i0
|| edge
->i0
== i1
||
1158 edge
->i1
== i0
|| edge
->i1
== i1
) continue;
1160 double *a0
= verts
[i0
],
1162 *b0
= verts
[edge
->i0
],
1163 *b1
= verts
[edge
->i1
];
1165 double dist
= segment_segment_dist( a0
, a1
, b0
, b1
, ca
, cb
);
1169 free( unique_verts
);
1176 free( unique_verts
);
1181 * Creates the 'maximal' solid that originates from this faceid
1183 * Returns the number of faces used
1185 static int cxr_buildsolid(
1192 faces_tagged
[faceid
] = faceid
;
1195 solid
[solid_len
++] = faceid
;
1197 int search_start
= 0;
1202 for( int j
=search_start
; j
<solid_len
; j
++ )
1204 cxr_polygon
*poly
= &mesh
->polys
[ solid
[j
] ];
1206 for( int k
=0; k
<poly
->loop_total
; k
++ )
1208 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+k
];
1209 cxr_edge
*edge
= &mesh
->edges
[ loop
->edge_index
];
1211 if( faces_tagged
[ loop
->poly_right
] == -1 )
1213 if( !reflex_edges
[loop
->edge_index
] )
1215 /* Check for dodgy edges */
1216 cxr_polygon
*newpoly
= &mesh
->polys
[loop
->poly_right
];
1218 if( cxr_solid_overlap(mesh
,poly
,newpoly
,loop
->edge_index
))
1221 /* Looking ahead by one step gives us an early out for invalid
1222 * configurations. This might just all be handled by the new
1223 * edge overlap detector, though.
1225 for( int l
=0; l
< newpoly
->loop_total
; l
++ )
1227 cxr_loop
*lp1
= &mesh
->loops
[ newpoly
->loop_start
+l
];
1228 cxr_polygon
*future_face
= &mesh
->polys
[ lp1
->poly_right
];
1230 if( reflex_edges
[ lp1
->edge_index
]
1231 || lp1
->poly_right
== loop
->poly_right
)
1234 for( int m
=0; m
<solid_len
; m
++ )
1235 if( solid
[m
] == lp1
->poly_right
)
1238 for( int m
=0; m
<solid_len
; m
++ )
1240 cxr_polygon
*polym
= &mesh
->polys
[solid
[m
]];
1241 double pdist
= v3_dot( polym
->normal
,future_face
->normal
);
1243 if( pdist
> CXR_PLANE_SIMILARITY_MAX
)
1250 /* Check for vertices in the new polygon that exist on a current
1251 * plane. This condition is invalid */
1252 solid
[ solid_len
] = loop
->poly_right
;
1254 if( cxr_valid_solid(mesh
,solid
,solid_len
+1 ) )
1256 faces_tagged
[ loop
->poly_right
] = faceid
;
1266 search_start
= solid_len
;
1268 goto search_iterate
;
1275 int start
, count
, edge_count
;
1279 struct temp_manifold
1281 struct manifold_loop
1291 enum manifold_status
1295 k_manifold_fragmented
,
1296 k_manifold_complete
,
1302 * Create polygon from entire manifold structure.
1304 * Must be completely co-planar
1306 static void cxr_create_poly_full( cxr_mesh
*mesh
, struct temp_manifold
*src
)
1308 if( cxr_create_poly( mesh
, src
->loop_count
) )
1310 for( int l
=0; l
<src
->loop_count
; l
++ )
1311 cxr_poly_push_index( mesh
, src
->loops
[ l
].loop
.index
);
1313 cxr_poly_finish( mesh
);
1318 * Links up all edges into a potential new manifold
1320 * The return status can be:
1321 * (err): Critical programming error
1322 * none: No manifold to create
1323 * fragmented: Multiple sections exist, not just one
1324 * complete: Optimial manifold was created
1326 static void cxr_link_manifold(
1328 struct csolid
*solid
,
1330 struct temp_manifold
*manifold
1332 cxr_loop
**edge_list
= malloc( sizeof(*edge_list
) * solid
->edge_count
);
1333 int *temp_solid
= malloc( solid
->count
*sizeof(int) );
1334 int temp_solid_len
= 0;
1336 int init_reverse
= 0;
1337 int unique_edge_count
= 0;
1339 /* Try remove splitting faces first */
1341 int split_total
= 0;
1342 for( int j
=0; j
<solid
->count
; j
++ )
1344 cxr_polygon
*poly
= &mesh
->polys
[ solid_buffer
[solid
->start
+j
] ];
1345 int interior_count
= 0;
1347 for( int k
=0; k
<poly
->loop_total
; k
++ )
1349 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+k
];
1351 for( int l
=0; l
<solid
->count
; l
++ )
1352 if( loop
->poly_right
== solid_buffer
[solid
->start
+l
] )
1361 if( interior_count
< poly
->loop_total
-1 )
1367 temp_solid
[ temp_solid_len
++ ] = solid_buffer
[solid
->start
+j
];
1370 if( temp_solid_len
< 3 || (split_total
& 0x2) /* unkown reasons */ )
1375 /* Overwrite original solid */
1376 for( int j
=0; j
<temp_solid_len
; j
++ )
1377 solid_buffer
[ solid
->start
+j
] = temp_solid
[ j
];
1379 solid
->count
= temp_solid_len
;
1385 for( int j
=0; j
<solid
->count
; j
++ )
1387 cxr_polygon
*poly
= &mesh
->polys
[ solid_buffer
[solid
->start
+j
] ];
1389 /* when discarding, if a face has only one loop that points outwards,
1393 for( int k
=0; k
<poly
->loop_total
; k
++ )
1395 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+k
];
1397 for( int l
=0; l
<unique_edge_count
; l
++ )
1398 if( edge_list
[l
]->edge_index
== loop
->edge_index
)
1401 for( int l
=0; l
<solid
->count
; l
++ )
1402 if( loop
->poly_right
== solid_buffer
[solid
->start
+l
] )
1405 edge_list
[ unique_edge_count
] = loop
;
1407 if( unique_edge_count
== 0 )
1409 cxr_edge
*edgeptr
= &mesh
->edges
[ loop
->edge_index
];
1410 if( edgeptr
->i1
== loop
->index
)
1414 unique_edge_count
++;
1419 if( unique_edge_count
== 0 )
1422 manifold
->status
= k_manifold_none
;
1426 /* Link edges together to form manifold */
1427 manifold
->loops
= malloc( solid
->edge_count
*sizeof(struct manifold_loop
));
1428 manifold
->split_count
= 0;
1429 manifold
->loop_count
= 0;
1431 cxr_edge
*current
= &mesh
->edges
[ edge_list
[0]->edge_index
];
1433 int endpt
= (!init_reverse
)? current
->i0
: current
->i1
,
1435 curface
= edge_list
[0]->poly_left
;
1438 for( int j
=0; j
<unique_edge_count
; j
++ )
1440 cxr_edge
*other
= &mesh
->edges
[ edge_list
[j
]->edge_index
];
1441 if( other
== current
)
1444 if( other
->i0
== endpt
|| other
->i1
== endpt
)
1449 if( other
->i0
== endpt
) endpt
= current
->i1
;
1450 else endpt
= current
->i0
;
1452 struct manifold_loop
*ml
= &manifold
->loops
[ manifold
->loop_count
++ ];
1454 if( curface
==edge_list
[j
]->poly_left
)
1457 manifold
->split_count
++;
1462 ml
->loop
.edge_index
= edge_list
[j
]->edge_index
;
1463 ml
->loop
.poly_left
= edge_list
[j
]->poly_left
;
1464 ml
->loop
.index
= lastpt
;
1465 ml
->loop
.poly_right
= edge_list
[j
]->poly_right
;
1467 curface
= edge_list
[j
]->poly_left
;
1471 if( manifold
->loop_count
< unique_edge_count
)
1472 manifold
->status
= k_manifold_fragmented
;
1474 manifold
->status
= k_manifold_complete
;
1476 goto manifold_complete
;
1479 goto manifold_continue
;
1483 /* Incomplete links */
1484 manifold
->status
= k_manifold_err
;
1493 * Reconstruct implied internal geometry where the manifold doesn't have
1494 * enough information (vertices) to create a full result.
1496 static int cxr_build_implicit_geo( cxr_mesh
*mesh
, int new_polys
, int start
)
1498 for( int i
=0; i
<new_polys
-2; i
++ )
1500 for( int j
=i
+1; j
<new_polys
-1; j
++ )
1502 for( int k
=j
+1; k
<new_polys
; k
++ )
1504 cxr_polygon
*ptri
= &mesh
->polys
[ start
+i
],
1505 *ptrj
= &mesh
->polys
[ start
+j
],
1506 *ptrk
= &mesh
->polys
[ start
+k
];
1508 v4f planei
, planej
, planek
;
1509 normal_to_plane(ptri
->normal
,ptri
->center
,planei
);
1510 normal_to_plane(ptrj
->normal
,ptrj
->center
,planej
);
1511 normal_to_plane(ptrk
->normal
,ptrk
->center
,planek
);
1515 if( plane_intersect(planei
,planej
,planek
,intersect
) )
1517 /* Make sure the point is inside the convex region */
1519 int point_valid
= 1;
1520 for( int l
=0; l
<mesh
->abpolys
.count
; l
++ )
1522 cxr_polygon
*ptrl
= &mesh
->polys
[l
];
1525 normal_to_plane(ptrl
->normal
, ptrl
->center
, planel
);
1527 if( plane_polarity( planel
, intersect
) > 0.01 )
1530 cxr_log( "degen vert, planes %d, %d, %d [max:%d]\n",
1533 cxr_debug_poly( mesh
, ptri
, colours_random
[3] );
1534 cxr_debug_poly( mesh
, ptrj
, colours_random
[1] );
1535 cxr_debug_poly( mesh
, ptrk
, colours_random
[2] );
1542 /* Extend faces to include this vert */
1544 int nvertid
= mesh
->p_abverts
->count
;
1545 cxr_ab_push( mesh
->p_abverts
, intersect
);
1547 ptrj
->loop_start
+= 1;
1548 ptrk
->loop_start
+= 2;
1550 cxr_ab_reserve( &mesh
->abloops
, 3);
1552 int newi
= ptri
->loop_start
+ptri
->loop_total
,
1553 newj
= ptrj
->loop_start
+ptrj
->loop_total
,
1554 newk
= ptrk
->loop_start
+ptrk
->loop_total
;
1557 *lloopi
= cxr_ab_empty_at(&mesh
->abloops
, newi
),
1558 *lloopj
= cxr_ab_empty_at(&mesh
->abloops
, newj
),
1559 *lloopk
= cxr_ab_empty_at(&mesh
->abloops
, newk
);
1561 lloopi
->index
= nvertid
;
1562 lloopi
->edge_index
= 0;
1563 lloopi
->poly_left
= start
+ i
;
1564 lloopi
->poly_right
= -1;
1566 lloopj
->index
= nvertid
;
1567 lloopj
->poly_left
= start
+ j
;
1568 lloopj
->edge_index
= 0;
1569 lloopj
->poly_right
= -1;
1571 lloopk
->index
= nvertid
;
1572 lloopk
->edge_index
= 0;
1573 lloopk
->poly_left
= start
+ k
;
1574 lloopk
->poly_right
= -1;
1576 v2_zero(lloopi
->uv
);
1577 v2_zero(lloopj
->uv
);
1578 v2_zero(lloopk
->uv
);
1580 ptri
->loop_total
++;
1581 ptrj
->loop_total
++;
1582 ptrk
->loop_total
++;
1584 double qi
= 1.0/(double)ptri
->loop_total
,
1585 qj
= 1.0/(double)ptrj
->loop_total
,
1586 qk
= 1.0/(double)ptrk
->loop_total
;
1588 /* Adjust centers of faces */
1589 v3_lerp( ptri
->center
, intersect
, qi
, ptri
->center
);
1590 v3_lerp( ptrj
->center
, intersect
, qj
, ptrj
->center
);
1591 v3_lerp( ptrk
->center
, intersect
, qk
, ptrk
->center
);
1600 static int cxr_reflex_err( cxr_mesh
*mesh
)
1603 int *reflex_check
= cxr_mesh_reflex_edges( mesh
);
1605 v3f
*temp
= cxr_ab_ptr(mesh
->p_abverts
, 0);
1607 for( int i
=0; i
<mesh
->abedges
.count
; i
++ )
1609 if( reflex_check
[i
] )
1611 cxr_debug_line( temp
[mesh
->edges
[i
].i0
],
1612 temp
[mesh
->edges
[i
].i1
],
1618 free( reflex_check
);
1622 static int cxr_non_manifold_err( cxr_mesh
*mesh
)
1624 if( !cxr_mesh_link_loops(mesh
) )
1627 cxr_log( "non-manifold edges are in the mesh: "
1628 "implicit internal geometry does not have full support\n" );
1630 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
1632 for( int i
=0; i
<mesh
->abloops
.count
; i
++ )
1634 cxr_loop
*lp
= &mesh
->loops
[i
];
1635 cxr_edge
*edge
= &mesh
->edges
[lp
->edge_index
];
1636 cxr_debug_line( verts
[edge
->i0
], verts
[edge
->i1
], colours_random
[1] );
1638 if( lp
->poly_left
== -1 || lp
->poly_right
== -1 )
1640 cxr_debug_line( verts
[edge
->i0
], verts
[edge
->i1
], colour_error
);
1651 * Convexer's main algorithm
1653 * Return the best availible convex solid from mesh, and patch the existing mesh
1654 * to fill the gap where the new mesh left it.
1656 * Returns NULL if shape is already convex or empty.
1657 * This function will not preserve edge data such as freestyle, sharp etc.
1659 static cxr_mesh
*cxr_pull_best_solid(
1661 int preserve_more_edges
,
1662 enum cxr_soliderr
*err
)
1664 *err
= k_soliderr_none
;
1666 if( cxr_non_manifold_err( mesh
) )
1668 *err
= k_soliderr_non_manifold
;
1672 int *edge_tagged
= cxr_mesh_reflex_edges( mesh
);
1673 int *vertex_tagged
= cxr_mesh_reflex_vertices( mesh
);
1676 * Connect all marked vertices that share an edge
1679 int *edge_important
= malloc(mesh
->abedges
.count
*sizeof(int));
1680 for( int i
=0; i
< mesh
->abedges
.count
; i
++ )
1681 edge_important
[i
] = 0;
1683 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
1685 cxr_polygon
*poly
= &mesh
->polys
[i
];
1686 int not_tagged
= -1,
1689 for( int j
=0; j
<poly
->loop_total
; j
++ )
1691 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
1693 if( !edge_tagged
[ loop
->edge_index
] )
1695 if( not_tagged
== -1 )
1696 not_tagged
= loop
->edge_index
;
1698 goto edge_unimportant
;
1702 if( not_tagged
!= -1 )
1703 edge_important
[not_tagged
]=1;
1709 * Connect edges where both vertices are reflex, only if we are not
1712 for( int i
=0; i
<mesh
->abedges
.count
; i
++ )
1714 if( edge_important
[i
] && preserve_more_edges
) continue;
1716 cxr_edge
*edge
= &mesh
->edges
[i
];
1717 if( vertex_tagged
[edge
->i0
] && vertex_tagged
[edge
->i1
] )
1721 free( edge_important
);
1723 int *faces_tagged
= malloc(mesh
->abpolys
.count
*sizeof(int));
1724 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
1725 faces_tagged
[i
] = -1;
1727 struct csolid
*candidates
;
1728 int *solid_buffer
= malloc( mesh
->abpolys
.count
*sizeof(int) ),
1729 solid_buffer_len
= 0,
1730 candidate_count
= 0;
1732 candidates
= malloc( mesh
->abpolys
.count
*sizeof(struct csolid
) );
1735 * Create a valid, non-overlapping solid for every face present in the mesh
1737 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
1739 if( faces_tagged
[i
] != -1 ) continue;
1740 faces_tagged
[i
] = i
;
1742 int *solid
= &solid_buffer
[ solid_buffer_len
];
1743 int len
= cxr_buildsolid( mesh
, i
, solid
, edge_tagged
, faces_tagged
);
1746 struct csolid
*csolid
= &candidates
[candidate_count
++];
1747 csolid
->start
= solid_buffer_len
;
1748 csolid
->count
= len
;
1749 csolid
->edge_count
= 0;
1751 v3_zero( csolid
->center
);
1752 for( int j
=0; j
<len
; j
++ )
1754 cxr_polygon
*polyj
= &mesh
->polys
[ solid
[j
] ];
1755 v3_add( polyj
->center
, csolid
->center
, csolid
->center
);
1756 csolid
->edge_count
+= polyj
->loop_total
;
1758 v3_divs( csolid
->center
, len
, csolid
->center
);
1759 solid_buffer_len
+= len
;
1762 free( edge_tagged
);
1763 free( vertex_tagged
);
1764 free( faces_tagged
);
1767 * Choosing the best solid: most defined manifold
1769 struct csolid
*best_solid
= NULL
;
1770 int fewest_manifold_splits
= INT32_MAX
;
1772 struct temp_manifold best_manifold
= { .loops
= NULL
, .loop_count
= 0 };
1773 int max_solid_faces
= 0;
1775 for( int i
=0; i
<candidate_count
; i
++ )
1777 struct csolid
*solid
= &candidates
[i
];
1778 max_solid_faces
= cxr_max(max_solid_faces
,solid
->count
);
1780 if( solid
->count
<= 2 )
1783 struct temp_manifold manifold
;
1784 cxr_link_manifold( mesh
, solid
, solid_buffer
, &manifold
);
1786 if( manifold
.status
== k_manifold_err
)
1788 *err
= k_soliderr_bad_manifold
;
1792 free(manifold
.loops
);
1793 free(best_manifold
.loops
);
1797 if( manifold
.status
== k_manifold_complete
)
1799 if( manifold
.split_count
< fewest_manifold_splits
)
1801 fewest_manifold_splits
= manifold
.split_count
;
1804 free( best_manifold
.loops
);
1805 best_manifold
= manifold
;
1810 if( manifold
.status
!= k_manifold_none
)
1811 free( manifold
.loops
);
1814 if( max_solid_faces
< 2 )
1816 *err
= k_soliderr_no_solids
;
1819 free(best_manifold
.loops
);
1823 if( best_solid
!= NULL
)
1825 cxr_mesh
*pullmesh
= cxr_alloc_mesh( best_solid
->edge_count
,
1826 best_solid
->edge_count
,
1830 for( int i
=0; i
<best_solid
->count
; i
++ )
1832 int nface_id
= pullmesh
->abpolys
.count
;
1833 int exist_plane_id
= solid_buffer
[best_solid
->start
+i
];
1835 cxr_polygon
*exist_face
= &mesh
->polys
[ exist_plane_id
],
1836 *new_face
= cxr_ab_empty( &pullmesh
->abpolys
);
1838 *new_face
= *exist_face
;
1839 new_face
->loop_start
= pullmesh
->abloops
.count
;
1841 for( int j
=0; j
<exist_face
->loop_total
; j
++ )
1843 cxr_loop
*exist_loop
= &mesh
->loops
[ exist_face
->loop_start
+j
],
1844 *new_loop
= cxr_ab_empty(&pullmesh
->abloops
);
1846 new_loop
->index
= exist_loop
->index
;
1847 new_loop
->poly_left
= nface_id
;
1848 new_loop
->poly_right
= -1;
1849 new_loop
->edge_index
= 0;
1850 v2_copy( exist_loop
->uv
, new_loop
->uv
);
1853 exist_face
->loop_total
= -1;
1857 int pullmesh_new_start
= pullmesh
->abpolys
.count
;
1859 if( fewest_manifold_splits
!= 0 )
1861 /* Unusual observation:
1862 * If the split count is odd, the manifold can be created easily
1864 * If it is even, implicit internal geometry is needed to be
1865 * constructed. So the manifold gets folded as we create it segment
1868 * I'm not sure if this is a well defined rule of geometry, but seems
1869 * to apply to the data we care about.
1871 int collapse_used_segments
= (u32
)fewest_manifold_splits
& 0x1? 0: 1;
1875 for( int j
=0; j
< best_manifold
.loop_count
; j
++ )
1877 if( !best_manifold
.loops
[j
].split
) continue;
1879 cxr_loop
*loop
= &best_manifold
.loops
[j
].loop
;
1881 for( int k
=1; k
< best_manifold
.loop_count
; k
++ )
1883 int index1
= cxr_range(j
+k
, best_manifold
.loop_count
);
1884 cxr_loop
*loop1
= &best_manifold
.loops
[index1
].loop
;
1886 if( best_manifold
.loops
[index1
].split
)
1893 if( new_polys
> best_manifold
.loop_count
)
1896 cxr_log( "Programming error: Too many new polys!\n" );
1901 if( cxr_create_poly( pullmesh
, k
+1 ) )
1903 for( int l
=0; l
<k
+1; l
++ )
1905 int i0
= cxr_range(j
+l
, best_manifold
.loop_count
),
1906 index
= best_manifold
.loops
[ i0
].loop
.index
;
1908 cxr_poly_push_index( pullmesh
, index
);
1910 cxr_poly_finish( pullmesh
);
1913 /* Collapse down manifold */
1914 if( collapse_used_segments
)
1916 best_manifold
.loops
[j
].split
= 0;
1917 best_manifold
.loops
[index1
].split
= 0;
1919 int new_length
= (best_manifold
.loop_count
-(k
-1));
1921 struct temp_manifold new_manifold
= {
1922 .loop_count
= new_length
1924 new_manifold
.loops
=
1925 malloc( new_length
*sizeof(*new_manifold
.loops
) );
1927 for( int l
=0; l
<new_length
; l
++ )
1929 int i_src
= cxr_range( j
+k
+l
, best_manifold
.loop_count
);
1930 new_manifold
.loops
[l
] = best_manifold
.loops
[i_src
];
1933 free( best_manifold
.loops
);
1934 best_manifold
= new_manifold
;
1936 goto manifold_repeat
;
1945 if( best_manifold
.loop_count
&& collapse_used_segments
)
1947 cxr_create_poly_full( pullmesh
, &best_manifold
);
1953 cxr_create_poly_full( pullmesh
, &best_manifold
);
1957 if( new_polys
>= 3 )
1959 if( !cxr_build_implicit_geo( pullmesh
, new_polys
, pullmesh_new_start
))
1963 free(best_manifold
.loops
);
1965 cxr_free_mesh( pullmesh
);
1966 *err
= k_soliderr_degenerate_implicit
;
1972 * Copy faces from the pullmesh into original, to patch up where there
1973 * would be gaps created
1975 for( int i
=0; i
<new_polys
; i
++ )
1977 int rface_id
= mesh
->abpolys
.count
;
1978 cxr_polygon
*pface
= &pullmesh
->polys
[pullmesh_new_start
+i
],
1979 *rip_face
= cxr_ab_empty(&mesh
->abpolys
);
1981 rip_face
->loop_start
= mesh
->abloops
.count
;
1982 rip_face
->loop_total
= pface
->loop_total
;
1983 rip_face
->material_id
= -1;
1985 for( int j
=0; j
<rip_face
->loop_total
; j
++ )
1988 &pullmesh
->loops
[ pface
->loop_start
+pface
->loop_total
-j
-1 ],
1989 *rloop
= cxr_ab_empty(&mesh
->abloops
);
1991 rloop
->index
= ploop
->index
;
1992 rloop
->poly_left
= rface_id
;
1993 rloop
->poly_right
= -1;
1994 rloop
->edge_index
= 0;
1995 v2_copy( ploop
->uv
, rloop
->uv
);
1998 v3_copy( pface
->center
, rip_face
->center
);
1999 v3_negate( pface
->normal
, rip_face
->normal
);
2002 cxr_mesh_update( mesh
);
2003 cxr_mesh_update( pullmesh
);
2005 cxr_mesh_clean_faces( mesh
);
2006 cxr_mesh_clean_edges( mesh
);
2007 cxr_mesh_clean_faces( pullmesh
);
2008 cxr_mesh_clean_edges( pullmesh
);
2012 free(best_manifold
.loops
);
2015 * Do final checks on the mesh to make sure we diddn't introduce any
2018 if( cxr_non_manifold_err( pullmesh
) || cxr_reflex_err( pullmesh
) )
2020 *err
= k_soliderr_bad_result
;
2029 free(best_manifold
.loops
);
2031 if( cxr_non_manifold_err( mesh
) || cxr_reflex_err( mesh
) )
2032 *err
= k_soliderr_bad_result
;
2038 * Convert from the format we recieve from blender into our internal format
2039 * with auto buffers.
2041 static cxr_mesh
*cxr_to_internal_format(
2042 cxr_static_mesh
*src
,
2043 cxr_abuffer
*abverts
2045 cxr_mesh
*mesh
= cxr_alloc_mesh( src
->edge_count
, src
->loop_count
,
2046 src
->poly_count
, abverts
);
2048 cxr_ab_init( abverts
, sizeof(v3f
), src
->vertex_count
);
2050 memcpy( mesh
->abedges
.arr
, src
->edges
, src
->edge_count
*sizeof(cxr_edge
));
2051 memcpy( mesh
->abpolys
.arr
, src
->polys
, src
->poly_count
*sizeof(cxr_polygon
));
2052 memcpy( abverts
->arr
, src
->vertices
, src
->vertex_count
*sizeof(v3f
));
2053 mesh
->abedges
.count
= src
->edge_count
;
2054 mesh
->abloops
.count
= src
->loop_count
;
2055 mesh
->abpolys
.count
= src
->poly_count
;
2057 cxr_mesh_update( mesh
);
2059 for( int i
=0; i
<src
->loop_count
; i
++ )
2061 cxr_loop
*lp
= &mesh
->loops
[i
];
2063 lp
->index
= src
->loops
[i
].index
;
2064 lp
->edge_index
= src
->loops
[i
].edge_index
;
2065 v2_copy( src
->loops
[i
].uv
, lp
->uv
);
2068 abverts
->count
= src
->vertex_count
;
2072 static int cxr_poly_convex( cxr_mesh
*mesh
, cxr_polygon
*poly
)
2074 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2076 for( int i
=0; i
<poly
->loop_total
; i
++ )
2078 int li0
= poly
->loop_start
+ i
,
2079 li1
= poly
->loop_start
+ cxr_range( i
+1, poly
->loop_total
),
2080 li2
= poly
->loop_start
+ cxr_range( i
+2, poly
->loop_total
);
2081 int i0
= mesh
->loops
[li0
].index
,
2082 i1
= mesh
->loops
[li1
].index
,
2083 i2
= mesh
->loops
[li2
].index
;
2087 v3_sub( verts
[i1
], verts
[i0
], v0
);
2088 v3_sub( verts
[i2
], verts
[i1
], v1
);
2090 v3_cross( v0
, v1
, c
);
2091 if( v3_dot( c
, poly
->normal
) <= 0.0 )
2094 cxr_debug_line( verts
[i0
], verts
[i1
], colour_error
);
2095 cxr_debug_box( verts
[i1
], 0.1, colour_error
);
2096 cxr_debug_line( verts
[i1
], verts
[i2
], colour_error
);
2097 cxr_debug_line( verts
[i1
], poly
->center
, colour_error
);
2106 static int cxr_solid_checkerr( cxr_mesh
*mesh
)
2108 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2111 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
2115 cxr_polygon
*poly
= &mesh
->polys
[i
];
2118 normal_to_plane( poly
->normal
, poly
->center
, plane
);
2120 for( int j
=0; j
<poly
->loop_total
; j
++ )
2122 cxr_loop
*loop
= &mesh
->loops
[ poly
->loop_start
+j
];
2123 double *vert
= verts
[ loop
->index
];
2125 if( fabs(plane_polarity(plane
,vert
)) > 0.0025 )
2131 plane_project_point( plane
, vert
, ref
);
2134 cxr_debug_line( ref
, vert
, colour_error
);
2135 cxr_debug_box( vert
, 0.1, colour_error
);
2142 cxr_debug_poly( mesh
, poly
, colour_error
);
2149 CXR_API
void cxr_free_world( cxr_world
*world
)
2151 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2153 cxr_solid
*solid
= cxr_ab_ptr( &world
->absolids
, i
);
2154 cxr_free_mesh( solid
->pmesh
);
2157 cxr_ab_free( &world
->abverts
);
2158 cxr_ab_free( &world
->absolids
);
2160 if( world
->materials
)
2162 for( int i
=0; i
<world
->material_count
; i
++ )
2163 free( world
->materials
[i
].name
);
2165 free( world
->materials
);
2170 CXR_API cxr_tri_mesh
*cxr_world_preview( cxr_world
*world
)
2172 cxr_tri_mesh
*out
= malloc( sizeof(cxr_tri_mesh
) );
2173 out
->vertex_count
= 0;
2174 out
->indices_count
= 0;
2176 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2178 cxr_solid
*solid
= cxr_ab_ptr( &world
->absolids
, i
);
2179 cxr_mesh
*mesh
= solid
->pmesh
;
2181 for( int j
=0; j
<mesh
->abpolys
.count
; j
++ )
2183 cxr_polygon
*poly
= &mesh
->polys
[j
];
2185 out
->vertex_count
+= poly
->loop_total
* 3; /* Polygon, edge strip */
2186 out
->indices_count
+= (poly
->loop_total
-2) * 3; /* Polygon */
2187 out
->indices_count
+= poly
->loop_total
* 2 * 3; /* Edge strip */
2191 out
->colours
= malloc( sizeof(v4f
)*out
->vertex_count
);
2192 out
->vertices
= malloc( sizeof(v3f
)*out
->vertex_count
);
2193 out
->indices
= malloc( sizeof(i32
)*out
->indices_count
);
2195 v3f
*overts
= out
->vertices
;
2196 v4f
*colours
= out
->colours
;
2197 i32
*indices
= out
->indices
;
2202 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2204 cxr_solid
*solid
= cxr_ab_ptr( &world
->absolids
, i
);
2205 cxr_mesh
*mesh
= solid
->pmesh
;
2207 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2210 colour_random_brush( i
, colour
);
2212 for( int j
=0; j
<mesh
->abpolys
.count
; j
++ )
2214 cxr_polygon
*poly
= &mesh
->polys
[j
];
2218 for( int k
=0; k
<poly
->loop_total
-2; k
++ )
2224 indices
[ ii
++ ] = istart
+i0
;
2225 indices
[ ii
++ ] = istart
+i1
;
2226 indices
[ ii
++ ] = istart
+i2
;
2229 for( int k
=0; k
<poly
->loop_total
; k
++ )
2231 cxr_loop
*lp
= &mesh
->loops
[poly
->loop_start
+k
];
2234 i1r
= cxr_range(k
+1,poly
->loop_total
)*3+1,
2236 i1i
= cxr_range(k
+1,poly
->loop_total
)*3+2;
2238 indices
[ ii
++ ] = istart
+i0i
;
2239 indices
[ ii
++ ] = istart
+i1i
;
2240 indices
[ ii
++ ] = istart
+i1r
;
2242 indices
[ ii
++ ] = istart
+i0i
;
2243 indices
[ ii
++ ] = istart
+i1r
;
2244 indices
[ ii
++ ] = istart
+i0r
;
2247 v3_muladds( verts
[lp
->index
], poly
->normal
, 0.02, overts
[vi
] );
2248 v4_copy( colour
, colours
[ vi
] );
2253 v3_lerp( verts
[lp
->index
], poly
->center
, 0.2, inner
);
2254 v3_muladds( inner
, poly
->normal
, 0.015, overts
[ vi
] );
2255 v4_copy( colour
, colours
[ vi
] );
2256 v4_copy( (v4f
){ 0.0, 0.0, 0.0, 0.0 }, colours
[vi
] );
2259 v3_muladds(verts
[lp
->index
], poly
->normal
, 0.0, overts
[ vi
] );
2260 v4_copy( colour
, colours
[ vi
] );
2261 v4_copy( (v4f
){ 1.0, 1.0, 1.0, 0.125 }, colours
[vi
] );
2270 CXR_API
void cxr_free_tri_mesh( cxr_tri_mesh
*mesh
)
2272 free( mesh
->colours
);
2273 free( mesh
->indices
);
2274 free( mesh
->vertices
);
2278 CXR_API cxr_world
*cxr_decompose( cxr_static_mesh
*src
, i32
*perrcode
)
2281 cxr_world
*world
= malloc( sizeof(*world
) );
2283 /* Copy data to internal formats */
2284 cxr_mesh
*main_mesh
= cxr_to_internal_format( src
, &world
->abverts
);
2285 cxr_ab_init( &world
->absolids
, sizeof(cxr_solid
), 2 );
2287 if( src
->material_count
)
2289 size_t dsize
= sizeof(cxr_material
) * src
->material_count
;
2290 world
->materials
= malloc( dsize
);
2291 memcpy( world
->materials
, src
->materials
, dsize
);
2293 for( int i
=0; i
<src
->material_count
; i
++ )
2295 world
->materials
[i
].name
= malloc(strlen(src
->materials
[i
].name
) +1);
2296 strcpy( world
->materials
[i
].name
, src
->materials
[i
].name
);
2298 world
->material_count
= src
->material_count
;
2300 else world
->materials
= NULL
;
2302 int invalid_count
= 0;
2305 * Preprocessor 1: Island seperation
2309 cxr_mesh
*res
= cxr_pull_island( main_mesh
);
2312 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ res
, 0, 0 });
2316 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ main_mesh
, 0, 0 } );
2319 * Preprocessor 2: Displacement processing & error checks
2321 for( int i
=0; i
<world
->absolids
.count
; i
++ )
2323 cxr_solid
*pinf
= cxr_ab_ptr(&world
->absolids
,i
);
2325 for( int j
=0; j
<pinf
->pmesh
->abpolys
.count
; j
++ )
2327 cxr_polygon
*poly
= &pinf
->pmesh
->polys
[ j
];
2329 for( int k
=0; k
<poly
->loop_total
; k
++ )
2331 cxr_loop
*lp
= &pinf
->pmesh
->loops
[ poly
->loop_start
+k
];
2332 cxr_edge
*edge
= &pinf
->pmesh
->edges
[ lp
->edge_index
];
2334 if( edge
->freestyle
)
2338 if( !cxr_poly_convex( pinf
->pmesh
, poly
) )
2342 error
= k_soliderr_non_convex_poly
;
2346 if( cxr_solid_checkerr( pinf
->pmesh
) )
2350 error
= k_soliderr_non_coplanar_vertices
;
2356 pinf
->displacement
= 1;
2360 * Main convex decomp algorithm
2362 int sources_count
= world
->absolids
.count
;
2367 for( int i
=0; i
<sources_count
; i
++ )
2369 cxr_solid pinf
= *(cxr_solid
*)cxr_ab_ptr(&world
->absolids
, i
);
2371 if( pinf
.displacement
|| pinf
.invalid
)
2376 cxr_mesh
*res
= cxr_pull_best_solid( pinf
.pmesh
, 0, &error
);
2380 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ res
, 0, 0 } );
2384 if( error
== k_soliderr_no_solids
)
2386 /* Retry if non-critical error, with extra edges */
2387 res
= cxr_pull_best_solid(pinf
.pmesh
, 1, &error
);
2390 cxr_ab_push( &world
->absolids
, &(cxr_solid
){ res
, 0, 0 } );
2406 cxr_log( "Error %d\n", error
);
2407 cxr_free_world( world
);
2416 * format specific functions: vdf, vmf, (v)bsp
2417 * ----------------------------------------------------------------------------
2419 #ifdef CXR_VALVE_MAP_FILE
2421 CXR_API cxr_vdf
*cxr_vdf_open(const char *path
)
2423 cxr_vdf
*vdf
= malloc(sizeof(cxr_vdf
));
2426 vdf
->fp
= fopen( path
, "w" );
2437 CXR_API
void cxr_vdf_close(cxr_vdf
*vdf
)
2443 CXR_API
void cxr_vdf_put(cxr_vdf
*vdf
, const char *str
)
2445 for( int i
=0; i
<vdf
->level
; i
++ )
2446 fputs( " ", vdf
->fp
);
2448 fputs( str
, vdf
->fp
);
2451 static void cxr_vdf_printf( cxr_vdf
*vdf
, const char *fmt
, ... )
2453 cxr_vdf_put(vdf
,"");
2456 va_start( args
, fmt
);
2457 vfprintf( vdf
->fp
, fmt
, args
);
2461 CXR_API
void cxr_vdf_node(cxr_vdf
*vdf
, const char *str
)
2463 cxr_vdf_put( vdf
, str
);
2464 putc( (u8
)'\n', vdf
->fp
);
2465 cxr_vdf_put( vdf
, "{\n" );
2470 CXR_API
void cxr_vdf_edon( cxr_vdf
*vdf
)
2473 cxr_vdf_put( vdf
, "}\n" );
2476 CXR_API
void cxr_vdf_kv( cxr_vdf
*vdf
, const char *strk
, const char *strv
)
2478 cxr_vdf_printf( vdf
, "\"%s\" \"%s\"\n", strk
, strv
);
2482 * Data-type specific Keyvalues
2484 static void cxr_vdf_ki32( cxr_vdf
*vdf
, const char *strk
, i32 val
)
2486 cxr_vdf_printf( vdf
, "\"%s\" \"%d\"\n", strk
, val
);
2489 static void cxr_vdf_kdouble( cxr_vdf
*vdf
, const char *strk
, double val
)
2491 cxr_vdf_printf( vdf
, "\"%s\" \"%f\"\n", strk
, val
);
2494 static void cxr_vdf_kaxis( cxr_vdf
*vdf
, const char *strk
,
2495 v3f normal
, double offset
, double scale
2497 cxr_vdf_printf( vdf
, "\"%s\" \"[%f %f %f %f] %f\"\n",
2498 strk
, normal
[0], normal
[1],normal
[2], offset
, scale
);
2501 static void cxr_vdf_kv3f( cxr_vdf
*vdf
, const char *strk
, v3f v
)
2503 cxr_vdf_printf( vdf
, "\"%s\" \"[%f %f %f]\"\n", strk
, v
[0], v
[1], v
[2] );
2506 static void cxr_vdf_karrdouble( cxr_vdf
*vdf
, const char *strk
,
2507 int id
, double *doubles
, int count
2509 cxr_vdf_put(vdf
,"");
2510 fprintf( vdf
->fp
, "\"%s%d\" \"", strk
, id
);
2511 for( int i
=0; i
<count
; i
++ )
2513 if( i
== count
-1 ) fprintf( vdf
->fp
, "%f", doubles
[i
] );
2514 else fprintf( vdf
->fp
, "%f ", doubles
[i
] );
2516 fprintf( vdf
->fp
, "\"\n" );
2519 static void cxr_vdf_karrv3f( cxr_vdf
*vdf
, const char *strk
,
2520 int id
, v3f
*vecs
, int count
2522 cxr_vdf_put(vdf
,"");
2523 fprintf( vdf
->fp
, "\"%s%d\" \"", strk
, id
);
2524 for( int i
=0; i
<count
; i
++ )
2526 const char *format
= i
== count
-1? "%f %f %f": "%f %f %f ";
2527 fprintf( vdf
->fp
, format
, vecs
[i
][0], vecs
[i
][1], vecs
[i
][2] );
2529 fprintf( vdf
->fp
, "\"\n" );
2532 static void cxr_vdf_plane( cxr_vdf
*vdf
, const char *strk
, v3f a
, v3f b
, v3f c
)
2534 cxr_vdf_printf( vdf
, "\"%s\" \"(%f %f %f) (%f %f %f) (%f %f %f)\"\n",
2535 strk
, a
[0], a
[1], a
[2], b
[0], b
[1], b
[2], c
[0], c
[1], c
[2] );
2538 static void cxr_vdf_colour255(cxr_vdf
*vdf
, const char *strk
, v4f colour
)
2541 v4_muls( colour
, 255.0, scale
);
2542 cxr_vdf_printf( vdf
, "\"%s\" \"%d %d %d %d\"\n",
2543 strk
,(int)scale
[0], (int)scale
[1], (int)scale
[2], (int)scale
[3]);
2546 static struct cxr_material cxr_nodraw
=
2548 .res
= { 512, 512 },
2549 .name
= "tools/toolsnodraw"
2553 * Find most extreme point along a given direction
2555 static double support_distance( v3f verts
[3], v3f dir
, double coef
)
2559 coef
* v3_dot( verts
[0], dir
),
2562 coef
* v3_dot( verts
[1], dir
),
2563 coef
* v3_dot( verts
[2], dir
)
2569 * Convert regular UV'd triangle int Source's u/vaxis vectors
2571 * This supports affine move, scale, rotation, parallel skewing
2573 static void cxr_calculate_axis( cxr_texinfo
*transform
, v3f verts
[3],
2574 v2f uvs
[3], v2f texture_res
2576 v2f tT
, bT
; /* Tangent/bitangent pairs for UV space and world */
2579 v2_sub( uvs
[0], uvs
[1], tT
);
2580 v2_sub( uvs
[2], uvs
[1], bT
);
2581 v3_sub( verts
[0], verts
[1], tW
);
2582 v3_sub( verts
[2], verts
[1], bW
);
2584 /* Use arbitrary projection if there is no UV */
2585 if( v2_length( tT
) < 0.0001 || v2_length( bT
) < 0.0001 )
2587 v3f uaxis
, normal
, vaxis
;
2589 v3_copy( tW
, uaxis
);
2590 v3_normalize( uaxis
);
2592 v3_cross( tW
, bW
, normal
);
2593 v3_cross( normal
, uaxis
, vaxis
);
2594 v3_normalize( vaxis
);
2596 v3_copy( uaxis
, transform
->uaxis
);
2597 v3_copy( vaxis
, transform
->vaxis
);
2598 v2_zero( transform
->offset
);
2600 v2_div( (v2f
){128.0, 128.0}, texture_res
, transform
->scale
);
2601 transform
->winding
= 1.0;
2605 /* Detect if UV is reversed */
2606 double winding
= v2_cross( tT
, bT
) >= 0.0f
? 1.0f
: -1.0f
;
2608 /* UV projection reference */
2610 v2_muls((v2f
){1,0}, winding
, vX
);
2611 v2_muls((v2f
){0,1}, winding
, vY
);
2613 /* Reproject reference into world space, including skew */
2616 v3_muls( tW
, v2_cross(vX
,bT
) / v2_cross(bT
,tT
), uaxis1
);
2617 v3_muladds( uaxis1
, bW
, v2_cross(vX
, tT
) / v2_cross(tT
,bT
), uaxis1
);
2619 v3_muls( tW
, v2_cross(vY
,bT
) / v2_cross(bT
,tT
), vaxis1
);
2620 v3_muladds( vaxis1
, bW
, v2_cross(vY
,tT
) / v2_cross(tT
,bT
), vaxis1
);
2622 v3_normalize( uaxis1
);
2623 v3_normalize( vaxis1
);
2625 /* Apply source transform to axis (yes, they also need to be swapped) */
2626 v3f norm
, uaxis
, vaxis
;
2628 v3_cross( bW
, tW
, norm
);
2630 v3_cross( vaxis1
, norm
, uaxis
);
2631 v3_cross( uaxis1
, norm
, vaxis
);
2634 v2f uvmin
, uvmax
, uvdelta
;
2635 v2_minv( uvs
[0], uvs
[1], uvmin
);
2636 v2_minv( uvmin
, uvs
[2], uvmin
);
2637 v2_maxv( uvs
[0], uvs
[1], uvmax
);
2638 v2_maxv( uvmax
, uvs
[2], uvmax
);
2640 v2_sub( uvmax
, uvmin
, uvdelta
);
2642 /* world-uv scale */
2643 v2f uvminw
, uvmaxw
, uvdeltaw
;
2644 uvminw
[0] = -support_distance( verts
, uaxis
, -1.0f
);
2645 uvmaxw
[0] = support_distance( verts
, uaxis
, 1.0f
);
2646 uvminw
[1] = -support_distance( verts
, vaxis
, -1.0f
);
2647 uvmaxw
[1] = support_distance( verts
, vaxis
, 1.0f
);
2649 v2_sub( uvmaxw
, uvminw
, uvdeltaw
);
2653 v2_div( uvdeltaw
, uvdelta
, uv_scale
);
2654 v2_div( uv_scale
, texture_res
, uv_scale
);
2656 /* Find offset via 'natural' point */
2657 v2f target_uv
, natural_uv
, tex_offset
;
2658 v2_mul( uvs
[0], texture_res
, target_uv
);
2660 natural_uv
[0] = v3_dot( uaxis
, verts
[0] );
2661 natural_uv
[1] = -v3_dot( vaxis
, verts
[0] );
2662 v2_div( natural_uv
, uv_scale
, natural_uv
);
2664 tex_offset
[0] = target_uv
[0]-natural_uv
[0];
2665 tex_offset
[1] = -(target_uv
[1]-natural_uv
[1]);
2667 /* Copy everything into output */
2668 v3_copy( uaxis
, transform
->uaxis
);
2669 v3_copy( vaxis
, transform
->vaxis
);
2670 v2_copy( tex_offset
, transform
->offset
);
2671 v2_copy( uv_scale
, transform
->scale
);
2672 transform
->winding
= winding
;
2676 * Get the maximal direction of a vector, while also ignoring an axis
2679 static int cxr_cardinal( v3f a
, int ignore
)
2682 double component_max
= -CXR_BIG_NUMBER
;
2684 for( int i
=0; i
<3; i
++ )
2686 if( i
== ignore
) continue;
2688 if( fabs(a
[i
]) > component_max
)
2690 component_max
= fabs(a
[i
]);
2694 double d
= a
[component
] >= 0.0? 1.0: -1.0;
2702 * Convert contiguous mesh to displacement patch
2704 static int cxr_write_disp( cxr_mesh
*mesh
, cxr_world
*world
,
2705 cxr_vmf_context
*ctx
, cxr_vdf
*output
2707 v3f
*verts
= cxr_ab_ptr( mesh
->p_abverts
, 0 );
2711 int con_start
, con_count
;
2719 *vertinfo
= malloc( sizeof(struct vertinfo
)*mesh
->p_abverts
->count
);
2720 int *graph
= malloc( sizeof(int) * mesh
->abedges
.count
*2 );
2723 for( int i
=0; i
<mesh
->p_abverts
->count
; i
++ )
2725 struct vertinfo
*info
= &vertinfo
[i
];
2726 info
->con_start
= con_pos
;
2727 info
->con_count
= 0;
2734 for( int j
=0; j
<mesh
->abedges
.count
; j
++ )
2736 cxr_edge
*edge
= &mesh
->edges
[j
];
2738 if( edge
->i0
== i
|| edge
->i1
== i
)
2740 graph
[ con_pos
++ ] = edge
->i0
== i
? edge
->i1
: edge
->i0
;
2743 if( edge
->freestyle
)
2749 v3f refv
, refu
, refn
;
2750 v3_zero(refv
); v3_zero(refu
); v3_zero(refn
);
2753 * Approximately match the area of the result brush faces to the actual
2756 * Necessary for accuracy and even lightmap texel allocation
2759 double uv_area
= 0.0, face_area
= 0.0, sf
;
2760 v2f uvboundmin
, uvboundmax
;
2761 v3f faceboundmin
, faceboundmax
;
2765 v2_fill( uvboundmin
, CXR_BIG_NUMBER
);
2766 v2_fill( uvboundmax
, -CXR_BIG_NUMBER
);
2767 v3_fill( faceboundmin
, CXR_BIG_NUMBER
);
2768 v3_fill( faceboundmax
, -CXR_BIG_NUMBER
);
2770 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
2772 cxr_polygon
*poly
= &mesh
->polys
[i
];
2774 for( int j
=0; j
<poly
->loop_total
; j
++ )
2776 cxr_loop
*lp0
= &mesh
->loops
[ poly
->loop_start
+j
];
2777 v2_minv( lp0
->uv
, uvboundmin
, uvboundmin
);
2778 v2_maxv( lp0
->uv
, uvboundmax
, uvboundmax
);
2779 v3_minv( verts
[lp0
->index
], faceboundmin
, faceboundmin
);
2780 v3_maxv( verts
[lp0
->index
], faceboundmax
, faceboundmax
);
2783 for( int j
=0; j
<poly
->loop_total
-2; j
++ )
2785 cxr_loop
*lp0
= &mesh
->loops
[poly
->loop_start
],
2786 *lp1
= &mesh
->loops
[poly
->loop_start
+j
+1],
2787 *lp2
= &mesh
->loops
[poly
->loop_start
+j
+2];
2790 v3_sub( verts
[lp1
->index
], verts
[lp0
->index
], va
);
2791 v3_sub( verts
[lp2
->index
], verts
[lp0
->index
], vb
);
2792 v3_cross( va
, vb
, orth
);
2794 face_area
+= v3_length( orth
) / 2.0;
2797 v2_sub( lp1
->uv
, lp0
->uv
, uva
);
2798 v2_sub( lp2
->uv
, lp0
->uv
, uvb
);
2800 uv_area
+= fabs(v2_cross( uva
, uvb
)) / 2.0;
2804 v3_add( faceboundmax
, faceboundmin
, face_center
);
2805 v3_muls( face_center
, 0.5, face_center
);
2806 v2_add( uvboundmin
, uvboundmax
, uv_center
);
2807 v2_muls( uv_center
, 0.5, uv_center
);
2809 sf
= sqrt( face_area
/ uv_area
);
2810 int corner_count
= 0;
2813 * Vertex classification
2814 * boundary vertices: they exist on a freestyle edge
2815 * corners: only connected to other boundaries
2817 for( int i
=0; i
<mesh
->p_abverts
->count
; i
++ )
2819 struct vertinfo
*info
= &vertinfo
[i
];
2820 if( !info
->boundary
) continue;
2825 for( int j
=0; j
<info
->con_count
; j
++ )
2827 int con
= graph
[info
->con_start
+j
];
2829 if( vertinfo
[con
].boundary
)
2835 if( count
> 2 || non_manifold
)
2843 * TODO(harry): This currently only supports power 2 displacements
2844 * its quite straightforward to upgrade it.
2846 * TODO(harry): Error checking is needed here for bad input data
2854 for( int i
=0; i
<mesh
->abpolys
.count
; i
++ )
2856 cxr_polygon
*basepoly
= &mesh
->polys
[i
];
2858 for( int h
=0; h
<basepoly
->loop_total
; h
++ )
2861 i1
= cxr_range(h
+1,basepoly
->loop_total
);
2863 cxr_loop
*l0
= &mesh
->loops
[ basepoly
->loop_start
+i0
],
2864 *l1
= &mesh
->loops
[ basepoly
->loop_start
+i1
];
2865 struct vertinfo
*info
= &vertinfo
[ l0
->index
];
2870 int corner_count
= 1;
2872 cxr_material
*matptr
=
2873 basepoly
->material_id
< 0 || !world
->materials
?
2875 &world
->materials
[ basepoly
->material_id
];
2878 dispedge
[0] = l0
->index
;
2879 dispedge
[1] = l1
->index
;
2880 v2_copy( l0
->uv
, corner_uvs
[0] );
2882 /* Consume (use) face from orignal mesh */
2883 basepoly
->loop_total
= -1;
2885 while( dispedge_count
< 17 )
2887 struct vertinfo
*edge_head
=
2888 &vertinfo
[dispedge
[dispedge_count
-1]];
2892 if( edge_head
->corner
)
2894 /* Find polygon that has edge C-1 -> C */
2895 for( int j
=0; j
<mesh
->abpolys
.count
&& !newvert
; j
++ )
2897 cxr_polygon
*poly
= &mesh
->polys
[j
];
2899 for( int k
=0; k
<poly
->loop_total
; k
++ )
2902 i1
= cxr_range(k
+1,poly
->loop_total
);
2904 cxr_loop
*l0
= &mesh
->loops
[ poly
->loop_start
+i0
],
2905 *l1
= &mesh
->loops
[ poly
->loop_start
+i1
];
2907 if( l0
->index
== dispedge
[dispedge_count
-2] &&
2908 l1
->index
== dispedge
[dispedge_count
-1] )
2910 /* Take the next edge */
2911 v2_copy( l1
->uv
, corner_uvs
[corner_count
++] );
2913 int i2
= cxr_range(i1
+1,poly
->loop_total
);
2914 cxr_loop
*l2
= &mesh
->loops
[ poly
->loop_start
+i2
];
2916 dispedge
[dispedge_count
++] = l2
->index
;
2918 poly
->loop_total
= -1;
2926 for( int j
=0; j
<edge_head
->con_count
; j
++ )
2928 int con
= graph
[edge_head
->con_start
+j
];
2933 if( dispedge_count
> 1 )
2934 if( con
== dispedge
[dispedge_count
-2] )
2937 struct vertinfo
*coninfo
= &vertinfo
[con
];
2939 if( !coninfo
->boundary
)
2942 dispedge
[ dispedge_count
++ ] = con
;
2955 /* All edges collected */
2958 v2_sub( corner_uvs
[1], corner_uvs
[0], va
);
2959 v2_sub( corner_uvs
[2], corner_uvs
[0], vb
);
2961 /* Connect up the grid
2969 * Example: a := common unused vertex that is connected to
2970 * by 1 and 15. Or y-1, and x-1 on the grid.
2971 * g := c and f common vert ^
2976 for( int j
=0; j
<5; j
++ ) grid
[j
] = dispedge
[j
];
2977 for( int j
=1; j
<5; j
++ ) grid
[j
*5+4] = dispedge
[j
+4];
2978 for( int j
=0; j
<4; j
++ ) grid
[4*5+3-j
] = dispedge
[j
+9];
2979 for( int j
=1; j
<4; j
++ ) grid
[j
*5] = dispedge
[16-j
];
2982 for( int j
=1; j
<4; j
++ )
2984 for( int k
=1; k
<4; k
++ )
2986 int s0
= grid
[(j
-1)*5+k
],
2989 struct vertinfo
*va
= &vertinfo
[s0
],
2990 *vb
= &vertinfo
[s1
];
2992 /* Find common non-used vertex */
2993 for( int l
=0; l
<va
->con_count
; l
++ )
2995 for( int m
=0; m
<vb
->con_count
; m
++ )
2997 int cona
= graph
[va
->con_start
+l
],
2998 conb
= graph
[vb
->con_start
+m
];
3002 if( vertinfo
[cona
].used
|| vertinfo
[cona
].boundary
)
3005 grid
[ j
*5+k
] = cona
;
3006 vertinfo
[cona
].used
= 1;
3014 cxr_log( "Broken displacement!\n" );
3025 * Create V reference based on first displacement.
3026 * TODO(harry): This is not the moststable selection method!
3027 * faces can come in any order, so the first disp will of
3028 * course always vary. Additionaly the triangle can be oriented
3031 * Improvement can be made by selecting a first disp/triangle
3032 * based on deterministic factors.
3034 if( disp_count
== 0 )
3038 v3_copy( verts
[dispedge
[0]], tri_ref
[0] );
3039 v3_copy( verts
[dispedge
[4]], tri_ref
[1] );
3040 v3_copy( verts
[dispedge
[8]], tri_ref
[2] );
3041 cxr_calculate_axis( &tx
, tri_ref
, corner_uvs
, (v2f
){512,512} );
3043 v3_muls( tx
.vaxis
, -1.0, refv
);
3044 int v_cardinal
= cxr_cardinal( refv
, -1 );
3046 v3_cross( tx
.vaxis
, tx
.uaxis
, refn
);
3047 v3_muls( refn
, -tx
.winding
, refn
);
3049 /* Computing new reference vectors */
3050 int n1_cardinal
= cxr_cardinal( refn
, v_cardinal
);
3054 for( int j
=0; j
<2; j
++ )
3055 if( u_cardinal
== n1_cardinal
|| u_cardinal
== v_cardinal
)
3059 refu
[u_cardinal
] = tx
.uaxis
[u_cardinal
] > 0.0? 1.0: -1.0;
3063 v3_copy( face_center
, p0
);
3064 v3_muladds( face_center
, refn
, 1.5, pn
);
3065 v3_muladds( face_center
, refv
, 1.5, pv
);
3066 v3_muladds( face_center
, refu
, 1.5, pu
);
3069 /* Create world coordinates */
3070 v3f world_corners
[8];
3073 for( int j
=0; j
<4; j
++ )
3076 v2_sub( corner_uvs
[j
], uv_center
, local_uv
);
3077 v2_copy( corner_uvs
[j
], world_uv
[j
] );
3078 v2_muls( local_uv
, sf
, local_uv
);
3080 v3_muls( refu
, local_uv
[0], world_corners
[j
] );
3081 v3_muladds( world_corners
[j
],refv
,local_uv
[1],world_corners
[j
] );
3082 v3_add( face_center
, world_corners
[j
], world_corners
[j
] );
3085 double *colour
= colours_random
[cxr_range(disp_count
,8)];
3087 for( int j
=0; j
<4; j
++ )
3088 v3_muladds( world_corners
[j
], refn
, -1.0, world_corners
[j
+4] );
3090 /* Apply world transform */
3091 for( int j
=0; j
<8; j
++ )
3093 double *p0
= world_corners
[j
];
3094 v3_muls( p0
, ctx
->scale
, p0
);
3095 v3_add( p0
, ctx
->offset
, p0
);
3098 cxr_texinfo texinfo_shared
;
3099 cxr_calculate_axis( &texinfo_shared
, world_corners
, world_uv
,
3100 (v2f
){ matptr
->res
[0], matptr
->res
[1] } );
3103 cxr_vdf_node( output
, "solid" );
3104 cxr_vdf_ki32( output
, "id", ++ ctx
->brush_count
);
3115 double distances
[25];
3117 v3f lside0
, lside1
, lref
, vdelta
, vworld
;
3120 for( int j
=0; j
<5; j
++ )
3122 ty
= (double)j
/(double)(5-1);
3124 v3_lerp( world_corners
[0], world_corners
[3], ty
, lside0
);
3125 v3_lerp( world_corners
[1], world_corners
[2], ty
, lside1
);
3127 for( int k
=0; k
<5; k
++ )
3131 tx
= (double)k
/(double)(5-1);
3132 v3_lerp( lside0
, lside1
, tx
, lref
);
3133 v3_muls( verts
[grid
[index
]], ctx
->scale
, vworld
);
3134 v3_add( ctx
->offset
, vworld
, vworld
);
3136 v3_sub( vworld
, lref
, vdelta
);
3137 v3_copy( vdelta
, normals
[index
] );
3138 v3_normalize( normals
[index
] );
3139 distances
[index
] = v3_dot( vdelta
, normals
[index
] );
3143 for( int j
=0; j
<6; j
++ )
3145 int *side
= sides
[j
];
3147 cxr_vdf_node( output
, "side" );
3148 cxr_vdf_ki32( output
, "id", ++ ctx
->face_count
);
3149 cxr_vdf_plane( output
, "plane", world_corners
[side
[2]],
3150 world_corners
[side
[1]],
3151 world_corners
[side
[0]] );
3153 cxr_vdf_kv( output
, "material", matptr
->name
);
3154 cxr_vdf_kaxis( output
, "uaxis",
3155 texinfo_shared
.uaxis
,
3156 texinfo_shared
.offset
[0],
3157 texinfo_shared
.scale
[0] );
3158 cxr_vdf_kaxis( output
, "vaxis",
3159 texinfo_shared
.vaxis
,
3160 texinfo_shared
.offset
[1],
3161 texinfo_shared
.scale
[1] );
3163 cxr_vdf_kdouble( output
, "rotation", 0.0 );
3164 cxr_vdf_ki32( output
, "lightmapscale", ctx
->lightmap_scale
);
3165 cxr_vdf_ki32( output
, "smoothing_groups", 0 );
3169 cxr_vdf_node( output
, "dispinfo" );
3170 cxr_vdf_ki32( output
, "power", 2 );
3171 cxr_vdf_kv3f( output
, "startposition", world_corners
[0] );
3172 cxr_vdf_ki32( output
, "flags", 0 );
3173 cxr_vdf_kdouble( output
, "elevation", 0.0 );
3174 cxr_vdf_ki32( output
, "subdiv", 0 );
3176 cxr_vdf_node( output
, "normals" );
3177 for( int k
=0; k
<5; k
++ )
3178 cxr_vdf_karrv3f( output
, "row", k
, &normals
[k
*5], 5 );
3179 cxr_vdf_edon( output
);
3181 cxr_vdf_node( output
, "distances" );
3182 for( int k
=0; k
<5; k
++ )
3183 cxr_vdf_karrdouble( output
, "row", k
, &distances
[k
*5], 5 );
3184 cxr_vdf_edon( output
);
3187 * TODO: This might be needed for the compilers. Opens fine in
3192 cxr_vdf_node( output, "offsets" );
3193 for( int k=0; k<5; k++ )
3194 cxr_vdf_printf( output,
3195 "\"row%d\" \"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\"\n", k );
3196 cxr_vdf_edon( output );
3198 cxr_vdf_node( output, "offset_normals" );
3199 for( int k=0; k<5; k++ )
3200 cxr_vdf_printf( output,
3201 "\"row%d\" \"0 0 1 0 0 1 0 0 1 0 0 1 0 0 1\"\n", k );
3202 cxr_vdf_edon( output );
3204 cxr_vdf_node( output, "alphas" );
3205 for( int k=0; k<5; k++ )
3206 cxr_vdf_printf( output, "\"row%d\" \"0 0 0 0 0\"\n", k );
3207 cxr_vdf_edon( output );
3209 cxr_vdf_node( output, "triangle_tags" );
3210 for( int k=0; k<5-1; k++ )
3211 cxr_vdf_printf( output,
3212 "\"row%d\" \"9 9 9 9 9 9 9 9\"\n", k );
3213 cxr_vdf_edon( output );
3215 cxr_vdf_node( output, "allowed_verts" );
3216 cxr_vdf_printf( output,
3217 "\"10\" \"-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\"\n" );
3218 cxr_vdf_edon( output );
3221 cxr_vdf_edon( output
);
3224 cxr_vdf_edon( output
);
3227 cxr_vdf_node( output
, "editor");
3228 cxr_vdf_colour255( output
, "color",
3229 colours_random
[cxr_range(ctx
->brush_count
,8)]);
3231 cxr_vdf_ki32( output
, "visgroupshown",1);
3232 cxr_vdf_ki32( output
, "visgroupautoshown",1);
3233 cxr_vdf_edon( output
);
3235 cxr_vdf_edon( output
);
3247 * Write header information for a vmf to vdf
3249 CXR_API
void cxr_begin_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*output
)
3251 cxr_vdf_node( output
, "versioninfo" );
3252 cxr_vdf_ki32( output
, "editorversion", 400 );
3253 cxr_vdf_ki32( output
, "editorbuild", 8456 );
3254 cxr_vdf_ki32( output
, "mapversion", ctx
->mapversion
);
3255 cxr_vdf_ki32( output
, "formatversion", 100 );
3256 cxr_vdf_ki32( output
, "prefab", 0 );
3257 cxr_vdf_edon( output
);
3259 cxr_vdf_node( output
, "visgroups" );
3260 cxr_vdf_edon( output
);
3262 cxr_vdf_node( output
, "viewsettings" );
3263 cxr_vdf_ki32( output
, "bSnapToGrid", 1 );
3264 cxr_vdf_ki32( output
, "bShowGrid", 1 );
3265 cxr_vdf_ki32( output
, "bShowLogicalGrid", 0 );
3266 cxr_vdf_ki32( output
, "nGridSpacing", 64 );
3267 cxr_vdf_ki32( output
, "bShow3DGrid", 0 );
3268 cxr_vdf_edon( output
);
3270 cxr_vdf_node( output
, "world" );
3271 cxr_vdf_ki32( output
, "id", 1 );
3272 cxr_vdf_ki32( output
, "mapversion", 1 ); /* ?? */
3273 cxr_vdf_kv( output
, "classname", "worldspawn" );
3274 cxr_vdf_kv( output
, "skyname", ctx
->skyname
);
3275 cxr_vdf_ki32( output
, "maxpropscreenwidth", -1 );
3276 cxr_vdf_kv( output
, "detailvbsp", ctx
->detailvbsp
);
3277 cxr_vdf_kv( output
, "detailmaterial", ctx
->detailmaterial
);
3280 /* Fairly useless but might need in the future */
3281 CXR_API
void cxr_vmf_begin_entities( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
)
3283 cxr_vdf_edon( vdf
);
3286 CXR_API
void cxr_end_vmf( cxr_vmf_context
*ctx
, cxr_vdf
*vdf
)
3291 * Write solids (and displacements) to VMF file
3293 CXR_API
void cxr_push_world_vmf( cxr_world
*world
, cxr_vmf_context
*ctx
,
3296 v3f
*verts
= cxr_ab_ptr( &world
->abverts
, 0 );
3298 /* Write all solids as VMF brushes */
3299 for( int i
=0; i
<world
->absolids
.count
; i
++ )
3301 cxr_solid
*solid
= cxr_ab_ptr(&world
->absolids
,i
);
3303 if( solid
->displacement
)
3305 cxr_write_disp( solid
->pmesh
, world
, ctx
, output
);
3309 cxr_vdf_node( output
, "solid" );
3310 cxr_vdf_ki32( output
, "id", ++ ctx
->brush_count
);
3312 for( int j
=0; j
<solid
->pmesh
->abpolys
.count
; j
++ )
3314 cxr_polygon
*poly
= &solid
->pmesh
->polys
[j
];
3315 cxr_loop
*ploops
= &solid
->pmesh
->loops
[poly
->loop_start
];
3317 cxr_material
*matptr
=
3318 poly
->material_id
< 0 || !world
->materials
?
3320 &world
->materials
[ poly
->material_id
];
3322 cxr_vdf_node( output
, "side" );
3323 cxr_vdf_ki32( output
, "id", ++ ctx
->face_count
);
3325 v3f tri
[3]; v2f uvs
[3];
3327 int i0
= ploops
[0].index
,
3328 i1
= ploops
[1].index
,
3329 i2
= ploops
[2].index
;
3331 v3_muls( verts
[i0
], ctx
->scale
, tri
[0] );
3332 v3_muls( verts
[i1
], ctx
->scale
, tri
[1] );
3333 v3_muls( verts
[i2
], ctx
->scale
, tri
[2] );
3335 v3_add( ctx
->offset
, tri
[0], tri
[0] );
3336 v3_add( ctx
->offset
, tri
[1], tri
[1] );
3337 v3_add( ctx
->offset
, tri
[2], tri
[2] );
3339 v2_copy( ploops
[0].uv
, uvs
[0] );
3340 v2_copy( ploops
[1].uv
, uvs
[1] );
3341 v2_copy( ploops
[2].uv
, uvs
[2] );
3343 cxr_vdf_plane( output
, "plane", tri
[2], tri
[1], tri
[0] );
3344 cxr_vdf_kv( output
, "material", matptr
->name
);
3347 cxr_calculate_axis( &tx
, tri
, uvs
,
3348 (double[2]){ matptr
->res
[0], matptr
->res
[1] });
3350 cxr_vdf_kaxis( output
, "uaxis", tx
.uaxis
, tx
.offset
[0], tx
.scale
[0]);
3351 cxr_vdf_kaxis( output
, "vaxis", tx
.vaxis
, tx
.offset
[1], tx
.scale
[1]);
3353 cxr_vdf_kdouble( output
, "rotation", 0.0 );
3354 cxr_vdf_ki32( output
, "lightmapscale", ctx
->lightmap_scale
);
3355 cxr_vdf_ki32( output
, "smoothing_groups", 0);
3357 cxr_vdf_edon( output
);
3360 cxr_vdf_node( output
, "editor" );
3361 cxr_vdf_colour255( output
, "color",
3362 colours_random
[cxr_range(ctx
->brush_count
,8)]);
3364 cxr_vdf_ki32( output
, "visgroupshown", 1 );
3365 cxr_vdf_ki32( output
, "visgroupautoshown", 1 );
3366 cxr_vdf_edon( output
);
3368 cxr_vdf_edon( output
);
3373 * Valve Source SDK 2015 CS:GO
3375 #define HEADER_LUMPS 64
3376 #define LUMP_WORLDLIGHTS 54
3378 #pragma pack(push,1)
3387 int fileofs
, filelen
;
3392 lumps
[ HEADER_LUMPS
];
3402 float shadow_cast_offset
[3];
3410 float constant_attn
;
3412 float quadratic_attn
;
3420 * Utility for patching BSP tools to remove -1 distance lights (we set them
3421 * like that, because we want these lights to go away)
3423 * Yes, there is no way to do this in hammer
3424 * Yes, the distance KV is unused but still gets compiled to this lump
3425 * No, Entities only compile will not do this for you
3427 CXR_API
int cxr_lightpatch_bsp( const char *path
)
3429 printf( "Lightpatch: %s\n", path
);
3431 FILE *fp
= fopen( path
, "r+b" );
3436 cxr_log( "Could not open BSP file for editing (r+b)\n" );
3442 struct header header
;
3443 fread( &header
, sizeof(struct header
), 1, fp
);
3444 struct lump
*lump
= &header
.lumps
[ LUMP_WORLDLIGHTS
];
3446 /* Read worldlight array */
3447 struct worldlight
*lights
= malloc( lump
->filelen
);
3448 fseek( fp
, lump
->fileofs
, SEEK_SET
);
3449 fread( lights
, lump
->filelen
, 1, fp
);
3451 /* Remove all marked lights */
3452 int light_count
= lump
->filelen
/ sizeof(struct worldlight
);
3455 for( int i
= 0; i
< light_count
; i
++ )
3456 if( lights
[i
].radius
>= 0.0f
)
3457 lights
[new_count
++] = lights
[i
];
3459 lump
->filelen
= new_count
*sizeof(struct worldlight
);
3461 /* Write changes back to file */
3462 fseek( fp
, lump
->fileofs
, SEEK_SET
);
3463 fwrite( lights
, lump
->filelen
, 1, fp
);
3464 fseek( fp
, 0, SEEK_SET
);
3465 fwrite( &header
, sizeof(struct header
), 1, fp
);
3468 cxr_log( "removed %d marked lights\n", light_count
-new_count
);
3476 #endif /* CXR_VALVE_MAP_FILE */
3477 #endif /* CXR_IMPLEMENTATION */