0
|
1 #include<stdio.h>
|
|
2 #include<stdlib.h>
|
|
3 #include<unistd.h>
|
|
4 #include<time.h>
|
|
5 #include<float.h>
|
|
6 #include<SDL.h>
|
|
7 #include<OpenGL/gl.h>
|
|
8 #include<OpenGL/glu.h>
|
|
9
|
|
10 #define DEBUGlog(f, args...) \
|
|
11 fprintf(stderr, "in %s: "f, __FUNCTION__, ## args)
|
|
12
|
|
13 #define W_HEIGHT 480
|
|
14 #define W_WIDTH 640
|
|
15
|
|
16 /*
|
|
17 N body problem
|
|
18 */
|
|
19
|
|
20 /* parameters */
|
|
21 static int NUM_BODY = 3;
|
|
22 //static float Gravitation = 6.67e-11 ;
|
|
23 //static float delta = 100;
|
|
24 //static float FIELD = 2e11;
|
1
|
25 static float Gravitation = 100.0f; // ?
|
|
26 static float delta = 0.05f; // 0.01 ~ 100 ?
|
0
|
27 static float FIELD = 400.0f; // -100 ~ 100
|
1
|
28 static const float eps = 15.0f;
|
0
|
29
|
|
30 /* for OpenGL Utility. */
|
|
31 GLUquadricObj **sphere;
|
|
32
|
|
33 typedef struct
|
|
34 {
|
|
35 /* star's parameter. */
|
|
36 float weight;
|
|
37 float a[3]; /* acceleration */
|
|
38 float v[3]; /* velocity */
|
|
39 float r[3]; /* position */
|
|
40 /* for SDL. */
|
|
41 //SDL_Rect rect;
|
|
42 } body;
|
|
43 body *stars_old, *stars_new;
|
|
44
|
|
45
|
|
46 void start(void);
|
|
47 __code initialize(int num);
|
|
48 __code randomInit(SDL_Surface *screen, int num);
|
|
49 __code starsInit(SDL_Surface *screen, int num);
|
|
50 __code loop(int count, SDL_Surface *screen, int num);
|
|
51 __code compute(int count, SDL_Surface *screen, int num);
|
|
52 __code nextTurn(int count, SDL_Surface *screen, int num);
|
|
53 __code moveCenter(int count, SDL_Surface *screen, int num);
|
|
54 __code CenteringVelocity(int count, SDL_Surface *screen, int num);
|
|
55 __code drawStars(int count, SDL_Surface *screen, int num);
|
|
56
|
|
57 int main(int argc, char **argv)
|
|
58 {
|
|
59 int ch;
|
|
60 while ((ch = getopt(argc, argv, "s:g:")) != -1) {
|
|
61 switch (ch) {
|
|
62 case 's':
|
|
63 delta = atof(optarg);
|
|
64 break;
|
|
65 case 'g':
|
|
66 Gravitation = atof(optarg);
|
|
67 break;
|
|
68 case '?':
|
|
69 default:
|
|
70 break;
|
|
71 }
|
|
72 }
|
|
73 start();
|
|
74 return 0;
|
|
75 }
|
|
76 void start()
|
|
77 {
|
|
78 goto initialize(NUM_BODY);
|
|
79 }
|
|
80
|
|
81 __code finish(int ret, int num)
|
|
82 {
|
|
83 int i;
|
|
84 DEBUGlog("Gravity = %e\n", Gravitation);
|
|
85 DEBUGlog("FLT_MAX = %e\n", FLT_MAX);
|
|
86 DEBUGlog("FLT_MAX_EXP = %d\n", FLT_MAX_EXP);
|
|
87 DEBUGlog("FLT_MIN = %e\n", FLT_MIN);
|
|
88 DEBUGlog("FLT_MIN_EXP = %d\n", FLT_MIN_EXP);
|
|
89 DEBUGlog("FLT_EPSILON = %e\n", FLT_EPSILON);
|
|
90 free(stars_old);
|
|
91 free(stars_new);
|
|
92 for( i=0; i<NUM_BODY; i++){
|
|
93 gluDeleteQuadric(sphere[i]);
|
|
94 }
|
|
95 free(sphere);
|
|
96 exit(ret);
|
|
97 }
|
|
98
|
|
99
|
|
100 __code initialize(int num)
|
|
101 {
|
|
102 SDL_Surface *screen;
|
|
103 int i;
|
|
104
|
|
105 /* malloc. */
|
|
106 stars_old = malloc( sizeof(body)*num );
|
|
107 stars_new = malloc( sizeof(body)*num );
|
|
108 sphere = malloc( sizeof(GLUquadricObj*)*num );
|
|
109 if (stars_old==NULL||stars_new==NULL||sphere==NULL){
|
|
110 perror("malloc");
|
|
111 goto finish(1, num);
|
|
112 }
|
|
113
|
|
114 /* SDL initialization. */
|
|
115 if(SDL_Init(SDL_INIT_VIDEO) < 0){ //Could we start SDL_VIDEO?
|
|
116 fprintf(stderr,"Couldn't init SDL"); //Nope, output to stderr and quit
|
|
117 exit(1);
|
|
118 }
|
|
119 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5);
|
|
120 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5);
|
|
121 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5);
|
|
122 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
|
|
123 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
|
|
124
|
|
125 screen = SDL_SetVideoMode(W_WIDTH, W_HEIGHT, 32, SDL_HWSURFACE | SDL_RESIZABLE | SDL_OPENGL ); //Create a 640x480x32 resizable window
|
|
126 atexit(SDL_Quit); //Now that we're enabled, make sure we cleanup
|
|
127
|
|
128 /* initialize OpenGL. */
|
|
129 glViewport(0,0,screen->w,screen->h);
|
|
130 glMatrixMode(GL_PROJECTION);
|
|
131 glLoadIdentity();
|
|
132 gluPerspective( 60.0, (float)screen->w/(float)screen->h, 1.0, 1000.0);
|
1
|
133 gluLookAt( 300.0,300.0,300.0, 0.0,0.0,0.0, 1.0,0.0,0.0);
|
0
|
134 glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
135 glMatrixMode(GL_MODELVIEW);
|
|
136
|
|
137 //DEBUGlog("scr->w=%d\n", screen->w);
|
|
138 //DEBUGlog("scr->h=%d\n", screen->h);
|
|
139 for( i=0; i<num; i++){
|
|
140 sphere[i] = gluNewQuadric();
|
|
141 gluQuadricDrawStyle(sphere[i], GLU_FILL);
|
|
142 }
|
|
143
|
|
144 goto starsInit(screen, num);
|
|
145 }
|
|
146
|
1
|
147 __code starsInitRandom(SDL_Surface *screen, int num)
|
0
|
148 {
|
|
149 int i;
|
|
150 srandom(time(NULL));
|
|
151 for (i=0; i<num; i++){ // this loop should be split into few code segment..
|
|
152 stars_old[i].weight = random()/(RAND_MAX+1.0)*5+5;
|
|
153 stars_old[i].v[0] = random()/(RAND_MAX+1.0)*5+5;
|
|
154 stars_old[i].v[1] = random()/(RAND_MAX+1.0)*5+5;
|
|
155 stars_old[i].v[2] = random()/(RAND_MAX+1.0)*5+5;
|
|
156 stars_old[i].r[0] = random()/(RAND_MAX+1.0)*5+5;
|
|
157 stars_old[i].r[1] = random()/(RAND_MAX+1.0)*5+5;
|
|
158 stars_old[i].r[2] = random()/(RAND_MAX+1.0)*5+5;
|
|
159 stars_new[i].weight = stars_old[i].weight;
|
|
160 }
|
|
161
|
|
162 goto loop(0, screen, num);
|
|
163 }
|
|
164 __code starsInit(SDL_Surface *screen, int num)
|
|
165 {
|
|
166 int i;
|
|
167 /* */
|
1
|
168 stars_old[0].weight = 1000;
|
0
|
169 stars_old[0].v[0] = 0.0;
|
|
170 stars_old[0].v[1] = 0.0;
|
|
171 stars_old[0].v[2] = 0.0;
|
|
172 stars_old[0].r[0] = 0.0;
|
|
173 stars_old[0].r[1] = 0.0;
|
|
174 stars_old[0].r[2] = 0.0;
|
1
|
175 /* */
|
|
176 stars_old[1].weight = 5;
|
|
177 stars_old[1].v[0] = 0.1;
|
|
178 stars_old[1].v[1] = 5.0;
|
0
|
179 stars_old[1].v[2] = 0.0;
|
1
|
180 stars_old[1].r[0] = 100.0;
|
0
|
181 stars_old[1].r[1] = 0.0;
|
|
182 stars_old[1].r[2] = 0.0;
|
1
|
183 /* */
|
|
184 stars_old[2].weight = 5;
|
0
|
185 stars_old[2].v[0] = 0.0;
|
1
|
186 stars_old[2].v[1] = -5.0;
|
|
187 stars_old[2].v[2] = 0.1;
|
|
188 stars_old[2].r[0] = -100.0;
|
0
|
189 stars_old[2].r[1] = 0.0;
|
|
190 stars_old[2].r[2] = 0.0;
|
|
191
|
|
192 for( i=0; i<num; i++){
|
|
193 stars_new[i].weight = stars_old[i].weight;
|
|
194 //stars_new[i].rect.h = 5, stars_new[i].rect.w = 5;
|
|
195 //stars_old[i].rect.h = 5, stars_old[i].rect.w = 5;
|
|
196 }
|
|
197
|
|
198 goto loop(0, screen, num);
|
|
199 }
|
|
200
|
|
201 __code loop(int count, SDL_Surface *screen, int num)
|
|
202 {
|
|
203 SDL_Event event;
|
|
204
|
|
205 /* check SDL event. */
|
|
206 while(SDL_PollEvent(&event)){ //Poll events
|
|
207 switch(event.type){ //Check event type
|
|
208 case SDL_QUIT: //User hit the X (or equivelent)
|
|
209 goto finish(1, num);
|
|
210 //case SDL_VIDEORESIZE: //User resized window
|
|
211 //screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32,
|
|
212 //SDL_HWSURFACE | SDL_RESIZABLE); // Create new window
|
|
213 //break; //Event handled, fetch next :)
|
|
214 case SDL_KEYDOWN:
|
|
215 if (event.key.keysym.sym==SDLK_UP && event.key.state==SDL_PRESSED)
|
|
216 FIELD *= 2.0;
|
|
217 else if (event.key.keysym.sym==SDLK_DOWN && event.key.state==SDL_PRESSED)
|
|
218 FIELD /= 2.0;
|
|
219 else if (event.key.keysym.sym==SDLK_r && event.key.state==SDL_PRESSED)
|
|
220 goto moveCenter(count, screen, num);
|
|
221 else if (event.key.keysym.sym==SDLK_v && event.key.state==SDL_PRESSED)
|
|
222 goto CenteringVelocity(count, screen, num);
|
|
223 else if (event.key.keysym.sym==SDLK_l && event.key.state==SDL_PRESSED)
|
|
224 goto AdjustLook(count, screen, num);
|
|
225 break;
|
|
226 default:
|
|
227 break;
|
|
228 } //Finished with current event
|
|
229 } //Done with all events for now
|
|
230
|
|
231 if ( count<num ){
|
|
232 DEBUGlog("count %d, goto commpute().\n", count);
|
|
233 goto compute(count, screen, num);
|
|
234 }else{
|
|
235 DEBUGlog("count %d, goto nextTurn()\n", count);
|
|
236 goto nextTurn(count, screen, num);
|
|
237 }
|
|
238 }
|
|
239
|
|
240 __code AdjustLook(int count, SDL_Surface *screen, int num)
|
|
241 {
|
|
242 int i;
|
|
243 float c0,c1,c2;
|
|
244 float v0,v1,v2,v;
|
|
245 c0=c1=c2=v0=v1=v2=0;
|
|
246
|
|
247 for (i=0; i<num; i++){
|
|
248 c0 += stars_new[i].r[0];
|
|
249 c1 += stars_new[i].r[1];
|
|
250 c2 += stars_new[i].r[2];
|
|
251 }
|
|
252 c0/=3.0; c1/=3.0; c2/=3.0;
|
|
253 for (i=0; i<num; i++){
|
|
254 v0 += (stars_new[i].r[0]-c0)*(stars_new[i].r[0]-c0);
|
|
255 v1 += (stars_new[i].r[1]-c1)*(stars_new[i].r[1]-c1);
|
|
256 v2 += (stars_new[i].r[2]-c2)*(stars_new[i].r[2]-c2);
|
|
257 }
|
|
258 v = v0+v1+v2;
|
|
259 v0 = sqrt(v0); v1 = sqrt(v1); v2 = sqrt(v2);
|
|
260 v = sqrt(v);
|
|
261
|
|
262 glMatrixMode(GL_PROJECTION);
|
|
263 glLoadIdentity();
|
|
264 gluPerspective( 60.0, (float)screen->w/(float)screen->h, 1.0, v);
|
|
265 gluLookAt( v,v,v, 0.0,0.0,0.0, 1.0,0.0,0.0);
|
|
266 glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
267 glMatrixMode(GL_MODELVIEW);
|
|
268
|
|
269 goto loop(0, screen, num);
|
|
270 }
|
|
271
|
|
272 __code CenteringVelocity(int count, SDL_Surface *screen, int num)
|
|
273 {
|
|
274 int i;
|
|
275 float v0,v1,v2;
|
|
276 v0=v1=v2=0.0;
|
|
277
|
|
278 for (i=0; i<num; i++){
|
|
279 v0 += stars_old[i].v[0];
|
|
280 v1 += stars_old[i].v[1];
|
|
281 v2 += stars_old[i].v[2];
|
|
282 }
|
|
283 v0/=(float)num; v1/=(float)num; v2/=(float)num;
|
|
284 for (i=0; i<num; i++){
|
|
285 stars_old[i].v[0] -= v0;
|
|
286 stars_old[i].v[1] -= v1;
|
|
287 stars_old[i].v[2] -= v2;
|
|
288 }
|
|
289
|
|
290 goto loop(0, screen, num);
|
|
291 }
|
|
292 __code moveCenter(int count, SDL_Surface *screen, int num)
|
|
293 {
|
|
294 int i;
|
|
295 float m0,m1,m2;
|
|
296 m0=m1=m2=0.0;
|
|
297
|
|
298 for (i=0; i<num; i++){
|
|
299 m0 += stars_old[i].r[0];
|
|
300 m1 += stars_old[i].r[1];
|
|
301 m2 += stars_old[i].r[2];
|
|
302 }
|
|
303 m0/=(float)num; m1/=(float)num; m2/=(float)num;
|
|
304 for (i=0; i<num; i++){
|
|
305 stars_old[i].r[0] -= m0;
|
|
306 stars_old[i].r[1] -= m1;
|
|
307 stars_old[i].r[2] -= m2;
|
|
308 }
|
|
309
|
|
310 goto loop(0, screen, num);
|
|
311 }
|
|
312
|
|
313 /*
|
|
314 * next x = x+dx
|
|
315 * dx = v*dt => dx/dt = v
|
|
316 * dv = a*dt => dv/dt = a
|
|
317 * a = F/m;
|
|
318 * F = F1 + F2 + ... + Fn
|
|
319 * Fi = G m1m2/r^2
|
|
320 *
|
|
321 */
|
|
322 __code compute(int count, SDL_Surface *screen, int num)
|
|
323 {
|
|
324 int i;
|
|
325 /* a is accel this planet receive now. */
|
|
326 stars_old[count].a[0]=stars_old[count].a[1]=stars_old[count].a[2]=0.0f;
|
|
327 DEBUGlog("count=%d\n", count);
|
|
328 for (i=0; i<num; i++){ // this loop should be split into few code segment..
|
|
329 //float F;
|
|
330 float d0, d1, d2, d;
|
|
331 float a;
|
|
332 //body *o, *m;
|
|
333 //o = &stars_old[i]; m = &stars_old[count];
|
|
334
|
|
335 /* skip compute with itself. */
|
|
336 if ( i==count ) continue;
|
|
337 /* compute distance between two i-th planet and itself. */
|
|
338 d0 = stars_old[i].r[0] - stars_old[count].r[0];
|
|
339 d1 = stars_old[i].r[1] - stars_old[count].r[1];
|
|
340 d2 = stars_old[i].r[2] - stars_old[count].r[2];
|
1
|
341 d = ( d0*d0+d1*d1+d2*d2+eps*eps );
|
0
|
342 /* compute force it receive from i-th planet. */
|
|
343 //F = Gravitation * stars_old[i].weight * stars_old[count].weight / d;
|
|
344 /* and accel. */
|
|
345 //a = F/stars_old[count].weight;
|
|
346 a = Gravitation/d * stars_old[i].weight ;
|
|
347 stars_old[count].a[0] += a*d0/sqrt(d);
|
|
348 stars_old[count].a[1] += a*d1/sqrt(d);
|
|
349 stars_old[count].a[2] += a*d2/sqrt(d);
|
|
350 DEBUGlog("a=%e, d=%e, d0=%e\n", a, d, d0);
|
|
351 DEBUGlog("g*w=%e\n", Gravitation*stars_old[i].weight);
|
|
352 }
|
|
353
|
|
354 stars_new[count].v[0] = stars_old[count].v[0]+stars_old[count].a[0]*delta;
|
|
355 stars_new[count].v[1] = stars_old[count].v[1]+stars_old[count].a[1]*delta;
|
|
356 stars_new[count].v[2] = stars_old[count].v[2]+stars_old[count].a[2]*delta;
|
|
357 stars_new[count].r[0] = stars_old[count].r[0]+stars_new[count].v[0]*delta/*+stars_old[count].a[0]*delta*delta/2.0*/;
|
|
358 stars_new[count].r[1] = stars_old[count].r[1]+stars_new[count].v[1]*delta/*+stars_old[count].a[1]*delta*delta/2.0*/;
|
|
359 stars_new[count].r[2] = stars_old[count].r[2]+stars_new[count].v[2]*delta/*+stars_old[count].a[2]*delta*delta/2.0*/;
|
|
360
|
|
361 DEBUGlog("x = %e\n", stars_new[count].r[0]);
|
|
362 DEBUGlog("y = %e\n", stars_new[count].r[1]);
|
|
363 DEBUGlog("z = %e\n", stars_new[count].r[2]);
|
|
364 DEBUGlog("vx = %e\n", stars_new[count].v[0]);
|
|
365 DEBUGlog("vy = %e\n", stars_new[count].v[1]);
|
|
366 DEBUGlog("vz = %e\n", stars_new[count].v[2]);
|
|
367 DEBUGlog("a0=%e,a1=%e,a2=%e\n", stars_new[count].a[0], stars_new[count].a[1], stars_new[count].a[2]);
|
|
368
|
|
369 goto loop(++count, screen, num);
|
|
370 }
|
|
371
|
|
372 /* it is executed after all bodies parameter is computed. */
|
|
373 __code nextTurn(int count, SDL_Surface *screen, int num)
|
|
374 {
|
|
375 body *tmp;
|
|
376 tmp = stars_old;
|
|
377 stars_old = stars_new;
|
|
378 stars_new = tmp;
|
|
379 SDL_UpdateRect(screen, 0, 0, 0, 0);
|
|
380
|
|
381 goto drawStars(count, screen, num);
|
|
382 }
|
|
383
|
|
384 __code drawStars(int count, SDL_Surface *screen, int num)
|
|
385 {
|
|
386 int i;
|
|
387 DEBUGlog("draw\n");
|
|
388 glClear( GL_COLOR_BUFFER_BIT );
|
|
389 for (i=0; i<num; i++){
|
|
390 glPushMatrix();
|
|
391 glTranslatef( stars_new[i].r[0], stars_new[i].r[1], stars_new[i].r[2]);
|
1
|
392 gluSphere( sphere[i], 2.0, 8.0, 8.0 );
|
0
|
393 glPopMatrix();
|
|
394 }
|
|
395
|
|
396 glColor3d( 1.0, 1.0, 1.0);
|
|
397 glBegin(GL_LINES);
|
|
398 glVertex3d( 0.0, 0.0, 0.0);
|
|
399 glVertex3d( 10.0, 0.0, 0.0);
|
|
400 glEnd();
|
|
401 glBegin(GL_LINES);
|
|
402 glVertex3d( 0.0, 0.0, 0.0);
|
|
403 glVertex3d( 0.0, 10.0, 0.0);
|
|
404 glEnd();
|
|
405 glBegin(GL_LINES);
|
|
406 glVertex3d( 0.0, 0.0, 0.0);
|
|
407 glVertex3d( 0.0, 0.0, 10.0);
|
|
408 glEnd();
|
|
409
|
|
410 SDL_GL_SwapBuffers();
|
|
411 goto loop(0, screen, num);
|
|
412 }
|
|
413
|