Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / CDTestFramework / AntTweakBar / examples / TwSimpleGLFW.c
1 //      ---------------------------------------------------------------------------
2 //
3 //      @file           TwSimpleGLFW.c
4 //      @brief          A simple example that uses AntTweakBar with 
5 //                              OpenGL and the GLFW windowing system.
6 //
7 //                              AntTweakBar: http://www.antisphere.com/Wiki/tools:anttweakbar
8 //                              OpenGL:          http://www.opengl.org
9 //                              GLFW:            http://glfw.sourceforge.net
10 //      
11 //      @author         Philippe Decaudin - http://www.antisphere.com
12 //      @date           2006/05/20
13 //
14 //      note:           TAB=4
15 //
16 //      Compilation:
17 //      http://www.antisphere.com/Wiki/tools:anttweakbar:examples#twsimpleglfw
18 //
19 //      ---------------------------------------------------------------------------
20
21
22 #include <AntTweakBar.h>
23
24 #define GLFW_DLL
25 #include "glfw.h"
26
27 #include <stdio.h>
28
29 // Callback function called by GLFW when window size changes
30 void GLFWCALL WindowSizeCB(int width, int height)
31 {
32         // Set OpenGL viewport and camera
33         glViewport(0, 0, width, height);
34         glMatrixMode(GL_PROJECTION);
35         glLoadIdentity();
36         gluPerspective(40, (double)width/height, 1, 10);
37         gluLookAt(-1,0,3, 0,0,0, 0,1,0);        
38         
39         // Send the new window size to AntTweakBar
40         TwWindowSize(width, height);
41 }
42
43
44 // This example program draws a possibly transparent cube 
45 void DrawModel(int wireframe)
46 {
47         int pass, numPass;
48
49         // Enable OpenGL transparency and light (could have been done once at init)
50         glEnable(GL_BLEND);
51         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
52         glEnable(GL_DEPTH_TEST);
53         glEnable(GL_LIGHT0);    // use default light diffuse and position
54         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
55         glEnable(GL_COLOR_MATERIAL);
56         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
57         glEnable(GL_LINE_SMOOTH);
58         glLineWidth(3.0);
59         
60         if( wireframe )
61         {
62                 glDisable(GL_CULL_FACE);        
63                 glDisable(GL_LIGHTING);
64                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
65                 numPass = 1;
66         }
67         else
68         {
69                 glEnable(GL_CULL_FACE); 
70                 glEnable(GL_LIGHTING);
71                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
72                 numPass = 2;
73         }
74
75         for( pass=0; pass<numPass; ++pass )
76         {
77                 // Since the material could be transparent, we draw the convex model in 2 passes:
78                 // first its back faces, and second its front faces.
79                 glCullFace( (pass==0) ? GL_FRONT : GL_BACK );
80
81                 // Draw the model (a cube)
82                 glBegin(GL_QUADS);
83                         glNormal3f(0,0,-1); glVertex3f(0,0,0); glVertex3f(0,1,0); glVertex3f(1,1,0); glVertex3f(1,0,0); // front face
84                         glNormal3f(0,0,+1); glVertex3f(0,0,1); glVertex3f(1,0,1); glVertex3f(1,1,1); glVertex3f(0,1,1); // back face
85                         glNormal3f(-1,0,0); glVertex3f(0,0,0); glVertex3f(0,0,1); glVertex3f(0,1,1); glVertex3f(0,1,0); // left face
86                         glNormal3f(+1,0,0); glVertex3f(1,0,0); glVertex3f(1,1,0); glVertex3f(1,1,1); glVertex3f(1,0,1); // right face
87                         glNormal3f(0,-1,0); glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(1,0,1); glVertex3f(0,0,1); // bottom face  
88                         glNormal3f(0,+1,0); glVertex3f(0,1,0); glVertex3f(0,1,1); glVertex3f(1,1,1); glVertex3f(1,1,0); // top face
89                 glEnd();
90         }
91 }
92
93
94 // Main
95 int main() 
96 {
97         GLFWvidmode mode;       // GLFW video mode
98         TwBar *bar;                     // Pointer to a tweak bar
99         
100         double time = 0, dt;// Current time and enlapsed time
101         double turn = 0;        // Model turn counter
102         double speed = 0.1;     // Model rotation speed
103         int wire = 0;           // Draw model in wireframe?
104         float bgColor[] = { 0.1f, 0.2f, 0.4f };                 // Background color     
105         unsigned char cubeColor[] = { 255, 0, 0, 255 }; // Model color (32bits RGBA)
106
107         // Intialize GLFW       
108         if( !glfwInit() )
109         {
110                 // A fatal error occured
111                 fprintf(stderr, "GLFW initialization failed\n");
112                 return 1;
113         }
114
115         // Create a window
116         glfwGetDesktopMode(&mode);
117         if( !glfwOpenWindow(640, 480, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 16, 0, GLFW_WINDOW /* or GLFW_FULLSCREEN */) )
118         {
119                 // A fatal error occured        
120                 fprintf(stderr, "Cannot open GLFW window\n");
121                 glfwTerminate();
122                 return 1;
123         }
124         glfwEnable(GLFW_MOUSE_CURSOR);
125         glfwEnable(GLFW_KEY_REPEAT);
126         glfwSetWindowTitle("AntTweakBar simple example using GLFW");
127
128         // Initialize AntTweakBar
129         if( !TwInit(TW_OPENGL, NULL) )
130         {
131                 // A fatal error occured        
132                 fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
133                 glfwTerminate();
134                 return 1;
135         }
136
137         // Create a tweak bar
138         bar = TwNewBar("TweakBar");
139
140         // Add 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
141         TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, &speed, " label='Rot speed (tr/sec)' min=0 max=2 step=0.01 keyIncr=s keyDecr=S ");
142         // Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
143         TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire, " label='Wireframe mode' key=w ");
144         // Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
145         TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, &time, " label='Time (sec)' precision=1 ");                     
146         // Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
147         TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");
148         // Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
149         TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, &cubeColor, " label='Cube color' alpha ");
150
151         // Set GLFW event callbacks
152         // - Redirect window size changes to the callback function WindowSizeCB
153         glfwSetWindowSizeCallback(WindowSizeCB);
154         // - Directly redirect GLFW mouse button events to AntTweakBar
155         glfwSetMouseButtonCallback((GLFWmousebuttonfun)TwEventMouseButtonGLFW);
156         // - Directly redirect GLFW mouse position events to AntTweakBar
157         glfwSetMousePosCallback((GLFWmouseposfun)TwEventMousePosGLFW);
158         // - Directly redirect GLFW mouse wheel events to AntTweakBar
159         glfwSetMouseWheelCallback((GLFWmousewheelfun)TwEventMouseWheelGLFW);
160         // - Directly redirect GLFW key events to AntTweakBar
161         glfwSetKeyCallback((GLFWkeyfun)TwEventKeyGLFW);
162         // - Directly redirect GLFW char events to AntTweakBar
163         glfwSetCharCallback((GLFWcharfun)TwEventCharGLFW);
164         // - Redirect window size changes to the callback function WindowSizeCB
165
166         // Initialize time
167         time = glfwGetTime();
168         // Main loop (repeated while window is not closed and [ESC] is not pressed)
169         while( glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC) )
170         {
171                 // Clear frame buffer using bgColor
172                 glClearColor(bgColor[0], bgColor[1], bgColor[2], 1);
173                 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
174
175                 // Rotate model
176                 dt = glfwGetTime() - time;
177                 time += dt;
178                 turn += speed*dt;
179                 glMatrixMode(GL_MODELVIEW);
180                 glLoadIdentity();
181                 glRotated(360.0*turn, 0.4, 1, 0.2);
182                 glTranslated(-0.5, -0.5, -0.5);         
183         
184                 // Set color and draw model
185                 glColor4ubv(cubeColor);
186                 DrawModel(wire);
187                 
188                 // Draw tweak bars
189                 TwDraw();
190
191                 // Present frame buffer
192                 glfwSwapBuffers();
193         }
194
195         // Terminate AntTweakBar and GLFW
196         TwTerminate();
197         glfwTerminate();
198
199         return 0;
200 }