PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/external/AntTweakBar-1.16/examples/TwSimpleGLUT.c

https://gitlab.com/dannywillems/mass_collide
C | 344 lines | 210 code | 56 blank | 78 comment | 3 complexity | 53ee93a0ea0a3e2ccb99bb869b8bad20 MD5 | raw file
  1. // ---------------------------------------------------------------------------
  2. //
  3. // @file TwSimpleGLUT.c
  4. // @brief A simple example that uses AntTweakBar with OpenGL and GLUT.
  5. //
  6. // AntTweakBar: http://anttweakbar.sourceforge.net/doc
  7. // OpenGL: http://www.opengl.org
  8. // GLUT: http://opengl.org/resources/libraries/glut
  9. //
  10. // @author Philippe Decaudin
  11. // @date 2006/05/20
  12. //
  13. // ---------------------------------------------------------------------------
  14. #include <AntTweakBar.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <math.h>
  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(_MACOSX)
  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_TORUS;
  35. // Shapes scale
  36. float g_Zoom = 1.0f;
  37. // Shape orientation (stored as a quaternion)
  38. float g_Rotation[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  39. // Auto rotate
  40. int g_AutoRotate = 0;
  41. int g_RotateTime = 0;
  42. float g_RotateStart[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  43. // Shapes material
  44. float g_MatAmbient[] = { 0.5f, 0.0f, 0.0f, 1.0f };
  45. float g_MatDiffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
  46. // Light parameter
  47. float g_LightMultiplier = 1.0f;
  48. float g_LightDirection[] = { -0.57735f, -0.57735f, -0.57735f };
  49. // Routine to set a quaternion from a rotation axis and angle
  50. // ( input axis = float[3] angle = float output: quat = float[4] )
  51. void SetQuaternionFromAxisAngle(const float *axis, float angle, float *quat)
  52. {
  53. float sina2, norm;
  54. sina2 = (float)sin(0.5f * angle);
  55. norm = (float)sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
  56. quat[0] = sina2 * axis[0] / norm;
  57. quat[1] = sina2 * axis[1] / norm;
  58. quat[2] = sina2 * axis[2] / norm;
  59. quat[3] = (float)cos(0.5f * angle);
  60. }
  61. // Routine to convert a quaternion to a 4x4 matrix
  62. // ( input: quat = float[4] output: mat = float[4*4] )
  63. void ConvertQuaternionToMatrix(const float *quat, float *mat)
  64. {
  65. float yy2 = 2.0f * quat[1] * quat[1];
  66. float xy2 = 2.0f * quat[0] * quat[1];
  67. float xz2 = 2.0f * quat[0] * quat[2];
  68. float yz2 = 2.0f * quat[1] * quat[2];
  69. float zz2 = 2.0f * quat[2] * quat[2];
  70. float wz2 = 2.0f * quat[3] * quat[2];
  71. float wy2 = 2.0f * quat[3] * quat[1];
  72. float wx2 = 2.0f * quat[3] * quat[0];
  73. float xx2 = 2.0f * quat[0] * quat[0];
  74. mat[0*4+0] = - yy2 - zz2 + 1.0f;
  75. mat[0*4+1] = xy2 + wz2;
  76. mat[0*4+2] = xz2 - wy2;
  77. mat[0*4+3] = 0;
  78. mat[1*4+0] = xy2 - wz2;
  79. mat[1*4+1] = - xx2 - zz2 + 1.0f;
  80. mat[1*4+2] = yz2 + wx2;
  81. mat[1*4+3] = 0;
  82. mat[2*4+0] = xz2 + wy2;
  83. mat[2*4+1] = yz2 - wx2;
  84. mat[2*4+2] = - xx2 - yy2 + 1.0f;
  85. mat[2*4+3] = 0;
  86. mat[3*4+0] = mat[3*4+1] = mat[3*4+2] = 0;
  87. mat[3*4+3] = 1;
  88. }
  89. // Routine to multiply 2 quaternions (ie, compose rotations)
  90. // ( input q1 = float[4] q2 = float[4] output: qout = float[4] )
  91. void MultiplyQuaternions(const float *q1, const float *q2, float *qout)
  92. {
  93. float qr[4];
  94. qr[0] = q1[3]*q2[0] + q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1];
  95. qr[1] = q1[3]*q2[1] + q1[1]*q2[3] + q1[2]*q2[0] - q1[0]*q2[2];
  96. qr[2] = q1[3]*q2[2] + q1[2]*q2[3] + q1[0]*q2[1] - q1[1]*q2[0];
  97. qr[3] = q1[3]*q2[3] - (q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2]);
  98. qout[0] = qr[0]; qout[1] = qr[1]; qout[2] = qr[2]; qout[3] = qr[3];
  99. }
  100. // Return elapsed time in milliseconds
  101. int GetTimeMs()
  102. {
  103. #if !defined(_WIN32)
  104. return glutGet(GLUT_ELAPSED_TIME);
  105. #else
  106. // glutGet(GLUT_ELAPSED_TIME) seems buggy on Windows
  107. return (int)GetTickCount();
  108. #endif
  109. }
  110. // Callback function called by GLUT to render screen
  111. void Display(void)
  112. {
  113. float v[4]; // will be used to set light parameters
  114. float mat[4*4]; // rotation matrix
  115. // Clear frame buffer
  116. glClearColor(0, 0, 0, 1);
  117. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  118. glEnable(GL_DEPTH_TEST);
  119. glDisable(GL_CULL_FACE);
  120. glEnable(GL_NORMALIZE);
  121. // Set light
  122. glEnable(GL_LIGHTING);
  123. glEnable(GL_LIGHT0);
  124. v[0] = v[1] = v[2] = g_LightMultiplier*0.4f; v[3] = 1.0f;
  125. glLightfv(GL_LIGHT0, GL_AMBIENT, v);
  126. v[0] = v[1] = v[2] = g_LightMultiplier*0.8f; v[3] = 1.0f;
  127. glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
  128. v[0] = -g_LightDirection[0]; v[1] = -g_LightDirection[1]; v[2] = -g_LightDirection[2]; v[3] = 0.0f;
  129. glLightfv(GL_LIGHT0, GL_POSITION, v);
  130. // Set material
  131. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, g_MatAmbient);
  132. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, g_MatDiffuse);
  133. // Rotate and draw shape
  134. glPushMatrix();
  135. glTranslatef(0.5f, -0.3f, 0.0f);
  136. if( g_AutoRotate )
  137. {
  138. float axis[3] = { 0, 1, 0 };
  139. float angle = (float)(GetTimeMs()-g_RotateTime)/1000.0f;
  140. float quat[4];
  141. SetQuaternionFromAxisAngle(axis, angle, quat);
  142. MultiplyQuaternions(g_RotateStart, quat, g_Rotation);
  143. }
  144. ConvertQuaternionToMatrix(g_Rotation, mat);
  145. glMultMatrixf(mat);
  146. glScalef(g_Zoom, g_Zoom, g_Zoom);
  147. glCallList(g_CurrentShape);
  148. glPopMatrix();
  149. // Draw tweak bars
  150. TwDraw();
  151. // Present frame buffer
  152. glutSwapBuffers();
  153. // Recall Display at next frame
  154. glutPostRedisplay();
  155. }
  156. // Callback function called by GLUT when window size changes
  157. void Reshape(int width, int height)
  158. {
  159. // Set OpenGL viewport and camera
  160. glViewport(0, 0, width, height);
  161. glMatrixMode(GL_PROJECTION);
  162. glLoadIdentity();
  163. gluPerspective(40, (double)width/height, 1, 10);
  164. glMatrixMode(GL_MODELVIEW);
  165. glLoadIdentity();
  166. gluLookAt(0,0,5, 0,0,0, 0,1,0);
  167. glTranslatef(0, 0.6f, -1);
  168. // Send the new window size to AntTweakBar
  169. TwWindowSize(width, height);
  170. }
  171. // Function called at exit
  172. void Terminate(void)
  173. {
  174. glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES);
  175. TwTerminate();
  176. }
  177. // Callback function called when the 'AutoRotate' variable value of the tweak bar has changed
  178. void TW_CALL SetAutoRotateCB(const void *value, void *clientData)
  179. {
  180. (void)clientData; // unused
  181. g_AutoRotate = *(const int *)value; // copy value to g_AutoRotate
  182. if( g_AutoRotate!=0 )
  183. {
  184. // init rotation
  185. g_RotateTime = GetTimeMs();
  186. g_RotateStart[0] = g_Rotation[0];
  187. g_RotateStart[1] = g_Rotation[1];
  188. g_RotateStart[2] = g_Rotation[2];
  189. g_RotateStart[3] = g_Rotation[3];
  190. // make Rotation variable read-only
  191. TwDefine(" TweakBar/ObjRotation readonly ");
  192. }
  193. else
  194. // make Rotation variable read-write
  195. TwDefine(" TweakBar/ObjRotation readwrite ");
  196. }
  197. // Callback function called by the tweak bar to get the 'AutoRotate' value
  198. void TW_CALL GetAutoRotateCB(void *value, void *clientData)
  199. {
  200. (void)clientData; // unused
  201. *(int *)value = g_AutoRotate; // copy g_AutoRotate to value
  202. }
  203. // Main
  204. int main(int argc, char *argv[])
  205. {
  206. TwBar *bar; // Pointer to the tweak bar
  207. float axis[] = { 0.7f, 0.7f, 0.0f }; // initial model rotation
  208. float angle = 0.8f;
  209. // Initialize GLUT
  210. glutInit(&argc, argv);
  211. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  212. glutInitWindowSize(640, 480);
  213. glutCreateWindow("AntTweakBar simple example using GLUT");
  214. glutCreateMenu(NULL);
  215. // Set GLUT callbacks
  216. glutDisplayFunc(Display);
  217. glutReshapeFunc(Reshape);
  218. atexit(Terminate); // Called after glutMainLoop ends
  219. // Initialize AntTweakBar
  220. TwInit(TW_OPENGL, NULL);
  221. // Set GLUT event callbacks
  222. // - Directly redirect GLUT mouse button events to AntTweakBar
  223. glutMouseFunc((GLUTmousebuttonfun)TwEventMouseButtonGLUT);
  224. // - Directly redirect GLUT mouse motion events to AntTweakBar
  225. glutMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  226. // - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion)
  227. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  228. // - Directly redirect GLUT key events to AntTweakBar
  229. glutKeyboardFunc((GLUTkeyboardfun)TwEventKeyboardGLUT);
  230. // - Directly redirect GLUT special key events to AntTweakBar
  231. glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT);
  232. // - Send 'glutGetModifers' function pointer to AntTweakBar;
  233. // required because the GLUT key event functions do not report key modifiers states.
  234. TwGLUTModifiersFunc(glutGetModifiers);
  235. // Create some 3D objects (stored in display lists)
  236. glNewList(SHAPE_TEAPOT, GL_COMPILE);
  237. glutSolidTeapot(1.0);
  238. glEndList();
  239. glNewList(SHAPE_TORUS, GL_COMPILE);
  240. glutSolidTorus(0.3, 1.0, 16, 32);
  241. glEndList();
  242. glNewList(SHAPE_CONE, GL_COMPILE);
  243. glutSolidCone(1.0, 1.5, 64, 4);
  244. glEndList();
  245. // Create a tweak bar
  246. bar = TwNewBar("TweakBar");
  247. TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLUT and OpenGL.' "); // Message added to the help bar.
  248. TwDefine(" TweakBar size='200 400' color='96 216 224' "); // change default tweak bar size and color
  249. // Add 'g_Zoom' to 'bar': this is a modifable (RW) variable of type TW_TYPE_FLOAT. Its key shortcuts are [z] and [Z].
  250. TwAddVarRW(bar, "Zoom", TW_TYPE_FLOAT, &g_Zoom,
  251. " min=0.01 max=2.5 step=0.01 keyIncr=z keyDecr=Z help='Scale the object (1=original size).' ");
  252. // Add 'g_Rotation' to 'bar': this is a variable of type TW_TYPE_QUAT4F which defines the object's orientation
  253. TwAddVarRW(bar, "ObjRotation", TW_TYPE_QUAT4F, &g_Rotation,
  254. " label='Object rotation' opened=true help='Change the object orientation.' ");
  255. // Add callback to toggle auto-rotate mode (callback functions are defined above).
  256. TwAddVarCB(bar, "AutoRotate", TW_TYPE_BOOL32, SetAutoRotateCB, GetAutoRotateCB, NULL,
  257. " label='Auto-rotate' key=space help='Toggle auto-rotate mode.' ");
  258. // Add 'g_LightMultiplier' to 'bar': this is a variable of type TW_TYPE_FLOAT. Its key shortcuts are [+] and [-].
  259. TwAddVarRW(bar, "Multiplier", TW_TYPE_FLOAT, &g_LightMultiplier,
  260. " label='Light booster' min=0.1 max=4 step=0.02 keyIncr='+' keyDecr='-' help='Increase/decrease the light power.' ");
  261. // Add 'g_LightDirection' to 'bar': this is a variable of type TW_TYPE_DIR3F which defines the light direction
  262. TwAddVarRW(bar, "LightDir", TW_TYPE_DIR3F, &g_LightDirection,
  263. " label='Light direction' opened=true help='Change the light direction.' ");
  264. // Add 'g_MatAmbient' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored)
  265. // and is inserted into a group named 'Material'.
  266. TwAddVarRW(bar, "Ambient", TW_TYPE_COLOR3F, &g_MatAmbient, " group='Material' ");
  267. // Add 'g_MatDiffuse' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored)
  268. // and is inserted into group 'Material'.
  269. TwAddVarRW(bar, "Diffuse", TW_TYPE_COLOR3F, &g_MatDiffuse, " group='Material' ");
  270. // Add the enum variable 'g_CurrentShape' to 'bar'
  271. // (before adding an enum variable, its enum type must be declared to AntTweakBar as follow)
  272. {
  273. // ShapeEV associates Shape enum values with labels that will be displayed instead of enum values
  274. TwEnumVal shapeEV[NUM_SHAPES] = { {SHAPE_TEAPOT, "Teapot"}, {SHAPE_TORUS, "Torus"}, {SHAPE_CONE, "Cone"} };
  275. // Create a type for the enum shapeEV
  276. TwType shapeType = TwDefineEnum("ShapeType", shapeEV, NUM_SHAPES);
  277. // add 'g_CurrentShape' to 'bar': this is a variable of type ShapeType. Its key shortcuts are [<] and [>].
  278. TwAddVarRW(bar, "Shape", shapeType, &g_CurrentShape, " keyIncr='<' keyDecr='>' help='Change object shape.' ");
  279. }
  280. // Store time
  281. g_RotateTime = GetTimeMs();
  282. // Init rotation
  283. SetQuaternionFromAxisAngle(axis, angle, g_Rotation);
  284. SetQuaternionFromAxisAngle(axis, angle, g_RotateStart);
  285. // Call the GLUT main loop
  286. glutMainLoop();
  287. return 0;
  288. }