/
glut_main.cpp
301 lines (253 loc) · 7.91 KB
/
glut_main.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
/*
* .file example/oglplus/glut_main.cpp
* Implements GLUT-based program main function for running examples
*
* Copyright 2008-2019 Matus Chochlik. Distributed under the Boost
* Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include <oglplus/gl.hpp>
#if OGLPLUS_FREEGLUT_FOUND
#include <GL/freeglut.h>
#elif defined(__APPLE__) && __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <oglplus/config/fix_gl_extension.hpp>
#include <oglplus/config/fix_gl_version.hpp>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
#include <oglplus/error/basic.hpp>
#include <oglplus/query.hpp>
#include <oglplus/os/steady_clock.hpp>
#include "example.hpp"
#include "example_main.hpp"
namespace oglplus {
class SingleExample {
private:
static SingleExample*& SingleInstance() {
static SingleExample* wrapper = nullptr;
return wrapper;
}
os::steady_clock _os_clock;
ExampleClock _clock;
double _fps_time, _prim_count;
unsigned long _frame_no;
GLuint _width, _height;
std::unique_ptr<Example> _example;
std::unique_ptr<Query> _primitive_query;
const char* _screenshot_path;
SingleExample(const SingleExample&);
public:
Example* operator->() {
assert(SingleInstance());
return SingleInstance()->_example.get();
}
SingleExample(
GLuint width,
GLuint height,
const ExampleParams& params,
const char* screenshot_path)
: _fps_time(0.0)
, _prim_count(0.0)
, _frame_no(0)
, _width(width)
, _height(height)
, _example(makeExample(params))
, _primitive_query(new Query())
, _screenshot_path(screenshot_path) {
assert(!SingleInstance());
SingleInstance() = this;
assert(_example);
_example->Reshape(width, height);
_example->MouseMove(width / 2, height / 2, width, height);
_os_clock.reset();
if(_screenshot_path)
_clock.Update(_example->HeatUpTime());
}
~SingleExample() {
assert(SingleInstance());
SingleInstance() = nullptr;
}
void Quit() {
#if OGLPLUS_FREEGLUT_FOUND
glutLeaveMainLoop();
#else
exit(0);
#endif
}
void Close() {
_primitive_query.reset();
_example.reset();
}
static void CloseFunc() {
assert(SingleInstance());
SingleInstance()->Close();
}
void Display() {
_clock.Update(ExampleTimePeriod::Seconds(_os_clock.seconds()));
double frame_time = _clock.Now().Seconds();
_frame_no++;
GLuint primitives_per_frame = 0;
if(_primitive_query) {
try {
auto query_exec = _primitive_query->Execute(
Query::Target::PrimitivesGenerated, primitives_per_frame);
_example->Render(_clock);
glutSwapBuffers();
} catch(Error&) {
_primitive_query.reset();
}
} else {
_example->Render(_clock);
glutSwapBuffers();
}
_prim_count += double(primitives_per_frame);
const double fps_interval = 10.0;
const double this_interval = frame_time - _fps_time;
if(this_interval >= fps_interval) {
_fps_time = frame_time;
std::cout.width(5);
std::cout.precision(3);
std::cout << _frame_no << " frames in " << std::fixed
<< this_interval << " seconds = " << std::fixed
<< _frame_no / this_interval << " FPS; "
<< std::scientific << _prim_count / this_interval
<< " PPS; " << std::scientific << _prim_count / _frame_no
<< " PPF; " << std::endl;
_frame_no = 0;
_prim_count = 0.0;
}
if(!_example->Continue(_clock)) {
Quit();
}
}
static void DisplayFunc() {
assert(SingleInstance());
SingleInstance()->Display();
}
void Screenshot() {
_example->Render(_clock);
if(_clock.Now() >= _example->ScreenshotTime()) {
glFinish();
std::vector<char> pixels(_width * _height * 3);
glReadPixels(
0, 0, _width, _height, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
std::ofstream output(_screenshot_path);
output.write(pixels.data(), pixels.size());
Quit();
}
glutSwapBuffers();
_clock.Advance(_example->FrameTime());
}
static void ScreenshotFunc() {
assert(SingleInstance());
SingleInstance()->Screenshot();
}
void Reshape(int width, int height) {
_width = width;
_height = height;
_example->Reshape(width, height);
}
static void ReshapeFunc(int width, int height) {
assert(SingleInstance());
SingleInstance()->Reshape(width, height);
}
void Motion(int x, int y) {
_example->MouseMove(x, _height - y, _width, _height);
}
static void MotionFunc(int x, int y) {
assert(SingleInstance());
SingleInstance()->Motion(x, y);
}
void KeyPress(unsigned char /*k*/) {
// TODO
}
static void KeyboardFunc(unsigned char k, int, int) {
if(k == 0x1B) // Escape
{
assert(SingleInstance());
SingleInstance()->Quit();
} else {
assert(SingleInstance());
SingleInstance()->KeyPress(k);
}
}
};
} // namespace oglplus
int glut_example_main(int argc, char** argv) {
GLuint width = 800, height = 600;
glutInit(&argc, argv);
glutInitDisplayMode(
#if defined(__APPLE__) && __APPLE__
GLUT_3_2_CORE_PROFILE |
#endif
GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
#if OGLPLUS_FREEGLUT_FOUND
glutInitContextVersion(OGLPLUS_GL_VERSION_MAJOR, OGLPLUS_GL_VERSION_MINOR);
#endif
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutCreateWindow("OGLplus example");
const char* screenshot_path = nullptr;
for(int a = 1; a != argc;) {
int aoffs = 0;
if(std::strcmp(argv[a], "--fullscreen") == 0) {
glutFullScreen();
aoffs = 1;
}
if(std::strcmp(argv[a], "--screenshot") == 0) {
if((a + 1) < argc) {
screenshot_path = argv[a + 1];
aoffs = 2;
} else {
screenshot_path = "screenshot.rgb";
aoffs = 1;
}
}
if(aoffs > 0) {
for(int ao = a + aoffs; ao != argc; ++ao) {
argv[ao - aoffs] = argv[ao];
}
argc -= aoffs;
} else
++a;
}
oglplus::GLAPIInitializer api_init;
// The parameters for this example
oglplus::ExampleParams params(argc, argv);
setupExample(params);
params.Check();
using oglplus::SingleExample;
// The main window/rendering context
if(screenshot_path) {
glutDisplayFunc(&SingleExample::ScreenshotFunc);
glutIdleFunc(&SingleExample::ScreenshotFunc);
} else {
glutDisplayFunc(&SingleExample::DisplayFunc);
glutIdleFunc(&SingleExample::DisplayFunc);
}
glutReshapeFunc(&SingleExample::ReshapeFunc);
glutMotionFunc(&SingleExample::MotionFunc);
glutPassiveMotionFunc(&SingleExample::MotionFunc);
glutKeyboardFunc(&SingleExample::KeyboardFunc);
#if OGLPLUS_FREEGLUT_FOUND
glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutCloseFunc(&SingleExample::CloseFunc);
#endif
// create the example
SingleExample example(width, height, params, screenshot_path);
// start the example main loop
glutMainLoop();
return 0;
}
int main(int argc, char* argv[]) {
return oglplus::example_main(glut_example_main, argc, argv);
}