# HG changeset patch # User kent # Date 1212118619 -32400 # Node ID 01290d71ef9c94c60596cc68a330b21c83d5c6d9 # Parent 3e543e31b6eb08b754ceee3e3ffb13288fb75704 add figures into comment area of main_GL.cbc diff -r 3e543e31b6eb -r 01290d71ef9c Makefile --- a/Makefile Fri May 30 00:29:56 2008 +0900 +++ b/Makefile Fri May 30 12:36:59 2008 +0900 @@ -2,9 +2,9 @@ CbCC = ~/work/cvs_tmp/CbC_project/build-dir/INSTALL-DIR/bin/gcc .SUFFIXES: .cbc .o -CFLAGS = -Wall -g -O2 +CFLAGS = -Wall -g -O0 INCLUDES = `sdl-config --cflags` -LIBS = `sdl-config --libs` +LIBS = `sdl-config --libs` -lm GL_LIBS = -Wl,-framework,OpenGL diff -r 3e543e31b6eb -r 01290d71ef9c main_GL.cbc --- a/main_GL.cbc Fri May 30 00:29:56 2008 +0900 +++ b/main_GL.cbc Fri May 30 12:36:59 2008 +0900 @@ -3,13 +3,17 @@ #include #include #include +#include #include #include #include -#define DEBUGlog(f, args...) \ - ; - //fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) +#ifdef DEBUG + #define DEBUGlog(f, args...) \ + fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) +#else + #define DEBUGlog(f, args...) ; +#endif #define W_HEIGHT 480 #define W_WIDTH 640 @@ -20,13 +24,13 @@ /* parameters */ static int NUM_BODY = 3; -//static float Gravitation = 6.67e-11 ; -//static float delta = 100; -//static float FIELD = 2e11; static float Gravitation = 100.0f; // ? static float delta = 0.05f; // 0.01 ~ 100 ? -static float FIELD = 400.0f; // -100 ~ 100 static const float eps = 15.0f; +/* + * だいたい、平均距離をn倍にするんだったら、Gravitationoはn^2倍、 + * epsはn倍 + */ /* for OpenGL Utility. */ GLUquadricObj **sphere; @@ -52,9 +56,7 @@ __code loop(int count, SDL_Surface *screen, int num); __code compute(int count, SDL_Surface *screen, int num); __code nextTurn(int count, SDL_Surface *screen, int num); -__code moveCenter(int count, SDL_Surface *screen, int num); -__code CenteringVelocity(int count, SDL_Surface *screen, int num); -__code drawStars(int count, SDL_Surface *screen, int num); +__code drawStars(SDL_Surface *screen, int num); __code AdjustLooking(float distance, int count, SDL_Surface *screen, int num); int main(int argc, char **argv) @@ -137,8 +139,6 @@ glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_MODELVIEW); - //DEBUGlog("scr->w=%d\n", screen->w); - //DEBUGlog("scr->h=%d\n", screen->h); for( i=0; i+----------+ + +-------------+ back to loop + */ __code loop(int count, SDL_Surface *screen, int num) { SDL_Event event; - usleep(80); + usleep(1000); /* check SDL event. */ while(SDL_PollEvent(&event)){ //Poll events switch(event.type){ //Check event type case SDL_QUIT: //User hit the X (or equivelent) goto finish(1, num); - //case SDL_VIDEORESIZE: //User resized window - //screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, - //SDL_HWSURFACE | SDL_RESIZABLE); // Create new window - //break; //Event handled, fetch next :) case SDL_KEYDOWN: if (event.key.state==SDL_PRESSED){ switch(event.key.keysym.sym){ @@ -255,87 +267,72 @@ goto loop(0, screen, num); } -__code CenteringVelocity(int count, SDL_Surface *screen, int num) -{ - int i; - float v0,v1,v2; - v0=v1=v2=0.0; - - for (i=0; i dx/dt = v * dv = a*dt => dv/dt = a * a = F/m; * F = F1 + F2 + ... + Fn * Fi = G m1m2/r^2 - * + | + | + +-----v-----+ + | compute | + +-----+-----+ + |<----------+ + +-----v---------+ | i++ + | computeForce +-+ + +-----+---------+ i=num) goto compute_out(count, screen, num); + /* skip compute with itself. */ + else if (i==count) goto computeForce(i+1, count, screen, num); - /* skip compute with itself. */ - if ( i==count ) continue; - /* compute distance between two i-th planet and itself. */ - d0 = stars_old[i].r[0] - stars_old[count].r[0]; - d1 = stars_old[i].r[1] - stars_old[count].r[1]; - d2 = stars_old[i].r[2] - stars_old[count].r[2]; - d = ( d0*d0+d1*d1+d2*d2+eps*eps ); - /* compute force it receive from i-th planet. */ - //F = Gravitation * stars_old[i].weight * stars_old[count].weight / d; - /* and accel. */ - //a = F/stars_old[count].weight; - a = Gravitation/d * stars_old[i].weight ; - stars_old[count].a[0] += a*d0/sqrt(d); - stars_old[count].a[1] += a*d1/sqrt(d); - stars_old[count].a[2] += a*d2/sqrt(d); - DEBUGlog("a=%e, d=%e, d0=%e\n", a, d, d0); - DEBUGlog("g*w=%e\n", Gravitation*stars_old[i].weight); - } + /* compute distance between two i-th planet and itself. */ + d0 = stars_old[i].r[0] - stars_old[count].r[0]; + d1 = stars_old[i].r[1] - stars_old[count].r[1]; + d2 = stars_old[i].r[2] - stars_old[count].r[2]; + d = ( d0*d0+d1*d1+d2*d2+eps*eps ); + /* compute force it receive from i-th planet. */ + //F = Gravitation * stars_old[i].weight * stars_old[count].weight / d; + /* and accel. */ + //a = F/stars_old[count].weight; + a = Gravitation/d * stars_old[i].weight ; + stars_old[count].a[0] += a*d0/sqrtf(d); + stars_old[count].a[1] += a*d1/sqrtf(d); + stars_old[count].a[2] += a*d2/sqrtf(d); + DEBUGlog("a=%e, d=%e, d0=%e\n", a, d, d0); + DEBUGlog("g*w=%e\n", Gravitation*stars_old[i].weight); + + goto computeForce( i+1, count, screen, num); +} +__code compute_out(int count, SDL_Surface *screen, int num) +{ stars_new[count].v[0] = stars_old[count].v[0]+stars_old[count].a[0]*delta; stars_new[count].v[1] = stars_old[count].v[1]+stars_old[count].a[1]*delta; @@ -352,7 +349,7 @@ DEBUGlog("vz = %e\n", stars_new[count].v[2]); DEBUGlog("a0=%e,a1=%e,a2=%e\n", stars_new[count].a[0], stars_new[count].a[1], stars_new[count].a[2]); - goto loop(++count, screen, num); + goto loop(1+count, screen, num); } /* it is executed after all bodies parameter is computed. */ @@ -362,23 +359,37 @@ tmp = stars_old; stars_old = stars_new; stars_new = tmp; - SDL_UpdateRect(screen, 0, 0, 0, 0); + //SDL_UpdateRect(screen, 0, 0, 0, 0); - goto drawStars(count, screen, num); + goto drawStars(screen, num); } -__code drawStars(int count, SDL_Surface *screen, int num) + + +/* + * DRAWING ROUTINs + * + | + +----+------+ + | drawStars | + +----+------+ + |<--------+ + +----v-----+ | count++ + | drawStar +---+ + +----+-----+ count