Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5098 | clevermous | 1 | #include |
2 | #include |
||
3 | #include |
||
4 | #include |
||
5 | |||
6 | |||
7 | void drawTorus(float rc, int numc, float rt, int numt) |
||
8 | { |
||
9 | int i, j, k; |
||
10 | double s, t; |
||
11 | double x, y, z; |
||
12 | double pi, twopi; |
||
13 | |||
14 | pi = 3.14159265358979323846; |
||
15 | twopi = 2 * pi; |
||
16 | |||
17 | for (i = 0; i < numc; i++) { |
||
18 | glBegin(GL_QUAD_STRIP); |
||
19 | for (j = 0; j <= numt; j++) { |
||
20 | for (k = 1; k >= 0; k--) { |
||
21 | s = (i + k) % numc + 0.5; |
||
22 | t = j % numt; |
||
23 | |||
24 | x = cos(t*twopi/numt) * cos(s*twopi/numc); |
||
25 | y = sin(t*twopi/numt) * cos(s*twopi/numc); |
||
26 | z = sin(s*twopi/numc); |
||
27 | glNormal3f(x, y, z); |
||
28 | |||
29 | x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt); |
||
30 | y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt); |
||
31 | z = rc * sin(s*twopi/numc); |
||
32 | glVertex3f(x, y, z); |
||
33 | } |
||
34 | } |
||
35 | glEnd(); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | static void normal3f( GLfloat x, GLfloat y, GLfloat z ) |
||
40 | { |
||
41 | GLdouble mag; |
||
42 | |||
43 | mag = sqrt( x*x + y*y + z*z ); |
||
44 | if (mag>0.00001F) { |
||
45 | x /= mag; |
||
46 | y /= mag; |
||
47 | z /= mag; |
||
48 | } |
||
49 | glNormal3f( x, y, z ); |
||
50 | } |
||
51 | |||
52 | void gluPerspective( GLdouble fovy, GLdouble aspect, |
||
53 | GLdouble zNear, GLdouble zFar ) |
||
54 | { |
||
55 | GLdouble xmin, xmax, ymin, ymax; |
||
56 | |||
57 | ymax = zNear * tan( fovy * M_PI / 360.0 ); |
||
58 | ymin = -ymax; |
||
59 | |||
60 | xmin = ymin * aspect; |
||
61 | xmax = ymax * aspect; |
||
62 | |||
63 | glFrustum( xmin, xmax, ymin, ymax, zNear, zFar ); |
||
64 | |||
65 | } |
||
66 | |||
67 | void |
||
68 | gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, |
||
69 | GLdouble centerx, GLdouble centery, GLdouble centerz, |
||
70 | GLdouble upx, GLdouble upy, GLdouble upz) |
||
71 | { |
||
72 | GLfloat m[16]; |
||
73 | GLdouble x[3], y[3], z[3]; |
||
74 | GLdouble mag; |
||
75 | |||
76 | /* Make rotation matrix */ |
||
77 | |||
78 | /* Z vector */ |
||
79 | z[0] = eyex - centerx; |
||
80 | z[1] = eyey - centery; |
||
81 | z[2] = eyez - centerz; |
||
82 | mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]); |
||
83 | if (mag) { /* mpichler, 19950515 */ |
||
84 | z[0] /= mag; |
||
85 | z[1] /= mag; |
||
86 | z[2] /= mag; |
||
87 | } |
||
88 | |||
89 | /* Y vector */ |
||
90 | y[0] = upx; |
||
91 | y[1] = upy; |
||
92 | y[2] = upz; |
||
93 | |||
94 | /* X vector = Y cross Z */ |
||
95 | x[0] = y[1] * z[2] - y[2] * z[1]; |
||
96 | x[1] = -y[0] * z[2] + y[2] * z[0]; |
||
97 | x[2] = y[0] * z[1] - y[1] * z[0]; |
||
98 | |||
99 | /* Recompute Y = Z cross X */ |
||
100 | y[0] = z[1] * x[2] - z[2] * x[1]; |
||
101 | y[1] = -z[0] * x[2] + z[2] * x[0]; |
||
102 | y[2] = z[0] * x[1] - z[1] * x[0]; |
||
103 | |||
104 | /* mpichler, 19950515 */ |
||
105 | /* cross product gives area of parallelogram, which is < 1.0 for |
||
106 | * non-perpendicular unit-length vectors; so normalize x, y here |
||
107 | */ |
||
108 | |||
109 | mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]); |
||
110 | if (mag) { |
||
111 | x[0] /= mag; |
||
112 | x[1] /= mag; |
||
113 | x[2] /= mag; |
||
114 | } |
||
115 | |||
116 | mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]); |
||
117 | if (mag) { |
||
118 | y[0] /= mag; |
||
119 | y[1] /= mag; |
||
120 | y[2] /= mag; |
||
121 | } |
||
122 | |||
123 | #define M(row,col) m[col*4+row] |
||
124 | M(0, 0) = x[0]; |
||
125 | M(0, 1) = x[1]; |
||
126 | M(0, 2) = x[2]; |
||
127 | M(0, 3) = 0.0; |
||
128 | M(1, 0) = y[0]; |
||
129 | M(1, 1) = y[1]; |
||
130 | M(1, 2) = y[2]; |
||
131 | M(1, 3) = 0.0; |
||
132 | M(2, 0) = z[0]; |
||
133 | M(2, 1) = z[1]; |
||
134 | M(2, 2) = z[2]; |
||
135 | M(2, 3) = 0.0; |
||
136 | M(3, 0) = 0.0; |
||
137 | M(3, 1) = 0.0; |
||
138 | M(3, 2) = 0.0; |
||
139 | M(3, 3) = 1.0; |
||
140 | #undef M |
||
141 | glMultMatrixf(m); |
||
142 | |||
143 | /* Translate Eye to Origin */ |
||
144 | glTranslatef(-eyex, -eyey, -eyez); |
||
145 | |||
146 | } |
||
147 | |||
148 | GLUquadricObj *gluNewQuadric(void) |
||
149 | { |
||
150 | return NULL; |
||
151 | } |
||
152 | |||
153 | void gluQuadricDrawStyle(GLUquadricObj *obj, int style) |
||
154 | { |
||
155 | } |
||
156 | |||
157 | void gluCylinder( GLUquadricObj *qobj, |
||
158 | GLdouble baseRadius, GLdouble topRadius, GLdouble height, |
||
159 | GLint slices, GLint stacks ) |
||
160 | { |
||
161 | GLdouble da, r, dr, dz; |
||
162 | GLfloat z, nz, nsign; |
||
163 | GLint i, j; |
||
164 | GLfloat du = 1.0 / slices; |
||
165 | GLfloat dv = 1.0 / stacks; |
||
166 | GLfloat tcx = 0.0, tcy = 0.0; |
||
167 | |||
168 | nsign = 1.0; |
||
169 | |||
170 | da = 2.0*M_PI / slices; |
||
171 | dr = (topRadius-baseRadius) / stacks; |
||
172 | dz = height / stacks; |
||
173 | nz = (baseRadius-topRadius) / height; /* Z component of normal vectors */ |
||
174 | |||
175 | for (i=0;i |
||
176 | GLfloat x1 = -sin(i*da); |
||
177 | GLfloat y1 = cos(i*da); |
||
178 | GLfloat x2 = -sin((i+1)*da); |
||
179 | GLfloat y2 = cos((i+1)*da); |
||
180 | z = 0.0; |
||
181 | r = baseRadius; |
||
182 | tcy = 0.0; |
||
183 | glBegin( GL_QUAD_STRIP ); |
||
184 | for (j=0;j<=stacks;j++) { |
||
185 | if (nsign==1.0) { |
||
186 | normal3f( x1*nsign, y1*nsign, nz*nsign ); |
||
187 | glTexCoord2f(tcx, tcy); |
||
188 | glVertex3f( x1*r, y1*r, z ); |
||
189 | normal3f( x2*nsign, y2*nsign, nz*nsign ); |
||
190 | glTexCoord2f(tcx+du, tcy); |
||
191 | glVertex3f( x2*r, y2*r, z ); |
||
192 | } |
||
193 | else { |
||
194 | normal3f( x2*nsign, y2*nsign, nz*nsign ); |
||
195 | glTexCoord2f(tcx, tcy); |
||
196 | glVertex3f( x2*r, y2*r, z ); |
||
197 | normal3f( x1*nsign, y1*nsign, nz*nsign ); |
||
198 | glTexCoord2f(tcx+du, tcy); |
||
199 | glVertex3f( x1*r, y1*r, z ); |
||
200 | } |
||
201 | z += dz; |
||
202 | r += dr; |
||
203 | tcy += dv; |
||
204 | } |
||
205 | glEnd(); |
||
206 | tcx += du; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /* Disk (adapted from Mesa) */ |
||
211 | |||
212 | void gluDisk( GLUquadricObj *qobj, |
||
213 | GLdouble innerRadius, GLdouble outerRadius, |
||
214 | GLint slices, GLint loops ) |
||
215 | { |
||
216 | GLdouble a, da; |
||
217 | GLfloat dr; |
||
218 | GLfloat r1, r2, dtc; |
||
219 | GLint s, l; |
||
220 | GLfloat sa,ca; |
||
221 | |||
222 | /* Normal vectors */ |
||
223 | glNormal3f( 0.0, 0.0, +1.0 ); |
||
224 | |||
225 | da = 2.0*M_PI / slices; |
||
226 | dr = (outerRadius-innerRadius) / (GLfloat) loops; |
||
227 | |||
228 | /* texture of a gluDisk is a cut out of the texture unit square */ |
||
229 | /* x, y in [-outerRadius, +outerRadius]; s, t in [0, 1] (linear mapping) */ |
||
230 | dtc = 2.0f * outerRadius; |
||
231 | |||
232 | r1 = innerRadius; |
||
233 | for (l=0;l |
||
234 | r2 = r1 + dr; |
||
235 | glBegin( GL_QUAD_STRIP ); |
||
236 | for (s=0;s<=slices;s++) { |
||
237 | if (s==slices) a = 0.0; |
||
238 | else a = s * da; |
||
239 | sa = sin(a); ca = cos(a); |
||
240 | glTexCoord2f(0.5+sa*r2/dtc,0.5+ca*r2/dtc); |
||
241 | glVertex2f( r2*sa, r2*ca ); |
||
242 | glTexCoord2f(0.5+sa*r1/dtc,0.5+ca*r1/dtc); |
||
243 | glVertex2f( r1*sa, r1*ca ); |
||
244 | } |
||
245 | glEnd(); |
||
246 | r1 = r2; |
||
247 | } |
||
248 | |||
249 | } |
||
250 | |||
251 | /* |
||
252 | * Sphere (adapted from Mesa) |
||
253 | */ |
||
254 | |||
255 | void gluSphere(GLUquadricObj *qobj, |
||
256 | float radius,int slices,int stacks) |
||
257 | { |
||
258 | float rho, drho, theta, dtheta; |
||
259 | float x, y, z; |
||
260 | float s, t, ds, dt; |
||
261 | int i, j, imin, imax; |
||
262 | int normals; |
||
263 | float nsign; |
||
264 | |||
265 | normals=1; |
||
266 | nsign=1; |
||
267 | |||
268 | drho = M_PI / (float) stacks; |
||
269 | dtheta = 2.0 * M_PI / (float) slices; |
||
270 | |||
271 | /* draw +Z end as a triangle fan */ |
||
272 | glBegin( GL_TRIANGLE_FAN ); |
||
273 | glNormal3f( 0.0, 0.0, 1.0 ); |
||
274 | glTexCoord2f(0.5,0.0); |
||
275 | glVertex3f( 0.0, 0.0, nsign * radius ); |
||
276 | for (j=0;j<=slices;j++) { |
||
277 | theta = (j==slices) ? 0.0 : j * dtheta; |
||
278 | x = -sin(theta) * sin(drho); |
||
279 | y = cos(theta) * sin(drho); |
||
280 | z = nsign * cos(drho); |
||
281 | if (normals) glNormal3f( x*nsign, y*nsign, z*nsign ); |
||
282 | glVertex3f( x*radius, y*radius, z*radius ); |
||
283 | } |
||
284 | glEnd(); |
||
285 | |||
286 | |||
287 | ds = 1.0 / slices; |
||
288 | dt = 1.0 / stacks; |
||
289 | t = 1.0; /* because loop now runs from 0 */ |
||
290 | if (1) { |
||
291 | imin = 0; |
||
292 | imax = stacks; |
||
293 | } |
||
294 | else { |
||
295 | imin = 1; |
||
296 | imax = stacks-1; |
||
297 | } |
||
298 | |||
299 | /* draw intermediate stacks as quad strips */ |
||
300 | for (i=imin;i |
||
301 | rho = i * drho; |
||
302 | glBegin( GL_QUAD_STRIP ); |
||
303 | s = 0.0; |
||
304 | for (j=0;j<=slices;j++) { |
||
305 | theta = (j==slices) ? 0.0 : j * dtheta; |
||
306 | x = -sin(theta) * sin(rho); |
||
307 | y = cos(theta) * sin(rho); |
||
308 | z = nsign * cos(rho); |
||
309 | if (normals) glNormal3f( x*nsign, y*nsign, z*nsign ); |
||
310 | glTexCoord2f(s,1-t); |
||
311 | glVertex3f( x*radius, y*radius, z*radius ); |
||
312 | x = -sin(theta) * sin(rho+drho); |
||
313 | y = cos(theta) * sin(rho+drho); |
||
314 | z = nsign * cos(rho+drho); |
||
315 | if (normals) glNormal3f( x*nsign, y*nsign, z*nsign ); |
||
316 | glTexCoord2f(s,1-(t-dt)); |
||
317 | s += ds; |
||
318 | glVertex3f( x*radius, y*radius, z*radius ); |
||
319 | } |
||
320 | glEnd(); |
||
321 | t -= dt; |
||
322 | } |
||
323 | |||
324 | /* draw -Z end as a triangle fan */ |
||
325 | glBegin( GL_TRIANGLE_FAN ); |
||
326 | glNormal3f( 0.0, 0.0, -1.0 ); |
||
327 | glTexCoord2f(0.5,1.0); |
||
328 | glVertex3f( 0.0, 0.0, -radius*nsign ); |
||
329 | rho = M_PI - drho; |
||
330 | s = 1.0; |
||
331 | t = dt; |
||
332 | for (j=slices;j>=0;j--) { |
||
333 | theta = (j==slices) ? 0.0 : j * dtheta; |
||
334 | x = -sin(theta) * sin(rho); |
||
335 | y = cos(theta) * sin(rho); |
||
336 | z = nsign * cos(rho); |
||
337 | if (normals) glNormal3f( x*nsign, y*nsign, z*nsign ); |
||
338 | glTexCoord2f(s,1-t); |
||
339 | s -= ds; |
||
340 | glVertex3f( x*radius, y*radius, z*radius ); |
||
341 | } |
||
342 | glEnd(); |
||
343 | }=slices;j++)>=slices;j++)>=slices;s++)>=stacks;j++)>>=>> |