1 // ---------------------------------------------------------------------------
3 // @file TwSimpleGLFW.c
4 // @brief A simple example that uses AntTweakBar with
5 // OpenGL and the GLFW windowing system.
7 // AntTweakBar: http://www.antisphere.com/Wiki/tools:anttweakbar
8 // OpenGL: http://www.opengl.org
9 // GLFW: http://glfw.sourceforge.net
11 // @author Philippe Decaudin - http://www.antisphere.com
17 // http://www.antisphere.com/Wiki/tools:anttweakbar:examples#twsimpleglfw
19 // ---------------------------------------------------------------------------
22 #include <AntTweakBar.h>
29 // Callback function called by GLFW when window size changes
30 void GLFWCALL WindowSizeCB(int width, int height)
32 // Set OpenGL viewport and camera
33 glViewport(0, 0, width, height);
34 glMatrixMode(GL_PROJECTION);
36 gluPerspective(40, (double)width/height, 1, 10);
37 gluLookAt(-1,0,3, 0,0,0, 0,1,0);
39 // Send the new window size to AntTweakBar
40 TwWindowSize(width, height);
44 // This example program draws a possibly transparent cube
45 void DrawModel(int wireframe)
49 // Enable OpenGL transparency and light (could have been done once at init)
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);
62 glDisable(GL_CULL_FACE);
63 glDisable(GL_LIGHTING);
64 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
69 glEnable(GL_CULL_FACE);
70 glEnable(GL_LIGHTING);
71 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
75 for( pass=0; pass<numPass; ++pass )
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 );
81 // Draw the model (a cube)
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
97 GLFWvidmode mode; // GLFW video mode
98 TwBar *bar; // Pointer to a tweak bar
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)
110 // A fatal error occured
111 fprintf(stderr, "GLFW initialization failed\n");
116 glfwGetDesktopMode(&mode);
117 if( !glfwOpenWindow(640, 480, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 16, 0, GLFW_WINDOW /* or GLFW_FULLSCREEN */) )
119 // A fatal error occured
120 fprintf(stderr, "Cannot open GLFW window\n");
124 glfwEnable(GLFW_MOUSE_CURSOR);
125 glfwEnable(GLFW_KEY_REPEAT);
126 glfwSetWindowTitle("AntTweakBar simple example using GLFW");
128 // Initialize AntTweakBar
129 if( !TwInit(TW_OPENGL, NULL) )
131 // A fatal error occured
132 fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
137 // Create a tweak bar
138 bar = TwNewBar("TweakBar");
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 ");
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
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) )
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 );
176 dt = glfwGetTime() - time;
179 glMatrixMode(GL_MODELVIEW);
181 glRotated(360.0*turn, 0.4, 1, 0.2);
182 glTranslated(-0.5, -0.5, -0.5);
184 // Set color and draw model
185 glColor4ubv(cubeColor);
191 // Present frame buffer
195 // Terminate AntTweakBar and GLFW