Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/src/graphics/gutil2.c
3203 views
1
/*****************************************************************************
2
*
3
* Elmer, A Finite Element Software for Multiphysical Problems
4
*
5
* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland
6
*
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
11
*
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
16
*
17
* You should have received a copy of the GNU General Public License
18
* along with this program (in file fem/GPL-2); if not, write to the
19
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
* Boston, MA 02110-1301, USA.
21
*
22
*****************************************************************************/
23
24
/*******************************************************************************
25
*
26
* Graphics utilities Part II. OpenGL is being called.
27
*
28
*******************************************************************************
29
*
30
* Author: Juha Ruokolainen
31
*
32
* Address: CSC - IT Center for Science Ltd.
33
* Keilaranta 14, P.O. BOX 405
34
* 02101 Espoo, Finland
35
* Tel. +358 0 457 2723
36
* Telefax: +358 0 457 2302
37
* EMail: [email protected]
38
*
39
* Date: 26 Sep 1995
40
*
41
* Modified by:
42
*
43
* Date of modification:
44
*
45
* $Id: gutil2.c,v 1.3 2003/02/06 09:37:50 jpr Exp $
46
*
47
* $Log: gutil2.c,v $
48
* Revision 1.3 2003/02/06 09:37:50 jpr
49
* *** empty log message ***
50
*
51
* Revision 1.2 1998/07/31 13:36:58 jpr
52
*
53
* Added id, started log.
54
*
55
*
56
******************************************************************************/
57
#include "../elmerpost.h"
58
59
static int GRA_SphereQuality=3,GRA_SphereInitDone=FALSE;
60
61
#define GRA_MAX_QUALITY 32
62
63
static double GRA_CosA[4*GRA_MAX_QUALITY];
64
static double GRA_SinA[4*GRA_MAX_QUALITY];
65
static double GRA_CosB[4*GRA_MAX_QUALITY];
66
static double GRA_SinB[4*GRA_MAX_QUALITY];
67
68
/*******************************************************************************
69
*
70
* Name: gra_sphere_quality
71
*
72
* Purpose: initialize internal cos & sin tables with given resolution
73
*
74
* Parameters:
75
*
76
* Input: (int) quality factor
77
*
78
* Output: none
79
*
80
* Return value: void
81
*
82
******************************************************************************/
83
void gra_sphere_quality(int quality)
84
{
85
int i,j,Division;
86
double a;
87
88
if ( GRA_SphereInitDone )
89
if ( GRA_SphereQuality == quality ) return;
90
91
if ( quality > GRA_MAX_QUALITY ) quality = GRA_MAX_QUALITY;
92
else if ( quality < 1 ) quality = 1;
93
94
GRA_SphereQuality = quality;
95
96
Division = 4*quality;
97
98
for( i=0; i<Division; i++ )
99
{
100
a = 2.0*M_PI*i/(double)Division;
101
GRA_CosA[i] = cos(a);
102
GRA_SinA[i] = sin(a);
103
}
104
GRA_CosA[i] = GRA_CosA[0];
105
GRA_SinA[i] = GRA_SinA[0];
106
107
for( i=-Division/2,j=0; i<=Division/2; i+=2, j++ )
108
{
109
a = M_PI*i/(double)Division;
110
GRA_CosB[j] = cos(a);
111
GRA_SinB[j] = sin(a);
112
}
113
114
GRA_SphereInitDone = TRUE;
115
}
116
117
/*******************************************************************************
118
*
119
* Name: gra_vector_to_angles
120
*
121
* Purpose: return angles about x and y axis to rotate x-axis
122
* to given vector
123
*
124
* Parameters:
125
*
126
* Input: (double vx,vy,vz) first end point
127
* (double *px,*py,*pz) second end point
128
*
129
* Output: (double *px,*py) computed angles
130
*
131
* Return value: void
132
*
133
******************************************************************************/
134
void gra_vector_to_angles(double vx,double vy,double vz,double *px,double *py,double *pz,double r)
135
{
136
double a,b,x,y,z,RadToDeg=180.0/M_PI;
137
138
x = (*px-vx)*r;
139
y = (*py-vy)*r;
140
z = (vz-*pz)*r;
141
142
b = atan2(y,z);
143
144
r = y*sin(b) + z*cos(b);
145
a = atan2(r,x);
146
147
*px = RadToDeg*b;
148
*py = RadToDeg*a;
149
*pz = 0.0;
150
}
151
152
/*******************************************************************************
153
*
154
* Name: gra_sphere
155
*
156
* Purpose: draw sphere centered at a point, with given color and
157
* radius
158
*
159
* Parameters:
160
*
161
* Input: (double x,y,z) sphere center point
162
* (double f) color
163
* (double r) radius
164
*
165
* Output: graphics
166
*
167
* Return value: void
168
*
169
******************************************************************************/
170
void gra_sphere(double x,double y,double z,double f,double r)
171
{
172
int Division = 4*GRA_SphereQuality;
173
int i,j;
174
175
if ( !GRA_SphereInitDone ) gra_sphere_quality( GRA_SphereQuality );
176
177
glPushMatrix();
178
179
glTranslatef(x,y,z);
180
glScalef(r,r,r);
181
182
for( i=0; i<Division/2; i++ )
183
{
184
glBegin(GL_QUAD_STRIP);
185
x = GRA_CosB[i];
186
y = 0.0;
187
z = GRA_SinB[i];
188
189
glTexCoord1d( f );
190
glNormal3f(-x,-y,-z);
191
glVertex3f(x,y,z);
192
193
x = GRA_CosB[i+1];
194
y = 0.0;
195
z = GRA_SinB[i+1];
196
197
glTexCoord1d( f );
198
glNormal3d( -x,-y,-z );
199
glVertex3d( x,y,z );
200
201
for( j=1; j<=Division; j++ )
202
{
203
x = GRA_CosA[j]*GRA_CosB[i];
204
y = GRA_SinA[j]*GRA_CosB[i];
205
z = GRA_SinB[i];
206
207
glTexCoord1d( f );
208
glNormal3d( -x,-y,-z );
209
glVertex3d( x,y,z );
210
211
x = GRA_CosA[j]*GRA_CosB[i+1];
212
y = GRA_SinA[j]*GRA_CosB[i+1];
213
z = GRA_SinB[i+1];
214
215
glTexCoord1d( f );
216
glNormal3d( -x,-y,-z );
217
glVertex3d( x,y,z );
218
}
219
glEnd();
220
}
221
222
glPopMatrix();
223
224
glNormal3d( 0.0,0.0,1.0 );
225
}
226
227
/*******************************************************************************
228
*
229
* Name: gra_point
230
*
231
* Purpose: draw point with given color and radius
232
*
233
* Parameters:
234
*
235
* Input: (double x,y,z) sphere center point
236
* (double f) color
237
* (double r) radius
238
*
239
* Output: graphics
240
*
241
* Return value: void
242
*
243
******************************************************************************/
244
void gra_point(double x,double y,double z,double f,double r)
245
{
246
glTexCoord1d( f );
247
glVertex3d( x,y,z );
248
}
249
250
/*******************************************************************************
251
*
252
* Name: gra_cylinder
253
*
254
* Purpose: draw cylinder between points given
255
*
256
* Parameters:
257
*
258
* Input: (double x0,y0,z0) first end point coordinates
259
* (double f0) first end point color
260
* (double x1,y1,z1)) second end point coordinates
261
* (double f1) second end point color
262
* (double R) cylinder radius
263
*
264
* Output: graphics
265
*
266
* Return value: void
267
*
268
******************************************************************************/
269
void gra_cylinder(double x0,double y0,double z0,double f0,double x1,double y1,double z1,double f1,double R)
270
{
271
double ax,ay,az,s;
272
273
int i,j;
274
275
if ( !GRA_SphereInitDone ) gra_sphere_quality( GRA_SphereQuality );
276
277
ax = x1;
278
ay = y1;
279
az = z1;
280
x1 = ax - x0;
281
y1 = ay - y0;
282
z1 = az - z0;
283
s = sqrt( x1*x1+y1*y1+z1*z1 );
284
if ( s < 1.0e-10 ) return;
285
286
gra_vector_to_angles( x0,y0,z0,&ax,&ay,&az,1/s );
287
288
glPushMatrix();
289
290
glTranslated( x0,y0,z0 );
291
292
glRotated( ax,1.0,0.0,0.0 );
293
glRotated( ay,0.0,1.0,0.0 );
294
295
glScaled( s,R,R );
296
297
glBegin(GL_QUAD_STRIP);
298
299
glTexCoord1d( f0 );
300
glNormal3d( 0.0,0.0,1.0 );
301
glVertex3d( 0.0,0.0,1.0 );
302
303
glTexCoord1d( f1 );
304
glVertex3d( 1.0,0.0,1.0 );
305
306
for( j=1; j<=GRA_SphereQuality*4;j++ )
307
{
308
y0 = GRA_SinA[j];
309
z0 = GRA_CosA[j];
310
311
glTexCoord1d( f0 );
312
glNormal3d( 0.0,y0,z0 );
313
glVertex3d( 0.0,y0,z0 );
314
315
glTexCoord1d( f1 );
316
glVertex3d( 1.0,y0,z0 );
317
}
318
319
glEnd();
320
321
glPopMatrix();
322
323
glNormal3d( 0.0,0.0,1.0 );
324
}
325
326
/*******************************************************************************
327
*
328
* Name: gra_cone
329
*
330
* Purpose: draw a cone between points given
331
*
332
* Parameters:
333
*
334
* Input: (double x0,y0,z0) first end point coordinates
335
* (double f0) first end point color
336
* (double R0) first end point radius
337
* (double x1,y1,z1)) second end point coordinates
338
* (double f1) second end point color
339
* (double R1) second end point radius
340
*
341
* Output: graphics
342
*
343
* Return value: void
344
*
345
******************************************************************************/
346
void gra_cone(double x0,double y0,double z0,double f0,double R0,double x1,double y1,double z1,double f1,double R1)
347
{
348
double ax,ay,az,r,s;
349
int i,j;
350
351
if ( !GRA_SphereInitDone ) gra_sphere_quality(GRA_SphereQuality);
352
353
ax = x1;
354
ay = y1;
355
az = z1;
356
357
x1 = ax - x0;
358
y1 = ay - y0;
359
z1 = az - z0;
360
r = sqrt( x1*x1+y1*y1+z1*z1 );
361
if ( r < 1.0E-10 ) return;
362
363
gra_vector_to_angles( x0,y0,z0,&ax,&ay,&az,1/r );
364
365
glPushMatrix();
366
367
glTranslated(x0,y0,z0);
368
369
glRotated( ax,1.0,0.0,0.0 );
370
glRotated( ay,0.0,1.0,0.0 );
371
372
glScaled( r,1.0,1.0 );
373
374
s = sqrt(1+(R0-R1)*(R0-R1));
375
s = 1/s;
376
377
#define NORMAL(x,y,z) glNormal3d(s*(x),s*(y),s*(z))
378
379
glBegin(GL_QUAD_STRIP);
380
381
NORMAL( R0-R1,0.0,1.0 );
382
glTexCoord1d( f0 );
383
glVertex3d( 0.0,0.0,R0 );
384
385
NORMAL( R0-R1,0.0,1.0 );
386
glTexCoord1d( f1 );
387
glVertex3d( 1.0,0.0,R1 );
388
389
for( j=1; j<=GRA_SphereQuality*4;j++ )
390
{
391
y0 = GRA_SinA[j];
392
z0 = GRA_CosA[j];
393
394
NORMAL( R0-R1,y0,z0 );
395
396
glTexCoord1d( f0 );
397
glVertex3d( 0.0,R0*y0,R0*z0 );
398
399
glTexCoord1d( f1 );
400
glVertex3d( 1.0,R1*y0,R1*z0 );
401
}
402
403
glEnd();
404
405
#undef NORMAL
406
407
glPopMatrix();
408
409
glNormal3d( 0.0,0.0,1.0 );
410
}
411
412