vbo: if 'end' is out of bounds, clamp it
[mesa.git] / progs / demos / spriteblast.c
index 4e1d86cdd08625c497ffb4d4ec51cbf4fce03ddd..d73b680b791dfd568a0ef5718a46e17eead88e68 100644 (file)
@@ -21,7 +21,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #endif
-#define GL_GLEXT_PROTOTYPES
+#include <GL/glew.h>
 #include <GL/glut.h>
 
 /* Some <math.h> files do not define M_PI... */
@@ -36,7 +36,6 @@
 static GLfloat angle = -150;   /* in degrees */
 static int spin = 0;
 static int moving, begin;
-static int newModel = 1;
 static float theTime;
 static int repeat = 1;
 static int blend = 1;
@@ -105,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
@@ -140,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;
@@ -165,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();
@@ -183,7 +188,6 @@ idle(void)
   updatePointList();
   if (spin) {
     angle += 0.3;
-    newModel = 1;
   }
   glutPostRedisplay();
 }
@@ -200,25 +204,18 @@ visible(int vis)
   }
 }
 
-static void
-recalcModelView(void)
-{
-  glPopMatrix();
-  glPushMatrix();
-  glRotatef(angle, 0.0, 1.0, 0.0);
-  newModel = 0;
-}
-
 static void
 redraw(void)
 {
   int i;
 
+  glDepthMask(GL_TRUE);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-  if (newModel)
-    recalcModelView();
 
-  glDepthMask(GL_FALSE);
+  glPushMatrix();
+  glRotatef(15.0, 1.0, 0.0, 0.0);
+  glRotatef(angle, 0.0, 1.0, 0.0);
+
 
   /* Draw the floor. */
 /*  glEnable(GL_TEXTURE_2D);*/
@@ -235,7 +232,7 @@ redraw(void)
   glEnd();
 
   /* Allow particles to blend with each other. */
-  glDepthMask(GL_TRUE);
+  glDepthMask(GL_FALSE);
 
   if (blend)
      glEnable(GL_BLEND);
@@ -264,6 +261,8 @@ redraw(void)
 #endif
   glDisable(GL_BLEND);
 
+  glPopMatrix();
+
   glutSwapBuffers();
 }
 
@@ -289,7 +288,6 @@ mouseMotion(int x, int y)
   if (moving) {
     angle = angle + (x - begin);
     begin = x;
-    newModel = 1;
     glutPostRedisplay();
   }
 }
@@ -301,7 +299,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;
@@ -318,7 +316,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;
@@ -335,13 +333,13 @@ menu(int option)
     smooth = 0;
     break;
   case 10:
-    glPointSize(4.0);
+    glPointSize(16.0);
     break;
   case 11:
-    glPointSize(8.0);
+    glPointSize(32.0);
     break;
   case 12:
-    glPointSize(16.0);
+    glPointSize(64.0);
     break;
   case 13:
     spin = 1 - spin;
@@ -390,8 +388,8 @@ key(unsigned char c, int x, int y)
   case 'o':
   case 'O':
     org ^= 1;
-#if GL_VERSION_2_0
-#if GL_ARB_point_parameters
+#ifdef GL_VERSION_2_0
+#ifdef GL_ARB_point_parameters
     glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN,
                       org ? GL_LOWER_LEFT : GL_UPPER_LEFT);
 #endif
@@ -413,19 +411,19 @@ key(unsigned char c, int x, int y)
     glutPostRedisplay();
     break;
   case '1':
-    glPointSize(2.0);
+    glPointSize(16.0);
     glutPostRedisplay();
     break;
   case '2':
-    glPointSize(4.0);
+    glPointSize(32.0);
     glutPostRedisplay();
     break;
   case '3':
-    glPointSize(8.0);
+    glPointSize(64.0);
     glutPostRedisplay();
     break;
   case '4':
-    glPointSize(16.0);
+    glPointSize(128.0);
     glutPostRedisplay();
     break;
   case 27:
@@ -485,10 +483,10 @@ reshape(int width, int height)
   glViewport(0, 0, (GLint) width, (GLint) height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-  glFrustum(-1.0, 1.0, -h, h, 2.0, 20.0);
+  glFrustum(-1.0, 1.0, -h, h, 2.0, 30.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
-  glTranslatef(0.0, 0.0, -60.0);
+  glTranslatef(0.0, 0.0, -10.0);
 }
 
 int
@@ -511,6 +509,7 @@ main(int argc, char **argv)
   glutInitWindowPosition(0, 0);
   glutInitWindowSize(600,300);
   glutCreateWindow("sprite blast");
+  glewInit();
   glutReshapeFunc(reshape);
   glutDisplayFunc(redraw);
   glutMouseFunc(mouse);
@@ -528,9 +527,9 @@ main(int argc, char **argv)
   glutAddMenuEntry("Threshold 10", 7);
   glutAddMenuEntry("Point smooth on", 8);
   glutAddMenuEntry("Point smooth off", 9);
-  glutAddMenuEntry("Point size 4", 10);
-  glutAddMenuEntry("Point size 8", 11);
-  glutAddMenuEntry("Point size 16", 12);
+  glutAddMenuEntry("Point size 16", 10);
+  glutAddMenuEntry("Point size 32", 11);
+  glutAddMenuEntry("Point size 64", 12);
   glutAddMenuEntry("Toggle spin", 13);
   glutAddMenuEntry("200 points ", 14);
   glutAddMenuEntry("500 points ", 15);
@@ -539,27 +538,17 @@ main(int argc, char **argv)
   glutAddMenuEntry("Quit", 666);
   glutAttachMenu(GLUT_RIGHT_BUTTON);
 
+  makePointList();
+  makeSprite();
+
   glShadeModel(GL_FLAT);
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_POINT_SMOOTH);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-  glPointSize(16.0);
-#if GL_ARB_point_parameters
+  glPointSize(32.0);
+#ifdef GL_ARB_point_parameters
   glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, theQuad);
 #endif
-  glMatrixMode(GL_PROJECTION);
-  gluPerspective( /* field of view in degree */ 40.0,
-  /* aspect ratio */ 1.0,
-    /* Z near */ 0.5, /* Z far */ 40.0);
-  glMatrixMode(GL_MODELVIEW);
-  gluLookAt(0.0, 1.0, 8.0, /* eye location */
-    0.0, 1.0, 0.0,      /* center is at (0,0,0) */
-    0.0, 1.0, 0.);      /* up is in postivie Y direction */
-  glPushMatrix();       /* dummy push so we can pop on model
-                           recalc */
-
-  makePointList();
-  makeSprite();
 
   glutMainLoop();
   return 0;             /* ANSI C requires main to return int. */