Merge branch 'mesa_7_6_branch'
[mesa.git] / progs / demos / gloss.c
index 4e7adc8d48fa94cc63dd4292d5e58fb6d95889e1..69694b23a09bc5cb44782a435da30761a39214fa 100644 (file)
 #include <stdlib.h>
 #include <math.h>
 #include <string.h>
+#include <GL/glew.h>
 #include <GL/glut.h>
 
 #include "readtex.h"
+#include "trackball.h"
+
 
 #define SPECULAR_TEXTURE_FILE "../images/reflect.rgb"
 #define BASE_TEXTURE_FILE "../images/tile.rgb"
 /* for convolution */
 #define FILTER_SIZE 7
 
+static GLint WinWidth = 500, WinHeight = 500;
 static GLuint CylinderObj = 0;
 static GLuint TeapotObj = 0;
 static GLuint Object = 0;
 static GLboolean Animate = GL_TRUE;
 
-static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
-static GLfloat DXrot = 20.0, DYrot = 50.;
+static float CurQuat[4] = { 0, 0, 0, 1 };
 
 static GLfloat Black[4] = { 0, 0, 0, 0 };
 static GLfloat White[4] = { 1, 1, 1, 1 };
@@ -54,6 +57,10 @@ static GLfloat Shininess = 6;
 static GLuint BaseTexture, SpecularTexture;
 static GLboolean DoSpecTexture = GL_TRUE;
 
+static GLboolean ButtonDown = GL_FALSE;
+static GLint ButtonX, ButtonY;
+
+
 /* performance info */
 static GLint T0 = 0;
 static GLint Frames = 0;
@@ -61,29 +68,31 @@ static GLint Frames = 0;
 
 static void Idle( void )
 {
+   static const float yAxis[3] = {0, 1, 0};
    static double t0 = -1.;
+   float quat[4];
    double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    if (t0 < 0.0)
       t0 = t;
    dt = t - t0;
    t0 = t;
 
-   if (Animate) {
-      Xrot += DXrot*dt;
-      Yrot += DYrot*dt;
-      glutPostRedisplay();
-   }
+   axis_to_quat(yAxis, 2.0 * dt, quat);
+   add_quats(quat, CurQuat, CurQuat);
+
+   glutPostRedisplay();
 }
 
 
 static void Display( void )
 {
+   GLfloat rot[4][4];
+
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
    glPushMatrix();
-   glRotatef(Xrot, 1.0, 0.0, 0.0);
-   glRotatef(Yrot, 0.0, 1.0, 0.0);
-   glRotatef(Zrot, 0.0, 0.0, 1.0);
+   build_rotmatrix(rot, CurQuat);
+   glMultMatrixf(&rot[0][0]);
 
    /* First pass: diffuse lighting with base texture */
    glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
@@ -123,6 +132,7 @@ static void Display( void )
          GLfloat seconds = (t - T0) / 1000.0;
          GLfloat fps = Frames / seconds;
          printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
+         fflush(stdout);
          T0 = t;
          Frames = 0;
       }
@@ -134,6 +144,8 @@ static void Reshape( int width, int height )
 {
    GLfloat h = 30.0;
    GLfloat w = h * width / height;
+   WinWidth = width;
+   WinHeight = height;
    glViewport( 0, 0, width, height );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
@@ -198,6 +210,7 @@ static void Key( unsigned char key, int x, int y )
          glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
          printf("Shininess = %g\n", Shininess);
          break;
+      case 'a':
       case ' ':
          ToggleAnimate();
          break;
@@ -209,33 +222,44 @@ static void Key( unsigned char key, int x, int y )
 }
 
 
-static void SpecialKey( int key, int x, int y )
+static void
+MouseMotion(int x, int y)
 {
-   float step = 3.0;
-   (void) x;
-   (void) y;
+   if (ButtonDown) {
+      float x0 = (2.0 * ButtonX - WinWidth) / WinWidth;
+      float y0 = (WinHeight - 2.0 * ButtonY) / WinHeight;
+      float x1 = (2.0 * x - WinWidth) / WinWidth;
+      float y1 = (WinHeight - 2.0 * y) / WinHeight;
+      float q[4];
+
+      trackball(q, x0, y0, x1, y1);
+      ButtonX = x;
+      ButtonY = y;
+      add_quats(q, CurQuat, CurQuat);
 
-   switch (key) {
-      case GLUT_KEY_UP:
-         Xrot += step;
-         break;
-      case GLUT_KEY_DOWN:
-         Xrot -= step;
-         break;
-      case GLUT_KEY_LEFT:
-         Yrot += step;
-         break;
-      case GLUT_KEY_RIGHT:
-         Yrot -= step;
-         break;
+      glutPostRedisplay();
    }
-   glutPostRedisplay();
+}
+
+
+static void
+MouseButton(int button, int state, int x, int y)
+{
+  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
+     ButtonDown = GL_TRUE;
+     ButtonX = x;
+     ButtonY = y;
+  }
+  else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
+     ButtonDown = GL_FALSE;
+  }
 }
 
 
 static void Init( int argc, char *argv[] )
 {
    GLboolean convolve = GL_FALSE;
+   GLboolean fullscreen = GL_FALSE;
    int i;
 
    for (i = 1; i < argc; i++) {
@@ -248,8 +272,13 @@ static void Init( int argc, char *argv[] )
       else if (strcmp(argv[i], "-c")==0) {
          convolve = GL_TRUE;
       }
+      else if (strcmp(argv[i], "-f")==0) {
+         fullscreen = GL_TRUE;
+      }
    }
 
+   if (fullscreen)
+      glutFullScreen();
 
    /* Cylinder object */
    {
@@ -408,20 +437,17 @@ static void Init( int argc, char *argv[] )
 int main( int argc, char *argv[] )
 {
    glutInit( &argc, argv );
-   glutInitWindowPosition(0, 0);
-   glutInitWindowSize( 500, 500 );
-
+   glutInitWindowSize(WinWidth, WinHeight);
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
-
    glutCreateWindow(argv[0] );
-
-   Init(argc, argv);
-
+   glewInit();
    glutReshapeFunc( Reshape );
    glutKeyboardFunc( Key );
-   glutSpecialFunc( SpecialKey );
    glutDisplayFunc( Display );
-   glutIdleFunc( Idle );
+   glutMotionFunc(MouseMotion);
+   glutMouseFunc(MouseButton);
+   if (Animate)
+      glutIdleFunc( Idle );
 
    glutCreateMenu(ModeMenu);
    glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE);
@@ -430,6 +456,8 @@ int main( int argc, char *argv[] )
    glutAddMenuEntry("Quit", QUIT);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 
+   Init(argc, argv);
+
    glutMainLoop();
    return 0;
 }