forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurveplot.cpp
More file actions
1159 lines (1009 loc) · 37.5 KB
/
curveplot.cpp
File metadata and controls
1159 lines (1009 loc) · 37.5 KB
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
// curveplot.cpp
//
// Copyright (C) 2009-2010 Chris Laurel <claurel@gmail.com>.
//
// curveplot is a module for rendering curves in OpenGL at high precision. A
// plot is a series of cubic curves. The curves are transformed
// to camera space in software because double precision is absolutely
// required. The cubics are adaptively subdivided based on distance from
// the camera position.
//
// curveplot is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// Alternatively, 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.
//
// curveplot 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 Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// CurvePlot. If not, see <http://www.gnu.org/licenses/>.
#define DEBUG_ADAPTIVE_SPLINE 0
#if DEBUG_ADAPTIVE_SPLINE
#define USE_VERTEX_BUFFER 0
#else
#define USE_VERTEX_BUFFER 1
#endif
#include "glsupport.h"
#include "curveplot.h"
#include "shadermanager.h"
#include <vector>
#include <iostream>
using namespace std;
using namespace Eigen;
static const unsigned int SubdivisionFactor = 8;
static const double InvSubdivisionFactor = 1.0 / (double) SubdivisionFactor;
#if DEBUG_ADAPTIVE_SPLINE
static float SplineColors[10][3] = {
{ 0, 0, 1 },
{ 0, 1, 1 },
{ 0, 1, 0 },
{ 1, 1, 0 },
{ 1, 0, 0 },
{ 1, 0, 1 },
{ 0.5f, 0.5f, 1.0f },
{ 0.5f, 1.0f, 1.0f },
{ 0.5f, 1.0f, 0.5f },
{ 1.0f, 1.0f, 0.5f },
};
static unsigned int SegmentCounts[32];
#endif
#ifndef EIGEN_VECTORIZE
// Vectorization should be enabled for improved performance.
#endif
// Convert a 3-vector to a 4-vector by adding a zero
static inline Vector4d zeroExtend(const Vector3d& v)
{
return Vector4d(v.x(), v.y(), v.z(), 0.0);
}
class HighPrec_Frustum
{
public:
HighPrec_Frustum(double nearZ, double farZ, const Vector3d planeNormals[]) :
m_nearZ(nearZ),
m_farZ(farZ)
{
for (unsigned int i = 0; i < 4; i++)
{
m_planeNormals[i] = zeroExtend(planeNormals[i]);
}
}
inline bool cullSphere(const Vector3d& center,
double radius) const
{
return (center.z() - radius > m_nearZ ||
center.z() + radius < m_farZ ||
center.dot(m_planeNormals[0].head(3)) < -radius ||
center.dot(m_planeNormals[1].head(3)) < -radius ||
center.dot(m_planeNormals[2].head(3)) < -radius ||
center.dot(m_planeNormals[3].head(3)) < -radius);
}
inline bool cullSphere(const Vector4d& center,
double radius) const
{
return (center.z() - radius > m_nearZ ||
center.z() + radius < m_farZ ||
center.dot(m_planeNormals[0]) < -radius ||
center.dot(m_planeNormals[1]) < -radius ||
center.dot(m_planeNormals[2]) < -radius ||
center.dot(m_planeNormals[3]) < -radius);
}
inline double nearZ() const { return m_nearZ; }
inline double farZ() const { return m_farZ; }
private:
double m_nearZ;
double m_farZ;
Vector4d m_planeNormals[4];
};
static inline Matrix4d cubicHermiteCoefficients(const Vector4d& p0,
const Vector4d& p1,
const Vector4d& v0,
const Vector4d& v1)
{
Matrix4d coeff;
coeff.col(0) = p0;
coeff.col(1) = v0;
coeff.col(2) = 3.0 * (p1 - p0) - (2.0 * v0 + v1);
coeff.col(3) = 2.0 * (p0 - p1) + (v1 + v0);
return coeff;
}
// Test a point to see if it lies within the frustum defined by
// planes z=nearZ, z=farZ, and the four side planes with specified
// normals.
#if 0
static inline bool frustumCull(const Vector4d& curvePoint,
double curveBoundingRadius,
double nearZ, double farZ,
const Vector4d viewFrustumPlaneNormals[])
{
return (curvePoint.z() - curveBoundingRadius > nearZ ||
curvePoint.z() + curveBoundingRadius < farZ ||
curvePoint.dot(viewFrustumPlaneNormals[0]) < -curveBoundingRadius ||
curvePoint.dot(viewFrustumPlaneNormals[1]) < -curveBoundingRadius ||
curvePoint.dot(viewFrustumPlaneNormals[2]) < -curveBoundingRadius ||
curvePoint.dot(viewFrustumPlaneNormals[3]) < -curveBoundingRadius);
}
#endif
class HighPrec_VertexBuffer
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
HighPrec_VertexBuffer() :
currentPosition(0),
capacity(4096),
data(nullptr),
vbobj(0),
currentStripLength(0),
lineAsTriangles(false)
{
data = new Vertex[(capacity + 1) * 2];
}
~HighPrec_VertexBuffer()
{
delete[] data;
}
void setup(bool lineAsTriangles)
{
#if USE_VERTEX_BUFFER
stripLengths.clear();
currentStripLength = 0;
currentPosition = 0;
this->lineAsTriangles = lineAsTriangles;
if (vbobj)
{
glBindBuffer(GL_ARRAY_BUFFER, vbobj);
}
glEnableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex);
glEnableVertexAttribArray(CelestiaGLProgram::ColorAttributeIndex);
int stride = lineAsTriangles ? sizeof(Vertex) : sizeof(Vertex) * 2;
Vector4f* vertexBase = vbobj ? (Vector4f*) offsetof(Vertex, position) : &data[0].position;
glVertexAttribPointer(CelestiaGLProgram::VertexCoordAttributeIndex,
3, GL_FLOAT, GL_FALSE, stride, vertexBase);
Vector4f* colorBase = vbobj ? (Vector4f*) offsetof(Vertex, color) : &data[0].color;
glVertexAttribPointer(CelestiaGLProgram::ColorAttributeIndex,
4, GL_FLOAT, GL_FALSE, stride, colorBase);
if (lineAsTriangles)
{
glEnableVertexAttribArray(CelestiaGLProgram::NextVCoordAttributeIndex);
glEnableVertexAttribArray(CelestiaGLProgram::ScaleFactorAttributeIndex);
float* scaleBase = vbobj ? (float*) offsetof(Vertex, scale) : &data[0].scale;
glVertexAttribPointer(CelestiaGLProgram::ScaleFactorAttributeIndex,
1, GL_FLOAT, GL_FALSE, stride, scaleBase);
Vector4f* nextVertexBase = vbobj ? (Vector4f*) (offsetof(Vertex, position) + (2 * sizeof(Vertex))) : &data[2].position;
glVertexAttribPointer(CelestiaGLProgram::NextVCoordAttributeIndex,
4, GL_FLOAT, GL_FALSE, stride, nextVertexBase);
}
#endif
}
void finish()
{
#if USE_VERTEX_BUFFER
glDisableVertexAttribArray(CelestiaGLProgram::ColorAttributeIndex);
glDisableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex);
if (lineAsTriangles)
{
glDisableVertexAttribArray(CelestiaGLProgram::NextVCoordAttributeIndex);
glDisableVertexAttribArray(CelestiaGLProgram::ScaleFactorAttributeIndex);
}
if (vbobj)
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
}
inline void vertex(const Vector3d& v)
{
#if USE_VERTEX_BUFFER
Vector3f pos = v.cast<float>();
int index = currentPosition * 2;
data[index].position.segment<3>(0) = pos;
data[index].color = color;
data[index].scale = -0.5f;
data[index + 1].position.segment<3>(0) = pos;
data[index + 1].color = color;
data[index + 1].scale = 0.5f;
++currentPosition;
++currentStripLength;
if (currentPosition == capacity)
{
flush();
data[0].position.segment<3>(0) = pos;
data[0].color = color;
data[0].scale = -0.5f;
data[1].position.segment<3>(0) = pos;
data[1].color = color;
data[1].scale = 0.5f;
currentPosition = 1;
currentStripLength = 1;
}
#else
glVertex3dv(v.data());
#endif
}
inline void vertex(const Vector4d& v)
{
vertex(v, color);
}
inline void vertex(const Vector4d& v, const Vector4f& color)
{
#if USE_VERTEX_BUFFER
Vector4f pos = v.cast<float>();
int index = currentPosition * 2;
data[index].position = pos;
data[index].color = color;
data[index].scale = -0.5f;
data[index + 1].position = pos;
data[index + 1].color = color;
data[index + 1].scale = 0.5f;
++currentPosition;
++currentStripLength;
if (currentPosition == capacity)
{
flush();
data[0].position = pos;
data[0].color = color;
data[0].scale = -0.5f;
data[1].position = pos;
data[1].color = color;
data[1].scale = 0.5f;
currentPosition = 1;
currentStripLength = 1;
}
#else
glColor4fv(color.data());
glVertex3dv(v.data());
#endif
}
inline void begin()
{
#if !USE_VERTEX_BUFFER
glBegin(GL_LINE_STRIP);
#endif
}
inline void end(bool flushIfNeeded = true)
{
#if USE_VERTEX_BUFFER
if (currentPosition > 1)
{
int index = currentPosition * 2;
memcpy(&data[index], &data[index - 4], 2 * sizeof(Vertex));
currentPosition += 1;
}
stripLengths.push_back(currentStripLength);
currentStripLength = 0;
if (flushIfNeeded && currentPosition == capacity)
flush(false);
#else
glEnd();
#endif
}
inline void flush(bool endIfNeeded = true)
{
#if USE_VERTEX_BUFFER
if (currentPosition > 0)
{
// Finish the current line strip
if (endIfNeeded && currentStripLength > 1)
end(false);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex) * currentPosition * 2, data);
unsigned int startIndex = 0;
for (vector<unsigned int>::const_iterator iter = stripLengths.begin(); iter != stripLengths.end(); ++iter)
{
int lineCount = *iter;
if (lineAsTriangles)
glDrawArrays(GL_TRIANGLE_STRIP, startIndex * 2, lineCount * 2);
else
glDrawArrays(GL_LINE_STRIP, startIndex, lineCount);
startIndex += lineCount + 1;
}
currentPosition = 0;
stripLengths.clear();
}
currentStripLength = 0;
#endif
}
void createVertexBuffer()
{
#if USE_VERTEX_BUFFER
if (!vbobj)
{
glGenBuffers(1, &vbobj);
glBindBuffer(GL_ARRAY_BUFFER, vbobj);
glBufferData(GL_ARRAY_BUFFER,
(2 * (capacity + 1)) * sizeof(Vertex),
nullptr,
GL_STREAM_DRAW);
}
#endif
}
void setColor(const Vector4f &aColor)
{
#if USE_VERTEX_BUFFER
color = aColor;
#else
glColor4fv(aColor.data());
#endif
}
private:
unsigned int currentPosition;
unsigned int capacity;
struct Vertex
{
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Vector4f position;
Vector4f color;
float scale;
};
Vertex* data;
GLuint vbobj;
unsigned int currentStripLength;
vector<unsigned int> stripLengths;
Vector4f color;
bool lineAsTriangles;
};
class HighPrec_RenderContext
{
public:
HighPrec_RenderContext(HighPrec_VertexBuffer& vbuf,
HighPrec_Frustum& viewFrustum,
double subdivisionThreshold) :
m_vbuf(vbuf),
m_viewFrustum(viewFrustum),
m_subdivisionThreshold(subdivisionThreshold)
{
}
~HighPrec_RenderContext()
{
/*
vbuf.flush();
vbuf.finish();
*/
}
// Return the GL restart status: true if the last segment of the
// curve was culled and we need to start a new primitive sequence
// with glBegin().
bool renderCubic(bool restartCurve,
const Matrix4d& coeff,
double t0, double t1,
double curveBoundingRadius,
int depth) const
{
const double dt = (t1 - t0) * InvSubdivisionFactor;
double segmentBoundingRadius = curveBoundingRadius * InvSubdivisionFactor;
#if DEBUG_ADAPTIVE_SPLINE
{
int c = depth % 10;
glColor4f(SplineColors[c][0], SplineColors[c][1], SplineColors[c][2], 1.0f);
++SegmentCounts[depth];
}
#endif
Vector4d lastP = coeff * Vector4d(1.0, t0, t0 * t0, t0 * t0 * t0);
for (unsigned int i = 1; i <= SubdivisionFactor; i++)
{
double t = t0 + dt * i;
Vector4d p = coeff * Vector4d(1.0, t, t * t, t * t * t);
double minDistance = max(-m_viewFrustum.nearZ(), abs(p.z()) - segmentBoundingRadius);
if (segmentBoundingRadius >= m_subdivisionThreshold * minDistance)
{
if (m_viewFrustum.cullSphere(p, segmentBoundingRadius))
{
if (!restartCurve)
{
m_vbuf.end();
restartCurve = true;
}
}
else
{
restartCurve = renderCubic(restartCurve,
coeff, t - dt, t,
segmentBoundingRadius,
depth + 1);
}
}
else
{
#if DEBUG_ADAPTIVE_SPLINE
{
int c = depth % 10;
glColor4f(SplineColors[c][0], SplineColors[c][1], SplineColors[c][2], i % 2 ? 0.25f : 1.0f);
}
#endif
if (restartCurve)
{
m_vbuf.begin();
m_vbuf.vertex(lastP);
restartCurve = false;
}
m_vbuf.vertex(p);
}
lastP = p;
}
return restartCurve;
}
// Return the GL restart status: true if the last segment of the
// curve was culled and we need to start a new primitive sequence
// with glBegin().
bool renderCubicFaded(bool restartCurve,
const Matrix4d& coeff,
double t0, double t1,
const Vector4f& color,
double fadeStart, double fadeRate,
double curveBoundingRadius,
int depth) const
{
const double dt = (t1 - t0) * InvSubdivisionFactor;
double segmentBoundingRadius = curveBoundingRadius * InvSubdivisionFactor;
#if DEBUG_ADAPTIVE_SPLINE
{
int c = depth % 10;
glColor4f(SplineColors[c][0], SplineColors[c][1], SplineColors[c][2], 1.0f);
++SegmentCounts[depth];
}
#endif
Vector4d lastP = coeff * Vector4d(1.0, t0, t0 * t0, t0 * t0 * t0);
double lastOpacity = (t0 - fadeStart) * fadeRate;
lastOpacity = max(0.0, min(1.0, lastOpacity)); // clamp
for (unsigned int i = 1; i <= SubdivisionFactor; i++)
{
double t = t0 + dt * i;
Vector4d p = coeff * Vector4d(1.0, t, t * t, t * t * t);
double opacity = (t - fadeStart) * fadeRate;
opacity = max(0.0, min(1.0, opacity)); // clamp
double minDistance = max(-m_viewFrustum.nearZ(), abs(p.z()) - segmentBoundingRadius);
if (segmentBoundingRadius >= m_subdivisionThreshold * minDistance)
{
if (m_viewFrustum.cullSphere(p, segmentBoundingRadius))
{
if (!restartCurve)
{
m_vbuf.end();
restartCurve = true;
}
}
else
{
restartCurve = renderCubicFaded(restartCurve,
coeff, t - dt, t,
color,
fadeStart, fadeRate,
segmentBoundingRadius,
depth + 1);
}
}
else
{
#if DEBUG_ADAPTIVE_SPLINE
{
int c = depth % 10;
glColor4f(SplineColors[c][0], SplineColors[c][1], SplineColors[c][2], i % 2 ? 0.25f : 1.0f);
}
#endif
if (restartCurve)
{
m_vbuf.begin();
m_vbuf.vertex(lastP, Vector4f(color.x(), color.y(), color.z(), color.w() * float(lastOpacity)));
restartCurve = false;
}
m_vbuf.vertex(p, Vector4f(color.x(), color.y(), color.z(), color.w() * float(opacity)));
}
lastP = p;
lastOpacity = opacity;
}
return restartCurve;
}
private:
HighPrec_VertexBuffer& m_vbuf;
HighPrec_Frustum& m_viewFrustum;
double m_subdivisionThreshold;
};
static HighPrec_VertexBuffer vbuf;
CurvePlot::CurvePlot()
{
}
/** Add a new sample to the path. If the sample time is less than the first time,
* it is added at the end. If it is greater than the last time, it is appended
* to the path. The sample is ignored if it has a time in between the first and
* last times of the path.
*/
void
CurvePlot::addSample(const CurvePlotSample& sample)
{
bool addToBack = false;
if (m_samples.empty() || sample.t > m_samples.back().t)
{
addToBack = true;
}
else if (sample.t < m_samples.front().t)
{
addToBack = false;
}
else
{
// Sample falls within range of current samples; discard it
return;
}
if (addToBack)
m_samples.push_back(sample);
else
m_samples.push_front(sample);
if (m_samples.size() > 1)
{
// Calculate a bounding radius for this segment. No point on the curve will
// be further from the start point than the bounding radius.
if (addToBack)
{
const CurvePlotSample& lastSample = m_samples[m_samples.size() - 2];
double dt = sample.t - lastSample.t;
Matrix4d coeff = cubicHermiteCoefficients(zeroExtend(lastSample.position),
zeroExtend(sample.position),
zeroExtend(lastSample.velocity * dt),
zeroExtend(sample.velocity * dt));
Vector4d extents = coeff.cwiseAbs() * Vector4d(0.0, 1.0, 1.0, 1.0);
m_samples[m_samples.size() - 1].boundingRadius = extents.norm();
}
else
{
const CurvePlotSample& nextSample = m_samples[1];
double dt = nextSample.t - sample.t;
Matrix4d coeff = cubicHermiteCoefficients(zeroExtend(sample.position),
zeroExtend(nextSample.position),
zeroExtend(sample.velocity * dt),
zeroExtend(nextSample.velocity * dt));
Vector4d extents = coeff.cwiseAbs() * Vector4d(0.0, 1.0, 1.0, 1.0);
m_samples[1].boundingRadius = extents.norm();
}
}
}
/** Remove all samples before the specified time.
*/
void
CurvePlot::removeSamplesBefore(double t)
{
while (!m_samples.empty() && m_samples.front().t < t)
{
m_samples.pop_front();
}
}
/** Delete all samples after the specified time.
*/
void
CurvePlot::removeSamplesAfter(double t)
{
while (!m_samples.empty() && m_samples.back().t > t)
{
m_samples.pop_back();
}
}
void
CurvePlot::setDuration(double duration)
{
m_duration = duration;
}
// Trajectory consists of segments, each of which is a cubic
// polynomial.
/** Draw a piecewise curve with transformation and frustum clipping.
*
* @param modelview an affine transformation that will be applied to the curve
* @param nearZ z coordinate of the near plane
* @param farZ z coordinate of the far plane
* @param viewFrustumPlaneNormals array of four normals (top, bottom, left, and right frustum planes)
* @param subdivisionThreshold the threashhold for subdivision
*/
void
CurvePlot::render(const Affine3d& modelview,
double nearZ,
double farZ,
const Vector3d viewFrustumPlaneNormals[],
double subdivisionThreshold,
const Vector4f& color,
bool lineAsTriangles) const
{
// Flag to indicate whether we need to issue a glBegin()
bool restartCurve = true;
const Vector3d& p0_ = m_samples[0].position;
const Vector3d& v0_ = m_samples[0].velocity;
Vector4d p0 = modelview * Vector4d(p0_.x(), p0_.y(), p0_.z(), 1.0);
Vector4d v0 = modelview * Vector4d(v0_.x(), v0_.y(), v0_.z(), 0.0);
HighPrec_Frustum viewFrustum(nearZ, farZ, viewFrustumPlaneNormals);
HighPrec_RenderContext rc(vbuf, viewFrustum, subdivisionThreshold);
#if DEBUG_ADAPTIVE_SPLINE
for (unsigned int i = 0; i < sizeof(SegmentCounts) / sizeof(SegmentCounts[0]); i++)
SegmentCounts[i] = 0;
#endif
vbuf.createVertexBuffer();
vbuf.setup(lineAsTriangles);
vbuf.setColor(color);
for (unsigned int i = 1; i < m_samples.size(); i++)
{
// Transform the points into camera space.
const Vector3d& p1_ = m_samples[i].position;
const Vector3d& v1_ = m_samples[i].velocity;
Vector4d p1 = modelview * Vector4d(p1_.x(), p1_.y(), p1_.z(), 1.0);
Vector4d v1 = modelview * Vector4d(v1_.x(), v1_.y(), v1_.z(), 0.0);
// O(t) is an approximating function for this segment of
// the orbit, with 0 <= t <= 1
// C is the viewer position
// d(t) = |O(t) - C|, the distance from viewer to the
// orbit segment.
double curveBoundingRadius = m_samples[i].boundingRadius;
// Estimate the minimum possible distance from the
// curve to the z=0 plane. If the curve is far enough
// away to be approximated as a straight line, we'll just
// render it. Otherwise, it should be a performance win
// to do a sphere-frustum cull test before subdividing and
// rendering segment.
double minDistance = abs(p0.z()) - curveBoundingRadius;
// Render close segments as splines with adaptive subdivision. The
// subdivisions eliminates kinks between line segments and also
// prevents clipping precision problems that occur when a
// very long line is rendered with a relatively small view
// volume.
if (curveBoundingRadius >= subdivisionThreshold * minDistance)
{
#if DEBUG_ADAPTIVE_SPLINE
++SegmentCounts[0];
#endif
// Skip rendering this section if it lies outside the view
// frustum.
if (viewFrustum.cullSphere(p0, curveBoundingRadius))
{
if (!restartCurve)
{
vbuf.end();
restartCurve = true;
}
}
else
{
double dt = m_samples[i].t - m_samples[i - 1].t;
Matrix4d coeff = cubicHermiteCoefficients(p0, p1, v0 * dt, v1 * dt);
restartCurve = rc.renderCubic(restartCurve, coeff, 0.0, 1.0, curveBoundingRadius, 1);
}
}
else
{
#if DEBUG_ADAPTIVE_SPLINE
glColor4f(SplineColors[0][0], SplineColors[0][1], SplineColors[0][2], 1.0f);
#endif
// Apparent size of curve is small enough that we can approximate
// it as a line.
// Simple cull test--just check the far plane
if (p0.z() + curveBoundingRadius < farZ)
{
if (!restartCurve)
{
vbuf.end();
restartCurve = true;
}
}
else
{
if (restartCurve)
{
vbuf.begin();
vbuf.vertex(p0);
restartCurve = false;
}
vbuf.vertex(p1);
}
}
p0 = p1;
v0 = v1;
}
if (!restartCurve)
{
vbuf.end();
}
vbuf.flush();
vbuf.finish();
#if DEBUG_ADAPTIVE_SPLINE3
for (unsigned int i = 0; SegmentCounts[i] != 0 || i < 3; i++)
{
clog << i << ":" << SegmentCounts[i] << ", ";
}
clog << endl;
#endif
}
/** Draw some range of a piecewise curve with transformation and frustum clipping.
*
* @param modelview an affine transformation that will be applied to the curve
* @param nearZ z coordinate of the near plane
* @param farZ z coordinate of the far plane
* @param viewFrustumPlaneNormals array of four normals (top, bottom, left, and right frustum planes)
* @param subdivisionThreshold the threashhold for subdivision
* @param startTime the beginning of the time interval
* @param endTime the end of the time interval
*/
void
CurvePlot::render(const Affine3d& modelview,
double nearZ,
double farZ,
const Vector3d viewFrustumPlaneNormals[],
double subdivisionThreshold,
double startTime,
double endTime,
const Vector4f& color,
bool lineAsTriangles) const
{
// Flag to indicate whether we need to issue a glBegin()
bool restartCurve = true;
if (m_samples.empty() || endTime <= m_samples.front().t || startTime >= m_samples.back().t)
return;
// Linear search for the first sample
unsigned int startSample = 0;
while (startSample < m_samples.size() - 1 && startTime > m_samples[startSample].t)
startSample++;
// Start at the first sample with time <= startTime
if (startSample > 0)
startSample--;
const Vector3d& p0_ = m_samples[startSample].position;
const Vector3d& v0_ = m_samples[startSample].velocity;
Vector4d p0 = modelview * Vector4d(p0_.x(), p0_.y(), p0_.z(), 1.0);
Vector4d v0 = modelview * Vector4d(v0_.x(), v0_.y(), v0_.z(), 0.0);
HighPrec_Frustum viewFrustum(nearZ, farZ, viewFrustumPlaneNormals);
HighPrec_RenderContext rc(vbuf, viewFrustum, subdivisionThreshold);
vbuf.createVertexBuffer();
vbuf.setup(lineAsTriangles);
vbuf.setColor(color);
bool firstSegment = true;
bool lastSegment = false;
for (unsigned int i = startSample + 1; i < m_samples.size() && !lastSegment; i++)
{
// Transform the points into camera space.
const Vector3d& p1_ = m_samples[i].position;
const Vector3d& v1_ = m_samples[i].velocity;
Vector4d p1 = modelview * Vector4d(p1_.x(), p1_.y(), p1_.z(), 1.0);
Vector4d v1 = modelview * Vector4d(v1_.x(), v1_.y(), v1_.z(), 0.0);
if (endTime <= m_samples[i].t)
{
lastSegment = true;
}
// O(t) is an approximating function for this segment of
// the orbit, with 0 <= t <= 1
// C is the viewer position
// d(t) = |O(t) - C|, the distance from viewer to the
// orbit segment.
double curveBoundingRadius = m_samples[i].boundingRadius;
// Estimate the minimum possible distance from the
// curve to the z=0 plane. If the curve is far enough
// away to be approximated as a straight line, we'll just
// render it. Otherwise, it should be a performance win
// to do a sphere-frustum cull test before subdividing and
// rendering segment.
double minDistance = abs(p0.z()) - curveBoundingRadius;
// Render close segments as splines with adaptive subdivision. The
// subdivisions eliminates kinks between line segments and also
// prevents clipping precision problems that occur when a
// very long line is rendered with a relatively small view
// volume.
if (curveBoundingRadius >= subdivisionThreshold * minDistance || lastSegment || firstSegment)
{
// Skip rendering this section if it lies outside the view
// frustum.
if (viewFrustum.cullSphere(p0, curveBoundingRadius))
{
if (!restartCurve)
{
vbuf.end();
restartCurve = true;
}
}
else
{
double dt = m_samples[i].t - m_samples[i - 1].t;
double t0 = 0.0;
double t1 = 1.0;
if (firstSegment)
{
t0 = (startTime - m_samples[i - 1].t) / dt;
t0 = std::max(0.0, std::min(1.0, t0));
firstSegment = false;
}
if (lastSegment)
{
t1 = (endTime - m_samples[i - 1].t) / dt;
}
Matrix4d coeff = cubicHermiteCoefficients(p0, p1, v0 * dt, v1 * dt);
restartCurve = rc.renderCubic(restartCurve, coeff, t0, t1, curveBoundingRadius, 1);
}
}
else
{
// Apparent size of curve is small enough that we can approximate
// it as a line.
// Simple cull test--just check the far plane. This is required because
// apparent clipping precision limitations can cause a GPU to draw lines
// that lie completely beyond the far plane.
if (p0.z() + curveBoundingRadius < farZ)
{
if (!restartCurve)
{
vbuf.end();
restartCurve = true;
}
}
else
{
if (restartCurve)
{
vbuf.begin();
vbuf.vertex(p0);
restartCurve = false;
}
vbuf.vertex(p1);
}
}
p0 = p1;
v0 = v1;
}
if (!restartCurve)
{
vbuf.end();
}
vbuf.flush();
vbuf.finish();
}
/** Draw a piecewise cubic curve with transformation and frustum clipping. Only
* the part of the curve between startTime and endTime will be drawn. Additionally,
* the curve is drawn with a fade effect. The curve is at full opacity at fadeStartTime
* and completely transparent at fadeEndTime. fadeStartTime may be greater than
* fadeEndTime--this just means that the fade direction will be reversed.
*
* @param modelview an affine transformation that will be applied to the curve
* @param nearZ z coordinate of the near plane
* @param farZ z coordinate of the far plane
* @param viewFrustumPlaneNormals array of four normals (top, bottom, left, and right frustum planes)
* @param subdivisionThreshold the threashhold for subdivision
* @param startTime the beginning of the time interval
* @param endTime the end of the time interval
* @param fadeStartTime points on the curve before this time are drawn with full opacity
* @param fadeEndTime points on the curve after this time are not drawn
*/
void
CurvePlot::renderFaded(const Eigen::Affine3d& modelview,
double nearZ,