forked from fritzo/jenn3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.C
1795 lines (1651 loc) · 55.1 KB
/
drawing.C
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
/*
This file is part of Jenn.
Copyright 2001-2007 Fritz Obermeyer.
Jenn is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Jenn is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Jenn; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "drawing.h"
#ifdef CYGWIN_HACKS
#define GLUT_STATIC
#endif
#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cmath>
#include <cstring> //for memcpy
#include <utility>
#include <algorithm>
#include <fstream>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#ifndef INFINITY
#define INFINITY 1.0e38f
#endif
//global instance
Drawings::Drawing *drawing = NULL;
namespace Drawings
{
//================ drawing parameters ================
#define SPH_FACTOR 0.7f
#define FILL_FACTOR 0.35f
float WIDTH_LINE;
float WIDTH_BORDER;
#define BASIC_WIDTH_LINE 4.0f
#define BASIC_WIDTH_BORDER 2.0f
#define LINE_OPACITY 0.3f
#define SOLID_OPACITY 0.7f
#define BASE_DENSITY 0.1f
#define CONTRAST_FACTOR 0.3f
#define TINY_FACTOR 0.25f
//detail params
#define CIRC_SCALE 30.0f
#define SPH_SCALE 240.0f
#define LOUSY_FACTOR 0.8f
#define LINE_SCALE 0.004f
#define FACE_SCALE 0.0025f
//#define STRIPED
#define STRIPES 1.0
#define BOWED
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif
#define PROJ_W0 1.0000001f
//testing
//#define TEST_DEPTH
#define NUM_BINS 40
float depth_bins[NUM_BINS];
//================ stl file object (stereolithography) ================
/** STL file format for stereolithography
*
* (1) facet normals are redundant, and must satisfy right-hand rule.
* (2) objects must lie entirely in first octant.
* (3) triangular mesh non self-intersecting.
* (4) no vertex of one triangle lies within the edge of another triangle.
*
* references:
* http://mech.fsv.cvut.cz/~dr/papers/Lisbon04/node2.html
* http://rpdrc.ic.polyu.edu.hk/old_files/stl_introduction.htm
*/
class STL
{
std::fstream file;
//buffer for quad strips
float buff1[3], buff2[3];
bool buffered;
public:
STL (std::string filename);
~STL ();
//internal triangle interface
private:
void point (float x, float y, float z);
void triangle (float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3);
//quad strip interface
public:
void new_quad_strip () { buffered = false; }
void new_segment (float x1, float y1, float z1,
float x2, float y2, float z2);
inline void new_segment (float* v1, float* v2);
};
STL::STL (std::string filename) : file(filename.c_str(), std::ios_base::out)
{
Assert(file.is_open(), "failed to open " << filename << " for writing");
file << "solid jenn3d";
}
STL::~STL ()
{
if (not file) return;
file << "\n\nendsolid jenn3d\n";
file.close();
}
void STL::point (float x, float y, float z)
{
file << "\n vertex " << x << ' ' << y << ' ' << z;
}
void STL::triangle (float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3)
{
//compute facet normal
Vect t12, t13, n;
t12[0] = x2-x1; t12[1] = y2-y1; t12[2] = z2-z1;
t13[0] = x3-x1; t13[1] = y3-y1; t13[2] = z3-z1;
cross3(t12, t13, n);
if (not normalize3(n)) return; //ignore degenerate faces
//write facet
file << "\n\nfacet normal " << n[0] << ' ' << n[1] << ' ' << n[2];
file << "\n outer loop";
point(x1,y1,z1);
point(x2,y2,z2);
point(x3,y3,z3);
file << "\n endloop";
file << "\nendfacet";
}
void STL::new_segment (float x1, float y1, float z1,
float x2, float y2, float z2)
{
if (buffered) {
//draw two triangles forming a quad
triangle (buff1[0], buff1[1], buff1[2],
buff2[0], buff2[1], buff2[2],
x1, y1, z1);
triangle (buff2[0], buff2[1], buff2[2],
x2, y2, z2,
x1, y1, z1);
}
//copy new points two buffer
buff1[0] = x1; buff1[1] = y1; buff1[2] = z1;
buff2[0] = x2; buff2[1] = y2; buff2[2] = z2;
buffered = true;
}
inline void STL::new_segment (float* v1, float* v2)
{
new_segment (v1[0], v1[1], v1[2],
v2[0], v2[1], v2[2]);
}
//================ colors ================
#define START_STATE 1
#define COLOR_LINE 0.5f, 0.5f, 0.5f
#define COLOR_BLACK 0.0f, 0.0f, 0.0f
#define COLOR_WHITE 1.0f, 1.0f, 1.0f
#define COLOR_FACE 0.4f, 0.7f, 1.0f
//#define COLOR_FACE 8.0f, 0.5f, 0.2f
//#define COLOR_FACE 0.2f, 0.5f, 0.8f
float *color_fg; //current foreground drawing color
float *color_fl; //current fill color
const float
C_bg[4] = {COLOR_BG, LINE_OPACITY},
C_iv[4] = {COLOR_BG, 0.0f},
C_ln[4] = {COLOR_LINE, LINE_OPACITY},
C_bl[4] = {COLOR_BLACK, SOLID_OPACITY},
C_wh[4] = {COLOR_WHITE, SOLID_OPACITY},
C_bl_ln[4] = {COLOR_BLACK, 1.0f},
C_wh_ln[4] = {COLOR_WHITE, 1.0f},
C_bb[4] = {C_ln[0], //bubble color
C_ln[1],
C_ln[2],
0.5f*LINE_OPACITY};
typedef float* Color;
Color
c_bg = const_cast<float*>(C_bg),
c_iv = const_cast<float*>(C_iv),
c_ln = const_cast<float*>(C_ln),
c_bl = const_cast<float*>(C_bl),
c_wh = const_cast<float*>(C_wh),
c_bl_ln = const_cast<float*>(C_bl_ln),
c_wh_ln = const_cast<float*>(C_wh_ln),
c_bb = const_cast<float*>(C_bb);
float color_fc[4] = {COLOR_FACE, 0.0f};
static float _result_color[3];
inline Color blend_colors (Color c1, Color c2, float s1, float s2)
{
for (int i=0; i<3; ++i) {
_result_color[i] = s1 * c1[i] + s2 * c2[i];
}
return _result_color;
}
inline Color get_color (float t)
{
float s = (1.0f+CONTRAST_FACTOR) * fabs(t) - CONTRAST_FACTOR;
return blend_colors(color_fg, color_fl, 1.0f-s, s);
}
inline void blend_colors (Color c1, Color c2,
float s1, float s2, float* result)
{
for (int i=0; i<3; ++i) {
result[i] = s1 * c1[i] + s2 * c2[i];
}
}
inline void set_color (float t, float* result)
{
//float s = 1.3f * fabs(t) - 0.3f;
float s = fabs(t);
blend_colors(color_fg, color_fl, 1.0f-s, s, result);
}
inline float modulate (complex t)
{
return 0.5 * (1.0 + sinf(STRIPES * std::arg(t) + elapsed_time()));
}
//optics & opacity
float base_density = BASE_DENSITY;
inline float optical_density (float dx2)
{//calculates opacity from depth-component of normal vector
return 1.0f - expf(-base_density * sqrtf(dx2));
}
inline float optical_density_approx (float dx2)
{//approximate optical density calculation
float t = dx2 * sqr(base_density);
return t / (1.0f + t);
}
//================ drawing class ================
Drawing::Drawing (ToddCoxeter::Graph* g)
: go(g),
graph(*go.graph),
ord(graph.ord),
deg(graph.deg),
ord_f(graph.ord_f),
w_bound0(-1.0f),
w_bound1( 1.0f),
h_bound0(-1.0f),
h_bound1( 1.0f),
cmp(ord),
cmp_f(ord_f),
sorted(ord, 0),
sorted_f(ord_f, 0),
vertices(ord),
centers(ord),
radii0(ord, 1.0f),
scales(ord, 1.0f),
radii(ord, 1.0f),
phases(ord, 0.0f),
faces(graph.faces),
vertices_f(ord_f),
normals(ord_f),
centers_f(ord_f),
_grid_on(false),
_drawing_verts(true),
_drawing_edges(true),
_drawing_faces(true),
_fancy(true),
_hazy(false),
_wireframe(false),
_curved(true),
_high_quality(false),
_clipping(true),
_update_needed(true)
{
logger.info() << "drawing " << ord << " verts, "
<< (ord * deg) / 2 << " edges" |0;
//fill in go board's history
std::vector<std::pair<float,int> > points_i(graph.ord);
for (int i=0; i<graph.ord; ++i) {
const Vect& p = graph.points[i];
float level = p[0]; //approximately lexicographical in [w,z,y,x]
for (int j=1; j<4; ++j) {
level = p[j] + 0.01 * level;
}
points_i[i] = std::make_pair(-level,i);
}
std::sort(points_i.begin(), points_i.end());
for (int i=0; i<graph.ord; ++i) {
go.play(points_i[i].second, START_STATE);
}
points_i.resize(0);
//define standard node radius, all pairs are assumed equidistant
rad0 = 0.5f * r4_dist(graph.points[0], graph.points[graph.adj[0][0]]);
float tot_tube_len = graph.ord * graph.deg * rad0;
set_tube_rad(FILL_FACTOR / sqrtf(tot_tube_len));
coating = 0.05;
//define oscillation phases
for (int v = 0; v < ord; ++v) {
phases[v] = hopf_phase(graph.points[v]);
}
logger.debug() << "oscillation phases defined." |0;
//define pre-projection matrix
mat_identity(project);
logger.debug() << "projection built and set to identity." |0;
//define depth-sorted list
for (int v=0; v<ord; ++v) { sorted [v] = v; }
for (int f=0; f<ord_f; ++f) { sorted_f[f] = f; }
logger.debug() << "sorted built and set to linear order." |0;
//define original polygon
for (int i = 0; i<POLY_SIDES; ++i) {
poly0[i][0] = cos((2.0f*M_PI*i)/POLY_SIDES);
poly0[i][1] = sin((2.0f*M_PI*i)/POLY_SIDES);
}
//define original sphere
for (int i = 0; i<=SPH_RHO; ++i) {
float rho = (0.5f*M_PI*i) / SPH_RHO;
float cos_rho = cos(rho);
float sin_rho = sin(rho);
for (int j=0; j<SPH_THETA; ++j) {
float theta = (2.0f*M_PI*j) / SPH_THETA;
float cos_theta = cos(theta);
float sin_theta = sin(theta);
sphere[i][j][0] = sin_rho * cos_theta;
sphere[i][j][1] = sin_rho * sin_theta;
sphere[i][j][2] = cos_rho;
}
}
}
//interface
void Drawing::set_scale (float _scale)
{
scale = _scale;
set_quality (_high_quality);
}
void Drawing::set_tube_rad (float rad)
{
tube_rad = min(0.8f*rad0, rad);
tube_rad = max(0.01f*rad0, tube_rad);
sph_rad = SPH_FACTOR * tube_rad * sqrtf(graph.deg);
sph_rad = max(tube_rad, sph_rad);
sph_rad0 = sph_rad / rad0;
tube_factor = tube_rad / sph_rad0;
}
void Drawing::set_bounds (float w0, float w1, float h0, float h1)
{
w_bound0 = w0;
w_bound1 = w1;
h_bound0 = h0;
h_bound1 = h1;
}
void Drawing::reproject (Mat& theta)
{
mat_copy(theta, project);
for (int v=0; v<ord; ++v) {
vect_mult(project, graph.points[v], vertices[v]);
update_vertex(v);
}
if (ord_f and _drawing_faces) {
for (int f=0; f<ord_f; ++f) {
vect_mult(project, graph.normals[f], normals[f]);
update_face(f);
}
}
sort();
}
GLenum FILL = GL_FILL;
GLenum LINE_STRIP = GL_LINE_STRIP;
void Drawing::display ()
{
if (_update_needed) _update();
//reset params jacked by windows
glDepthRange(-1.0f, 1.0f);
if (_wireframe) {
glLineWidth(1.0f);
FILL = GL_LINE;
LINE_STRIP = GL_LINES;
} else {
FILL = GL_FILL;
LINE_STRIP = GL_LINE_STRIP;
}
//draw vertices
#ifdef TEST_DEPTH
if (_fancy) {
for (int i=0; i<NUM_BINS; ++i) { depth_bins[i] = 0; }
}
#endif
if (_fancy or _drawing_faces) glEnable (GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
for (int v = 0; v < ord; ++v) {
int u = sorted[v];
if (not _grid_on and go.state(u)==0) continue;
display_vertex(u);
}
#ifdef TEST_DEPTH
if (_fancy) {
for (int i=0; i<NUM_BINS; ++i) { std::cout << depth_bins[i] << "\n"; }
std::cout << "---------------------------" << std::endl;
}
#endif
//draw faces
if (ord_f and _drawing_faces and not (not _curved and _wireframe)) {
glLineWidth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
for (int f = 0; f < ord_f; ++f) {
int g = sorted_f[f];
_draw_face(g);
}
glDepthMask(GL_TRUE);
}
}
STL *export_file = NULL; //a single global export file
void Drawing::export_stl ()
{
if (_update_needed) _update();
//open file
export_file = new STL("jenn_export.stl");
//export
for (int v = 0; v < ord; ++v) {
int u = sorted[v];
if (not _grid_on and go.state(u)==0) continue;
export_vertex(u);
}
delete export_file;
#ifdef __EMSCRIPTEN__
EM_ASM(saveFile("jenn_export.stl"));
#endif
}
void Drawing::export_graph () {
graph.save();
#ifdef __EMSCRIPTEN__
EM_ASM(saveFile("jenn.graph"));
#endif
}
int Drawing::select (float x,float y)
{
if (not _grid_on) return -1;
for (int n = ord-1; n >= 0; --n) {
int v = sorted[n];
if (sqr(x - centers[v][0]) + sqr(y - centers[v][1]) < sqr(radii[v])) {
return v;
}
}
return -1;
}
//depth function
#ifdef TEST_DEPTH
inline float clamp_depth (float z)
{
float result = (2.0f/M_PI) * atanf(z);
++depth_bins[static_cast<int>((NUM_BINS-1)*0.5f*(result+1))];
return result;
}
#else
inline float clamp_depth (float z) { return (2.0f/M_PI) * atanf(0.5f*z); }
inline float clamp_depth_alt (float z) { return z / sqrtf(1.0f+z*z); }
#endif
//projections functions
inline float proj (float w)
{
return fabs(1.0f / (PROJ_W0 + w));
}
inline void stereo_project (Vect& x)
{
float scale = proj(x[3]);
x[0] *= scale;
x[1] *= scale;
x[2] *= scale;
}
inline float stereo_project (const Vect& x, Vect& pi_x)
{
float scale = proj(x[3]);
pi_x[0] = scale * x[0];
pi_x[1] = scale * x[1];
pi_x[2] = scale * x[2];
return scale;
}
inline float stereo_project (const Vect& x, float* pi_x)
{
float scale = proj(x[3]);
pi_x[0] = scale * x[0];
pi_x[1] = scale * x[1];
pi_x[2] = clamp_depth(scale * x[2]);
return scale;
}
inline void stereo_project (const Vect& x, const Vect& dx, Vect& y)
{//projects position and surface cross-section
float s = proj(x[3]);
y[0] = s * x[0];
y[1] = s * x[1];
y[2] = s * x[2];
float dw = - dx[3];
float da2 = sqr(dx[0] + dw * y[0]);
float db2 = sqr(dx[1] + dw * y[1]);
float dc2 = sqr(dx[2] + dw * y[2]);
y[3] = (da2 + db2 + dc2) / dc2; //depth component
}
inline float stereo_project (const Vect& x, const Vect& dx,
Vect& y, Vect& N, Vect& B)
{//projects a tangent vector : R^3 >--> S^3 --> R^3 to normal vectors
//map to tangent space of S^3
float s = inner(x,dx);
Vect pi;
for (int i=0; i<4; ++i) pi[i] = dx[i] - s*x[i];
//don't bother to normalize
//stereo project back to R^3
// pi(x)_i = x_i / (1+x_3)
// pi(x + dx)_i = dx_i / (1+x_3) - x_i dx_3 / (1+x_3)^2
// = dx_i / (1+x_3) - pi(x)_i dx_3 / (1+x_3)
float scale = proj(x[3]);
y[0] = scale * x[0];
y[1] = scale * x[1];
y[2] = scale * x[2];
float p = -pi[3];
pi[0] += p * y[0];
pi[1] += p * y[1];
pi[2] += p * y[2];
//find basis for normal space
cross3(pi, y, N); N[3] = 0;
cross3(N, pi, B); B[3] = 0;
float n = scale / r3_norm(N);
float b = scale / r3_norm(B);
for (int i=0; i<3; ++i) {
N[i] *= n;
B[i] *= b;
}
return scale; // = norm of N, B
}
float Drawing::get_radius ()
{
float max_rad = 0;
Vect projected;
for (int v=0; v<ord; ++v) {
stereo_project(graph.points[v], projected);
max_rad = max(r3_norm(projected), max_rad);
}
return max_rad;
}
//================ drawing primitives ================
int Drawing::get_params ()
{
int params = 0;
params = (params << 1) + _grid_on;
params = (params << 1) + _drawing_verts;
params = (params << 1) + _drawing_edges;
params = (params << 1) + _drawing_faces;
params = (params << 1) + _fancy;
params = (params << 1) + _hazy;
params = (params << 1) + _wireframe;
params = (params << 1) + _curved;
return params;
}
void Drawing::set_params (int params)
{
//reverse order from above!
_curved = params & 1; params >>= 1;
_wireframe = params & 1; params >>= 1;
_hazy = params & 1; params >>= 1;
_fancy = params & 1; params >>= 1;
_drawing_faces = params & 1; params >>= 1;
_drawing_edges = params & 1; params >>= 1;
_drawing_verts = params & 1; params >>= 1;
_grid_on = params & 1; params >>= 1;
update();
}
void Drawing::toggle_fancy () { _fancy = not _fancy; update(); }
void Drawing::toggle_hazy () { _hazy = not _hazy; update(); }
void Drawing::toggle_wireframe () { _wireframe = not _wireframe; update(); }
void Drawing::toggle_curved () { _curved = not _curved; update(); }
void Drawing::set_quality (bool quality)
{
_high_quality = quality;
q_scale = (_high_quality ? 4.0f : 1.0f) * scale;
update();
}
void Drawing::_update ()
{
base_density = _fancy or not _curved ? BASE_DENSITY : 2.0f * BASE_DENSITY;
glShadeModel(GL_SMOOTH);
glEnable(GL_MULTISAMPLE);
if (_fancy) {
c_bl = const_cast<float*>(C_bl_ln);
c_wh = const_cast<float*>(C_wh_ln);
//glDisable(GL_BLEND);
glEnable(GL_MULTISAMPLE);
} else {
c_bl = const_cast<float*>(C_bl);
c_wh = const_cast<float*>(C_wh);
//glEnable(GL_BLEND);
}
if (_hazy) {
glEnable(GL_FOG);
glFogfv(GL_FOG_COLOR, c_bg);
glFogf(GL_FOG_START, 0.0f);
glFogf(GL_FOG_END, 2.0f);
glFogi(GL_FOG_MODE, GL_LINEAR);
//glFogfv(GL_FOG_DENSITY, GL_EXP);
//glFogf(GL_FOG_DENSITY, 0.5f);
} else {
glDisable(GL_FOG);
}
_update_needed = false;
}
//non-isotropic shading function for tubes
inline float max_z (Vect &u, Vect &v, float scale)
{
return scale * sqrtf(1.0f-sqr((u[0]*v[1]-u[1]*v[0]) / sqr(scale)));
}
//face/edge subdivision tools
int Drawing::_num_segments (float w, float dist)
{
if (!_curved) return 1;
float detail = LINE_SCALE * sqrtf(q_scale) * dist * proj(w);
int segs = int(1.0f + LINE_SIDES * detail);
int line_sides = _high_quality ? LINE_SIDES : LINE_SIDES / 2;
if (segs > line_sides) segs = line_sides;
return segs;
}
int Drawing::_secant_stride (float w, float rad)
{
float radius = rad * proj(w);
int step = int(2.0f + CIRC_SCALE/sqrtf(radius * q_scale));
if (step > 10) step = 10;
return step;
}
int Drawing::_num_subdivs (float w, int Nfaces)
{
if (!_curved) return 1;
float detail = Nfaces *FACE_SCALE *sqrtf(q_scale) *rad0 *powf(proj(w),0.8);
int sides = int(1.0f + FACE_SIDES * detail);
int face_sides = _high_quality ? FACE_SIDES : FACE_SIDES / 2;
sides = min(sides, face_sides);
return sides;
}
//================ drawing features ================
void Drawing::_draw_bulb (float* center, float radius)
{
//calculate detail
int step = int(1.0f + CIRC_SCALE/sqrtf(fabs(radius) * q_scale));
if (step > 10) step = 10;
//define transformed polygon
float outer = radius * 1.0f;
float inner = radius * 0.9f;
float depth = clamp_depth(center[2]);
for (unsigned i = 0; i < POLY_SIDES; i+=step) {
poly1[i][0] = center[0] + inner * poly0[i][0];
poly1[i][1] = center[1] + inner * poly0[i][1];
poly1[i][2] = depth;
poly2[i][0] = center[0] + outer * poly0[i][0];
poly2[i][1] = center[1] + outer * poly0[i][1];
poly2[i][2] = depth;
}
glLineWidth(1.5f);
glPolygonMode(GL_FRONT_AND_BACK, FILL);
//draw filled center
glColor4fv(color_fl);
glBegin(GL_POLYGON);
for (unsigned i = 0; i < POLY_SIDES; i+=step) {
glVertex3fv(poly1[i]);
}
glEnd();
//draw outline
glColor4fv(color_fg);
glBegin(GL_QUAD_STRIP);
for (unsigned i = 0; i < POLY_SIDES; i+=step) {
glVertex3fv(poly1[i]);
glVertex3fv(poly2[i]);
}
glVertex3fv(poly1[0]);
glVertex3fv(poly2[0]);
glEnd();
/*
//draw antialiased outlines
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_POLYGON);
for (int i = 0; i < POLY_SIDES; i+=step) {
glVertex3fv(poly2[i]);
}
glEnd();
glColor4fv(color_fl);
glBegin(GL_POLYGON);
for (int i = 0; i < POLY_SIDES; i+=step) {
glVertex3fv(poly1[i]);
}
glEnd();
*/
}
void Drawing::_draw_sphere (float* center, float radius, int v)
{
//calculate detail
int step = int(1 + SPH_SCALE/(fabs(radius) * q_scale));
if (step > 5) step = 6;
else if (step > 4) step = 4;
//drawing flags
glPolygonMode(GL_FRONT, FILL);
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
if (radius < 0) glFrontFace(GL_CW);
for (unsigned i=step; i<=SPH_RHO; i += step) {
float inner_color[3], outer_color[3];
#ifdef STRIPED
float mod = modulate(phases[v]);
set_color(mod * sphere[i-step][0][2], inner_color);
set_color(mod * sphere[ i ][0][2], outer_color);
#else
set_color(sphere[i-step][0][2], inner_color);
set_color(sphere[ i ][0][2], outer_color);
#endif
float inner_depth = clamp_depth(center[2]+radius*sphere[i-step][0][2]);
float outer_depth = clamp_depth(center[2]+radius*sphere[ i ][0][2]);
#ifdef __EMSCRIPTEN__
if (FILL == GL_LINE) {
glBegin(GL_LINE_STRIP);
for (unsigned j=0; j<SPH_THETA; j+=step) {
glColor3fv(inner_color);
glVertex3f(center[0] + radius * sphere[i-step][j][0],
center[1] + radius * sphere[i-step][j][1],
inner_depth);
}
glColor3fv(inner_color);
glVertex3f(center[0] + radius * sphere[i-step][0][0],
center[1] + radius * sphere[i-step][0][1],
inner_depth);
for (unsigned j=0; j<SPH_THETA; j+=step) {
glColor3fv(outer_color);
glVertex3f(center[0] + radius * sphere[i][j][0],
center[1] + radius * sphere[i][j][1],
outer_depth);
}
glColor3fv(outer_color);
glVertex3f(center[0] + radius * sphere[i][0][0],
center[1] + radius * sphere[i][0][1],
outer_depth);
glEnd();
}
glBegin(FILL == GL_FILL ? GL_QUAD_STRIP : GL_LINES);
#else
glBegin(GL_QUAD_STRIP);
#endif
for (unsigned j=0; j<SPH_THETA; j+=step) {
glColor3fv(inner_color);
glVertex3f(center[0] + radius * sphere[i-step][j][0],
center[1] + radius * sphere[i-step][j][1],
inner_depth);
glColor3fv(outer_color);
glVertex3f(center[0] + radius * sphere[i][j][0],
center[1] + radius * sphere[i][j][1],
outer_depth);
}
glColor3fv(inner_color);
glVertex3f(center[0] + radius * sphere[i-step][0][0],
center[1] + radius * sphere[i-step][0][1],
inner_depth);
glColor3fv(outer_color);
glVertex3f(center[0] + radius * sphere[i][0][0],
center[1] + radius * sphere[i][0][1],
outer_depth);
glEnd();
}
glDisable(GL_CULL_FACE);
if (radius < 0) glFrontFace(GL_CCW);
}
void Drawing::_export_sphere (float* center, float radius, int v)
{
radius += coating;
//calculate detail
int step = int(1 + SPH_SCALE/(fabs(radius) * q_scale));
if (step > 5) step = 6;
else if (step > 4) step = 4;
//draw front and back faces
float sign = 1.0f;
for (int side = 0; side <=1; ++side) {
sign = -sign;
for (unsigned i=step; i<=SPH_RHO; i += step) {
float inner_depth = center[2] + sign*radius*sphere[i-step][0][2];
float outer_depth = center[2] + sign*radius*sphere[ i ][0][2];
export_file->new_quad_strip();
for (int j=0; j<SPH_THETA; j+=step) {
export_file->new_segment(
center[0] + sign * radius * sphere[i-step][j][0],
center[1] + radius * sphere[i-step][j][1],
inner_depth,
center[0] + sign * radius * sphere[i][j][0],
center[1] + radius * sphere[i][j][1],
outer_depth
);
}
export_file->new_segment(
center[0] + sign * radius * sphere[i-step][0][0],
center[1] + radius * sphere[i-step][0][1],
inner_depth,
center[0] + sign * radius * sphere[i][0][0],
center[1] + radius * sphere[i][0][1],
outer_depth
);
}
}
}
void Drawing::_draw_arc (Vect& begin, Vect& end, float w)
{//draws a line-based arc from far to near
//check whether the tube needs to be drawn
if (_clipping) { //clipping fails when panning
float s0 = 1/(1+begin[3]);
float s1 = 1/(1+end[3]);
float bx = s0 * begin[0];
float by = s0 * begin[1];
float ex = s1 * end[0];
float ey = s1 * end[1];
if ((bx > w_bound1) and (ex > w_bound1)) return;
if ((bx < w_bound0) and (ex < w_bound0)) return;
if ((by > h_bound1) and (ey > h_bound1)) return;
if ((by < h_bound0) and (ey < h_bound0)) return;
}
//calculate scale
int S = _num_segments(w, rad0);
float line_scale = LINE_SCALE * scale * rad0 * proj(w);
if (line_scale > 1) line_scale = 1;
WIDTH_LINE = line_scale * BASIC_WIDTH_LINE;
glLineWidth(WIDTH_LINE);
glColor4fv(color_fl);
float point[3];
if (S == 1) {
//draw line
glBegin(GL_LINES);
stereo_project(begin, point); glVertex3fv(point);
stereo_project(end, point); glVertex3fv(point);
glEnd();
} else {
//calculate segment locations
glBegin(LINE_STRIP);
for (int s=0; s<=S; ++s) {
Vect temp;
for (int i=0; i<4; ++i) {
temp[i] = s * begin[i] + (S-s) * end[i];
}
normalize(temp);
stereo_project(temp, point);
glVertex3fv(point);
}
glEnd();
}
}
void Drawing::_draw_arc2 (Vect& begin, Vect& end, float w)
{//draws a line-based arc from far to near
//check whether the tube needs to be drawn
if (_clipping) { //clipping fails when panning
float s0 = 1/(1+begin[3]);
float s1 = 1/(1+end[3]);
float bx = s0 * begin[0];
float by = s0 * begin[1];
float ex = s1 * end[0];
float ey = s1 * end[1];
if ((bx > w_bound1) and (ex > w_bound1)) return;
if ((bx < w_bound0) and (ex < w_bound0)) return;
if ((by > h_bound1) and (ey > h_bound1)) return;
if ((by < h_bound0) and (ey < h_bound0)) return;
}
//calculate scale
int S = _num_segments(w, rad0);
float line_scale = LINE_SCALE * q_scale * rad0 * proj(w);
if (line_scale > 1) line_scale = 1;
WIDTH_LINE = line_scale * BASIC_WIDTH_LINE;
WIDTH_BORDER = line_scale * BASIC_WIDTH_BORDER;
if (S == 1) {
//calculate line location
stereo_project(begin, lines[0]);
stereo_project(end, lines[1]);
//draw foreground border
glLineWidth(WIDTH_LINE + 2*WIDTH_BORDER);
glColor4fv(color_fg);
glBegin(GL_LINES);
glVertex3fv(lines[0]);
glVertex3fv(lines[1]);
glEnd();
//center fill
//glEnable(GL_POLYGON_OFFSET_LINE);
glLineWidth(WIDTH_LINE);
glColor4fv(color_fl);
glBegin(GL_LINES);
glVertex3fv(lines[0]);
glVertex3fv(lines[1]);
glEnd();
//glDisable(GL_POLYGON_OFFSET_LINE);
} else {
//calculate segment locations
for (int s=0; s<=S; ++s) {
Vect temp;
for (int i=0; i<4; ++i) {
temp[i] = s * begin[i] + (S-s) * end[i];
}
normalize(temp);
stereo_project(temp, lines[s]);
}
//draw foreground border
glLineWidth(WIDTH_LINE + 2*WIDTH_BORDER);
glColor4fv(color_fg);
glBegin(LINE_STRIP);
for (int s=0; s<=S; ++s) {
glVertex3fv(lines[s]);
}
glEnd();
//center fill
//glEnable(GL_POLYGON_OFFSET_LINE);
glLineWidth(WIDTH_LINE);
glColor4fv(color_fl);
glBegin(LINE_STRIP);
for (int s=0; s<=S; ++s) {
glVertex3fv(lines[s]);
}
glEnd();
//glDisable(GL_POLYGON_OFFSET_LINE);
}
}
void Drawing::_draw_arc_wide (Vect& begin, Vect& end, float w)
{
//calculate scale
int S = _num_segments(w, rad0);