1
+ #include < GLFW/glfw3.h>
2
+ #include < AntTweakBar.h>
3
+
4
+ #define px (x ) x * ( fWidth / width )
5
+
6
+ int width = 1280 ;
7
+ int height = 720 ;
8
+ int fWidth = width;
9
+ int fHeight = height;
10
+
11
+ void onResizeWindow (GLFWwindow* window, int width, int height) {
12
+ TwWindowSize (width, height);
13
+ }
14
+
15
+ void onKeyPress (GLFWwindow* window, int key, int scancode, int action, int mods) {
16
+ if (glfwGetKey (window, GLFW_KEY_BACKSPACE) == GLFW_PRESS)
17
+ TwKeyPressed (TW_KEY_BACKSPACE, TW_KMOD_NONE);
18
+ }
19
+
20
+ void onCharacter (GLFWwindow* window, unsigned int codepoint) {
21
+ TwKeyPressed (codepoint, TW_KMOD_NONE);
22
+ }
23
+
24
+ void onMouseButton (GLFWwindow * window, int button, int action, int mods)
25
+ {
26
+ auto a = action == GLFW_PRESS ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
27
+ auto b = TW_MOUSE_LEFT;
28
+
29
+ TwMouseButton (a, b);
30
+ }
31
+
32
+ void onMouseMotion (GLFWwindow * window, double xpos, double ypos)
33
+ {
34
+ TwMouseMotion (px (static_cast <int >(xpos)), px (static_cast <int >(ypos)));
35
+ }
36
+
37
+ int main ()
38
+ {
39
+ glfwInit ();
40
+
41
+ glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3 );
42
+ glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2 );
43
+ glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
44
+ glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
45
+
46
+ GLFWwindow* window = glfwCreateWindow (width, height, " AntTweakBar en GLFW" , nullptr , nullptr );
47
+ glfwMakeContextCurrent (window);
48
+
49
+ TwInit (TW_OPENGL_CORE, NULL );
50
+ TwWindowSize (fWidth , fHeight );
51
+
52
+ glfwGetFramebufferSize (window, &fWidth , &fHeight );
53
+
54
+ glfwSetCursorPosCallback (window, onMouseMotion);
55
+ glfwSetMouseButtonCallback (window, onMouseButton);
56
+ glfwSetKeyCallback (window, onKeyPress);
57
+ glfwSetCharCallback (window, onCharacter);
58
+ glfwSetWindowSizeCallback (window, onResizeWindow);
59
+
60
+ TwBar* bar = TwNewBar (" Tweak Bar" );
61
+
62
+ double speed = 8.10 ;
63
+ double acel = 10.25 ;
64
+ int wire = 1 ;
65
+ float color[] = { 0 .1f , 0 .2f , 0 .4f };
66
+ unsigned char color2[] = { 255 , 0 , 0 , 128 };
67
+ float g_Rotation[] = { 0 .0f , 0 .0f , 0 .0f , 1 .0f };
68
+ float g_LightDirection[] = { -0 .57735f , -0 .57735f , -0 .57735f };
69
+
70
+ TwAddVarRW (bar, " velocidad" , TW_TYPE_DOUBLE, &speed,
71
+ " label='Velocidad Rotacion' min=0 max=20 step=0.1 keyIncr=s keyDecr=S help='Aumenta o disminuye la velocidad de rotacion' " );
72
+
73
+ TwAddVarRO (bar, " acelaracion" , TW_TYPE_DOUBLE, &acel, " label='Aceleracion' help='Aumenta o disminuye la velocidad de rotacion' " );
74
+
75
+ TwAddVarRW (bar, " wire" , TW_TYPE_BOOL32, &wire, " label='Bool variable' " );
76
+
77
+ TwAddVarRW (bar, " color1" , TW_TYPE_COLOR3F, &color, " label='Color de Fondo' " );
78
+
79
+ TwAddVarRW (bar, " color2" , TW_TYPE_COLOR32, &color2, " label='RGBA 32 bits color' alpha " );
80
+ TwAddVarRW (bar, " LightDir" , TW_TYPE_DIR3F, &g_LightDirection,
81
+ " label='Light direction' opened=true help='Change the light direction.' " );
82
+
83
+ TwAddVarRW (bar, " ObjRotation" , TW_TYPE_QUAT4F, &g_Rotation,
84
+ " label='Object rotation' opened=true help='Change the object orientation.' " );
85
+
86
+ glViewport (0 , 0 , fWidth , fHeight );
87
+ glMatrixMode (GL_PROJECTION);
88
+ glLoadIdentity ();
89
+ glOrtho (0 .0f , fWidth , fHeight , 0 .0f , 0 .0f , 1 .0f );
90
+ glMatrixMode (GL_MODELVIEW);
91
+ glLoadIdentity ();
92
+
93
+ while (!glfwWindowShouldClose (window))
94
+ {
95
+ glClearColor (color[0 ], color[1 ], color[2 ], 1 );
96
+ glClear (GL_COLOR_BUFFER_BIT);
97
+
98
+ TwDraw ();
99
+
100
+ glfwSwapBuffers (window);
101
+ glfwPollEvents ();
102
+ }
103
+
104
+ glfwDestroyWindow (window);
105
+
106
+ TwTerminate ();
107
+ glfwTerminate ();
108
+
109
+ return 0 ;
110
+ }
0 commit comments