forked from prideout/par
-
Notifications
You must be signed in to change notification settings - Fork 0
/
par_shapes.h
2152 lines (1975 loc) · 71.1 KB
/
par_shapes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SHAPES :: https://github.com/prideout/par
// Simple C library for creation and manipulation of triangle meshes.
//
// The API is divided into three sections:
//
// - Generators. Create parametric surfaces, platonic solids, etc.
// - Queries. Ask a mesh for its axis-aligned bounding box, etc.
// - Transforms. Rotate a mesh, merge it with another, add normals, etc.
//
// In addition to the comment block above each function declaration, the API
// has informal documentation here:
//
// https://prideout.net/shapes
//
// For our purposes, a "mesh" is a list of points and a list of triangles; the
// former is a flattened list of three-tuples (32-bit floats) and the latter is
// also a flattened list of three-tuples (16-bit uints). Triangles are always
// oriented such that their front face winds counter-clockwise.
//
// Optionally, meshes can contain 3D normals (one per vertex), and 2D texture
// coordinates (one per vertex). That's it! If you need something fancier,
// look elsewhere.
//
// Distributed under the MIT License, see bottom of file.
#ifndef PAR_SHAPES_H
#define PAR_SHAPES_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#if !defined(_MSC_VER)
# include <stdbool.h>
#else // MSVC
# if _MSC_VER >= 1800
# include <stdbool.h>
# else // stdbool.h missing prior to MSVC++ 12.0 (VS2013)
# define bool int
# define true 1
# define false 0
# endif
#endif
#ifndef PAR_SHAPES_T
#define PAR_SHAPES_T uint16_t
#endif
typedef struct par_shapes_mesh_s {
float* points; // Flat list of 3-tuples (X Y Z X Y Z...)
int npoints; // Number of points
PAR_SHAPES_T* triangles; // Flat list of 3-tuples (I J K I J K...)
int ntriangles; // Number of triangles
float* normals; // Optional list of 3-tuples (X Y Z X Y Z...)
float* tcoords; // Optional list of 2-tuples (U V U V U V...)
} par_shapes_mesh;
void par_shapes_free_mesh(par_shapes_mesh*);
// Generators ------------------------------------------------------------------
// Instance a cylinder that sits on the Z=0 plane using the given tessellation
// levels across the UV domain. Think of "slices" like a number of pizza
// slices, and "stacks" like a number of stacked rings. Height and radius are
// both 1.0, but they can easily be changed with par_shapes_scale.
par_shapes_mesh* par_shapes_create_cylinder(int slices, int stacks);
// Cone is similar to cylinder but the radius diminishes to zero as Z increases.
// Again, height and radius are 1.0, but can be changed with par_shapes_scale.
par_shapes_mesh* par_shapes_create_cone(int slices, int stacks);
// Create a disk of radius 1.0 with texture coordinates and normals by squashing
// a cone flat on the Z=0 plane.
par_shapes_mesh* par_shapes_create_parametric_disk(int slices, int stacks);
// Create a donut that sits on the Z=0 plane with the specified inner radius.
// The outer radius can be controlled with par_shapes_scale.
par_shapes_mesh* par_shapes_create_torus(int slices, int stacks, float radius);
// Create a sphere with texture coordinates and small triangles near the poles.
par_shapes_mesh* par_shapes_create_parametric_sphere(int slices, int stacks);
// Approximate a sphere with a subdivided icosahedron, which produces a nice
// distribution of triangles, but no texture coordinates. Each subdivision
// level scales the number of triangles by four, so use a very low number.
par_shapes_mesh* par_shapes_create_subdivided_sphere(int nsubdivisions);
// More parametric surfaces.
par_shapes_mesh* par_shapes_create_klein_bottle(int slices, int stacks);
par_shapes_mesh* par_shapes_create_trefoil_knot(int slices, int stacks,
float radius);
par_shapes_mesh* par_shapes_create_hemisphere(int slices, int stacks);
par_shapes_mesh* par_shapes_create_plane(int slices, int stacks);
// Create a parametric surface from a callback function that consumes a 2D
// point in [0,1] and produces a 3D point.
typedef void (*par_shapes_fn)(float const*, float*, void*);
par_shapes_mesh* par_shapes_create_parametric(par_shapes_fn, int slices,
int stacks, void* userdata);
// Generate points for a 20-sided polyhedron that fits in the unit sphere.
// Texture coordinates and normals are not generated.
par_shapes_mesh* par_shapes_create_icosahedron();
// Generate points for a 12-sided polyhedron that fits in the unit sphere.
// Again, texture coordinates and normals are not generated.
par_shapes_mesh* par_shapes_create_dodecahedron();
// More platonic solids.
par_shapes_mesh* par_shapes_create_octahedron();
par_shapes_mesh* par_shapes_create_tetrahedron();
par_shapes_mesh* par_shapes_create_cube();
// Generate an orientable disk shape in 3-space. Does not include normals or
// texture coordinates.
par_shapes_mesh* par_shapes_create_disk(float radius, int slices,
float const* center, float const* normal);
// Create an empty shape. Useful for building scenes with merge_and_free.
par_shapes_mesh* par_shapes_create_empty();
// Generate a rock shape that sits on the Y=0 plane, and sinks into it a bit.
// This includes smooth normals but no texture coordinates. Each subdivision
// level scales the number of triangles by four, so use a very low number.
par_shapes_mesh* par_shapes_create_rock(int seed, int nsubdivisions);
// Create trees or vegetation by executing a recursive turtle graphics program.
// The program is a list of command-argument pairs. See the unit test for
// an example. Texture coordinates and normals are not generated.
par_shapes_mesh* par_shapes_create_lsystem(char const* program, int slices,
int maxdepth);
// Queries ---------------------------------------------------------------------
// Dump out a text file conforming to the venerable OBJ format.
void par_shapes_export(par_shapes_mesh const*, char const* objfile);
// Take a pointer to 6 floats and set them to min xyz, max xyz.
void par_shapes_compute_aabb(par_shapes_mesh const* mesh, float* aabb);
// Make a deep copy of a mesh. To make a brand new copy, pass null to "target".
// To avoid memory churn, pass an existing mesh to "target".
par_shapes_mesh* par_shapes_clone(par_shapes_mesh const* mesh,
par_shapes_mesh* target);
// Transformations -------------------------------------------------------------
void par_shapes_merge(par_shapes_mesh* dst, par_shapes_mesh const* src);
void par_shapes_translate(par_shapes_mesh*, float x, float y, float z);
void par_shapes_rotate(par_shapes_mesh*, float radians, float const* axis);
void par_shapes_scale(par_shapes_mesh*, float x, float y, float z);
void par_shapes_merge_and_free(par_shapes_mesh* dst, par_shapes_mesh* src);
// Reverse the winding of a run of faces. Useful when drawing the inside of
// a Cornell Box. Pass 0 for nfaces to reverse every face in the mesh.
void par_shapes_invert(par_shapes_mesh*, int startface, int nfaces);
// Remove all triangles whose area is less than minarea.
void par_shapes_remove_degenerate(par_shapes_mesh*, float minarea);
// Dereference the entire index buffer and replace the point list.
// This creates an inefficient structure, but is useful for drawing facets.
// If create_indices is true, a trivial "0 1 2 3..." index buffer is generated.
void par_shapes_unweld(par_shapes_mesh* mesh, bool create_indices);
// Merge colocated verts, build a new index buffer, and return the
// optimized mesh. Epsilon is the maximum distance to consider when
// welding vertices. The mapping argument can be null, or a pointer to
// npoints integers, which gets filled with the mapping from old vertex
// indices to new indices.
par_shapes_mesh* par_shapes_weld(par_shapes_mesh const*, float epsilon,
PAR_SHAPES_T* mapping);
// Compute smooth normals by averaging adjacent facet normals.
void par_shapes_compute_normals(par_shapes_mesh* m);
// Global Config ---------------------------------------------------------------
void par_shapes_set_epsilon_welded_normals(float epsilon);
void par_shapes_set_epsilon_degenerate_sphere(float epsilon);
// Advanced --------------------------------------------------------------------
void par_shapes__compute_welded_normals(par_shapes_mesh* m);
void par_shapes__connect(par_shapes_mesh* scene, par_shapes_mesh* cylinder,
int slices);
#ifndef PAR_PI
#define PAR_PI (3.14159265359)
#define PAR_MIN(a, b) (a > b ? b : a)
#define PAR_MAX(a, b) (a > b ? a : b)
#define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
#define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
#define PAR_SQR(a) ((a) * (a))
#endif
#ifndef PAR_MALLOC
#define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
#define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
#define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
#define PAR_FREE(BUF) free(BUF)
#endif
#ifdef __cplusplus
}
#endif
// -----------------------------------------------------------------------------
// END PUBLIC API
// -----------------------------------------------------------------------------
#ifdef PAR_SHAPES_IMPLEMENTATION
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <float.h>
#include <string.h>
#include <math.h>
#include <errno.h>
static float par_shapes__epsilon_welded_normals = 0.001;
static float par_shapes__epsilon_degenerate_sphere = 0.0001;
static void par_shapes__sphere(float const* uv, float* xyz, void*);
static void par_shapes__hemisphere(float const* uv, float* xyz, void*);
static void par_shapes__plane(float const* uv, float* xyz, void*);
static void par_shapes__klein(float const* uv, float* xyz, void*);
static void par_shapes__cylinder(float const* uv, float* xyz, void*);
static void par_shapes__cone(float const* uv, float* xyz, void*);
static void par_shapes__torus(float const* uv, float* xyz, void*);
static void par_shapes__trefoil(float const* uv, float* xyz, void*);
struct osn_context;
static int par__simplex_noise(int64_t seed, struct osn_context** ctx);
static void par__simplex_noise_free(struct osn_context* ctx);
static double par__simplex_noise2(struct osn_context* ctx, double x, double y);
static void par_shapes__copy3(float* result, float const* a)
{
result[0] = a[0];
result[1] = a[1];
result[2] = a[2];
}
static float par_shapes__dot3(float const* a, float const* b)
{
return b[0] * a[0] + b[1] * a[1] + b[2] * a[2];
}
static void par_shapes__transform3(float* p, float const* x, float const* y,
float const* z)
{
float px = par_shapes__dot3(p, x);
float py = par_shapes__dot3(p, y);
float pz = par_shapes__dot3(p, z);
p[0] = px;
p[1] = py;
p[2] = pz;
}
static void par_shapes__cross3(float* result, float const* a, float const* b)
{
float x = (a[1] * b[2]) - (a[2] * b[1]);
float y = (a[2] * b[0]) - (a[0] * b[2]);
float z = (a[0] * b[1]) - (a[1] * b[0]);
result[0] = x;
result[1] = y;
result[2] = z;
}
static void par_shapes__mix3(float* d, float const* a, float const* b, float t)
{
float x = b[0] * t + a[0] * (1 - t);
float y = b[1] * t + a[1] * (1 - t);
float z = b[2] * t + a[2] * (1 - t);
d[0] = x;
d[1] = y;
d[2] = z;
}
static void par_shapes__scale3(float* result, float a)
{
result[0] *= a;
result[1] *= a;
result[2] *= a;
}
static void par_shapes__normalize3(float* v)
{
float lsqr = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if (lsqr > 0) {
par_shapes__scale3(v, 1.0f / lsqr);
}
}
static void par_shapes__subtract3(float* result, float const* a)
{
result[0] -= a[0];
result[1] -= a[1];
result[2] -= a[2];
}
static void par_shapes__add3(float* result, float const* a)
{
result[0] += a[0];
result[1] += a[1];
result[2] += a[2];
}
static float par_shapes__sqrdist3(float const* a, float const* b)
{
float dx = a[0] - b[0];
float dy = a[1] - b[1];
float dz = a[2] - b[2];
return dx * dx + dy * dy + dz * dz;
}
void par_shapes__compute_welded_normals(par_shapes_mesh* m)
{
const float epsilon = par_shapes__epsilon_welded_normals;
m->normals = PAR_MALLOC(float, m->npoints * 3);
PAR_SHAPES_T* weldmap = PAR_MALLOC(PAR_SHAPES_T, m->npoints);
par_shapes_mesh* welded = par_shapes_weld(m, epsilon, weldmap);
par_shapes_compute_normals(welded);
float* pdst = m->normals;
for (int i = 0; i < m->npoints; i++, pdst += 3) {
int d = weldmap[i];
float const* pnormal = welded->normals + d * 3;
pdst[0] = pnormal[0];
pdst[1] = pnormal[1];
pdst[2] = pnormal[2];
}
PAR_FREE(weldmap);
par_shapes_free_mesh(welded);
}
par_shapes_mesh* par_shapes_create_cylinder(int slices, int stacks)
{
if (slices < 3 || stacks < 1) {
return 0;
}
return par_shapes_create_parametric(par_shapes__cylinder, slices,
stacks, 0);
}
par_shapes_mesh* par_shapes_create_cone(int slices, int stacks)
{
if (slices < 3 || stacks < 1) {
return 0;
}
return par_shapes_create_parametric(par_shapes__cone, slices,
stacks, 0);
}
par_shapes_mesh* par_shapes_create_parametric_disk(int slices, int stacks)
{
par_shapes_mesh* m = par_shapes_create_cone(slices, stacks);
if (m) {
par_shapes_scale(m, 1.0f, 1.0f, 0.0f);
}
return m;
}
par_shapes_mesh* par_shapes_create_parametric_sphere(int slices, int stacks)
{
if (slices < 3 || stacks < 3) {
return 0;
}
par_shapes_mesh* m = par_shapes_create_parametric(par_shapes__sphere,
slices, stacks, 0);
par_shapes_remove_degenerate(m, par_shapes__epsilon_degenerate_sphere);
return m;
}
par_shapes_mesh* par_shapes_create_hemisphere(int slices, int stacks)
{
if (slices < 3 || stacks < 3) {
return 0;
}
par_shapes_mesh* m = par_shapes_create_parametric(par_shapes__hemisphere,
slices, stacks, 0);
par_shapes_remove_degenerate(m, par_shapes__epsilon_degenerate_sphere);
return m;
}
par_shapes_mesh* par_shapes_create_torus(int slices, int stacks, float radius)
{
if (slices < 3 || stacks < 3) {
return 0;
}
assert(radius <= 1.0 && "Use smaller radius to avoid self-intersection.");
assert(radius >= 0.1 && "Use larger radius to avoid self-intersection.");
void* userdata = (void*) &radius;
return par_shapes_create_parametric(par_shapes__torus, slices,
stacks, userdata);
}
par_shapes_mesh* par_shapes_create_klein_bottle(int slices, int stacks)
{
if (slices < 3 || stacks < 3) {
return 0;
}
par_shapes_mesh* mesh = par_shapes_create_parametric(
par_shapes__klein, slices, stacks, 0);
int face = 0;
for (int stack = 0; stack < stacks; stack++) {
for (int slice = 0; slice < slices; slice++, face += 2) {
if (stack < 27 * stacks / 32) {
par_shapes_invert(mesh, face, 2);
}
}
}
par_shapes__compute_welded_normals(mesh);
return mesh;
}
par_shapes_mesh* par_shapes_create_trefoil_knot(int slices, int stacks,
float radius)
{
if (slices < 3 || stacks < 3) {
return 0;
}
assert(radius <= 3.0 && "Use smaller radius to avoid self-intersection.");
assert(radius >= 0.5 && "Use larger radius to avoid self-intersection.");
void* userdata = (void*) &radius;
return par_shapes_create_parametric(par_shapes__trefoil, slices,
stacks, userdata);
}
par_shapes_mesh* par_shapes_create_plane(int slices, int stacks)
{
if (slices < 1 || stacks < 1) {
return 0;
}
return par_shapes_create_parametric(par_shapes__plane, slices,
stacks, 0);
}
par_shapes_mesh* par_shapes_create_parametric(par_shapes_fn fn,
int slices, int stacks, void* userdata)
{
par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
// Generate verts.
mesh->npoints = (slices + 1) * (stacks + 1);
mesh->points = PAR_CALLOC(float, 3 * mesh->npoints);
float uv[2];
float xyz[3];
float* points = mesh->points;
for (int stack = 0; stack < stacks + 1; stack++) {
uv[0] = (float) stack / stacks;
for (int slice = 0; slice < slices + 1; slice++) {
uv[1] = (float) slice / slices;
fn(uv, xyz, userdata);
*points++ = xyz[0];
*points++ = xyz[1];
*points++ = xyz[2];
}
}
// Generate texture coordinates.
mesh->tcoords = PAR_CALLOC(float, 2 * mesh->npoints);
float* uvs = mesh->tcoords;
for (int stack = 0; stack < stacks + 1; stack++) {
uv[0] = (float) stack / stacks;
for (int slice = 0; slice < slices + 1; slice++) {
uv[1] = (float) slice / slices;
*uvs++ = uv[0];
*uvs++ = uv[1];
}
}
// Generate faces.
mesh->ntriangles = 2 * slices * stacks;
mesh->triangles = PAR_CALLOC(PAR_SHAPES_T, 3 * mesh->ntriangles);
int v = 0;
PAR_SHAPES_T* face = mesh->triangles;
for (int stack = 0; stack < stacks; stack++) {
for (int slice = 0; slice < slices; slice++) {
int next = slice + 1;
*face++ = v + slice + slices + 1;
*face++ = v + next;
*face++ = v + slice;
*face++ = v + slice + slices + 1;
*face++ = v + next + slices + 1;
*face++ = v + next;
}
v += slices + 1;
}
par_shapes__compute_welded_normals(mesh);
return mesh;
}
void par_shapes_free_mesh(par_shapes_mesh* mesh)
{
PAR_FREE(mesh->points);
PAR_FREE(mesh->triangles);
PAR_FREE(mesh->normals);
PAR_FREE(mesh->tcoords);
PAR_FREE(mesh);
}
void par_shapes_export(par_shapes_mesh const* mesh, char const* filename)
{
FILE* objfile = fopen(filename, "wt");
float const* points = mesh->points;
float const* tcoords = mesh->tcoords;
float const* norms = mesh->normals;
PAR_SHAPES_T const* indices = mesh->triangles;
if (tcoords && norms) {
for (int nvert = 0; nvert < mesh->npoints; nvert++) {
fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
fprintf(objfile, "vt %f %f\n", tcoords[0], tcoords[1]);
fprintf(objfile, "vn %f %f %f\n", norms[0], norms[1], norms[2]);
points += 3;
norms += 3;
tcoords += 2;
}
for (int nface = 0; nface < mesh->ntriangles; nface++) {
int a = 1 + *indices++;
int b = 1 + *indices++;
int c = 1 + *indices++;
fprintf(objfile, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
a, a, a, b, b, b, c, c, c);
}
} else if (norms) {
for (int nvert = 0; nvert < mesh->npoints; nvert++) {
fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
fprintf(objfile, "vn %f %f %f\n", norms[0], norms[1], norms[2]);
points += 3;
norms += 3;
}
for (int nface = 0; nface < mesh->ntriangles; nface++) {
int a = 1 + *indices++;
int b = 1 + *indices++;
int c = 1 + *indices++;
fprintf(objfile, "f %d//%d %d//%d %d//%d\n", a, a, b, b, c, c);
}
} else if (tcoords) {
for (int nvert = 0; nvert < mesh->npoints; nvert++) {
fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
fprintf(objfile, "vt %f %f\n", tcoords[0], tcoords[1]);
points += 3;
tcoords += 2;
}
for (int nface = 0; nface < mesh->ntriangles; nface++) {
int a = 1 + *indices++;
int b = 1 + *indices++;
int c = 1 + *indices++;
fprintf(objfile, "f %d/%d %d/%d %d/%d\n", a, a, b, b, c, c);
}
} else {
for (int nvert = 0; nvert < mesh->npoints; nvert++) {
fprintf(objfile, "v %f %f %f\n", points[0], points[1], points[2]);
points += 3;
}
for (int nface = 0; nface < mesh->ntriangles; nface++) {
int a = 1 + *indices++;
int b = 1 + *indices++;
int c = 1 + *indices++;
fprintf(objfile, "f %d %d %d\n", a, b, c);
}
}
fclose(objfile);
}
static void par_shapes__sphere(float const* uv, float* xyz, void* userdata)
{
float phi = uv[0] * PAR_PI;
float theta = uv[1] * 2 * PAR_PI;
xyz[0] = cosf(theta) * sinf(phi);
xyz[1] = sinf(theta) * sinf(phi);
xyz[2] = cosf(phi);
}
static void par_shapes__hemisphere(float const* uv, float* xyz, void* userdata)
{
float phi = uv[0] * PAR_PI;
float theta = uv[1] * PAR_PI;
xyz[0] = cosf(theta) * sinf(phi);
xyz[1] = sinf(theta) * sinf(phi);
xyz[2] = cosf(phi);
}
static void par_shapes__plane(float const* uv, float* xyz, void* userdata)
{
xyz[0] = uv[0];
xyz[1] = uv[1];
xyz[2] = 0;
}
static void par_shapes__klein(float const* uv, float* xyz, void* userdata)
{
float u = uv[0] * PAR_PI;
float v = uv[1] * 2 * PAR_PI;
u = u * 2;
if (u < PAR_PI) {
xyz[0] = 3 * cosf(u) * (1 + sinf(u)) + (2 * (1 - cosf(u) / 2)) *
cosf(u) * cosf(v);
xyz[2] = -8 * sinf(u) - 2 * (1 - cosf(u) / 2) * sinf(u) * cosf(v);
} else {
xyz[0] = 3 * cosf(u) * (1 + sinf(u)) + (2 * (1 - cosf(u) / 2)) *
cosf(v + PAR_PI);
xyz[2] = -8 * sinf(u);
}
xyz[1] = -2 * (1 - cosf(u) / 2) * sinf(v);
}
static void par_shapes__cylinder(float const* uv, float* xyz, void* userdata)
{
float theta = uv[1] * 2 * PAR_PI;
xyz[0] = sinf(theta);
xyz[1] = cosf(theta);
xyz[2] = uv[0];
}
static void par_shapes__cone(float const* uv, float* xyz, void* userdata)
{
float r = 1.0f - uv[0];
float theta = uv[1] * 2 * PAR_PI;
xyz[0] = r * sinf(theta);
xyz[1] = r * cosf(theta);
xyz[2] = uv[0];
}
static void par_shapes__torus(float const* uv, float* xyz, void* userdata)
{
float major = 1;
float minor = *((float*) userdata);
float theta = uv[0] * 2 * PAR_PI;
float phi = uv[1] * 2 * PAR_PI;
float beta = major + minor * cosf(phi);
xyz[0] = cosf(theta) * beta;
xyz[1] = sinf(theta) * beta;
xyz[2] = sinf(phi) * minor;
}
static void par_shapes__trefoil(float const* uv, float* xyz, void* userdata)
{
float minor = *((float*) userdata);
const float a = 0.5f;
const float b = 0.3f;
const float c = 0.5f;
const float d = minor * 0.1f;
const float u = (1 - uv[0]) * 4 * PAR_PI;
const float v = uv[1] * 2 * PAR_PI;
const float r = a + b * cos(1.5f * u);
const float x = r * cos(u);
const float y = r * sin(u);
const float z = c * sin(1.5f * u);
float q[3];
q[0] =
-1.5f * b * sin(1.5f * u) * cos(u) - (a + b * cos(1.5f * u)) * sin(u);
q[1] =
-1.5f * b * sin(1.5f * u) * sin(u) + (a + b * cos(1.5f * u)) * cos(u);
q[2] = 1.5f * c * cos(1.5f * u);
par_shapes__normalize3(q);
float qvn[3] = {q[1], -q[0], 0};
par_shapes__normalize3(qvn);
float ww[3];
par_shapes__cross3(ww, q, qvn);
xyz[0] = x + d * (qvn[0] * cos(v) + ww[0] * sin(v));
xyz[1] = y + d * (qvn[1] * cos(v) + ww[1] * sin(v));
xyz[2] = z + d * ww[2] * sin(v);
}
void par_shapes_set_epsilon_welded_normals(float epsilon) {
par_shapes__epsilon_welded_normals = epsilon;
}
void par_shapes_set_epsilon_degenerate_sphere(float epsilon) {
par_shapes__epsilon_degenerate_sphere = epsilon;
}
void par_shapes_merge(par_shapes_mesh* dst, par_shapes_mesh const* src)
{
PAR_SHAPES_T offset = dst->npoints;
int npoints = dst->npoints + src->npoints;
int vecsize = sizeof(float) * 3;
dst->points = PAR_REALLOC(float, dst->points, 3 * npoints);
memcpy(dst->points + 3 * dst->npoints, src->points, vecsize * src->npoints);
dst->npoints = npoints;
if (src->normals || dst->normals) {
dst->normals = PAR_REALLOC(float, dst->normals, 3 * npoints);
if (src->normals) {
memcpy(dst->normals + 3 * offset, src->normals,
vecsize * src->npoints);
}
}
if (src->tcoords || dst->tcoords) {
int uvsize = sizeof(float) * 2;
dst->tcoords = PAR_REALLOC(float, dst->tcoords, 2 * npoints);
if (src->tcoords) {
memcpy(dst->tcoords + 2 * offset, src->tcoords,
uvsize * src->npoints);
}
}
int ntriangles = dst->ntriangles + src->ntriangles;
dst->triangles = PAR_REALLOC(PAR_SHAPES_T, dst->triangles, 3 * ntriangles);
PAR_SHAPES_T* ptriangles = dst->triangles + 3 * dst->ntriangles;
PAR_SHAPES_T const* striangles = src->triangles;
for (int i = 0; i < src->ntriangles; i++) {
*ptriangles++ = offset + *striangles++;
*ptriangles++ = offset + *striangles++;
*ptriangles++ = offset + *striangles++;
}
dst->ntriangles = ntriangles;
}
par_shapes_mesh* par_shapes_create_disk(float radius, int slices,
float const* center, float const* normal)
{
par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
mesh->npoints = slices + 1;
mesh->points = PAR_MALLOC(float, 3 * mesh->npoints);
float* points = mesh->points;
*points++ = 0;
*points++ = 0;
*points++ = 0;
for (int i = 0; i < slices; i++) {
float theta = i * PAR_PI * 2 / slices;
*points++ = radius * cos(theta);
*points++ = radius * sin(theta);
*points++ = 0;
}
float nnormal[3] = {normal[0], normal[1], normal[2]};
par_shapes__normalize3(nnormal);
mesh->normals = PAR_MALLOC(float, 3 * mesh->npoints);
float* norms = mesh->normals;
for (int i = 0; i < mesh->npoints; i++) {
*norms++ = nnormal[0];
*norms++ = nnormal[1];
*norms++ = nnormal[2];
}
mesh->ntriangles = slices;
mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, 3 * mesh->ntriangles);
PAR_SHAPES_T* triangles = mesh->triangles;
for (int i = 0; i < slices; i++) {
*triangles++ = 0;
*triangles++ = 1 + i;
*triangles++ = 1 + (i + 1) % slices;
}
float k[3] = {0, 0, -1};
float axis[3];
par_shapes__cross3(axis, nnormal, k);
par_shapes__normalize3(axis);
par_shapes_rotate(mesh, acos(nnormal[2]), axis);
par_shapes_translate(mesh, center[0], center[1], center[2]);
return mesh;
}
par_shapes_mesh* par_shapes_create_empty()
{
return PAR_CALLOC(par_shapes_mesh, 1);
}
void par_shapes_translate(par_shapes_mesh* m, float x, float y, float z)
{
float* points = m->points;
for (int i = 0; i < m->npoints; i++) {
*points++ += x;
*points++ += y;
*points++ += z;
}
}
void par_shapes_rotate(par_shapes_mesh* mesh, float radians, float const* axis)
{
float s = sinf(radians);
float c = cosf(radians);
float x = axis[0];
float y = axis[1];
float z = axis[2];
float xy = x * y;
float yz = y * z;
float zx = z * x;
float oneMinusC = 1.0f - c;
float col0[3] = {
(((x * x) * oneMinusC) + c),
((xy * oneMinusC) + (z * s)), ((zx * oneMinusC) - (y * s))
};
float col1[3] = {
((xy * oneMinusC) - (z * s)),
(((y * y) * oneMinusC) + c), ((yz * oneMinusC) + (x * s))
};
float col2[3] = {
((zx * oneMinusC) + (y * s)),
((yz * oneMinusC) - (x * s)), (((z * z) * oneMinusC) + c)
};
float* p = mesh->points;
for (int i = 0; i < mesh->npoints; i++, p += 3) {
float x = col0[0] * p[0] + col1[0] * p[1] + col2[0] * p[2];
float y = col0[1] * p[0] + col1[1] * p[1] + col2[1] * p[2];
float z = col0[2] * p[0] + col1[2] * p[1] + col2[2] * p[2];
p[0] = x;
p[1] = y;
p[2] = z;
}
float* n = mesh->normals;
if (n) {
for (int i = 0; i < mesh->npoints; i++, n += 3) {
float x = col0[0] * n[0] + col1[0] * n[1] + col2[0] * n[2];
float y = col0[1] * n[0] + col1[1] * n[1] + col2[1] * n[2];
float z = col0[2] * n[0] + col1[2] * n[1] + col2[2] * n[2];
n[0] = x;
n[1] = y;
n[2] = z;
}
}
}
void par_shapes_scale(par_shapes_mesh* m, float x, float y, float z)
{
float* points = m->points;
for (int i = 0; i < m->npoints; i++) {
*points++ *= x;
*points++ *= y;
*points++ *= z;
}
float* n = m->normals;
if (n && !(x == y && y == z)) {
bool x_zero = x == 0;
bool y_zero = y == 0;
bool z_zero = z == 0;
if (!x_zero && !y_zero && !z_zero) {
x = 1.0f / x;
y = 1.0f / y;
z = 1.0f / z;
} else {
x = x_zero && !y_zero && !z_zero;
y = y_zero && !x_zero && !z_zero;
z = z_zero && !x_zero && !y_zero;
}
for (int i = 0; i < m->npoints; i++, n += 3) {
n[0] *= x;
n[1] *= y;
n[2] *= z;
par_shapes__normalize3(n);
}
}
}
void par_shapes_merge_and_free(par_shapes_mesh* dst, par_shapes_mesh* src)
{
par_shapes_merge(dst, src);
par_shapes_free_mesh(src);
}
void par_shapes_compute_aabb(par_shapes_mesh const* m, float* aabb)
{
float* points = m->points;
aabb[0] = aabb[3] = points[0];
aabb[1] = aabb[4] = points[1];
aabb[2] = aabb[5] = points[2];
points += 3;
for (int i = 1; i < m->npoints; i++, points += 3) {
aabb[0] = PAR_MIN(points[0], aabb[0]);
aabb[1] = PAR_MIN(points[1], aabb[1]);
aabb[2] = PAR_MIN(points[2], aabb[2]);
aabb[3] = PAR_MAX(points[0], aabb[3]);
aabb[4] = PAR_MAX(points[1], aabb[4]);
aabb[5] = PAR_MAX(points[2], aabb[5]);
}
}
void par_shapes_invert(par_shapes_mesh* m, int face, int nfaces)
{
nfaces = nfaces ? nfaces : m->ntriangles;
PAR_SHAPES_T* tri = m->triangles + face * 3;
for (int i = 0; i < nfaces; i++) {
PAR_SWAP(PAR_SHAPES_T, tri[0], tri[2]);
tri += 3;
}
}
par_shapes_mesh* par_shapes_create_icosahedron()
{
static float verts[] = {
0.000, 0.000, 1.000,
0.894, 0.000, 0.447,
0.276, 0.851, 0.447,
-0.724, 0.526, 0.447,
-0.724, -0.526, 0.447,
0.276, -0.851, 0.447,
0.724, 0.526, -0.447,
-0.276, 0.851, -0.447,
-0.894, 0.000, -0.447,
-0.276, -0.851, -0.447,
0.724, -0.526, -0.447,
0.000, 0.000, -1.000
};
static PAR_SHAPES_T faces[] = {
0,1,2,
0,2,3,
0,3,4,
0,4,5,
0,5,1,
7,6,11,
8,7,11,
9,8,11,
10,9,11,
6,10,11,
6,2,1,
7,3,2,
8,4,3,
9,5,4,
10,1,5,
6,7,2,
7,8,3,
8,9,4,
9,10,5,
10,6,1
};
par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
mesh->npoints = sizeof(verts) / sizeof(verts[0]) / 3;
mesh->points = PAR_MALLOC(float, sizeof(verts) / 4);
memcpy(mesh->points, verts, sizeof(verts));
mesh->ntriangles = sizeof(faces) / sizeof(faces[0]) / 3;
mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, sizeof(faces) / 2);
memcpy(mesh->triangles, faces, sizeof(faces));
return mesh;
}
par_shapes_mesh* par_shapes_create_dodecahedron()
{
static float verts[20 * 3] = {
0.607, 0.000, 0.795,
0.188, 0.577, 0.795,
-0.491, 0.357, 0.795,
-0.491, -0.357, 0.795,
0.188, -0.577, 0.795,
0.982, 0.000, 0.188,
0.304, 0.934, 0.188,
-0.795, 0.577, 0.188,
-0.795, -0.577, 0.188,
0.304, -0.934, 0.188,
0.795, 0.577, -0.188,
-0.304, 0.934, -0.188,
-0.982, 0.000, -0.188,
-0.304, -0.934, -0.188,
0.795, -0.577, -0.188,
0.491, 0.357, -0.795,
-0.188, 0.577, -0.795,
-0.607, 0.000, -0.795,
-0.188, -0.577, -0.795,
0.491, -0.357, -0.795,
};
static PAR_SHAPES_T pentagons[12 * 5] = {
0,1,2,3,4,
5,10,6,1,0,
6,11,7,2,1,
7,12,8,3,2,
8,13,9,4,3,
9,14,5,0,4,
15,16,11,6,10,
16,17,12,7,11,
17,18,13,8,12,
18,19,14,9,13,
19,15,10,5,14,
19,18,17,16,15
};
int npentagons = sizeof(pentagons) / sizeof(pentagons[0]) / 5;
par_shapes_mesh* mesh = PAR_CALLOC(par_shapes_mesh, 1);
int ncorners = sizeof(verts) / sizeof(verts[0]) / 3;
mesh->npoints = ncorners;
mesh->points = PAR_MALLOC(float, mesh->npoints * 3);
memcpy(mesh->points, verts, sizeof(verts));
PAR_SHAPES_T const* pentagon = pentagons;
mesh->ntriangles = npentagons * 3;
mesh->triangles = PAR_MALLOC(PAR_SHAPES_T, mesh->ntriangles * 3);
PAR_SHAPES_T* tris = mesh->triangles;
for (int p = 0; p < npentagons; p++, pentagon += 5) {
*tris++ = pentagon[0];
*tris++ = pentagon[1];
*tris++ = pentagon[2];
*tris++ = pentagon[0];
*tris++ = pentagon[2];
*tris++ = pentagon[3];
*tris++ = pentagon[0];
*tris++ = pentagon[3];
*tris++ = pentagon[4];
}
return mesh;
}
par_shapes_mesh* par_shapes_create_octahedron()
{
static float verts[6 * 3] = {
0.000, 0.000, 1.000,
1.000, 0.000, 0.000,
0.000, 1.000, 0.000,
-1.000, 0.000, 0.000,
0.000, -1.000, 0.000,
0.000, 0.000, -1.000
};
static PAR_SHAPES_T triangles[8 * 3] = {
0,1,2,
0,2,3,