-
Notifications
You must be signed in to change notification settings - Fork 41
/
sample_gl2.cpp
208 lines (172 loc) · 6.68 KB
/
sample_gl2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// sample_gl2.cpp - public domain
// authored from 2012-2013 by Adrien Herubel
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <iostream>
#include "glew/glew.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include "GL/glfw.h"
#include "imgui.h"
#include "imguiRenderGL2.h"
int main( int argc, char **argv )
{
int width = 1024, height=768;
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
exit( EXIT_FAILURE );
}
// Open a window and create its OpenGL context
if( !glfwOpenWindow( width, height, 0,0,0,0, 24,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window\n" );
glfwTerminate();
exit( EXIT_FAILURE );
}
glfwSetWindowTitle( "imgui sample imguiRenderGL2" );
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
exit( EXIT_FAILURE );
}
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
// Enable vertical sync (on cards that support it)
glfwSwapInterval( 1 );
// Init UI
if (!imguiRenderGLInit("DroidSans.ttf"))
{
fprintf(stderr, "Could not init GUI renderer.\n");
exit(EXIT_FAILURE);
}
glClearColor(0.8f, 0.8f, 0.8f, 1.f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
// imgui states
bool checked1 = false;
bool checked2 = false;
bool checked3 = true;
bool checked4 = false;
float value1 = 50.f;
float value2 = 30.f;
int scrollarea1 = 0;
int scrollarea2 = 0;
// glfw scrolling
int glfwscroll = 0;
do
{
glfwGetWindowSize(&width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Mouse states
unsigned char mousebutton = 0;
int currentglfwscroll = glfwGetMouseWheel();
int mscroll = 0;
if (currentglfwscroll < glfwscroll)
mscroll = 2;
if (currentglfwscroll > glfwscroll)
mscroll = -2;
glfwscroll = currentglfwscroll;
int mousex; int mousey;
glfwGetMousePos(&mousex, &mousey);
mousey = height - mousey;
int leftButton = glfwGetMouseButton( GLFW_MOUSE_BUTTON_LEFT );
int rightButton = glfwGetMouseButton( GLFW_MOUSE_BUTTON_RIGHT );
int middleButton = glfwGetMouseButton( GLFW_MOUSE_BUTTON_MIDDLE );
int toggle = 0;
if( leftButton == GLFW_PRESS )
mousebutton |= IMGUI_MBUT_LEFT;
// Draw UI
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float projection[16] = { 2.f/width, 0.f, 0.f, 0.f,
0.f, 2.f/height, 0.f, 0.f,
0.f, 0.f, -2.f, 0.f,
-1.f, -1.f, -1.f, 1.f };
glLoadMatrixf(projection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glUseProgram(0);
imguiBeginFrame(mousex, mousey, mousebutton, mscroll);
imguiBeginScrollArea("Scroll area", 10, 10, width / 5, height - 20, &scrollarea1);
imguiSeparatorLine();
imguiSeparator();
imguiButton("Button");
imguiButton("Disabled button", false);
imguiItem("Item");
imguiItem("Disabled item", false);
toggle = imguiCheck("Checkbox", checked1);
if (toggle)
checked1 = !checked1;
toggle = imguiCheck("Disabled checkbox", checked2, false);
if (toggle)
checked2 = !checked2;
toggle = imguiCollapse("Collapse", "subtext", checked3);
if (checked3)
{
imguiIndent();
imguiLabel("Collapsible element");
imguiUnindent();
}
if (toggle)
checked3 = !checked3;
toggle = imguiCollapse("Disabled collapse", "subtext", checked4, false);
if (toggle)
checked4 = !checked4;
imguiLabel("Label");
imguiValue("Value");
imguiSlider("Slider", &value1, 0.f, 100.f, 1.f);
imguiSlider("Disabled slider", &value2, 0.f, 100.f, 1.f, false);
imguiIndent();
imguiLabel("Indented");
imguiUnindent();
imguiLabel("Unindented");
imguiEndScrollArea();
imguiBeginScrollArea("Scroll area", 20 + width / 5, 500, width / 5, height - 510, &scrollarea2);
imguiSeparatorLine();
imguiSeparator();
for (int i = 0; i < 100; ++i)
imguiLabel("A wall of text");
imguiEndScrollArea();
imguiEndFrame();
imguiDrawText(30 + width / 5 * 2, height - 20, IMGUI_ALIGN_LEFT, "Free text", imguiRGBA(32,192, 32,192));
imguiDrawText(30 + width / 5 * 2 + 100, height - 40, IMGUI_ALIGN_RIGHT, "Free text", imguiRGBA(32, 32, 192, 192));
imguiDrawText(30 + width / 5 * 2 + 50, height - 60, IMGUI_ALIGN_CENTER, "Free text", imguiRGBA(192, 32, 32,192));
imguiDrawLine(30 + width / 5 * 2, height - 80, 30 + width / 5 * 2 + 100, height - 60, 1.f, imguiRGBA(32,192, 32,192));
imguiDrawLine(30 + width / 5 * 2, height - 100, 30 + width / 5 * 2 + 100, height - 80, 2.f, imguiRGBA(32, 32, 192, 192));
imguiDrawLine(30 + width / 5 * 2, height - 120, 30 + width / 5 * 2 + 100, height - 100, 3.f, imguiRGBA(192, 32, 32,192));
imguiDrawRoundedRect(30 + width / 5 * 2, height - 240, 100, 100, 5.f, imguiRGBA(32,192, 32,192));
imguiDrawRoundedRect(30 + width / 5 * 2, height - 350, 100, 100, 10.f, imguiRGBA(32, 32, 192, 192));
imguiDrawRoundedRect(30 + width / 5 * 2, height - 470, 100, 100, 20.f, imguiRGBA(192, 32, 32,192));
imguiDrawRect(30 + width / 5 * 2, height - 590, 100, 100, imguiRGBA(32, 192, 32, 192));
imguiDrawRect(30 + width / 5 * 2, height - 710, 100, 100, imguiRGBA(32, 32, 192, 192));
imguiDrawRect(30 + width / 5 * 2, height - 830, 100, 100, imguiRGBA(192, 32, 32,192));
imguiRenderGLDraw(width, height);
// Check for errors
GLenum err = glGetError();
if(err != GL_NO_ERROR)
{
fprintf(stderr, "OpenGL Error : %s\n", gluErrorString(err));
}
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
// Clean UI
imguiRenderGLDestroy();
// Close OpenGL window and terminate GLFW
glfwTerminate();
exit( EXIT_SUCCESS );
}