Mercurial > hg > Members > kent > N-BodyProblem
comparison main_GL.cbc @ 2:3e543e31b6eb
adjustment main_GL.cbc
author | kent |
---|---|
date | Fri, 30 May 2008 00:29:56 +0900 |
parents | 09e774f4433f |
children | 01290d71ef9c |
comparison
equal
deleted
inserted
replaced
1:09e774f4433f | 2:3e543e31b6eb |
---|---|
6 #include<SDL.h> | 6 #include<SDL.h> |
7 #include<OpenGL/gl.h> | 7 #include<OpenGL/gl.h> |
8 #include<OpenGL/glu.h> | 8 #include<OpenGL/glu.h> |
9 | 9 |
10 #define DEBUGlog(f, args...) \ | 10 #define DEBUGlog(f, args...) \ |
11 fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) | 11 ; |
12 //fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) | |
12 | 13 |
13 #define W_HEIGHT 480 | 14 #define W_HEIGHT 480 |
14 #define W_WIDTH 640 | 15 #define W_WIDTH 640 |
15 | 16 |
16 /* | 17 /* |
27 static float FIELD = 400.0f; // -100 ~ 100 | 28 static float FIELD = 400.0f; // -100 ~ 100 |
28 static const float eps = 15.0f; | 29 static const float eps = 15.0f; |
29 | 30 |
30 /* for OpenGL Utility. */ | 31 /* for OpenGL Utility. */ |
31 GLUquadricObj **sphere; | 32 GLUquadricObj **sphere; |
33 float DefDistance = 300.0f; | |
32 | 34 |
33 typedef struct | 35 typedef struct |
34 { | 36 { |
35 /* star's parameter. */ | 37 /* star's parameter. */ |
36 float weight; | 38 float weight; |
51 __code compute(int count, SDL_Surface *screen, int num); | 53 __code compute(int count, SDL_Surface *screen, int num); |
52 __code nextTurn(int count, SDL_Surface *screen, int num); | 54 __code nextTurn(int count, SDL_Surface *screen, int num); |
53 __code moveCenter(int count, SDL_Surface *screen, int num); | 55 __code moveCenter(int count, SDL_Surface *screen, int num); |
54 __code CenteringVelocity(int count, SDL_Surface *screen, int num); | 56 __code CenteringVelocity(int count, SDL_Surface *screen, int num); |
55 __code drawStars(int count, SDL_Surface *screen, int num); | 57 __code drawStars(int count, SDL_Surface *screen, int num); |
58 __code AdjustLooking(float distance, int count, SDL_Surface *screen, int num); | |
56 | 59 |
57 int main(int argc, char **argv) | 60 int main(int argc, char **argv) |
58 { | 61 { |
59 int ch; | 62 int ch; |
60 while ((ch = getopt(argc, argv, "s:g:")) != -1) { | 63 while ((ch = getopt(argc, argv, "s:g:")) != -1) { |
189 stars_old[2].r[1] = 0.0; | 192 stars_old[2].r[1] = 0.0; |
190 stars_old[2].r[2] = 0.0; | 193 stars_old[2].r[2] = 0.0; |
191 | 194 |
192 for( i=0; i<num; i++){ | 195 for( i=0; i<num; i++){ |
193 stars_new[i].weight = stars_old[i].weight; | 196 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 } |
197 | 198 |
198 goto loop(0, screen, num); | 199 goto loop(0, screen, num); |
199 } | 200 } |
200 | 201 |
201 __code loop(int count, SDL_Surface *screen, int num) | 202 __code loop(int count, SDL_Surface *screen, int num) |
202 { | 203 { |
203 SDL_Event event; | 204 SDL_Event event; |
204 | 205 |
206 usleep(80); | |
205 /* check SDL event. */ | 207 /* check SDL event. */ |
206 while(SDL_PollEvent(&event)){ //Poll events | 208 while(SDL_PollEvent(&event)){ //Poll events |
207 switch(event.type){ //Check event type | 209 switch(event.type){ //Check event type |
208 case SDL_QUIT: //User hit the X (or equivelent) | 210 case SDL_QUIT: //User hit the X (or equivelent) |
209 goto finish(1, num); | 211 goto finish(1, num); |
210 //case SDL_VIDEORESIZE: //User resized window | 212 //case SDL_VIDEORESIZE: //User resized window |
211 //screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, | 213 //screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, |
212 //SDL_HWSURFACE | SDL_RESIZABLE); // Create new window | 214 //SDL_HWSURFACE | SDL_RESIZABLE); // Create new window |
213 //break; //Event handled, fetch next :) | 215 //break; //Event handled, fetch next :) |
214 case SDL_KEYDOWN: | 216 case SDL_KEYDOWN: |
215 if (event.key.keysym.sym==SDLK_UP && event.key.state==SDL_PRESSED) | 217 if (event.key.state==SDL_PRESSED){ |
216 FIELD *= 2.0; | 218 switch(event.key.keysym.sym){ |
217 else if (event.key.keysym.sym==SDLK_DOWN && event.key.state==SDL_PRESSED) | 219 case SDLK_UP: |
218 FIELD /= 2.0; | 220 goto AdjustLooking(DefDistance/=1.2, count, screen, num); |
219 else if (event.key.keysym.sym==SDLK_r && event.key.state==SDL_PRESSED) | 221 case SDLK_DOWN: |
220 goto moveCenter(count, screen, num); | 222 goto AdjustLooking(DefDistance*=1.2, count, screen, num); |
221 else if (event.key.keysym.sym==SDLK_v && event.key.state==SDL_PRESSED) | 223 case SDLK_l: |
222 goto CenteringVelocity(count, screen, num); | 224 goto AdjustLooking(DefDistance, count, screen, num); |
223 else if (event.key.keysym.sym==SDLK_l && event.key.state==SDL_PRESSED) | 225 case SDLK_q: |
224 goto AdjustLook(count, screen, num); | 226 goto finish(0, num); |
227 default: | |
228 break; | |
229 } | |
230 } | |
225 break; | 231 break; |
226 default: | 232 default: |
227 break; | 233 break; |
228 } //Finished with current event | 234 } //Finished with current event |
229 } //Done with all events for now | 235 } //Done with all events for now |
235 DEBUGlog("count %d, goto nextTurn()\n", count); | 241 DEBUGlog("count %d, goto nextTurn()\n", count); |
236 goto nextTurn(count, screen, num); | 242 goto nextTurn(count, screen, num); |
237 } | 243 } |
238 } | 244 } |
239 | 245 |
240 __code AdjustLook(int count, SDL_Surface *screen, int num) | 246 __code AdjustLooking(float distance, int count, SDL_Surface *screen, int num) |
241 { | 247 { |
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); | 248 glMatrixMode(GL_PROJECTION); |
263 glLoadIdentity(); | 249 glLoadIdentity(); |
264 gluPerspective( 60.0, (float)screen->w/(float)screen->h, 1.0, v); | 250 gluPerspective( 60.0, (float)screen->w/(float)screen->h, 1.0, distance*2.5); |
265 gluLookAt( v,v,v, 0.0,0.0,0.0, 1.0,0.0,0.0); | 251 gluLookAt( distance,distance,distance, 0.0,0.0,0.0, 1.0,0.0,0.0); |
266 glClearColor(0.0, 0.0, 0.0, 0.0); | 252 glClearColor(0.0, 0.0, 0.0, 0.0); |
267 glMatrixMode(GL_MODELVIEW); | 253 glMatrixMode(GL_MODELVIEW); |
268 | 254 |
269 goto loop(0, screen, num); | 255 goto loop(0, screen, num); |
270 } | 256 } |
387 DEBUGlog("draw\n"); | 373 DEBUGlog("draw\n"); |
388 glClear( GL_COLOR_BUFFER_BIT ); | 374 glClear( GL_COLOR_BUFFER_BIT ); |
389 for (i=0; i<num; i++){ | 375 for (i=0; i<num; i++){ |
390 glPushMatrix(); | 376 glPushMatrix(); |
391 glTranslatef( stars_new[i].r[0], stars_new[i].r[1], stars_new[i].r[2]); | 377 glTranslatef( stars_new[i].r[0], stars_new[i].r[1], stars_new[i].r[2]); |
392 gluSphere( sphere[i], 2.0, 8.0, 8.0 ); | 378 gluSphere( sphere[i], 2.0, 32.0, 32.0 ); |
393 glPopMatrix(); | 379 glPopMatrix(); |
394 } | 380 } |
395 | 381 |
396 glColor3d( 1.0, 1.0, 1.0); | 382 glColor3d( 1.0, 1.0, 1.0); |
397 glBegin(GL_LINES); | 383 glBegin(GL_LINES); |
398 glVertex3d( 0.0, 0.0, 0.0); | 384 glVertex3d( 0.0, 0.0, 0.0); |
399 glVertex3d( 10.0, 0.0, 0.0); | 385 glVertex3d( 200.0, 0.0, 0.0); |
400 glEnd(); | 386 glEnd(); |
401 glBegin(GL_LINES); | 387 glBegin(GL_LINES); |
402 glVertex3d( 0.0, 0.0, 0.0); | 388 glVertex3d( 0.0, 0.0, 0.0); |
403 glVertex3d( 0.0, 10.0, 0.0); | 389 glVertex3d( 0.0, 200.0, 0.0); |
404 glEnd(); | 390 glEnd(); |
405 glBegin(GL_LINES); | 391 glBegin(GL_LINES); |
406 glVertex3d( 0.0, 0.0, 0.0); | 392 glVertex3d( 0.0, 0.0, 0.0); |
407 glVertex3d( 0.0, 0.0, 10.0); | 393 glVertex3d( 0.0, 0.0, 200.0); |
408 glEnd(); | 394 glEnd(); |
409 | 395 |
410 SDL_GL_SwapBuffers(); | 396 SDL_GL_SwapBuffers(); |
411 goto loop(0, screen, num); | 397 goto loop(0, screen, num); |
412 } | 398 } |