example.cpp 5.6 KB

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