Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[7f9531]: oglutils.c Maximize Restore History

Download this file

oglutils.c    318 lines (261 with data), 8.3 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
/* Asteroids3D - a first person game of blowing up asteroids
* Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
* Copyright Š Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2003 - 2005
*
*
* This program is free software; 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.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Stuart Pomerantz
* 3935 Stonecliffe drive
* Monroeville, PA 15146
*/
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include "asteroids3D.h"
#include "vecops.h"
/* find the rotation matrix for rotating
* angle DEGREES about vector V
*/
void find_rotation_mat(double angle, const Vector *v, double *m) {
double x, y, z; // direction cosines
double len; // length of the vector V
double cos_a, sin_a;
/* calculate the normalized vector v and store its components in x,y,z */
len = vec_length(v);
x = v->x / len;
y = v->y / len;
z = v->z / len;
/* pre-compute the cos(angle), sin(angle) */
cos_a = cos(angle * M_PI / 180);
sin_a = sin(angle * M_PI / 180);
/* calculate the elements of the matrix */
/* first row */
m[0] = x*x + cos_a*( 1 - x*x ) ;
m[1] = x*y - cos_a*x*y - z*sin_a ;
m[2] = x*z - cos_a*x*z + y*sin_a ;
m[3] = 0 ;
/* second row */
m[4] = y*x - cos_a*y*x + z*sin_a ;
m[5] = y*y + cos_a*( 1 - y*y ) ;
m[6] = y*z - cos_a*y*z - x*sin_a ;
m[7] = 0 ;
/* third row */
m[8] = z*x - cos_a*z*x - y*sin_a ;
m[9] = z*y - cos_a*z*y + x*sin_a ;
m[10] = z*z + cos_a*( 1 - z*z ) ;
m[11] = 0 ;
/* fourth row */
m[12] = 0 ;
m[13] = 0 ;
m[14] = 0 ;
m[15] = 1 ;
return ;
}
void find_rotation_angles(const Vector *v, double *x, double *y) {
double rho, phi, theta;
double cos_a; // cosine of the angle
Vector i, j, k; /* i,j,k are just for correcting the angle if v
is not in the +x, +y, +z quadrant */
/* given a vector in rectangular co-ordinates,
* convert it to spherical coordinates.
* This will yield the vector's rotation about the
* y-axis (theta) and its rotation about the x-axis (phi)
*
* see schaum's calculus book on page 539 & 540
*
* since my coordinate system is layed out differently
* than schaums, my spherical coorinates look like this:
*
* x = p sin(phi) sin(theta)
* y = p cos(theta)
* z = p sin(phi) cos(theta)
*
* e.g. in my coordinate system, z comes out of the page, and
* x and y are in the plane of the page. In schaums, x comes out of
* the page and z and y are in the plane of the page.
*
*/
/*
* Note that after the rotations are computed,
* they must be applied like this:
*
* glPushMatrix() ;
* glRotatef(y,0,1,0) ;
* glRotatef(x,1,0,0) ;
*
* display the object
*
* glPopMatrix() ;
*/
/* one last rub, I must make sure that
* the number I'm feeding to acos is between -1 and 1
*/
/* get vector into spherical coordinates */
rho = vec_length(v);
cos_a = v->y / rho;
if( cos_a < -1 )
cos_a = -1 ;
if( cos_a > 1 )
cos_a = 1 ;
phi = acos( cos_a ) ;
cos_a = v->z / (rho * sin(phi));
if( cos_a < -1 )
cos_a = -1 ;
if( cos_a > 1 )
cos_a = 1 ;
theta = acos( cos_a ) ;
phi = phi * 180 / M_PI;
theta = theta * 180 / M_PI;
*y = theta ;
*x = -90 + phi;
/* the subroutine could return here, except that I have
* to account for all 8 octants, I have to do some checking
*/
/* set up the unit basis vectors */
i.x = 1 ; i.y = 0 ; i.z = 0 ; i.w = 1 ;
j.x = 0 ; j.y = 1 ; j.z = 0 ; j.w = 1 ;
k.x = 0 ; k.y = 0 ; k.z = 1 ; k.w = 1 ;
if(vec_dot(v, &i) < 0)
*x = 180 - *x ;
if(vec_dot(v, &i) < 0)
*y = 180 - *y ;
return ;
}
void gen_rand_vec(Vector *v) {
int x,y,z ;
double m[16];
Vector u, a;
/* initial unit vector down the z - axis */
u.x = 0 ; u.y = 0 ; u.z = 1 ; u.w = 0;
x = 359.0 * rand() / (RAND_MAX + 1.0);
y = 359.0 * rand() / (RAND_MAX + 1.0);
z = 359.0 * rand() / (RAND_MAX + 1.0);
/* set the axis a to be the x axis first */
a.x = 1 ; a.y = 0 ; a.z = 0 ; a.w = 1 ;
/* rotate u */
find_rotation_mat(x, &a, m);
apply_v2m( &u,m ) ;
/* set the axis a to be the y axis next */
a.x = 0 ; a.y = 1 ; a.z = 0 ; a.w = 1 ;
/* rotate u */
find_rotation_mat(y, &a, m);
apply_v2m( &u,m ) ;
/* set the axis a to be the z axis finally */
a.x = 0 ; a.y = 0 ; a.z = 1 ; a.w = 1 ;
/* rotate u */
find_rotation_mat(z, &a, m);
apply_v2m( &u,m ) ;
/* put the results back into v */
*v = u;
return ;
}
/* generate random floats in .1 (tenth) increments */
double gen_rand_float(double min, double max) {
double k, len = max - min;
/* generate a number between 0 and len*10 */
k = 0 + ( (10.0*len) *rand()/(RAND_MAX+1.0)) ;
/* scale the number back down */
k = k / 10.0 - (double)abs(min);
return(k) ;
}
int gen_rand_int(int min, int max) {
int len = max - min + 1 ;
int k ;
/* generate a number between 0 and len */
k = (double)len * rand() / (RAND_MAX + 1.0);
/* scale the number back down */
k -= abs(min);
/* printf("min %d, max %d, picked %d\n",min,max,k) ;*/
return(k) ;
}
void apply_v2m(Vector *v, const double *m) {
Vector a = *v;
v->x = a.x*m[0] + a.y*m[1] + a.z*m[2] + a.w*m[3] ;
v->y = a.x*m[4] + a.y*m[5] + a.z*m[6] + a.w*m[7] ;
v->z = a.x*m[8] + a.y*m[9] + a.z*m[10] + a.w*m[11] ;
return ;
}
void draw_axes(void) {
static const float high_red[] = {1.0, 0.0, 0.0, 1.0};
static const float low_red[] = {0.3, 0.0, 0.0, 1.0};
static const float high_green[]= {0.0, 1.0, 0.0, 1.0};
static const float low_green[] = {0.0, 0.3, 0.0, 1.0};
static const float high_blue[] = {0.0, 0.0, 1.0, 1.0};
static const float low_blue[] = {0.0, 0.0, 0.3, 1.0};
static const float white[] = {1.0, 1.0, 1.0, 1.0};
static const float ad[] = {0.0, 0.0, 0.0, 0.1};
static const float normal[] = {0.0, 0.0, 0.0, 1.0};
double k = 5;
glLineWidth( 3.0 ) ;
glPointSize( 6.0 ) ;
glBegin( GL_LINES ) ;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ad) ;
/* x axis RED */
glMaterialfv(GL_FRONT, GL_EMISSION, high_red) ;
glVertex3f( k , 0.0 , 0.0 ) ;
glVertex3f( 0.0 , 0.0 , 0.0 ) ;
glMaterialfv(GL_FRONT, GL_EMISSION, low_red) ;
glVertex3f( -k , 0.0 , 0.0 ) ;
glVertex3f( 0.0 , 0.0 , 0.0 ) ;
/* y axis GREEN */
glMaterialfv(GL_FRONT, GL_EMISSION, high_green) ;
glVertex3f( 0.0 , k ,0.0 ) ;
glVertex3f( 0.0 , 0.0 , 0.0 ) ;
glMaterialfv(GL_FRONT, GL_EMISSION, low_green) ;
glVertex3f( 0.0 , -k , 0.0 ) ;
glVertex3f( 0.0 , 0.0 , 0.0 ) ;
/* z axis BLUE */
glMaterialfv(GL_FRONT, GL_EMISSION, high_blue) ;
glVertex3f( 0.0 , 0.0 , k ) ;
glVertex3f( 0.0 , 0.0 , 0.0 ) ;
glMaterialfv(GL_FRONT, GL_EMISSION, low_blue) ;
glVertex3f( 0.0 , 0.0 , -k ) ;
glVertex3f( 0.0 , 0.0 , 0.0 ) ;
glEnd() ;
glBegin( GL_POINTS ) ;
glMaterialfv(GL_FRONT, GL_EMISSION, white) ;
glVertex3f( k , 0.0 , 0.0 ) ;
glVertex3f( 0.0 , k ,0.0 ) ;
glVertex3f( 0.0 , 0.0 , k ) ;
glVertex3f(-k, 0, 0);
glVertex3f(0, -k, 0);
glVertex3f(0, 0, -k);
glEnd() ;
glMaterialfv(GL_FRONT, GL_EMISSION, normal);
glLineWidth( 1.0 ) ;
glPointSize( 1.0 ) ;
}
void draw_rectangle(void) {
static const float high_blue[] = {0.0, 0.0, 1.0, 1.0};
double k = 3;
glLineWidth( 2.0 ) ;
glMaterialfv(GL_FRONT, GL_EMISSION, high_blue) ;
glBegin(GL_LINE_LOOP) ;
/* upper left corner */
glVertex3f( -k/2, -k/2, 0 ) ;
/* upper right corner */
glVertex3f( k/2, -k/2, 0 ) ;
/* lower right corner */
glVertex3f( k/2, k/2, 0 ) ;
/* lower left corner */
glVertex3f( -k/2, k/2, 0 ) ;
glEnd() ;
glBegin(GL_LINES) ;
glVertex3f( 0, 0, 0 ) ;
glVertex3f( 0, 0, k ) ;
glEnd() ;
return ;
}