example.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. This is an example program that came with AntTweakBar modified to not use
  3. AntTweakBar and just show how a trackball works
  4. On mac os x compile with:
  5. g++ -c example.cpp -o example.o
  6. g++ -o example example.o -framework OpenGL -framework GLUT
  7. rm *.o
  8. */
  9. #ifdef __APPLE__
  10. #define _MACOSX
  11. #endif
  12. // ORIGINAL COPYRIGHT INFO
  13. // ---------------------------------------------------------------------------
  14. //
  15. // @file TwSimpleGLUT.c
  16. // @brief A simple example that uses AntTweakBar with OpenGL and GLUT.
  17. //
  18. // AntTweakBar: http://www.antisphere.com/Wiki/tools:anttweakbar
  19. // OpenGL: http://www.opengl.org
  20. // GLUT: http://opengl.org/resources/libraries/glut
  21. //
  22. // @author Philippe Decaudin - http://www.antisphere.com
  23. // @date 2006/05/20
  24. //
  25. // Compilation:
  26. // http://www.antisphere.com/Wiki/tools:anttweakbar:examples#twsimpleglut
  27. //
  28. // ---------------------------------------------------------------------------
  29. // IGL library
  30. #include "quat_to_mat.h"
  31. #include "quat_mult.h"
  32. #include "axis_angle_to_quat.h"
  33. #include "trackball.h"
  34. using namespace igl;
  35. #include <stdlib.h>
  36. #include <cstdio>
  37. #include <cmath>
  38. using namespace std;
  39. #if defined(_WIN32) || defined(_WIN64)
  40. // MiniGLUT.h is provided to avoid the need of having GLUT installed to
  41. // recompile this example. Do not use it in your own programs, better
  42. // install and use the actual GLUT library SDK.
  43. # define USE_MINI_GLUT
  44. #endif
  45. #if defined(USE_MINI_GLUT)
  46. # include "../src/MiniGLUT.h"
  47. #elif defined(_MACOSX)
  48. # include <GLUT/glut.h>
  49. #else
  50. # include <GL/glut.h>
  51. #endif
  52. // This example displays one of the following shapes
  53. typedef enum { SHAPE_TEAPOT=1, SHAPE_TORUS, SHAPE_CONE } Shape;
  54. #define NUM_SHAPES 3
  55. Shape g_CurrentShape = SHAPE_TEAPOT;
  56. // Shape orientation (stored as a quaternion)
  57. float rotation[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  58. float down_rotation[4];
  59. // Shapes material
  60. const float g_MatAmbient[] = { 0.5f, 0.0f, 0.0f, 1.0f };
  61. const float g_MatDiffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
  62. // Light parameter
  63. const double g_LightMultiplier = 1.0f;
  64. const float g_LightDirection[] = { -0.57735f, -0.57735f, -0.57735f };
  65. // Keep track of mouse down
  66. int down_mouse_x, down_mouse_y;
  67. // keep track of size
  68. int width, height;
  69. double speed_factor = 1;
  70. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  71. {
  72. down_mouse_x = mouse_x;
  73. down_mouse_y = mouse_y;
  74. copy(rotation,rotation+4,down_rotation);
  75. }
  76. void mouse_move(int mouse_x, int mouse_y)
  77. {
  78. trackball(
  79. width,
  80. height,
  81. speed_factor,
  82. down_rotation,
  83. down_mouse_x,
  84. down_mouse_y,
  85. mouse_x,
  86. mouse_y,
  87. rotation);
  88. glutPostRedisplay();
  89. }
  90. void key(unsigned char key, int mouse_x, int mouse_y)
  91. {
  92. if(key == ' ')
  93. {
  94. g_CurrentShape = (Shape)((g_CurrentShape)%NUM_SHAPES+1);
  95. }
  96. glutPostRedisplay();
  97. }
  98. // Callback function called by GLUT to render screen
  99. void Display(void)
  100. {
  101. // Clear frame buffer
  102. glClearColor(0, 0, 0, 1);
  103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104. glEnable(GL_DEPTH_TEST);
  105. glDisable(GL_CULL_FACE);
  106. glEnable(GL_NORMALIZE);
  107. float v[4]; // will be used to set light paramters
  108. // Set light
  109. glEnable(GL_LIGHTING);
  110. glEnable(GL_LIGHT0);
  111. v[0] = v[1] = v[2] = g_LightMultiplier*0.4f; v[3] = 1.0f;
  112. glLightfv(GL_LIGHT0, GL_AMBIENT, v);
  113. v[0] = v[1] = v[2] = g_LightMultiplier*0.8f; v[3] = 1.0f;
  114. glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
  115. v[0] = -g_LightDirection[0]; v[1] = -g_LightDirection[1]; v[2] = -g_LightDirection[2]; v[3] = 0.0f;
  116. glLightfv(GL_LIGHT0, GL_POSITION, v);
  117. // Set material
  118. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, g_MatAmbient);
  119. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, g_MatDiffuse);
  120. // Rotate and draw shape
  121. glPushMatrix();
  122. glTranslatef(0.5f, -0.3f, 0.0f);
  123. float mat[4*4]; // rotation matrix
  124. quat_to_mat(rotation,mat);
  125. glMultMatrixf(mat);
  126. glCallList(g_CurrentShape);
  127. glPopMatrix();
  128. // Present frame buffer
  129. glutSwapBuffers();
  130. //// Recall Display at next frame
  131. //glutPostRedisplay();
  132. }
  133. // Callback function called by GLUT when window size changes
  134. void Reshape(int width, int height)
  135. {
  136. // keep track of size
  137. ::width = width;
  138. ::height = height;
  139. // Set OpenGL viewport and camera
  140. glViewport(0, 0, width, height);
  141. glMatrixMode(GL_PROJECTION);
  142. glLoadIdentity();
  143. gluPerspective(40, (double)width/height, 1, 10);
  144. glMatrixMode(GL_MODELVIEW);
  145. glLoadIdentity();
  146. gluLookAt(0,0,5, 0,0,0, 0,1,0);
  147. glTranslatef(0, 0.6f, -1);
  148. }
  149. // Function called at exit
  150. void Terminate(void)
  151. {
  152. glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES);
  153. }
  154. // Main
  155. int main(int argc, char *argv[])
  156. {
  157. bool help_and_quit = false;
  158. if(argc > 1)
  159. {
  160. if(
  161. argv[1][0] == '-' &&
  162. argv[1][1] == 'h')
  163. {
  164. help_and_quit = true;
  165. }else
  166. {
  167. int count = sscanf(argv[1],"%lg",&speed_factor);
  168. if(count != 1)
  169. {
  170. printf("Error: %s is not a valid speed factor.",
  171. argv[1]);
  172. help_and_quit = true;
  173. }
  174. }
  175. }
  176. if(help_and_quit)
  177. {
  178. printf(
  179. "Usage:\n ./example\nor\n ./example [positive speed factor]\n\n"
  180. "Interaction:\n Drag on screen to use trackball\n"
  181. " Press SPACE to change shape\n");
  182. return 1;
  183. }
  184. // Initialize GLUT
  185. glutInit(&argc, argv);
  186. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  187. glutInitWindowSize(640, 480);
  188. glutCreateWindow("AntTweakBar simple example using GLUT");
  189. glutCreateMenu(NULL);
  190. // Set GLUT callbacks
  191. glutDisplayFunc(Display);
  192. glutReshapeFunc(Reshape);
  193. atexit(Terminate); // Called after glutMainLoop ends
  194. // Set GLUT event callbacks
  195. // Directly redirect GLUT mouse button events to trackball example
  196. glutMouseFunc(mouse);
  197. // Directly redirect GLUT mouse motion events to trackball example
  198. glutMotionFunc(mouse_move);
  199. //glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  200. // Catch keyboard action
  201. glutKeyboardFunc(key);
  202. //glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT);
  203. //TwGLUTModifiersFunc(glutGetModifiers);
  204. // Create some 3D objects (stored in display lists)
  205. glNewList(SHAPE_TEAPOT, GL_COMPILE);
  206. glutSolidTeapot(1.0);
  207. glEndList();
  208. glNewList(SHAPE_TORUS, GL_COMPILE);
  209. glutSolidTorus(0.3, 1.0, 16, 32);
  210. glEndList();
  211. glNewList(SHAPE_CONE, GL_COMPILE);
  212. glutSolidCone(1.0, 1.5, 64, 4);
  213. glEndList();
  214. // Init rotation
  215. float axis[] = { 0.7f, 0.7f, 0.0f };
  216. float angle = 0.8f;
  217. axis_angle_to_quat(axis,angle,rotation);
  218. // Call the GLUT main loop
  219. glutMainLoop();
  220. return 0;
  221. }