OSDN Git Service

better animate rate (Marcelo Magallon)
authorBrian Paul <brian.paul@tungstengraphics.com>
Sun, 9 Jan 2005 17:37:50 +0000 (17:37 +0000)
committerBrian Paul <brian.paul@tungstengraphics.com>
Sun, 9 Jan 2005 17:37:50 +0000 (17:37 +0000)
17 files changed:
progs/demos/bounce.c
progs/demos/geartrain.c
progs/demos/geartrain.dat
progs/demos/gloss.c
progs/demos/ipers.c
progs/demos/morph3d.c
progs/demos/pointblast.c
progs/demos/reflect.c
progs/demos/renormal.c
progs/demos/shadowtex.c
progs/demos/spectex.c
progs/demos/spriteblast.c
progs/demos/terrain.c
progs/demos/texcyl.c
progs/demos/texobj.c
progs/demos/tunnel.c
progs/demos/tunnel2.c

index c0f739e..2f3fa9f 100644 (file)
@@ -14,6 +14,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <GL/glut.h>
+#include <sys/time.h>
+#include <time.h>
 
 #define COS(X)   cos( (X) * 3.14159/180.0 )
 #define SIN(X)   sin( (X) * 3.14159/180.0 )
 GLboolean IndexMode = GL_FALSE;
 GLuint Ball;
 GLenum Mode;
-GLfloat Zrot = 0.0, Zstep = 6.0;
+GLfloat Zrot = 0.0, Zstep = 180.0;
 GLfloat Xpos = 0.0, Ypos = 1.0;
-GLfloat Xvel = 0.2, Yvel = 0.0;
+GLfloat Xvel = 2.0, Yvel = 0.0;
 GLfloat Xmin = -4.0, Xmax = 4.0;
 GLfloat Ymin = -3.8, Ymax = 4.0;
-GLfloat G = -0.1;
+GLfloat G = -9.8;
 
 static GLuint 
 make_ball(void)
@@ -149,10 +151,17 @@ static void
 idle(void)
 {
   static float vel0 = -100.0;
+  static double t0 = -1.;
+  double t, dt;
+  t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
+  if (t0 < 0.)
+     t0 = t;
+  dt = t - t0;
+  t0 = t;
 
-  Zrot += Zstep;
+  Zrot += Zstep*dt;
 
-  Xpos += Xvel;
+  Xpos += Xvel*dt;
   if (Xpos >= Xmax) {
     Xpos = Xmax;
     Xvel = -Xvel;
@@ -163,8 +172,8 @@ idle(void)
     Xvel = -Xvel;
     Zstep = -Zstep;
   }
-  Ypos += Yvel;
-  Yvel += G;
+  Ypos += Yvel*dt;
+  Yvel += G*dt;
   if (Ypos < Ymin) {
     Ypos = Ymin;
     if (vel0 == -100.0)
index ec31e74..3feb2a0 100644 (file)
@@ -912,14 +912,18 @@ draw (void)
 }
 
 
-
-
 static void
 idle (void)
 {
     int i;
+    static double t0 = -1.;
+    double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+    if (t0 < 0.0)
+       t0 = t;
+    dt = t - t0;
+    t0 = t;
     for (i = 0; i < number_of_gears; i++)
-      g[i].angle += g[i].angular_velocity;
+      g[i].angle += g[i].angular_velocity * dt;
     glutPostRedisplay();
 }
 
index 6b46745..374de48 100644 (file)
@@ -7,7 +7,7 @@ APOSITION = -7.000 0.000 0.000
 ACOLOR = 0.800 0.500 0.200\r
 ALENGTH = 6.000\r
 AMOTORED = 1\r
-AANGULARVELOCITY = 2.000\r
+AANGULARVELOCITY = 90.000\r
 ADIRECTION = 1\r
 \r
 ANAME = AXLE2\r
index e7d6875..4e7adc8 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include <string.h>
 #include <GL/glut.h>
 
-#include "readtex.c"   /* I know, this is a hack. */
+#include "readtex.h"
 
 #define SPECULAR_TEXTURE_FILE "../images/reflect.rgb"
 #define BASE_TEXTURE_FILE "../images/tile.rgb"
@@ -43,7 +44,7 @@ static GLuint Object = 0;
 static GLboolean Animate = GL_TRUE;
 
 static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
-static GLfloat DXrot = 1.0, DYrot = 2.5;
+static GLfloat DXrot = 20.0, DYrot = 50.;
 
 static GLfloat Black[4] = { 0, 0, 0, 0 };
 static GLfloat White[4] = { 1, 1, 1, 1 };
@@ -58,13 +59,18 @@ static GLint T0 = 0;
 static GLint Frames = 0;
 
 
-
-
 static void Idle( void )
 {
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+
    if (Animate) {
-      Xrot += DXrot;
-      Yrot += DYrot;
+      Xrot += DXrot*dt;
+      Yrot += DYrot*dt;
       glutPostRedisplay();
    }
 }
index d43c662..6e153c0 100644 (file)
@@ -9,7 +9,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <time.h>
+#include <string.h>
 
 #if defined (WIN32)|| defined(_WIN32)
 #include <windows.h>
@@ -18,7 +18,7 @@
 
 #include <GL/glut.h>
 
-#include "readtex.c"
+#include "readtex.h"
 
 #ifdef XMESA
 #include "GL/xmesa.h"
@@ -543,6 +543,12 @@ draw(void)
    static GLfloat alpha = 0.0f;
    static GLfloat beta = 0.0f;
    static float fr = 0.0;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
 
    dojoy();
 
@@ -586,8 +592,8 @@ draw(void)
    drawipers(0, FROM_NONE);
    glPopMatrix();
 
-   alpha += 0.5f;
-   beta += 0.3f;
+   alpha += 4.f * dt;
+   beta += 2.4f * dt;
 
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
index 2ce04a3..047d571 100644 (file)
@@ -660,6 +660,13 @@ static void draw_ico( void )
 }
 
 static void draw ( void ) {
+  static double t0 = -1.;
+  double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+  if (t0 < 0.0)
+     t0 = t;
+  dt = t - t0;
+  t0 = t;
+
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
   glPushMatrix();
@@ -681,7 +688,7 @@ static void draw ( void ) {
 
   glutSwapBuffers();
 
-  step+=0.05;
+  step += dt;
 }
 
 static void idle_( void )
index 14e9cf3..715813f 100644 (file)
@@ -79,7 +79,6 @@ static float float_rand(void) { return rand() / (float) RAND_MAX; }
 
 #define MEAN_VELOCITY 3.0
 #define GRAVITY 2.0
-#define TIME_DELTA 0.025  /* The speed of time. */
 
 /* Modeling units of ground extent in each X and Z direction. */
 #define EDGE 12
@@ -114,6 +113,13 @@ updatePointList(void)
   float distance;
   int i;
 
+  static double t0 = -1.;
+  double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+  if (t0 < 0.0)
+    t0 = t;
+  dt = t - t0;
+  t0 = t;
+
   motion = 0;
   for (i=0; i<numPoints; i++) {
     distance = pointVelocity[i][0] * theTime;
@@ -139,9 +145,9 @@ updatePointList(void)
       pointTime[i] = 0.0;  /* Reset the particles sense of up time. */
     }
     motion = 1;
-    pointTime[i] += TIME_DELTA;
+    pointTime[i] += dt;
   }
-  theTime += TIME_DELTA;
+  theTime += dt;
   if (!motion && !spin) {
     if (repeat) {
       makePointList();
@@ -264,7 +270,7 @@ menu(int option)
   case 0:
     makePointList();
     break;
-#if GL_ARB_point_parameters
+#ifdef GL_ARB_point_parameters
   case 1:
     glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, constant);
     break;
@@ -281,7 +287,7 @@ menu(int option)
   case 5:
     blend = 0;
     break;
-#if GL_ARB_point_parameters
+#ifdef GL_ARB_point_parameters
   case 6:
     glPointParameterfARB(GL_POINT_FADE_THRESHOLD_SIZE_ARB, 1.0);
     break;
index 91b2ad9..d259d7b 100644 (file)
@@ -31,8 +31,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "GL/glut.h"
-#include "../util/showbuffer.c"
-#include "../util/readtex.c"
+#include "showbuffer.h"
+#include "readtex.h"
 
 
 #define DEG2RAD (3.14159/180.0)
@@ -318,8 +318,14 @@ static void draw_scene( void )
 
 static void idle( void )
 {
-   spin += 2.0;
-   yrot += 3.0;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+   spin += 60.0 * dt;
+   yrot += 90.0 * dt;
    glutPostRedisplay();
 }
 
index 4fc1957..9e5da95 100644 (file)
 static GLfloat Phi = 0.0;
 
 
-static void Idle(void)
+static void Idle( void )
 {
-   Phi += 0.1;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+   Phi += 3.0 * dt;
    glutPostRedisplay();
 }
 
index 18aad47..e918751 100644 (file)
@@ -33,7 +33,7 @@
 #include <stdlib.h>
 #include <math.h>
 #include <GL/glut.h>
-#include "../util/showbuffer.c"
+#include "showbuffer.h"
 
 #if 0 /* change to 1 if you want to use the old SGIX extensions */
 #undef GL_ARB_depth_texture
@@ -386,7 +386,13 @@ Reshape(int width, int height)
 static void
 Idle(void)
 {
-   Yrot += 5.0;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+   Yrot += 75.0 * dt;
    /*LightLongitude -= 5.0;*/
    glutPostRedisplay();
 }
index 9364245..cfa7f74 100644 (file)
@@ -25,8 +25,8 @@
 static GLUquadricObj *Quadric;
 static GLuint Sphere;
 static GLfloat LightPos[4] = {10.0, 10.0, 10.0, 1.0};
-static GLfloat Delta = 1.0;
-static GLint Mode = 0;
+static GLfloat Delta = 20.0;
+static GLint Mode = 4;
 
 /*static GLfloat Blue[4] = {0.0, 0.0, 1.0, 1.0};*/
 /*static GLfloat Gray[4] = {0.5, 0.5, 0.5, 1.0};*/
@@ -34,14 +34,18 @@ static GLfloat Black[4] = {0.0, 0.0, 0.0, 1.0};
 static GLfloat White[4] = {1.0, 1.0, 1.0, 1.0};
 
 
-
-static void Idle( void )
+static void
+Idle(void)
 {
-   LightPos[0] += Delta;
-   if (LightPos[0]>15.0)
-      Delta = -1.0;
-   else if (LightPos[0]<-15.0)
-      Delta = 1.0;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+   LightPos[0] += Delta * dt;
+   if (LightPos[0]>15.0 || LightPos[0]<-15.0)
+      Delta = -Delta;
 
    glutPostRedisplay();
 }
index 63dc7d1..d6e9a4d 100644 (file)
@@ -104,7 +104,6 @@ static float float_rand(void) { return rand() / (float) RAND_MAX; }
 
 #define MEAN_VELOCITY 3.0
 #define GRAVITY 2.0
-#define TIME_DELTA 0.025  /* The speed of time. */
 
 /* Modeling units of ground extent in each X and Z direction. */
 #define EDGE 12
@@ -139,6 +138,13 @@ updatePointList(void)
   float distance;
   int i;
 
+  static double t0 = -1.;
+  double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+  if (t0 < 0.0)
+    t0 = t;
+  dt = t - t0;
+  t0 = t;
+
   motion = 0;
   for (i=0; i<numPoints; i++) {
     distance = pointVelocity[i][0] * theTime;
@@ -164,9 +170,9 @@ updatePointList(void)
       pointTime[i] = 0.0;  /* Reset the particles sense of up time. */
     }
     motion = 1;
-    pointTime[i] += TIME_DELTA;
+    pointTime[i] += dt;
   }
-  theTime += TIME_DELTA;
+  theTime += dt;
   if (!motion && !spin) {
     if (repeat) {
       makePointList();
index 9063c93..990c7b8 100644 (file)
@@ -69,7 +69,7 @@ static int scrheight = HEIGHT;
 
 static float obs[3] = { OBSSTARTX, heightMnt * 1.3, OBSSTARTY };
 static float dir[3], v1[2], v2[2];
-static float v = 15.0;
+static float v = 900.0;
 static float alpha = 75.0;
 static float beta = 90.0;
 
@@ -77,6 +77,12 @@ static void
 calcposobs(void)
 {
    float alpha1, alpha2;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
 
    dir[0] = sin(alpha * M_PI / 180.0);
    dir[2] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
@@ -97,9 +103,9 @@ calcposobs(void)
    v2[0] = sin(alpha2 * M_PI / 180.0);
    v2[1] = cos(alpha2 * M_PI / 180.0);
 
-   obs[0] += v * dir[0];
-   obs[1] += v * dir[1];
-   obs[2] += v * dir[2];
+   obs[0] += v * dir[0] * dt;
+   obs[1] += v * dir[1] * dt;
+   obs[2] += v * dir[2] * dt;
 
    if (obs[1] < 0.0)
       obs[1] = 0.0;
@@ -432,10 +438,10 @@ key(unsigned char k, int x, int y)
       exit(0);
       break;
    case 'a':
-      v += 0.5;
+      v += 50.;
       break;
    case 'z':
-      v -= 0.5;
+      v -= 50.;
       break;
    case 'p':
       if (poutline) {
index 52f7da0..c04d500 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include <string.h>
 #include <GL/glut.h>
 
-#include "readtex.c"   /* I know, this is a hack. */
+#include "readtex.h"
 
 #define TEXTURE_FILE "../images/reflect.rgb"
 
@@ -30,7 +31,7 @@ static GLuint CylinderObj = 0;
 static GLboolean Animate = GL_TRUE;
 
 static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
-static GLfloat DXrot = 1.0, DYrot = 2.5;
+static GLfloat DXrot = 50.0, DYrot = 125.0;
 
 /* performance info */
 static GLint T0 = 0;
@@ -39,9 +40,16 @@ static GLint Frames = 0;
 
 static void Idle( void )
 {
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+
    if (Animate) {
-      Xrot += DXrot;
-      Yrot += DYrot;
+      Xrot += DXrot * dt;
+      Yrot += DYrot * dt;
       glutPostRedisplay();
    }
 }
index f30ede4..40bce6e 100644 (file)
@@ -85,7 +85,13 @@ static void draw( void )
 
 static void idle( void )
 {
-   Angle += 2.0;
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+   Angle += 120.0*dt;
    glutPostRedisplay();
 }
 
index f0faa61..a47b916 100644 (file)
@@ -9,14 +9,14 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <time.h>
+#include <string.h>
 
 #ifdef WIN32
 #include <windows.h>
 #endif
 
 #include <GL/glut.h>
-#include "readtex.c"
+#include "readtex.h"
 #include "tunneldat.h"
 
 #ifdef XMESA
@@ -55,7 +55,7 @@ static int win = 0;
 
 static float obs[3] = { 1000.0, 0.0, 2.0 };
 static float dir[3];
-static float v = 0.5;
+static float v = 30.;
 static float alpha = 90.0;
 static float beta = 90.0;
 
@@ -84,7 +84,7 @@ inittextures(void)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
-                  GL_LINEAR_MIPMAP_LINEAR);
+                  GL_LINEAR_MIPMAP_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
    glGenTextures(1, &t2id);
@@ -160,6 +160,13 @@ drawobjs(const int *l, const float *f)
 static void
 calcposobs(void)
 {
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+
    dir[0] = sin(alpha * M_PI / 180.0);
    dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
    dir[2] = cos(beta * M_PI / 180.0);
@@ -171,9 +178,9 @@ calcposobs(void)
    if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5)
       dir[2] = 0;
 
-   obs[0] += v * dir[0];
-   obs[1] += v * dir[1];
-   obs[2] += v * dir[2];
+   obs[0] += v * dir[0] * dt;
+   obs[1] += v * dir[1] * dt;
+   obs[2] += v * dir[2] * dt;
 }
 
 static void
@@ -204,10 +211,10 @@ key(unsigned char k, int x, int y)
       break;
 
    case 'a':
-      v += 0.01;
+      v += 5.;
       break;
    case 'z':
-      v -= 0.01;
+      v -= 5.;
       break;
 
 #ifdef XMESA
index 436f01b..14248c0 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
-#include <time.h>
+#include <string.h>
 
 #ifdef WIN32
 #include <windows.h>
 #endif
 
 #include <GL/glut.h>
-#include "readtex.c"
+#include "readtex.h"
 #include "tunneldat.h"
 
 #ifdef FX
@@ -51,11 +51,11 @@ static GLint Frames = 0;
 
 static float obs[3] = { 1000.0, 0.0, 2.0 };
 static float dir[3];
-static float v = 0.5;
+static float v = 30.;
 static float alpha = 90.0;
 static float beta = 90.0;
 
-static int fog = 0;
+static int fog = 1;
 static int bfcull = 1;
 static int usetex = 1;
 static int cstrip = 0;
@@ -97,7 +97,7 @@ inittextures(void)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
-                  GL_LINEAR_MIPMAP_NEAREST);
+                  GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
@@ -158,6 +158,13 @@ drawobjs(const int *l, const float *f)
 static void
 calcposobs(void)
 {
+   static double t0 = -1.;
+   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+   if (t0 < 0.0)
+      t0 = t;
+   dt = t - t0;
+   t0 = t;
+
    dir[0] = sin(alpha * M_PI / 180.0);
    dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
    dir[2] = cos(beta * M_PI / 180.0);
@@ -169,9 +176,9 @@ calcposobs(void)
    if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5)
       dir[2] = 0;
 
-   obs[0] += v * dir[0];
-   obs[1] += v * dir[1];
-   obs[2] += v * dir[2];
+   obs[0] += v * dir[0] * dt;
+   obs[1] += v * dir[1] * dt;
+   obs[2] += v * dir[2] * dt;
 }
 
 static void
@@ -202,10 +209,10 @@ key(unsigned char k, int x, int y)
       break;
 
    case 'a':
-      v += 0.01;
+      v += 5.;
       break;
    case 'z':
-      v -= 0.01;
+      v -= 5.;
       break;
 
 #ifdef XMESA