/
glutwidget.cpp
339 lines (274 loc) · 8.54 KB
/
glutwidget.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "glutwidget.hpp"
#include "shader_utils.hpp"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
unsigned int glutWidget::m_frame;
unsigned int glutWidget::m_program;
unsigned int glutWidget::m_fragmentsh;
bool glutWidget::guide = 0;
int glutWidget::color = 1;
float glutWidget::cx = 0.5, glutWidget::cy = 0.0;
float glutWidget::scale = 3.0;
int glutWidget::itr = 300;
const float glutWidget::zoom_factor = 0.1;
/*
Initializes GLUT context
*/
glutWidget::glutWidget(int argc, char** argv)
{
m_frame = 0;
glutInitWindowSize(glutWidget::m_width, glutWidget::m_height);
glutInit(&argc,argv);
glutInitDisplayString("samples rgb double depth");
glutCreateWindow("Mandelbrot");
glutMouseFunc(mouseHandler); //what to call when user clicks or scrolls
glutKeyboardFunc(keyDown); //what to call when user presses a key
glutKeyboardUpFunc(keyUp); //what to call when user releases a key
glutSpecialFunc(specialKeyDown); //what to call when user presses a special key
glutSpecialFunc(specialKeyUp); //what to call when user releases a special key
glutDisplayFunc(render); //what to call when window needs redrawing
glutIdleFunc(update); //what to call when no user input given
initOpenGL();
}
/*
Checks whether graphics driver supports all required OpenGL features
*/
void glutWidget::checkExtensions()
{
//query some extensions to make sure they are present
if(glewGetExtension("GL_ARB_shading_language_100") != GL_TRUE)
{
std::cout << "ERROR: Shading language extension not present." << std::endl;
}
if(glewGetExtension("GL_ARB_vertex_program") != GL_TRUE)
{
std::cout << "ERROR: Vertex program extension not present." << std::endl;
}
if(glewGetExtension("GL_ARB_vertex_shader") != GL_TRUE)
{
std::cout << "ERROR: Vertex shader extension not present." << std::endl;
}
if(glewGetExtension("GL_ARB_fragment_program") != GL_TRUE)
{
std::cout << "ERROR: Fragment program extension not present." << std::endl;
}
if(glewGetExtension("GL_ARB_fragment_shader") != GL_TRUE)
{
std::cout << "ERROR: Fragment shader extension not present." << std::endl;
}
if(glewGetExtension("GL_ARB_vertex_buffer_object") != GL_TRUE)
{
std::cout << "ERROR: VBO extension not present." << std::endl;
}
}
/*
Initializes opengl states
*/
void glutWidget::initOpenGL()
{
glewExperimental = GL_TRUE;
GLenum err = glewInit(); //initialize GLEW - this enables us to use extensions
if(err != GLEW_OK)
{
std::cout << "ERROR: Loading GLEW failed." << std::endl;
exit(-1);
}
checkExtensions();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //default "empty"/background color is set to white
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
makeShaders(); //load data of fragment and vertex programs/shaders - compile shaders
srand(time(NULL));
color = rand() % 100 + 1;
}
/*
Redraws window contents
*/
void glutWidget::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears color and depth bits of framebuffer
if(guide)
{
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 0.0);
glVertex2f((0.2+cx)/(scale*0.5), (-0.1+cy)/(scale*0.5));
glVertex2f((0.45+cx)/(scale*0.5), (-0.1+cy)/(scale*0.5));
glVertex2f((0.325+cx)/(scale*0.5), (0.15+cy)/(scale*0.5));
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 0.0);
glVertex2f((-0.2+cx)/(scale*0.5), (0.8+cy)/(scale*0.5));
glVertex2f((0.0+cx)/(scale*0.5), (0.8+cy)/(scale*0.5));
glVertex2f((0.0+cx)/(scale*0.5), (1.0+cy)/(scale*0.5));
glVertex2f((-0.2+cx)/(scale*0.5), (1.0+cy)/(scale*0.5));
glEnd();
//Circle
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 0.0);
for(int i = 0; i < 360; i++)
{
glVertex2f((cos((float) i * (3.14159265/180.0)) / 6 + cx + (-1.35)) / (scale*0.5), (sin((float) i * (3.14159265/180.0)) / 6 + cy) / (scale*0.5));
}
glEnd();
}
glUseProgram(m_program); //Enables fragment shader
//Pass parameters from main program to fragment shader
setUniform2f(m_program, "center", glutWidget::cx, glutWidget::cy);
setUniform1f(m_program, "scale", glutWidget::scale);
setUniform1i(m_program, "itr", itr);
setUniform1i(m_program, "color", color);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1, 0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0, 1);
glVertex2f(-1, 1);
glEnd();
glUseProgram(0); //Disables fragment shader
glutSwapBuffers(); //swaps front and back buffer for double buffering
}
/*
Handles user event: a mouse button was pressed
*/
void glutWidget::mouseHandler(int button, int state, int x, int y)
{
if (state == GLUT_UP)
return;
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
float zscale = (1 / zoom_factor);
//Get distance from center
float dx = (((float) x / (float) w) - 0.5);
float dy = (((float) y / (float) h) - 0.5);
switch(button)
{
case 0: //Left click
break;
case 2: //Right click
break;
case 3: //Scroll up
scale *= 1 - zoom_factor;
cx -= (dx * scale) / zscale;
cy += (dy * scale) / zscale;
break;
case 4: //Scroll down
scale *= 1 + zoom_factor;
cx += dx * scale / zscale;
cy -= dy * scale / zscale;
break;
}
}
/*
Handles user event: a key was pressed
*/
void glutWidget::keyDown(unsigned char key, int, int)
{
switch(key)
{
case 'q':
case 27:
stop(); //quit the main loop, exit program
break;
case 'c':
//change color scheme
color = rand() % 100 + 1;
break;
case 'h':
//toggle guidance
guide = !guide;
break;
case 'w':
scale *= 1 - zoom_factor; //zoom in
break;
case 's':
scale *= 1 + zoom_factor; //zoom out
break;
}
}
/*
Handles user event: a key was released
*/
void glutWidget::keyUp(unsigned char key, int, int)
{
}
/*
Handles user event: a special key was pressed
*/
void glutWidget::specialKeyDown(int key, int, int)
{
}
/*
Handles user event: a special key was released
*/
void glutWidget::specialKeyUp(int key, int, int)
{
float zscale = (1 / zoom_factor);
switch(key)
{
case GLUT_KEY_UP:
cy -= (0.5 * scale) / zscale;
break;
case GLUT_KEY_DOWN:
cy += (0.5 * scale) / zscale;
break;
case GLUT_KEY_LEFT:
cx += (0.5 * scale) / zscale;
break;
case GLUT_KEY_RIGHT:
cx -= (0.5 * scale) / zscale;
break;
}
}
/*
Forces program to stop, cleans up, and exits
*/
void glutWidget::stop()
{
//clean up shaders
glDetachShader(m_program,m_fragmentsh);
glDeleteProgram(m_program);
glDeleteShader(m_fragmentsh);
exit(0);
}
/*
Reads in and compiles shader objects
*/
void glutWidget::makeShaders()
{
m_program = glCreateProgram();
char *shadercode = readShader("shaders/fragmentshader.frag"); //reads shader code (you can edit shader code with a text editor)
m_fragmentsh = glCreateShader(GL_FRAGMENT_SHADER_ARB);
glShaderSource(m_fragmentsh, 1, (const GLcharARB **)&shadercode,NULL);
delete[] shadercode;
glCompileShader(m_fragmentsh); //compiles shader
printInfoLog(m_fragmentsh); //prints errors during shader compilation
glAttachShader(m_program,m_fragmentsh);
glLinkProgram(m_program); //compiles fragment and vertex shader into a shader program
printInfoLog(m_program); //prints errors during program compilation
}
/*
Starts the main loop
*/
void glutWidget::run()
{
glutMainLoop();
}
/*
Called whenever no user input given
*/
void glutWidget::update()
{
m_frame++;
glutPostRedisplay(); //marks window for redrawing
setUniform2f(m_program, "center", glutWidget::cx, glutWidget::cy);
setUniform1f(m_program, "scale", glutWidget::scale);
}