changeset 3:01290d71ef9c

add figures into comment area of main_GL.cbc
author kent
date Fri, 30 May 2008 12:36:59 +0900
parents 3e543e31b6eb
children 5535e1d5e693
files Makefile main_GL.cbc
diffstat 2 files changed, 131 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- 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
 
--- 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<unistd.h>
 #include<time.h>
 #include<float.h>
+#include<math.h>
 #include<SDL.h>
 #include<OpenGL/gl.h>
 #include<OpenGL/glu.h>
 
-#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<num; i++){
 		sphere[i] = gluNewQuadric();
 		gluQuadricDrawStyle(sphere[i], GLU_FILL);
@@ -199,20 +199,32 @@
 	goto loop(0, screen, num);
 }
 
+
+/* MAIN LOOP ROUTIN
+ *
+  +------+
+  | loop |<-----------------------+
+  +---+--+                        |
+      +-----------+               |
+      |count=num  | count<num     |
+  +---v------+ +--v-------------+ |
+  | nextTurn | | COMPUTE ROUTIN | |
+  +---+------+ +-------+--------+ |
+      |                |          |
+  +---v---------+      v          |
+  | DRAW ROUTIN +----->+----------+
+  +-------------+    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<num; i++){
-		v0 += stars_old[i].v[0];
-		v1 += stars_old[i].v[1];
-		v2 += stars_old[i].v[2];
-	}
-	v0/=(float)num; v1/=(float)num; v2/=(float)num;
-	for (i=0; i<num; i++){
-		stars_old[i].v[0] -= v0;
-		stars_old[i].v[1] -= v1;
-		stars_old[i].v[2] -= v2;
-	}
-
-	goto loop(0, screen, num);
-}
-__code moveCenter(int count, SDL_Surface *screen, int num)
-{
-	int i;
-	float m0,m1,m2;
-	m0=m1=m2=0.0;
-
-	for (i=0; i<num; i++){
-		m0 += stars_old[i].r[0];
-		m1 += stars_old[i].r[1];
-		m2 += stars_old[i].r[2];
-	}
-	m0/=(float)num; m1/=(float)num; m2/=(float)num;
-	for (i=0; i<num; i++){
-		stars_old[i].r[0] -= m0;
-		stars_old[i].r[1] -= m1;
-		stars_old[i].r[2] -= m2;
-	}
-
-	goto loop(0, screen, num);
-}
-
 /*
+ * COMPUTING ROUTINs
+ *
  *  next x = x+dx
  *  dx = v*dt  =>  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
+        | i==num
+  +-----+---------+
+  |  com|ute_out  |
+  +-----+---------+
+        v to loop
  */
+__code compute_out(int count, SDL_Surface *screen, int num);
+__code computeForce(int i, int count, SDL_Surface *screen, int num);
 __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<num; i++){ // this loop should be split into few code segment..
-		//float F;
-		float d0, d1, d2, d;
-		float a;
-		//body *o, *m;
-		//o = &stars_old[i]; m = &stars_old[count];
+
+	goto computeForce( 0, count, screen, num);
+}
+__code computeForce(int i, int count, SDL_Surface *screen, int num)
+{
+	//float F;
+	float d0, d1, d2, d;
+	float a;
+	//body *o, *m;
+	//o = &stars_old[i]; m = &stars_old[count];
+	
+	if (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<num
+       | count==num
+  +----v----------+
+  | drawStars_out |
+  +----+----------+
+       | to loop
+ */
+__code drawStar(int i, SDL_Surface *screen, int num);
+__code drawStars_out(SDL_Surface *screen, int num);
+
+__code drawStars(SDL_Surface *screen, int num)
 {
-	int i;
 	DEBUGlog("draw\n");
 	glClear( GL_COLOR_BUFFER_BIT );
-	for (i=0; i<num; i++){
-		glPushMatrix();
-		glTranslatef( stars_new[i].r[0], stars_new[i].r[1], stars_new[i].r[2]);
-		gluSphere( sphere[i], 2.0, 32.0, 32.0 );
-		glPopMatrix();
-	}
-
 	glColor3d( 1.0, 1.0, 1.0);
 	glBegin(GL_LINES);
 	glVertex3d( 0.0, 0.0, 0.0);
@@ -393,7 +404,29 @@
 	glVertex3d( 0.0, 0.0, 200.0);
 	glEnd();
 
+	goto drawStar(0, screen, num);
+}
+
+__code drawStars_out(SDL_Surface *screen, int num)
+{
+	DEBUGlog("draw axis: screen=%p, num=%d\n", screen, num);
 	SDL_GL_SwapBuffers();
+	DEBUGlog("go loop\n");
 	goto loop(0, screen, num);
 }
 
+__code drawStar(int i, SDL_Surface *screen, int num)
+{
+	if (i==num)
+		goto drawStars_out(screen, num);
+
+	DEBUGlog("i=%d, num=%d\n", i, num);
+	glPushMatrix();
+	glTranslatef( stars_new[i].r[0], stars_new[i].r[1], stars_new[i].r[2]);
+	gluSphere( sphere[i], 2.0, 32.0, 32.0 );
+	glPopMatrix();
+
+	DEBUGlog("to myself\n");
+	goto drawStar(i+1, screen, num);
+}
+