-
Notifications
You must be signed in to change notification settings - Fork 1
/
gl_renderer.cpp
209 lines (165 loc) · 4.55 KB
/
gl_renderer.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
/*
* This file is part of mandelgpu, a free GPU accelerated fractal viewer,
* Copyright (C) 2016 Aksel Alpay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <vector>
#include "gl_renderer.hpp"
#include "image.hpp"
std::function<void()> display_func = [](){};
std::function<void(unsigned char,int,int)> keyboard_func =
[](unsigned char,int,int){};
std::function<void(int,int)> reshape_func =
[](int, int){};
std::function<void(int,int,int,int)> mouse_func =
[](int,int,int,int){};
std::function<void(int,int)> motion_func =
[](int,int){};
std::function<void()> idle_func =
[](){};
void glut_display_func()
{
display_func();
glutSwapBuffers();
gl_renderer::instance().post_redisplay();
}
void glut_keyboard_func(unsigned char c, int x, int y)
{
keyboard_func(c,x,y);
gl_renderer::instance().post_redisplay();
}
void glut_reshape_func(int width, int height)
{
gl_renderer::instance()._width = width;
gl_renderer::instance()._height = height;
reshape_func(width, height);
gl_renderer::instance().post_redisplay();
}
void glut_mouse_func(int button, int state, int x, int y)
{
mouse_func(button, state, x, y);
gl_renderer::instance().post_redisplay();
}
void glut_motion_func(int x, int y)
{
motion_func(x,y);
gl_renderer::instance().post_redisplay();
}
void glut_idle_func()
{
idle_func();
gl_renderer::instance().post_redisplay();
}
gl_renderer::gl_renderer()
: _window_handle(0), _is_fullscreen(false)
{}
void gl_renderer::init(const std::string& title,
std::size_t width, std::size_t height,
int argc, char** argv)
{
if(this->_window_handle)
this->close();
this->_width = width;
this->_height = height;
glutInit(&argc, argv);
int screen_width = glutGet(GLUT_SCREEN_WIDTH);
int screen_height = glutGet(GLUT_SCREEN_HEIGHT);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition((screen_width - this->_width) / 2,
(screen_height - this->_height) / 2 );
glutInitWindowSize(this->_width, this->_height);
_window_handle = glutCreateWindow(title.c_str());
glutDisplayFunc(glut_display_func);
glutKeyboardFunc(glut_keyboard_func);
glutIdleFunc(glut_idle_func);
glutMouseFunc(glut_mouse_func);
glutMotionFunc(glut_motion_func);
glutReshapeFunc(glut_reshape_func);
// Setup initial GL State
glClearColor( 0.0f, 1.0f, 1.0f, 1.0f );
glClearDepth( 1.0f );
glShadeModel( GL_SMOOTH );
}
void gl_renderer::close()
{
if(this->_window_handle)
{
glutDestroyWindow(this->_window_handle);
this->_window_handle = 0;
}
}
void gl_renderer::on_display(std::function<void ()> f)
{
display_func = f;
}
void gl_renderer::on_keyboard(std::function<void (unsigned char, int, int)> f)
{
keyboard_func = f;
}
void gl_renderer::on_reshape(std::function<void(int,int)> f)
{
reshape_func = f;
}
void gl_renderer::on_mouse(std::function<void(int,int,int,int)> f)
{
mouse_func = f;
}
void gl_renderer::on_motion(std::function<void(int,int)> f)
{
motion_func = f;
}
void gl_renderer::on_idle(std::function<void()> f)
{
idle_func = f;
}
void gl_renderer::render_loop()
{
glutMainLoop();
}
void gl_renderer::toggle_fullscreen(bool fullscreen)
{
if(fullscreen != _is_fullscreen)
{
if(fullscreen)
glutFullScreen();
else
{
glutPositionWindow(20,20);
glutReshapeWindow(1200, 900);
}
_is_fullscreen = fullscreen;
}
}
bool gl_renderer::is_fullscreen() const
{
return _is_fullscreen;
}
void gl_renderer::save_png_screenshot(const std::string& name) const
{
std::size_t num_bytes = 3 * _width * _height;
std::vector<unsigned char> buffer(num_bytes);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadPixels(0, 0, _width, _height, GL_RGB, GL_UNSIGNED_BYTE, buffer.data());
save_png(name, buffer, _width, _height);
}
void gl_renderer::post_redisplay()
{
if(this->_window_handle)
glutPostRedisplay();
}
gl_renderer::~gl_renderer()
{
}