Xcode/TemplatesForProjectBuilder/SDL OpenGL Application/atlantis/atlantis.c
changeset 2220 4d2d0548f5b2
parent 2213 59a667370c57
equal deleted inserted replaced
2219:f6099efa3f0b 2220:4d2d0548f5b2
    52 
    52 
    53 GLboolean Timing = GL_TRUE;
    53 GLboolean Timing = GL_TRUE;
    54 
    54 
    55 int w_win = 640;
    55 int w_win = 640;
    56 int h_win = 480;
    56 int h_win = 480;
    57 GLint count = 0;
    57 GLint count  = 0;
    58 GLenum StrMode = GL_VENDOR;
    58 GLenum StrMode = GL_VENDOR;
    59 
    59 
    60 GLboolean moving;
    60 GLboolean moving;
    61 
    61 
    62 static double
    62 static double mtime(void)
    63 mtime(void)
    63 {
    64 {
    64    struct timeval tk_time;
    65     struct timeval tk_time;
    65    struct timezone tz;
    66     struct timezone tz;
    66    
    67 
    67    gettimeofday(&tk_time, &tz);
    68     gettimeofday(&tk_time, &tz);
    68    
    69 
    69    return 4294.967296 * tk_time.tv_sec + 0.000001 * tk_time.tv_usec;
    70     return 4294.967296 * tk_time.tv_sec + 0.000001 * tk_time.tv_usec;
    70 }
    71 }
    71 
    72 
    72 static double filter(double in, double *save)
    73 static double
    73 {
    74 filter(double in, double *save)
    74 	static double k1 = 0.9;
    75 {
    75 	static double k2 = 0.05;
    76     static double k1 = 0.9;
    76 
    77     static double k2 = 0.05;
    77 	save[3] = in;
    78 
    78 	save[1] = save[0]*k1 + k2*(save[3] + save[2]);
    79     save[3] = in;
    79 
    80     save[1] = save[0] * k1 + k2 * (save[3] + save[2]);
    80 	save[0]=save[1];
    81 
    81 	save[2]=save[3];
    82     save[0] = save[1];
    82 
    83     save[2] = save[3];
    83 	return(save[1]);
    84 
    84 }
    85     return (save[1]);
    85 
    86 }
    86 void DrawStr(const char *str)
    87 
    87 {
    88 void
    88 	GLint i = 0;
    89 DrawStr(const char *str)
    89 	
    90 {
    90 	if(!str) return;
    91     GLint i = 0;
    91         
    92 
    92 	while(str[i])
    93     if (!str)
    93 	{
    94         return;
    94 		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);
    95 
    95 		i++;
    96     while (str[i]) {
    96 	}
    97         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);
       
    98         i++;
       
    99     }
       
   100 }
    97 }
   101 
    98 
   102 void
    99 void
   103 InitFishs(void)
   100 InitFishs(void)
   104 {
   101 {
   135 }
   132 }
   136 
   133 
   137 void
   134 void
   138 Atlantis_Init(void)
   135 Atlantis_Init(void)
   139 {
   136 {
   140     static float ambient[] = { 0.2, 0.2, 0.2, 1.0 };
   137     static float ambient[] = {0.2, 0.2, 0.2, 1.0};
   141     static float diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
   138     static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
   142     static float position[] = { 0.0, 1.0, 0.0, 0.0 };
   139     static float position[] = {0.0, 1.0, 0.0, 0.0};
   143     static float mat_shininess[] = { 90.0 };
   140     static float mat_shininess[] = {90.0};
   144     static float mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
   141     static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
   145     static float mat_diffuse[] = { 0.46, 0.66, 0.795, 1.0 };
   142     static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
   146     static float mat_ambient[] = { 0.3, 0.4, 0.5, 1.0 };
   143     static float mat_ambient[] = {0.3, 0.4, 0.5, 1.0};
   147     static float lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
   144     static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
   148     static float lmodel_localviewer[] = { 0.0 };
   145     static float lmodel_localviewer[] = {0.0};
   149     //GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
   146     //GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
   150     //GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
   147     //GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
   151     static float fog_color[] = { 0.0, 0.5, 0.9, 1.0 };
   148     static float fog_color[] = {0.0, 0.5, 0.9, 1.0};
   152 
   149 
   153     glFrontFace(GL_CCW);
   150     glFrontFace(GL_CCW);
   154 
   151 
   155     glDepthFunc(GL_LESS);
   152     glDepthFunc(GL_LESS);
   156     glEnable(GL_DEPTH_TEST);
   153     glEnable(GL_DEPTH_TEST);
   169     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   166     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   170 
   167 
   171     InitFishs();
   168     InitFishs();
   172 
   169 
   173     glEnable(GL_FOG);
   170     glEnable(GL_FOG);
   174     glFogi(GL_FOG_MODE, GL_EXP);
   171 	glFogi(GL_FOG_MODE, GL_EXP);
   175     glFogf(GL_FOG_DENSITY, 0.0000025);
   172 	glFogf(GL_FOG_DENSITY, 0.0000025);
   176     glFogfv(GL_FOG_COLOR, fog_color);
   173 	glFogfv(GL_FOG_COLOR, fog_color);
   177 
   174 
   178     glClearColor(0.0, 0.5, 0.9, 1.0);
   175     glClearColor(0.0, 0.5, 0.9, 1.0);
   179 }
   176 }
   180 
   177 
   181 void
   178 void
   182 Atlantis_Reshape(int width, int height)
   179 Atlantis_Reshape(int width, int height)
   183 {
   180 {
   184     w_win = width;
   181 	w_win = width;
   185     h_win = height;
   182 	h_win = height;
   186 
   183 	
   187     glViewport(0, 0, width, height);
   184     glViewport(0, 0, width, height);
   188 
   185 
   189     glMatrixMode(GL_PROJECTION);
   186     glMatrixMode(GL_PROJECTION);
   190     glLoadIdentity();
   187     glLoadIdentity();
   191     gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 20000.0,
   188     gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 20000.0, 300000.0);
   192                    300000.0);
       
   193     glMatrixMode(GL_MODELVIEW);
   189     glMatrixMode(GL_MODELVIEW);
   194 }
   190 }
   195 
   191 
   196 void
   192 void
   197 Atlantis_Animate(void)
   193 Atlantis_Animate(void)
   214 void
   210 void
   215 Atlantis_Key(unsigned char key, int x, int y)
   211 Atlantis_Key(unsigned char key, int x, int y)
   216 {
   212 {
   217     switch (key) {
   213     switch (key) {
   218     case 't':
   214     case 't':
   219         Timing = !Timing;
   215     	Timing = !Timing;
   220         break;
   216     break;
   221     case ' ':
   217     case ' ':
   222         switch (StrMode) {
   218     	switch(StrMode)
   223         case GL_EXTENSIONS:
   219     	{
   224             StrMode = GL_VENDOR;
   220 		    case GL_EXTENSIONS:
   225             break;
   221     			StrMode = GL_VENDOR;
   226         case GL_VENDOR:
   222 		    break;
   227             StrMode = GL_RENDERER;
   223 		    case GL_VENDOR:
   228             break;
   224 		    	StrMode = GL_RENDERER;
   229         case GL_RENDERER:
   225 		    break;
   230             StrMode = GL_VERSION;
   226 		    case GL_RENDERER:
   231             break;
   227 		    	StrMode = GL_VERSION;
   232         case GL_VERSION:
   228 		    break;
   233             StrMode = GL_EXTENSIONS;
   229 		    case GL_VERSION:
   234             break;
   230 		    	StrMode = GL_EXTENSIONS;
   235         }
   231 		    break;
   236         break;
   232 		}
   237     case 27:                   /* Esc will quit */
   233 	break;
       
   234     case 27:           /* Esc will quit */
   238         exit(1);
   235         exit(1);
   239         break;
   236     break;
   240     case 's':                  /* "s" start animation */
   237     case 's':             		/* "s" start animation */
   241         moving = GL_TRUE;
   238         moving = GL_TRUE;
   242         //glutIdleFunc(Animate);
   239         //glutIdleFunc(Animate);
   243         break;
   240     break;
   244     case 'a':                  /* "a" stop animation */
   241     case 'a':          			/* "a" stop animation */
   245         moving = GL_FALSE;
   242         moving = GL_FALSE;
   246         //glutIdleFunc(NULL);
   243         //glutIdleFunc(NULL);
   247         break;
   244     break;
   248     case '.':                  /* "." will advance frame */
   245     case '.':          			/* "." will advance frame */
   249         if (!moving) {
   246         if (!moving) {
   250             Atlantis_Animate();
   247             Atlantis_Animate();
   251         }
   248         }
   252     }
   249     }
   253 }
   250 }
   254 
       
   255 /*
   251 /*
   256 void Display(void)
   252 void Display(void)
   257 {
   253 {
   258 	static float P123[3] = {-448.94, -203.14, 9499.60};
   254 	static float P123[3] = {-448.94, -203.14, 9499.60};
   259 	static float P124[3] = {-442.64, -185.20, 9528.07};
   255 	static float P124[3] = {-442.64, -185.20, 9528.07};
   275 
   271 
   276 void
   272 void
   277 Atlantis_Display(void)
   273 Atlantis_Display(void)
   278 {
   274 {
   279     int i;
   275     int i;
   280     static double th[4] = { 0.0, 0.0, 0.0, 0.0 };
   276     static double th[4] = {0.0, 0.0, 0.0, 0.0};
   281     static double t1 = 0.0, t2 = 0.0, t;
   277 	static double t1 = 0.0, t2 = 0.0, t;
   282     char num_str[128];
   278 	char num_str[128];
   283 
   279     
   284     t1 = t2;
   280     t1 = t2;
   285 
   281 
   286     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   282     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   287 
   283 
   288     for (i = 0; i < NUM_SHARKS; i++) {
   284     for (i = 0; i < NUM_SHARKS; i++) {
   305     glPushMatrix();
   301     glPushMatrix();
   306     FishTransform(&babyWhale);
   302     FishTransform(&babyWhale);
   307     glScalef(0.45, 0.45, 0.3);
   303     glScalef(0.45, 0.45, 0.3);
   308     DrawWhale(&babyWhale);
   304     DrawWhale(&babyWhale);
   309     glPopMatrix();
   305     glPopMatrix();
   310 
   306     
   311     if (Timing) {
   307     if(Timing)
   312         t2 = mtime();
   308     {
   313         t = t2 - t1;
   309 		t2 = mtime();
   314         if (t > 0.0001)
   310 		t = t2 - t1;
   315             t = 1.0 / t;
   311 		if(t > 0.0001) t = 1.0 / t;
   316 
   312 		
   317         glDisable(GL_LIGHTING);
   313 		glDisable(GL_LIGHTING);
   318         //glDisable(GL_DEPTH_TEST);
   314 		//glDisable(GL_DEPTH_TEST);
   319 
   315 		
   320         glColor3f(1.0, 0.0, 0.0);
   316 		glColor3f(1.0, 0.0, 0.0);
   321 
   317 		
   322         glMatrixMode(GL_PROJECTION);
   318 		glMatrixMode (GL_PROJECTION);
   323         glPushMatrix();
   319 		glPushMatrix();
   324         glLoadIdentity();
   320 		glLoadIdentity();
   325         glOrtho(0, w_win, 0, h_win, -10.0, 10.0);
   321 		glOrtho(0, w_win, 0, h_win, -10.0, 10.0);
   326 
   322 		
   327         glRasterPos2f(5.0, 5.0);
   323 		glRasterPos2f(5.0, 5.0);
   328 
   324 		
   329         switch (StrMode) {
   325 		switch(StrMode)
   330         case GL_VENDOR:
   326 		{
   331             sprintf(num_str, "%0.2f Hz, %dx%d, VENDOR: ", filter(t, th),
   327 			case GL_VENDOR:
   332                     w_win, h_win);
   328 				sprintf(num_str, "%0.2f Hz, %dx%d, VENDOR: ", filter(t, th), w_win, h_win);
   333             DrawStr(num_str);
   329 				DrawStr(num_str);
   334             DrawStr(glGetString(GL_VENDOR));
   330 				DrawStr(glGetString(GL_VENDOR));
   335             break;
   331 			break;
   336         case GL_RENDERER:
   332 			case GL_RENDERER:
   337             sprintf(num_str, "%0.2f Hz, %dx%d, RENDERER: ", filter(t, th),
   333 				sprintf(num_str, "%0.2f Hz, %dx%d, RENDERER: ", filter(t, th), w_win, h_win);
   338                     w_win, h_win);
   334 				DrawStr(num_str);
   339             DrawStr(num_str);
   335 				DrawStr(glGetString(GL_RENDERER));
   340             DrawStr(glGetString(GL_RENDERER));
   336 			break;
   341             break;
   337 			case GL_VERSION:
   342         case GL_VERSION:
   338 				sprintf(num_str, "%0.2f Hz, %dx%d, VERSION: ", filter(t, th), w_win, h_win);
   343             sprintf(num_str, "%0.2f Hz, %dx%d, VERSION: ", filter(t, th),
   339 				DrawStr(num_str);
   344                     w_win, h_win);
   340 				DrawStr(glGetString(GL_VERSION));
   345             DrawStr(num_str);
   341 			break;
   346             DrawStr(glGetString(GL_VERSION));
   342 			case GL_EXTENSIONS:
   347             break;
   343 				sprintf(num_str, "%0.2f Hz, %dx%d, EXTENSIONS: ", filter(t, th), w_win, h_win);
   348         case GL_EXTENSIONS:
   344 				DrawStr(num_str);
   349             sprintf(num_str, "%0.2f Hz, %dx%d, EXTENSIONS: ", filter(t, th),
   345 				DrawStr(glGetString(GL_EXTENSIONS));
   350                     w_win, h_win);
   346 			break;
   351             DrawStr(num_str);
   347 		}
   352             DrawStr(glGetString(GL_EXTENSIONS));
   348 		
   353             break;
   349 		glPopMatrix();
   354         }
   350 		glMatrixMode(GL_MODELVIEW);
   355 
   351 		
   356         glPopMatrix();
   352 		glEnable(GL_LIGHTING);
   357         glMatrixMode(GL_MODELVIEW);
   353 		//glEnable(GL_DEPTH_TEST);
   358 
   354 	}
   359         glEnable(GL_LIGHTING);
   355 	
   360         //glEnable(GL_DEPTH_TEST);
       
   361     }
       
   362 
       
   363     count++;
   356     count++;
   364 
   357 
   365     glutSwapBuffers();
   358     glutSwapBuffers();
   366 }
   359 }
   367 
   360