1 // ---------------------------------------------------------------------------
3 // @file TwSimpleGLUT.c
4 // @brief A simple example that uses AntTweakBar with OpenGL and GLUT.
6 // AntTweakBar: http://www.antisphere.com/Wiki/tools:anttweakbar
7 // OpenGL: http://www.opengl.org
8 // GLUT: http://opengl.org/resources/libraries/glut
10 // @author Philippe Decaudin - http://www.antisphere.com
16 // http://www.antisphere.com/Wiki/tools:anttweakbar:examples#twsimpleglut
18 // ---------------------------------------------------------------------------
21 #include <AntTweakBar.h>
29 // This example displays one of the following shapes
30 typedef enum { SHAPE_TEAPOT=1, SHAPE_TORUS, SHAPE_CONE } Shape;
32 Shape g_CurrentShape = SHAPE_TORUS;
36 float g_MatAmbient[] = { 0.5f, 0.0f, 0.0f, 1.0f };
37 float g_MatDiffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
39 float g_LightMultiplier = 1.0f;
42 // Callback function called by GLUT to render screen
45 float angle = (float)glutGet(GLUT_ELAPSED_TIME)/10.0f;
46 float v[4]; // will be used to set light paramters
49 glClearColor(0, 0, 0, 1);
50 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
52 glEnable(GL_DEPTH_TEST);
53 glDisable(GL_CULL_FACE);
54 glEnable(GL_NORMALIZE);
57 glEnable(GL_LIGHTING);
59 v[0] = v[1] = v[2] = g_LightMultiplier*0.4f; v[3] = 1.0f;
60 glLightfv(GL_LIGHT0, GL_AMBIENT, v);
61 v[0] = v[1] = v[2] = g_LightMultiplier*0.8f; v[3] = 1.0f;
62 glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
63 v[0] = v[1] = v[2] = 1.0f; v[3] = 0.0f;
64 glLightfv(GL_LIGHT0, GL_POSITION, v);
67 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, g_MatAmbient);
68 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, g_MatDiffuse);
70 // Rotate and draw shape
72 glTranslatef(0.5f, -0.3f, 0.0f);
73 glRotatef(angle, 1.0f, 5.0f, 0.0f);
74 glScalef(g_Zoom, g_Zoom, g_Zoom);
75 glCallList(g_CurrentShape);
81 // Present frame buffer
84 // Recall Display at next frame
89 // Callback function called by GLUT when window size changes
90 void Reshape(int width, int height)
92 // Set OpenGL viewport and camera
93 glViewport(0, 0, width, height);
94 glMatrixMode(GL_PROJECTION);
96 gluPerspective(40, (double)width/height, 1, 10);
97 glMatrixMode(GL_MODELVIEW);
99 gluLookAt(0,0,5, 0,0,0, 0,1,0);
100 glTranslatef(0, 0.6f, -1);
102 // Send the new window size to AntTweakBar
103 TwWindowSize(width, height);
107 // Function called at exit
110 glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES);
117 int main(int argc, char *argv[])
119 TwBar *bar; // Pointer to a tweak bar
121 // Initialize AntTweakBar
122 // (note that AntTweakBar could also be intialize after GLUT, no matter)
123 if( !TwInit(TW_OPENGL, NULL) )
125 // A fatal error occured
126 fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
131 glutInit(&argc, argv);
132 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
133 glutInitWindowSize(640, 480);
134 glutCreateWindow("AntTweakBar simple example using GLUT");
135 glutCreateMenu(NULL);
137 // Set GLUT callbacks
138 glutDisplayFunc(Display);
139 glutReshapeFunc(Reshape);
140 atexit(Terminate); // Called after glutMainLoop ends
142 // Set GLUT event callbacks
143 // - Directly redirect GLUT mouse button events to AntTweakBar
144 glutMouseFunc((GLUTmousebuttonfun)TwEventMouseButtonGLUT);
145 // - Directly redirect GLUT mouse motion events to AntTweakBar
146 glutMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
147 // - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion)
148 glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
149 // - Directly redirect GLUT key events to AntTweakBar
150 glutKeyboardFunc((GLUTkeyboardfun)TwEventKeyboardGLUT);
151 // - Directly redirect GLUT special key events to AntTweakBar
152 glutSpecialFunc((GLUTspecialfun)TwEventSpecialGLUT);
153 // - Send 'glutGetModifers' function pointer to AntTweakBar;
154 // required because the GLUT key event functions do not report key modifiers states.
155 TwGLUTModifiersFunc(glutGetModifiers);
157 // Create some 3D objects (stored in display lists)
158 glNewList(SHAPE_TEAPOT, GL_COMPILE);
159 glutSolidTeapot(1.0);
161 glNewList(SHAPE_TORUS, GL_COMPILE);
162 glutSolidTorus(0.3, 1.0, 16, 32);
164 glNewList(SHAPE_CONE, GL_COMPILE);
165 glutSolidCone(1.0, 1.5, 64, 4);
168 // Create a tweak bar
169 bar = TwNewBar("TweakBar");
170 // Add 'g_Zoom' to 'bar': it is a modifable (RW) variable of type TW_TYPE_FLOAT. Its key shortcuts are [z] and [Z].
171 TwAddVarRW(bar, "Zoom", TW_TYPE_FLOAT, &g_Zoom, " min=0.01 max=2.5 step=0.01 keyIncr=z keyDecr=Z ");
172 // Add 'g_LightMultiplier' to 'bar': it is a variable of type TW_TYPE_FLOAT. Its key shortcuts are [+] and [-].
173 TwAddVarRW(bar, "Multiplier", TW_TYPE_FLOAT, &g_LightMultiplier, " label='Light booster' min=0.1 max=4 step=0.02 keyIncr='+' keyDecr='-' ");
174 // Add 'g_MatAmbient' to 'bar': it is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored). It is inserted into a group named 'Material'.
175 TwAddVarRW(bar, "Ambient", TW_TYPE_COLOR3F, &g_MatAmbient, " group='Material' ");
176 // Add 'g_MatDiffuse' to 'bar': it is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored). It is inserted into group 'Material'.
177 TwAddVarRW(bar, "Diffuse", TW_TYPE_COLOR3F, &g_MatDiffuse, " group='Material' ");
178 // Add the enum variable 'g_CurrentShape' to 'bar'
179 // (before adding an enum variable, its enum type must be declared to AntTweakBar as follow)
181 // ShapeEV associates Shape enum values with labels that will be displayed instead of enum values
182 TwEnumVal shapeEV[NUM_SHAPES] = { {SHAPE_TEAPOT, "Teapot"}, {SHAPE_TORUS, "Torus"}, {SHAPE_CONE, "Cone"} };
183 // Create a type for the enum shapeEV
184 TwType shapeType = TwDefineEnum("ShapeType", shapeEV, NUM_SHAPES);
185 // add 'g_CurrentShape' to 'bar': it is a variable of type ShapeType. Its key shortcuts are [<] and [>].
186 TwAddVarRW(bar, "Shape", shapeType, &g_CurrentShape, " keyIncr='<' keyDecr='>' ");
189 // Call the GLUT main loop