瀏覽代碼

Added some header ifdefs,
translator for Cocoa key codes to anttweakbar's
small trackball GLUT example


Former-commit-id: b9fa47bc2fd2dacbfac0294fc59806790644d90d

jalec 13 年之前
父節點
當前提交
12b2bc14aa
共有 9 個文件被更改,包括 430 次插入10 次删除
  1. 2 0
      EPS.h
  2. 34 0
      axis_angle_to_quat.h
  3. 82 0
      cocoa_key_to_anttweakbar_key.h
  4. 1 2
      examples/ReAntTweakBar/example.cpp
  5. 21 0
      examples/trackball/Makefile
  6. 18 0
      examples/trackball/README
  7. 255 0
      examples/trackball/example.cpp
  8. 5 0
      quat_mult.h
  9. 12 8
      trackball.h

+ 2 - 0
EPS.h

@@ -5,5 +5,7 @@ namespace igl
 {
   const double DOUBLE_EPS    = 1.0e-14;
   const double DOUBLE_EPS_SQ = 1.0e-28;
+  const float FLOAT_EPS    = 1.0e-7;
+  const float FLOAT_EPS_SQ = 1.0e-14;
 }
 #endif

+ 34 - 0
axis_angle_to_quat.h

@@ -1,4 +1,8 @@
+#ifndef IGL_AXIS_ANGLE_TO_QUAT_H
+#define IGL_AXIS_ANGLE_TO_QUAT_H
+
 #include <EPS.h>
+#include <cmath>
 namespace igl
 {
   // Convert axis angle representation of a rotation to a quaternion
@@ -13,6 +17,11 @@ namespace igl
     const double *axis, 
     const double angle,
     double *out);
+  // Same but with floats
+  inline void axis_angle_to_quat(
+    const float *axis, 
+    const float angle,
+    float *out);
 }
 
 // Implementation
@@ -39,3 +48,28 @@ inline void igl::axis_angle_to_quat(
         out[0] = out[1] = out[2] = 0.0;
     }
 }
+
+// http://www.antisphere.com/Wiki/tools:anttweakbar
+inline void igl::axis_angle_to_quat(
+  const float *axis, 
+  const float angle,
+  float *out)
+{
+    float n = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
+    if( fabs(n)>igl::FLOAT_EPS )
+    {
+        float f = 0.5*angle;
+        out[3] = cos(f);
+        f = sin(f)/sqrt(n);
+        out[0] = axis[0]*f;
+        out[1] = axis[1]*f;
+        out[2] = axis[2]*f;
+    }
+    else
+    {
+        out[3] = 1.0;
+        out[0] = out[1] = out[2] = 0.0;
+    }
+}
+
+#endif

+ 82 - 0
cocoa_key_to_anttweakbar_key.h

@@ -0,0 +1,82 @@
+#include <AntTweakBar.h>
+
+namespace igl
+{
+  // Convert an unsigned char (like that from Cocoa apps) to AntTweakBar key
+  // code.
+  // See also: TranslateKey() in TwMgr.cpp in AntTweakBar source
+  // Inputs:
+  //   key  unsigned char key from keyboard
+  // Returns int of new key code 
+  int cocoa_key_to_anttweakbar_key(int key);
+}
+
+// Implementation
+int igl::cocoa_key_to_anttweakbar_key(int key)
+{
+  // I've left commented the AntTweakBar key codes that correspond to keys I
+  // don't have on my keyboard. Please fill this in if you have those keys
+  switch(key)
+  {
+    case 127:
+      return TW_KEY_BACKSPACE;
+    case 9:
+      return TW_KEY_TAB;
+  //TW_KEY_CLEAR        = 0x0c,
+    case 3://ENTER
+    case 13:
+      return TW_KEY_RETURN;
+    case 27:
+      return TW_KEY_ESCAPE;
+    case 32:
+      return TW_KEY_SPACE;
+    case 63272:
+      return TW_KEY_DELETE;
+    case 63232:
+      return TW_KEY_UP;
+    case 63233:
+      return TW_KEY_DOWN;
+    case 63235:
+      return TW_KEY_RIGHT;
+    case 63234:
+      return TW_KEY_LEFT;
+  //TW_KEY_INSERT,
+  //TW_KEY_HOME,
+  //TW_KEY_END,
+  //TW_KEY_PAGE_UP,
+  //TW_KEY_PAGE_DOWN,
+    case 63236:
+      return TW_KEY_F1;
+    case 63237:
+      return TW_KEY_F2;
+    case 63238:
+      return TW_KEY_F3;
+    case 63239:
+      return TW_KEY_F4;
+    case 63240:
+      return TW_KEY_F5;
+    case 63241:
+      return TW_KEY_F6;
+    case 63242:
+      return TW_KEY_F7;
+    case 63243:
+      return TW_KEY_F8;
+    case 63244:
+      return TW_KEY_F9;
+    case 63245:
+      return TW_KEY_F10;
+    case 63246:
+      return TW_KEY_F11;
+    case 63247:
+      return TW_KEY_F12;
+    case 63248:
+      return TW_KEY_F13;
+    case 63249:
+      return TW_KEY_F14;
+    case 63250:
+      return TW_KEY_F15;
+    otherwise:
+      break;
+  }
+  return key;
+}

+ 1 - 2
examples/ReAntTweakBar/example.cpp

@@ -3,9 +3,8 @@
 
   On mac os x compile with:
 
-  g++ -c ReAntTweakBar.cpp -o ReAntTweakBar.o 
   g++ -c example.cpp -o example.o 
-  g++ -o example ReAntTweakBar.o example.o -framework OpenGL -framework GLUT -lAntTweakBar
+  g++ -o example example.o -framework OpenGL -framework GLUT -lAntTweakBar
   rm *.o
 
 */

+ 21 - 0
examples/trackball/Makefile

@@ -0,0 +1,21 @@
+
+.PHONY: all
+
+all: example
+
+.PHONY: example
+
+igl_lib=../../
+
+CFLAGS=-g
+inc=-I$(igl_lib)
+
+example: example.o
+	g++ $(CFLAGS) -o example example.o -framework OpenGL -framework GLUT $(lib)
+	rm example.o
+
+example.o: example.cpp
+	g++ $(CFLAGS) -c example.cpp -o example.o $(inc)
+clean:
+	rm -f example.o
+	rm -f example

+ 18 - 0
examples/trackball/README

@@ -0,0 +1,18 @@
+This is a simple GLUT-based example that shows how to use igl's trackball.h
+function.
+
+To compile:
+  make
+
+Usage:
+  ./example
+or
+  ./example [positive speed factor]
+
+Interaction:
+  Drag on screen to use trackball
+  Press SPACE to change shape
+
+Example #1:
+  # use a speed factor of 2: twice as fast trackball
+  ./example 2

+ 255 - 0
examples/trackball/example.cpp

@@ -0,0 +1,255 @@
+/*
+  This is an example program that came with AntTweakBar modified to not use
+  AntTweakBar and just show how a trackball works
+
+  On mac os x compile with:
+
+  g++ -c example.cpp -o example.o 
+  g++ -o example example.o -framework OpenGL -framework GLUT
+  rm *.o
+
+*/
+
+#ifdef __APPLE__
+#define _MACOSX
+#endif
+// ORIGINAL COPYRIGHT INFO
+//  ---------------------------------------------------------------------------
+//
+//  @file       TwSimpleGLUT.c
+//  @brief      A simple example that uses AntTweakBar with OpenGL and GLUT.
+//
+//              AntTweakBar: http://www.antisphere.com/Wiki/tools:anttweakbar
+//              OpenGL:      http://www.opengl.org
+//              GLUT:        http://opengl.org/resources/libraries/glut
+//  
+//  @author     Philippe Decaudin - http://www.antisphere.com
+//  @date       2006/05/20
+//
+//  Compilation:
+//  http://www.antisphere.com/Wiki/tools:anttweakbar:examples#twsimpleglut
+//
+//  ---------------------------------------------------------------------------
+
+
+// IGL library
+#include "quat_to_mat.h"
+#include "quat_mult.h"
+#include "axis_angle_to_quat.h"
+#include "trackball.h"
+using namespace igl;
+
+#include <stdlib.h>
+#include <cstdio>
+#include <cmath>
+using namespace std;
+
+#if defined(_WIN32) || defined(_WIN64)
+//  MiniGLUT.h is provided to avoid the need of having GLUT installed to 
+//  recompile this example. Do not use it in your own programs, better
+//  install and use the actual GLUT library SDK.
+#   define USE_MINI_GLUT
+#endif
+
+#if defined(USE_MINI_GLUT)
+#   include "../src/MiniGLUT.h"
+#elif defined(_MACOSX)
+#   include <GLUT/glut.h>
+#else
+#   include <GL/glut.h>
+#endif
+
+// This example displays one of the following shapes
+typedef enum { SHAPE_TEAPOT=1, SHAPE_TORUS, SHAPE_CONE } Shape;
+#define NUM_SHAPES 3
+Shape g_CurrentShape = SHAPE_TEAPOT;
+// Shape orientation (stored as a quaternion)
+float rotation[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+float down_rotation[4];
+// Shapes material
+const float g_MatAmbient[] = { 0.5f, 0.0f, 0.0f, 1.0f };
+const float g_MatDiffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
+// Light parameter
+const double g_LightMultiplier = 1.0f;
+const float g_LightDirection[] = { -0.57735f, -0.57735f, -0.57735f };
+// Keep track of mouse down
+int down_mouse_x, down_mouse_y;
+// keep track of size
+int width, height;
+double speed_factor = 1;
+
+void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
+{
+  down_mouse_x = mouse_x;
+  down_mouse_y = mouse_y;
+  copy(rotation,rotation+4,down_rotation);
+}
+
+void mouse_move(int mouse_x, int mouse_y)
+{
+  trackball(
+    width,
+    height,
+    speed_factor,
+    down_rotation,
+    down_mouse_x,
+    down_mouse_y,
+    mouse_x,
+    mouse_y,
+    rotation);
+  glutPostRedisplay();
+}
+
+void key(unsigned char key, int mouse_x, int mouse_y)
+{
+  if(key == ' ')
+  {
+    g_CurrentShape = (Shape)((g_CurrentShape)%NUM_SHAPES+1);
+  }
+  glutPostRedisplay();
+}
+
+// Callback function called by GLUT to render screen
+void Display(void)
+{
+  
+  // Clear frame buffer
+  glClearColor(0, 0, 0, 1);
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  
+  glEnable(GL_DEPTH_TEST);
+  glDisable(GL_CULL_FACE);
+  glEnable(GL_NORMALIZE);
+  
+  float v[4]; // will be used to set light paramters
+  // Set light
+  glEnable(GL_LIGHTING);
+  glEnable(GL_LIGHT0);
+  v[0] = v[1] = v[2] = g_LightMultiplier*0.4f; v[3] = 1.0f;
+  glLightfv(GL_LIGHT0, GL_AMBIENT, v);
+  v[0] = v[1] = v[2] = g_LightMultiplier*0.8f; v[3] = 1.0f;
+  glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
+  v[0] = -g_LightDirection[0]; v[1] = -g_LightDirection[1]; v[2] = -g_LightDirection[2]; v[3] = 0.0f;
+  glLightfv(GL_LIGHT0, GL_POSITION, v);
+  
+  // Set material
+  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, g_MatAmbient);
+  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, g_MatDiffuse);
+  
+  // Rotate and draw shape
+  glPushMatrix();
+  glTranslatef(0.5f, -0.3f, 0.0f);
+  float mat[4*4]; // rotation matrix
+  quat_to_mat(rotation,mat);
+  glMultMatrixf(mat);
+  glCallList(g_CurrentShape);
+  glPopMatrix();
+  
+  // Present frame buffer
+  glutSwapBuffers();
+  
+  //// Recall Display at next frame
+  //glutPostRedisplay();
+}
+
+
+// Callback function called by GLUT when window size changes
+void Reshape(int width, int height)
+{
+  // keep track of size
+  ::width = width;
+  ::height = height;
+  // Set OpenGL viewport and camera
+  glViewport(0, 0, width, height);
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  gluPerspective(40, (double)width/height, 1, 10);
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+  gluLookAt(0,0,5, 0,0,0, 0,1,0);
+  glTranslatef(0, 0.6f, -1);
+}
+
+
+// Function called at exit
+void Terminate(void)
+{ 
+  glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES);
+}
+
+// Main
+int main(int argc, char *argv[])
+{
+  bool help_and_quit = false;
+  if(argc > 1)
+  {
+    if(
+      argv[1][0] == '-' &&
+      argv[1][1] == 'h')
+    {
+      help_and_quit = true;
+    }else
+    {
+      int count = sscanf(argv[1],"%lg",&speed_factor);
+      if(count != 1)
+      {
+        printf("Error: %s is not a valid speed factor.",
+          argv[1]);
+        help_and_quit = true;
+      }
+    }
+  }
+  if(help_and_quit)
+  {
+    printf(
+      "Usage:\n  ./example\nor\n  ./example [positive speed factor]\n\n"
+      "Interaction:\n  Drag on screen to use trackball\n"
+      "  Press SPACE to change shape\n");
+    return 1;
+  }
+  
+  // Initialize GLUT
+  glutInit(&argc, argv);
+  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
+  glutInitWindowSize(640, 480);
+  glutCreateWindow("AntTweakBar simple example using GLUT");
+  glutCreateMenu(NULL);
+  
+  // Set GLUT callbacks
+  glutDisplayFunc(Display);
+  glutReshapeFunc(Reshape);
+  atexit(Terminate);  // Called after glutMainLoop ends
+  
+  // Set GLUT event callbacks
+  // Directly redirect GLUT mouse button events to trackball example
+  glutMouseFunc(mouse);
+  // Directly redirect GLUT mouse motion events to trackball example
+  glutMotionFunc(mouse_move);
+  //glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
+  // Catch keyboard action
+  glutKeyboardFunc(key);
+  //glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT);
+  //TwGLUTModifiersFunc(glutGetModifiers);
+  
+  // Create some 3D objects (stored in display lists)
+  glNewList(SHAPE_TEAPOT, GL_COMPILE);
+  glutSolidTeapot(1.0);
+  glEndList();
+  glNewList(SHAPE_TORUS, GL_COMPILE);
+  glutSolidTorus(0.3, 1.0, 16, 32);
+  glEndList();
+  glNewList(SHAPE_CONE, GL_COMPILE);
+  glutSolidCone(1.0, 1.5, 64, 4);
+  glEndList();
+  
+  // Init rotation
+  float axis[] = { 0.7f, 0.7f, 0.0f }; 
+  float angle = 0.8f;
+  axis_angle_to_quat(axis,angle,rotation);
+
+  // Call the GLUT main loop
+  glutMainLoop();
+  
+  return 0;
+}
+

+ 5 - 0
quat_mult.h

@@ -1,3 +1,6 @@
+#ifndef IGL_QUAT_MULT_H
+#define IGL_QUAT_MULT_H
+
 namespace igl
 {
   // Computes out = q1 * q2 with quaternion multiplication
@@ -26,3 +29,5 @@ inline void igl::quat_mult(
     out[2] = q1[3]*q2[2] + q1[2]*q2[3] + q1[0]*q2[1] - q1[1]*q2[0];
     out[3] = q1[3]*q2[3] - (q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2]);
 }
+
+#endif

+ 12 - 8
trackball.h

@@ -1,7 +1,3 @@
-#include <dot.h>
-#include <cross.h>
-#include <axis_angle_to_quat.h>
-#include <quat_mult.h>
 
 namespace igl
 {
@@ -32,16 +28,24 @@ namespace igl
 
 // Implementation
 
+#include <dot.h>
+#include <cross.h>
+#include <axis_angle_to_quat.h>
+#include <quat_mult.h>
+#include <cmath>
+#include <cstdlib>
+#include <algorithm>
+
 // Utility inline functions
-inline float _QuatD(int w, int h)
+static inline float _QuatD(int w, int h)
 {
-    return (float)min(abs(w), abs(h)) - 4;
+    return (float)std::min(abs(w), abs(h)) - 4;
 }
-inline float _QuatIX(int x, int w, int h)
+static inline float _QuatIX(int x, int w, int h)
 {
     return (2.0f*(float)x - (float)w - 1.0f)/_QuatD(w, h);
 }
-inline float _QuatIY(int y, int w, int h)
+static inline float _QuatIY(int y, int w, int h)
 {
     return (-2.0f*(float)y + (float)h - 1.0f)/_QuatD(w, h);
 }