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