-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopennurbs_gl.cpp
754 lines (672 loc) · 21.7 KB
/
opennurbs_gl.cpp
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
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2011 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Assoicates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
*/
#include "opennurbs.h"
#include "opennurbs_gl.h" // ON_GL() function declarations
void ON_GL( const int order, // ON_NurbsCurve order
const int cv_count, // ON_NurbsCurve cv count
const double* knot, // ON_NurbsCurve knot vector
GLfloat* glknot, // GL knot vector
int bPermitScaling, // true if knot scaling is allowed
double* scale // If not NULL and knot scaling is
// allowed, then the scaling
// parameters are returned here.
// glknot = (knot - scale[0])*scale[1]
)
{
// Because GL uses floats instead of doubles for knot vectors and
// because some GLs are intolerant of closely spaced knots,
// the returned glknots[] may be re-scaled when bPermitScaling
// is true. When the knots belong to a trimmed surface, any rescaling
// done to the surface's knots must be applied to the trimming geometry.
const int knot_count = order + cv_count - 2;
const int nknots = knot_count+2;
// GL knot vectors have old-fashioned extra knot at start and end
const double k0 = ON_SuperfluousKnot( order, cv_count, knot,0);
const double k1 = ON_SuperfluousKnot( order, cv_count, knot,1);
if ( scale ) {
scale[0] = 0.0;
scale[1] = 1.0;
}
int i, j;
if ( bPermitScaling ) {
double d0 = knot[order-2];
double dk = 1.0;
if ( bPermitScaling ) {
double dmin = 1.0;
double dmax = 1.0;
double d;
for ( i = 1; i < knot_count; i++ ) {
d = knot[i] - knot[i-1];
if ( d <= 0.0 )
continue; // multiple knot
if ( d < dmin )
dmin = d;
else if ( d > dmax )
dmax = d;
}
if ( dmin > 0.0 && dmax >= dmin ) {
if ( dmin < 1.0e-2 )
dk = 1.0e-2/dmin;
else if ( dmax > 1.0e4 ) {
if ( 1.0e4*dmin >= 1.0e-2*dmax )
dk = 1.0e4/dmax;
}
}
}
if ( scale ) {
scale[0] = d0;
scale[1] = dk;
}
glknot[0] = (GLfloat)((k0-d0)*dk);
for( i = 1, j = 0; j < knot_count; i++, j++ )
glknot[i] = (GLfloat)((knot[j]-d0)*dk);
glknot[nknots-1] = (GLfloat)((k1-d0)*dk);
}
else {
glknot[0] = (GLfloat)k0;
for( i = 1, j = 0; j < knot_count; i++, j++ )
glknot[i] = (GLfloat)knot[j];
glknot[nknots-1] = (GLfloat)k1;
}
}
static void GetGLCV( const int dim, const int is_rat, const double* cv,
double xform[4][4],
GLfloat* glcv )
{
if ( xform ) {
const double x = cv[0];
const double y = cv[1];
const double z = (dim == 3) ? cv[2] : 0.0;
const double w = (is_rat) ? cv[dim] : 1.0;
glcv[0] = (GLfloat)(xform[0][0]*x + xform[0][1]*y + xform[0][2]*z + xform[0][3]*w);
glcv[1] = (GLfloat)(xform[1][0]*x + xform[1][1]*y + xform[1][2]*z + xform[1][3]*w);
if ( dim == 3 )
glcv[2] = (GLfloat)(xform[2][0]*x + xform[2][1]*y + xform[2][2]*z + xform[2][3]*w);
if ( is_rat )
glcv[dim] = (GLfloat)(xform[3][0]*x + xform[3][1]*y + xform[3][2]*z + xform[3][3]*w);
}
else {
glcv[0] = (GLfloat)cv[0];
glcv[1] = (GLfloat)cv[1];
if ( dim == 3)
glcv[2] = (GLfloat)cv[2];
if ( is_rat )
glcv[dim] = (GLfloat)cv[dim];
}
}
void ON_GL( const ON_NurbsCurve& nurbs_curve,
GLUnurbsObj* nobj, // created with gluNewNurbsRenderer )
GLenum type, // = 0 (and type is automatically set)
int bPermitKnotScaling,
double* knot_scale,
double xform[][4]
)
{
ON_GL( nurbs_curve.Dimension(),
nurbs_curve.IsRational(),
nurbs_curve.Order(),
nurbs_curve.CVCount(),
nurbs_curve.Knot(),
nurbs_curve.m_cv_stride,
nurbs_curve.m_cv,
nobj,
type,
bPermitKnotScaling,
knot_scale,
xform
);
}
void ON_GL( const ON_Curve& curve,
GLUnurbsObj* nobj, // created with gluNewNurbsRenderer )
GLenum type, // = 0 (and type is automatically set)
double xform[][4]
)
{
const ON_PolyCurve* poly_curve = ON_PolyCurve::Cast(&curve);
if ( poly_curve )
{
ON_Curve* pSegmentCurve = 0;
int segment_count = poly_curve->Count();
int i;
for ( i = 0; i < segment_count; i++ ) {
pSegmentCurve = poly_curve->SegmentCurve(i);
if ( pSegmentCurve )
ON_GL( *pSegmentCurve, nobj, type, xform );
}
return;
}
const ON_CurveProxy* curve_proxy = ON_CurveProxy::Cast(&curve);
if ( curve_proxy && !curve_proxy->ProxyCurveIsReversed() )
{
const ON_Curve* real_curve = curve_proxy->ProxyCurve();
if ( 0 == real_curve )
return;
if ( curve_proxy == real_curve )
return;
if ( curve_proxy->ProxyCurveDomain() == real_curve->Domain() )
{
ON_GL( *real_curve, nobj, type, xform );
return;
}
}
{
ON_NurbsCurve tmp;
const ON_NurbsCurve* nurbs_curve = ON_NurbsCurve::Cast(&curve);
if ( !nurbs_curve )
{
if ( curve.GetNurbForm(tmp) )
nurbs_curve = &tmp;
}
ON_GL( *nurbs_curve, nobj, type, true, NULL, xform );
}
}
void ON_GL( int dim, int is_rat, int nurb_order, int cv_count,
const double* knot_vector,
int cv_stride, const double* cv,
GLUnurbsObj* nobj,
GLenum type,
int bPermitKnotScaling,
double* knot_scale,
double xform[][4]
)
{
ON_BOOL32 bCallgluBeginEndCurve = false;
int i;
GLint nknots = nurb_order + cv_count; // GL knot count = TL knot count + 2
GLfloat* knot = (GLfloat*)onmalloc( nknots*sizeof(*knot) );
ON_GL( nurb_order, cv_count, knot_vector, knot, bPermitKnotScaling, knot_scale );
// control vertices
//const int cv_size = (is_rat) ? dim+1: dim;
GLint stride = cv_stride;
GLfloat* ctlarray = (GLfloat*)onmalloc( stride*cv_count*sizeof(*ctlarray) );
for ( i = 0; i < cv_count; i++ ) {
GetGLCV( dim, is_rat, cv + i*cv_stride, xform, ctlarray + stride*i );
}
GLint order = nurb_order;
switch(type)
{
case 0:
{
switch ( dim ) {
case 2: // must be a GLU_MAP1_TRIM_2/3
type = ( is_rat )
? GLU_MAP1_TRIM_3 // rational 2d trim uses homogeneous coords
: GLU_MAP1_TRIM_2; // non-rational 2d trim uses euclidean coords
break;
case 3: // must be a GLU_MAP1_VERTEX_3/4
type = ( is_rat )
? GL_MAP1_VERTEX_4 // rational 3d curve uses homogeneous coords
: GL_MAP1_VERTEX_3; // non-rational 3d curve used euclidean coords
bCallgluBeginEndCurve = true;
break;
}
}
break;
case GLU_MAP1_TRIM_2:
case GLU_MAP1_TRIM_3:
// make sure type matches rational flag
type = ( is_rat )
? GLU_MAP1_TRIM_3 // rational 2d trim uses homogeneous coords
: GLU_MAP1_TRIM_2; // non-rational 2d trim uses euclidean coords
break;
case GL_MAP1_VERTEX_3:
case GL_MAP1_VERTEX_4:
// make sure type matches rational flag
type = ( is_rat )
? GL_MAP1_VERTEX_4 // rational 3d curve uses homogeneous coords
: GL_MAP1_VERTEX_3; // non-rational 3d curve used euclidean coords
bCallgluBeginEndCurve = true;
break;
}
if ( bCallgluBeginEndCurve )
gluBeginCurve(nobj);
gluNurbsCurve(
nobj,
nknots,
knot,
stride,
ctlarray,
order,
type
);
if ( bCallgluBeginEndCurve )
gluEndCurve(nobj);
onfree( ctlarray );
onfree( knot );
}
// See comments in opennurbs_gl.h for calling instructions.
void ON_GL( const ON_NurbsSurface& s,
GLUnurbsObj* nobj, // created with gluNewNurbsRenderer )
GLenum type, // = 0 (and type is automatically set)
int bPermitKnotScaling,
double* knot_scale0,
double* knot_scale1
)
{
int i, j, k;
// The "bPermitScaling" parameters to the ON_GL() call that
// fills in the knot vectors is set to false because any
// rescaling that is applied to a surface domain must also
// be applied to parameter space trimming curve geometry.
// GL "s" knots
GLint sknot_count = s.KnotCount(0) + 2;
GLfloat* sknot = (GLfloat*)onmalloc( sknot_count*sizeof(*sknot) );
ON_GL( s.Order(0), s.CVCount(0), s.Knot(0), sknot,
bPermitKnotScaling, knot_scale0 );
// GL "t" knots
GLint tknot_count = s.KnotCount(1) + 2;
GLfloat* tknot = (GLfloat*)onmalloc( tknot_count*sizeof(*tknot) );
ON_GL( s.Order(1), s.CVCount(1), s.Knot(1), tknot,
bPermitKnotScaling, knot_scale1 );
// control vertices
const int cv_size= s.CVSize();
const int cv_count[2] = {s.CVCount(0), s.CVCount(1)};
GLint s_stride = cv_size*cv_count[1];
GLint t_stride = cv_size;
GLfloat* ctlarray = (GLfloat*)onmalloc( s_stride*cv_count[0]*sizeof(*ctlarray) );
for ( i = 0; i < cv_count[0]; i++ ) {
for ( j = 0; j < cv_count[1]; j++ ) {
const double* cv = s.CV(i,j);
GLfloat* gl_cv = ctlarray + s_stride*i + t_stride*j;
for ( k = 0; k < cv_size; k++ ) {
gl_cv[k] = (GLfloat)cv[k];
}
}
}
GLint sorder = s.Order(0);
GLint torder = s.Order(1);
if ( type == 0 ) {
// set GL surface type for 3d CVs in homogeneous/euclidean form.
type = ( s.IsRational() ) ? GL_MAP2_VERTEX_4 : GL_MAP2_VERTEX_3;
}
gluNurbsSurface (
nobj,
sknot_count,
sknot,
tknot_count,
tknot,
s_stride,
t_stride,
ctlarray,
sorder,
torder,
type
);
onfree( ctlarray );
onfree( tknot );
onfree( sknot );
}
void ON_GL( const ON_Brep& brep,
GLUnurbsObj* nobj // created with gluNewNurbsRenderer )
)
{
const int face_count = brep.m_F.Count();
int face_index;
for ( face_index = 0; face_index < face_count; face_index++ ) {
const ON_BrepFace& face = brep.m_F[face_index];
ON_GL( face, nobj );
}
}
// See comments in opennurbs_gl.h for calling instructions.
void ON_GL( const ON_BrepFace& face,
GLUnurbsObj* nobj // created with gluNewNurbsRenderer )
)
{
bool bSkipTrims = false;
const ON_Mesh* mesh;
mesh = face.Mesh(ON::render_mesh);
if ( mesh )
{
// use saved render mesh
ON_GL(*mesh);
}
else
{
// use (slow and buggy) glu trimmed NURBS rendering
double knot_scale[2][2] = {{0.0,1.0},{0.0,1.0}};
const ON_Brep* brep = face.Brep();
if ( !brep )
return;
// untrimmed surface
{
ON_NurbsSurface tmp_nurbssrf;
const ON_Surface* srf = brep->m_S[face.m_si];
const ON_NurbsSurface* nurbs_srf = ON_NurbsSurface::Cast(srf);
if ( !nurbs_srf )
{
// attempt to get NURBS form of this surface
if ( srf->GetNurbForm( tmp_nurbssrf ) )
nurbs_srf = &tmp_nurbssrf;
}
if ( !nurbs_srf )
return;
gluBeginSurface( nobj );
ON_GL( *nurbs_srf,
nobj,
(nurbs_srf->IsRational()) ? GL_MAP2_VERTEX_4 : GL_MAP2_VERTEX_3,
true, knot_scale[0], knot_scale[1]
);
}
if ( bSkipTrims || brep->FaceIsSurface( face.m_face_index ) ) {
gluEndSurface( nobj );
return; // face is trivially trimmed
}
int fli, li, lti, ti;
// any knot scaling applied to the surface must also be applied to
// the parameter space trimming geometry
double xform[4][4]
= {{knot_scale[0][1], 0.0, 0.0, -knot_scale[0][0]*knot_scale[0][1] },
{0.0, knot_scale[1][1], 0.0, -knot_scale[1][0]*knot_scale[1][1] },
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0}};
// Add face's 2d trimming loop(s)
const int face_loop_count = face.m_li.Count();
for ( fli = 0; fli < face_loop_count; fli++ )
{
gluBeginTrim( nobj );
li = face.m_li[fli];
const ON_BrepLoop& loop = brep->m_L[li];
const int loop_trim_count = loop.m_ti.Count();
for ( lti = 0; lti < loop_trim_count; lti++ )
{
ti = loop.m_ti[lti];
const ON_BrepTrim& trim = brep->m_T[ti];
ON_GL( trim,
nobj,
GLU_MAP1_TRIM_2,
xform
);
}
gluEndTrim( nobj );
}
gluEndSurface( nobj );
}
}
void ON_GL( const ON_Mesh& mesh )
{
int i0, i1, i2, j0, j1, j2;
int fi;
ON_3fPoint v[4];
ON_3fVector n[4];
ON_2fPoint t[4];
const int face_count = mesh.FaceCount();
const ON_BOOL32 bHasNormals = mesh.HasVertexNormals();
const ON_BOOL32 bHasTCoords = mesh.HasTextureCoordinates();
glBegin(GL_TRIANGLES);
for ( fi = 0; fi < face_count; fi++ ) {
const ON_MeshFace& f = mesh.m_F[fi];
v[0] = mesh.m_V[f.vi[0]];
v[1] = mesh.m_V[f.vi[1]];
v[2] = mesh.m_V[f.vi[2]];
if ( bHasNormals ) {
n[0] = mesh.m_N[f.vi[0]];
n[1] = mesh.m_N[f.vi[1]];
n[2] = mesh.m_N[f.vi[2]];
}
if ( bHasTCoords ) {
t[0] = mesh.m_T[f.vi[0]];
t[1] = mesh.m_T[f.vi[1]];
t[2] = mesh.m_T[f.vi[2]];
}
if ( f.IsQuad() ) {
// quadrangle - render as two triangles
v[3] = mesh.m_V[f.vi[3]];
if ( bHasNormals )
n[3] = mesh.m_N[f.vi[3]];
if ( bHasTCoords )
t[3] = mesh.m_T[f.vi[3]];
if ( v[0].DistanceTo(v[2]) <= v[1].DistanceTo(v[3]) ) {
i0 = 0; i1 = 1; i2 = 2;
j0 = 0; j1 = 2; j2 = 3;
}
else {
i0 = 1; i1 = 2; i2 = 3;
j0 = 1; j1 = 3; j2 = 0;
}
}
else {
// single triangle
i0 = 0; i1 = 1; i2 = 2;
j0 = j1 = j2 = 0;
}
// first triangle
if ( bHasNormals )
glNormal3f( n[i0].x, n[i0].y, n[i0].z );
if ( bHasTCoords )
glTexCoord2f( t[i0].x, t[i0].y );
glVertex3f( v[i0].x, v[i0].y, v[i0].z );
if ( bHasNormals )
glNormal3f( n[i1].x, n[i1].y, n[i1].z );
if ( bHasTCoords )
glTexCoord2f( t[i1].x, t[i1].y );
glVertex3f( v[i1].x, v[i1].y, v[i1].z );
if ( bHasNormals )
glNormal3f( n[i2].x, n[i2].y, n[i2].z );
if ( bHasTCoords )
glTexCoord2f( t[i2].x, t[i2].y );
glVertex3f( v[i2].x, v[i2].y, v[i2].z );
if ( j0 != j1 ) {
// if we have a quad, second triangle
if ( bHasNormals )
glNormal3f( n[j0].x, n[j0].y, n[j0].z );
if ( bHasTCoords )
glTexCoord2f( t[j0].x, t[j0].y );
glVertex3f( v[j0].x, v[j0].y, v[j0].z );
if ( bHasNormals )
glNormal3f( n[j1].x, n[j1].y, n[j1].z );
if ( bHasTCoords )
glTexCoord2f( t[j1].x, t[j1].y );
glVertex3f( v[j1].x, v[j1].y, v[j1].z );
if ( bHasNormals )
glNormal3f( n[j2].x, n[j2].y, n[j2].z );
if ( bHasTCoords )
glTexCoord2f( t[j2].x, t[j2].y );
glVertex3f( v[j2].x, v[j2].y, v[j2].z );
}
}
glEnd();
}
void ON_GL(
const ON_3dPoint& point
)
{
glVertex3d( point.x, point.y, point.z );
}
void ON_GL(
const ON_Point& point
)
{
glBegin(GL_POINTS);
ON_GL(point.point);
glEnd();
}
void ON_GL( const ON_PointCloud& cloud )
{
int i;
ON_3dPoint P;
glBegin(GL_POINTS);
for ( i = 0; i < cloud.PointCount(); i++ ) {
ON_GL( cloud.m_P[i] );
}
glEnd();
}
void ON_GL( const ON_Material& m )
{
ON_GL( &m );
}
void ON_GL( const ON_Color& rc, double alpha, GLfloat c[4] )
{
c[0] = (GLfloat)rc.FractionRed();
c[1] = (GLfloat)rc.FractionGreen();
c[2] = (GLfloat)rc.FractionBlue();
c[3] = (GLfloat)alpha;
}
void ON_GL( const ON_Color& rc, GLfloat c[4] )
{
c[0] = (GLfloat)rc.FractionRed();
c[1] = (GLfloat)rc.FractionGreen();
c[2] = (GLfloat)rc.FractionBlue();
c[3] = (GLfloat)1.0;
}
void ON_GL( const ON_Material* pMat )
{
// set GL material to match Rhino material
if ( !pMat ) {
ON_Material default_mat;
ON_GL( &default_mat );
}
else {
GLfloat ambient[4], diffuse[4], specular[4], emission[4];
GLfloat alpha = (GLfloat)(1.0 - pMat->Transparency());
ON_GL( pMat->Ambient(), alpha, ambient );
ON_GL( pMat->Diffuse(), alpha, diffuse );
ON_GL( pMat->Specular(), alpha, specular );
ON_GL( pMat->Emission(), alpha, emission );
GLint shine = (GLint)(128.0*(pMat->Shine() / ON_Material::MaxShine()));
if ( shine == 0 ) {
specular[0]=specular[1]=specular[2]=(GLfloat)0.0;
}
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, ambient );
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse );
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emission );
glMateriali( GL_FRONT_AND_BACK, GL_SHININESS, shine );
}
}
void ON_GL( const ON_Light* light, GLenum light_index )
{
ON_Light default_light;
if ( !light ) {
default_light.Default();
light = &default_light;
}
ON_GL( *light, light_index );
}
void ON_GL( const ON_Light& light, GLenum light_index )
{
ON_BOOL32 bPopModelViewMatrix = false;
ON_BOOL32 bPopProjectionMatrix = false;
switch ( light.CoordinateSystem() )
{
case ON::world_cs:
break;
case ON::clip_cs:
bPopProjectionMatrix = true;
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// no break here
case ON::camera_cs:
bPopModelViewMatrix = true;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
break;
case ON::screen_cs:
break;
}
GLfloat ambient[4], diffuse[4], specular[4];
ON_GL( light.Ambient(), ambient );
ON_GL( light.Diffuse(), diffuse );
ON_GL( light.Specular(), specular );
glLightfv( light_index, GL_AMBIENT, ambient );
glLightfv( light_index, GL_DIFFUSE, diffuse );
glLightfv( light_index, GL_SPECULAR, specular );
ON_3dPoint loc = light.Location();
GLfloat f[4] = {(GLfloat)loc.x,(GLfloat)loc.y,(GLfloat)loc.z,(GLfloat)1.0};
glLightfv( light_index, GL_POSITION, f );
ON_3dVector dir = light.Direction();
f[0] = (GLfloat)dir.x;
f[1] = (GLfloat)dir.y;
f[2] = (GLfloat)dir.z;
glLightfv( light_index, GL_SPOT_DIRECTION, f );
glLightf( light_index, GL_SPOT_EXPONENT, (GLfloat)(light.SpotExponent()*128.0) );
glLightf( light_index, GL_SPOT_CUTOFF, (GLfloat)light.SpotAngleRadians() );
ON_3dVector attenuation = light.Attenuation();
glLightf( light_index, GL_CONSTANT_ATTENUATION, (GLfloat)attenuation.x );
glLightf( light_index, GL_LINEAR_ATTENUATION, (GLfloat)attenuation.y );
glLightf( light_index, GL_QUADRATIC_ATTENUATION, (GLfloat)attenuation.z );
if ( light.IsEnabled() )
glEnable( light_index );
else
glDisable( light_index );
if ( bPopProjectionMatrix ) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
if ( bPopModelViewMatrix ) {
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
}
void ON_GL( ON_Viewport& viewport,
int port_left, int port_right,
int port_bottom, int port_top
)
{
// Sets viewport's port to port_* values and adjusts frustum
// so it's aspect matches the port's.
ON_Xform projectionMatrix; // camera to clip transformation
const int port_width = abs(port_right - port_left);
const int port_height = abs(port_top - port_bottom);
if ( port_width == 0 || port_height == 0 )
return;
const double port_aspect = ((double)port_width)/((double)port_height);
viewport.SetFrustumAspect( port_aspect );
viewport.SetScreenPort( port_left, port_right, port_bottom, port_top,
0, 0xff );
ON_BOOL32 bHaveCameraToClip = viewport.GetXform(
ON::camera_cs,
ON::clip_cs,
projectionMatrix
);
if ( bHaveCameraToClip ) {
projectionMatrix.Transpose();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd( &projectionMatrix.m_xform[0][0] );
}
}
void ON_GL( const ON_Viewport& viewport )
{
// sets model view matrix (world to camera transformation)
ON_Xform modelviewMatrix; // world to camera transformation
ON_BOOL32 bHaveWorldToCamera = viewport.GetXform(
ON::world_cs,
ON::camera_cs,
modelviewMatrix
);
if ( bHaveWorldToCamera ) {
modelviewMatrix.Transpose();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd( &modelviewMatrix.m_xform[0][0] );
}
}
void ON_GL(
const ON_Surface& surface, //
GLUnurbsObj* nobj // created with gluNewNurbsRenderer
)
{
ON_NurbsSurface tmp;
const ON_NurbsSurface* nurbs_surface;
nurbs_surface = ON_NurbsSurface::Cast(&surface);
if ( !nurbs_surface ) {
if ( surface.GetNurbForm(tmp) ) {
nurbs_surface = &tmp;
}
}
if ( nurbs_surface )
ON_GL( *nurbs_surface, nobj, 0, true );
}