Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / CDTestFramework / AntTweakBar / examples / TwSimpleGLUT.c
1 //      ---------------------------------------------------------------------------
2 //
3 //      @file           TwSimpleGLUT.c
4 //      @brief          A simple example that uses AntTweakBar with OpenGL and GLUT.
5 //
6 //                              AntTweakBar: http://www.antisphere.com/Wiki/tools:anttweakbar
7 //                              OpenGL:          http://www.opengl.org
8 //                              GLUT:            http://opengl.org/resources/libraries/glut
9 //      
10 //      @author         Philippe Decaudin - http://www.antisphere.com
11 //      @date           2006/05/20
12 //
13 //      note:           TAB=4
14 //
15 //      Compilation:
16 //      http://www.antisphere.com/Wiki/tools:anttweakbar:examples#twsimpleglut
17 //
18 //      ---------------------------------------------------------------------------
19
20
21 #include <AntTweakBar.h>
22
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include <GL/glut.h>
27
28
29 // This example displays one of the following shapes
30 typedef enum { SHAPE_TEAPOT=1, SHAPE_TORUS, SHAPE_CONE } Shape;
31 #define NUM_SHAPES 3
32 Shape g_CurrentShape = SHAPE_TORUS;
33 // Shapes scale
34 float g_Zoom = 1.0f;
35 // Shapes material
36 float g_MatAmbient[] = { 0.5f, 0.0f, 0.0f, 1.0f };
37 float g_MatDiffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
38 // Light parameter
39 float g_LightMultiplier = 1.0f;
40
41
42 // Callback function called by GLUT to render screen
43 void Display(void)
44 {
45         float angle = (float)glutGet(GLUT_ELAPSED_TIME)/10.0f;
46         float v[4];     // will be used to set light paramters
47
48         // Clear frame buffer
49         glClearColor(0, 0, 0, 1);
50         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
51
52         glEnable(GL_DEPTH_TEST);
53         glDisable(GL_CULL_FACE);
54         glEnable(GL_NORMALIZE);
55
56         // Set light
57         glEnable(GL_LIGHTING);
58         glEnable(GL_LIGHT0);
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);
65
66         // Set material
67         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, g_MatAmbient);
68         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, g_MatDiffuse);
69
70         // Rotate and draw shape
71         glPushMatrix();
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);
76         glPopMatrix();
77
78         // Draw tweak bars
79         TwDraw();
80
81         // Present frame buffer
82         glutSwapBuffers();
83
84         // Recall Display at next frame
85         glutPostRedisplay();
86 }
87
88
89 // Callback function called by GLUT when window size changes
90 void Reshape(int width, int height)
91 {
92         // Set OpenGL viewport and camera
93         glViewport(0, 0, width, height);
94         glMatrixMode(GL_PROJECTION);
95         glLoadIdentity();
96         gluPerspective(40, (double)width/height, 1, 10);
97         glMatrixMode(GL_MODELVIEW);
98         glLoadIdentity();
99         gluLookAt(0,0,5, 0,0,0, 0,1,0);
100         glTranslatef(0, 0.6f, -1);
101
102         // Send the new window size to AntTweakBar
103         TwWindowSize(width, height);
104 }
105
106
107 // Function called at exit
108 void Terminate(void)
109
110         glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES);
111
112         TwTerminate();
113 }
114  
115
116 // Main
117 int main(int argc, char *argv[])
118 {
119         TwBar *bar;     // Pointer to a tweak bar
120
121         // Initialize AntTweakBar
122         // (note that AntTweakBar could also be intialize after GLUT, no matter)
123         if( !TwInit(TW_OPENGL, NULL) )
124         {
125                 // A fatal error occured        
126                 fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
127                 return 1;
128         }
129
130         // Initialize GLUT
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);
136
137         // Set GLUT callbacks
138         glutDisplayFunc(Display);
139         glutReshapeFunc(Reshape);
140         atexit(Terminate);      // Called after glutMainLoop ends
141
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);
156
157         // Create some 3D objects (stored in display lists)
158         glNewList(SHAPE_TEAPOT, GL_COMPILE);
159         glutSolidTeapot(1.0);
160         glEndList();
161         glNewList(SHAPE_TORUS, GL_COMPILE);
162         glutSolidTorus(0.3, 1.0, 16, 32);
163         glEndList();
164         glNewList(SHAPE_CONE, GL_COMPILE);
165         glutSolidCone(1.0, 1.5, 64, 4);
166         glEndList();
167
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)
180         {
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='>' ");
187         }
188
189         // Call the GLUT main loop
190         glutMainLoop();
191
192         return 0;
193 }
194