# HG changeset patch # User kent # Date 1212057324 -32400 # Node ID 249965d0a68f85ef910c79d1d5e80be7dd266ade GL is no finish. diff -r 000000000000 -r 249965d0a68f Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Thu May 29 19:35:24 2008 +0900 @@ -0,0 +1,27 @@ + +CbCC = ~/work/cvs_tmp/CbC_project/build-dir/INSTALL-DIR/bin/gcc +.SUFFIXES: .cbc .o + +CFLAGS = -Wall -g -O2 +INCLUDES = `sdl-config --cflags` +LIBS = `sdl-config --libs` + +GL_LIBS = -Wl,-framework,OpenGL + +TARGETS = main main_GL + +%.o: %.c + +all: $(TARGETS) + +.cbc.o: + $(CbCC) -c $(INCLUDES) $(CFLAGS) $^ -o $@ + +main: main.o + $(CbCC) $(LIBS) $(CFLAGS) $^ -o $@ +main_GL: main_GL.o + $(CbCC) $(LIBS) $(GL_LIBS) $(CFLAGS) $^ -o $@ + +clean: + rm -f *.o *.s $(TARGETS) + diff -r 000000000000 -r 249965d0a68f main.cbc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cbc Thu May 29 19:35:24 2008 +0900 @@ -0,0 +1,406 @@ +#include +#include +#include +#include +#include +#include + +#define DEBUGlog(f, args...) \ + ; + //fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) + +#define W_HEIGHT 480 +#define W_WIDTH 640 + +/* + N body problem +*/ + +static int NUM_BODY = 3; +//static float Gravitation = 6.67e-11 ; +//static float delta = 100; +//static float FIELD = 2e11; +static float Gravitation = 1.0f; // ? +static float delta = 0.05f; // +static float FIELD = 400.0f; // -100 ~ 100 +static const float eps = 1.5f; + +typedef struct +{ + /* star's parameter. */ + float weight; + float a[3]; /* acceleration */ + float v[3]; /* velocity */ + float r[3]; /* position */ + /* for SDL. */ + SDL_Rect rect; +} body; +body *stars_old, *stars_new; + +void start(void); +__code initialize(int num); +__code randomInit(SDL_Surface *screen, int num); +__code starsInit(SDL_Surface *screen, int num); +__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); + +int main(int argc, char **argv) +{ + int ch; + while ((ch = getopt(argc, argv, "s:g:")) != -1) { + switch (ch) { + case 's': + delta = atof(optarg); + break; + case 'g': + Gravitation = atof(optarg); + break; + case '?': + default: + break; + } + } + start(); + return 0; +} +void start() +{ + goto initialize(NUM_BODY); +} + +__code finish(int ret) +{ + DEBUGlog("Gravity = %e\n", Gravitation); + DEBUGlog("FLT_MAX = %e\n", FLT_MAX); + DEBUGlog("FLT_MAX_EXP = %d\n", FLT_MAX_EXP); + DEBUGlog("FLT_MIN = %e\n", FLT_MIN); + DEBUGlog("FLT_MIN_EXP = %d\n", FLT_MIN_EXP); + DEBUGlog("FLT_EPSILON = %e\n", FLT_EPSILON); + free(stars_old); + free(stars_new); + exit(ret); +} + + +__code initialize(int num) +{ + SDL_Surface *screen; + + /* malloc. */ + stars_old = malloc( sizeof(body)*num ); + stars_new = malloc( sizeof(body)*num ); + if (stars_old==NULL||stars_new==NULL){ + perror("malloc"); + goto finish(1); + } + + /* SDL initialization. */ + if(SDL_Init(SDL_INIT_VIDEO) < 0){ //Could we start SDL_VIDEO? + fprintf(stderr,"Couldn't init SDL"); //Nope, output to stderr and quit + exit(1); + } + screen = SDL_SetVideoMode(W_WIDTH, W_HEIGHT, 32, SDL_HWSURFACE | SDL_RESIZABLE); //Create a 640x480x32 resizable window + atexit(SDL_Quit); //Now that we're enabled, make sure we cleanup + + goto starsInit(screen, num); +} + +__code starsInit0(SDL_Surface *screen, int num) +{ + int i; + srandom(time(NULL)); + 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 + * + */ +__code compute(int count, SDL_Surface *screen, int num) +{ + int i; + /* a is accel this planet receive now. */ + stars_old[count].a[0]=stars_old[count].a[1]=stars_old[count].a[2]=0.0f; + DEBUGlog("count=%d\n", count); + for (i=0; iformat, 0x00,0x00,0x00)); + SDL_FillRect(screen, &stars_new[count].rect, SDL_MapRGB(screen->format, 0xff,0x88,0x33)); + //SDL_BlitSurface(screen, &stars_old[count].rect, screen, &stars_new[count].rect); + + goto loop(++count, screen, num); +} + +/* it is executed after all bodies parameter is computed. */ +__code nextTurn(int count, SDL_Surface *screen, int num) +{ + body *tmp; + tmp = stars_old; + stars_old = stars_new; + stars_new = tmp; + SDL_UpdateRect(screen, 0, 0, 0, 0); + + goto loop(0, screen, num); +} + + + +/* + +--------+-----------+ + | v | + | +-----------+ | + | | loop | | + | +-----+-----+ | + | | | + | +-----v-----+ | + | + compute +-----+ + | +-----+-----+ count++ + | | + | | count==num + | +-----v-----+ + | | nextTurn | + | +-----+-----+ + | | count=0 + +--------+ +*/ + + + diff -r 000000000000 -r 249965d0a68f main_GL.cbc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main_GL.cbc Thu May 29 19:35:24 2008 +0900 @@ -0,0 +1,450 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEBUGlog(f, args...) \ + fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) + +#define W_HEIGHT 480 +#define W_WIDTH 640 + +/* + N body problem +*/ + +/* parameters */ +static int NUM_BODY = 3; +//static float Gravitation = 6.67e-11 ; +//static float delta = 100; +//static float FIELD = 2e11; +static float Gravitation = 0.2f; // ? +static float delta = 100.0f; // 0.01 ~ 100 ? +static float FIELD = 400.0f; // -100 ~ 100 +static const float eps = 0.0f; + +/* for OpenGL Utility. */ +GLUquadricObj **sphere; + +typedef struct +{ + /* star's parameter. */ + float weight; + float a[3]; /* acceleration */ + float v[3]; /* velocity */ + float r[3]; /* position */ + /* for SDL. */ + //SDL_Rect rect; +} body; +body *stars_old, *stars_new; + + +void start(void); +__code initialize(int num); +__code randomInit(SDL_Surface *screen, int num); +__code starsInit(SDL_Surface *screen, int num); +__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); + +int main(int argc, char **argv) +{ + int ch; + while ((ch = getopt(argc, argv, "s:g:")) != -1) { + switch (ch) { + case 's': + delta = atof(optarg); + break; + case 'g': + Gravitation = atof(optarg); + break; + case '?': + default: + break; + } + } + start(); + return 0; +} +void start() +{ + goto initialize(NUM_BODY); +} + +__code finish(int ret, int num) +{ + int i; + DEBUGlog("Gravity = %e\n", Gravitation); + DEBUGlog("FLT_MAX = %e\n", FLT_MAX); + DEBUGlog("FLT_MAX_EXP = %d\n", FLT_MAX_EXP); + DEBUGlog("FLT_MIN = %e\n", FLT_MIN); + DEBUGlog("FLT_MIN_EXP = %d\n", FLT_MIN_EXP); + DEBUGlog("FLT_EPSILON = %e\n", FLT_EPSILON); + free(stars_old); + free(stars_new); + for( i=0; iw,screen->h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective( 60.0, (float)screen->w/(float)screen->h, 1.0, 1000.0); + gluLookAt( 500.0,500.0,500.0, 0.0,0.0,0.0, 1.0,0.0,0.0); + 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; iw/(float)screen->h, 1.0, v); + gluLookAt( v,v,v, 0.0,0.0,0.0, 1.0,0.0,0.0); + glClearColor(0.0, 0.0, 0.0, 0.0); + glMatrixMode(GL_MODELVIEW); + + 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 + * + */ +__code compute(int count, SDL_Surface *screen, int num) +{ + int i; + /* a is accel this planet receive now. */ + stars_old[count].a[0]=stars_old[count].a[1]=stars_old[count].a[2]=0.0f; + DEBUGlog("count=%d\n", count); + for (i=0; i