1 | /************************************************************************
|
---|
2 |
|
---|
3 | Visualization of 3D quadric error metric
|
---|
4 |
|
---|
5 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
6 |
|
---|
7 | $Id: MxQVis3.cxx,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
8 |
|
---|
9 | ************************************************************************/
|
---|
10 |
|
---|
11 | #include "stdmix.h"
|
---|
12 | #include "MxGL.h"
|
---|
13 | #include "MxQMetric3.h"
|
---|
14 | #include "MxMatrix.h"
|
---|
15 |
|
---|
16 | class _QContext
|
---|
17 | {
|
---|
18 | public:
|
---|
19 |
|
---|
20 | GLUquadricObj *quadric;
|
---|
21 |
|
---|
22 | _QContext()
|
---|
23 | {
|
---|
24 | quadric = gluNewQuadric();
|
---|
25 | if( !quadric )
|
---|
26 | fatal_error("Unable to allocate GL quadric context.");
|
---|
27 |
|
---|
28 | gluQuadricNormals(quadric, (GLenum)GLU_SMOOTH);
|
---|
29 | gluQuadricDrawStyle(quadric, (GLenum)GLU_FILL);
|
---|
30 | gluQuadricOrientation(quadric, (GLenum)GLU_OUTSIDE);
|
---|
31 | }
|
---|
32 | };
|
---|
33 |
|
---|
34 |
|
---|
35 | //
|
---|
36 | // NOTE: This relies on C++ static constructors being called during
|
---|
37 | // initialization. This may not work if the program is linked
|
---|
38 | // with a C compiler.
|
---|
39 | //
|
---|
40 | static _QContext context;
|
---|
41 |
|
---|
42 | static
|
---|
43 | GLfloat colors[][4] = {
|
---|
44 | { 0.4f, 0.1f, 0.1f, 1.0f },
|
---|
45 | { 0.1f, 0.4f, 0.1f, 1.0f },
|
---|
46 | { 0.02f, 0.02f, 0.02f, 1.0f },
|
---|
47 | };
|
---|
48 |
|
---|
49 | void mx_quadric_shading(int color, bool twosided)
|
---|
50 | {
|
---|
51 | if( color < 1 ) color = 1;
|
---|
52 | if( color > 3 ) color = 3;
|
---|
53 |
|
---|
54 | glEnable(GL_LIGHTING);
|
---|
55 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, twosided);
|
---|
56 |
|
---|
57 | GLfloat mat_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
|
---|
58 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
|
---|
59 | glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0f);
|
---|
60 | glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, colors[color-1]);
|
---|
61 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, colors[color-1]);
|
---|
62 | }
|
---|
63 |
|
---|
64 | //
|
---|
65 | // !!BUG: This procedure will not work if Q.offset()==0.
|
---|
66 | //
|
---|
67 | void mx_draw_quadric(const MxQuadric3& quadric, double radius, const float *v)
|
---|
68 | {
|
---|
69 | Mat4 Q, Qh = quadric.homogeneous();
|
---|
70 |
|
---|
71 | glMatrixMode(GL_MODELVIEW);
|
---|
72 | if( mxm_cholesky(Q, Qh, 4) == 0 )
|
---|
73 | {
|
---|
74 | glPushMatrix();
|
---|
75 |
|
---|
76 | Mat4 N;
|
---|
77 | Q.invert(N);
|
---|
78 |
|
---|
79 | glMultMatrix(N);
|
---|
80 | gluSphere(context.quadric, radius, 10, 10);
|
---|
81 | glPopMatrix();
|
---|
82 | }
|
---|
83 | else if( v )
|
---|
84 | {
|
---|
85 | glPushAttrib(GL_LIGHTING_BIT);
|
---|
86 | mx_quadric_shading(MX_RED_ELLIPSOIDS);
|
---|
87 |
|
---|
88 | glPushMatrix();
|
---|
89 | glTranslated(v[X], v[Y], v[Z]);
|
---|
90 | gluSphere(context.quadric, 0.0025, 3, 3);
|
---|
91 | glPopMatrix();
|
---|
92 |
|
---|
93 | glPopAttrib();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | static
|
---|
98 | void q_vertex(float k1, float k2, float x, float y)
|
---|
99 | {
|
---|
100 | float g = sqrt(1 + k1*k1*x*x + k2*k2*y*y);
|
---|
101 | glNormal3f(-1/g, k1*x/g, k2*y/g);
|
---|
102 | glVertex3f(0.5*(k1*x*x + k2*y*y), x, y);
|
---|
103 | }
|
---|
104 |
|
---|
105 | void mx_draw_osculant(float k1, float k2, float extent)
|
---|
106 | {
|
---|
107 | int i,j;
|
---|
108 | int ring_count = 8;
|
---|
109 | int N = 12;
|
---|
110 |
|
---|
111 | float dR = extent/ring_count;
|
---|
112 | float R = dR;
|
---|
113 | float dT = 2*M_PI/N;
|
---|
114 | float theta;
|
---|
115 |
|
---|
116 | glOffsetForMesh();
|
---|
117 | mx_quadric_shading(MX_GREEN_ELLIPSOIDS);
|
---|
118 |
|
---|
119 | // Emit the central ring
|
---|
120 | glBegin(GL_TRIANGLE_FAN);
|
---|
121 | q_vertex(k1, k2, 0, 0);
|
---|
122 | for(j=0, theta=0; j<N; j++, theta+=dT)
|
---|
123 | q_vertex(k1, k2, R*cos(theta), R*sin(theta));
|
---|
124 | q_vertex(k1, k2, R, 0);
|
---|
125 | glEnd();
|
---|
126 |
|
---|
127 | for(i=1; i<ring_count; i++)
|
---|
128 | {
|
---|
129 | R += dR;
|
---|
130 | glBegin(GL_QUAD_STRIP);
|
---|
131 | for(j=0, theta=0; j<N; j++, theta+=dT)
|
---|
132 | {
|
---|
133 | q_vertex(k1, k2, R*cos(theta), R*sin(theta));
|
---|
134 | q_vertex(k1, k2, (R-dR)*cos(theta), (R-dR)*sin(theta));
|
---|
135 | }
|
---|
136 | q_vertex(k1, k2, R, 0);
|
---|
137 | q_vertex(k1, k2, R-dR, 0);
|
---|
138 | glEnd();
|
---|
139 | }
|
---|
140 |
|
---|
141 | mx_quadric_shading(MX_CHARCOAL_ELLIPSOIDS);
|
---|
142 |
|
---|
143 | glBegin(GL_LINE_STRIP);
|
---|
144 | for(i=0, R=0; i<=ring_count; i++, R+=dR)
|
---|
145 | q_vertex(k1, k2, R, 0);
|
---|
146 | glEnd();
|
---|
147 | glBegin(GL_LINE_STRIP);
|
---|
148 | for(i=0, R=0; i<=ring_count; i++, R+=dR)
|
---|
149 | q_vertex(k1, k2, -R, 0);
|
---|
150 | glEnd();
|
---|
151 | mx_quadric_shading(MX_RED_ELLIPSOIDS);
|
---|
152 | glBegin(GL_LINE_STRIP);
|
---|
153 | for(i=0, R=0; i<=ring_count; i++, R+=dR)
|
---|
154 | q_vertex(k1, k2, 0, R);
|
---|
155 | glEnd();
|
---|
156 | glBegin(GL_LINE_STRIP);
|
---|
157 | for(i=0, R=0; i<=ring_count; i++, R+=dR)
|
---|
158 | q_vertex(k1, k2, 0, -R);
|
---|
159 | glEnd();
|
---|
160 | }
|
---|