example.cpp 5.6 KB

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