OGLplus (0.52.0) a C++ wrapper for OpenGL

example.hpp
Go to the documentation of this file.
1 
9 #ifndef __OGLPLUS_EXAMPLE_EXAMPLE_1119071146_HPP__
10 #define __OGLPLUS_EXAMPLE_EXAMPLE_1119071146_HPP__
11 
12 #include <set>
13 #include <memory>
14 #include <cassert>
15 
16 #ifndef _NDEBUG
17 #include <iostream>
18 #endif
19 
20 #ifdef _MSC_VER
21 #pragma warning ( disable : 4244 )
22 #pragma warning ( disable : 4305 )
23 #endif //_MSC_VER
24 
25 namespace oglplus {
26 
29 {
31  int argc;
32 
34  char ** argv;
35 
37  float quality;
38 
40  unsigned num_gpus;
41 
43  unsigned num_threads;
44 
46  unsigned max_threads;
47 
49  std::set<unsigned> compat_context_threads;
50 
51  ExampleParams(int argn, char ** args)
52  : argc(argn)
53  , argv(args)
54  , quality(0.5f)
55  , num_gpus(1)
56  , num_threads(0)
57  , max_threads(0)
58  { }
59 
60  void Check(void)
61  {
62  assert(num_threads <= max_threads);
63  }
64 
65  bool HighQuality(void) const
66  {
67  return quality > 0.9f;
68  }
69 };
70 
71 void setupExample(ExampleParams& params);
72 
75 {
76 private:
77  double _time;
78 public:
79  ExampleTimePeriod(double time)
80  : _time(time)
81  {
82  assert(_time >= 0.0);
83  }
84 
86  double Seconds(void) const
87  {
88  return _time;
89  }
90 
92  int Second(void) const
93  {
94  return int(Seconds()) % 60;
95  }
96 
98  double Minutes(void) const
99  {
100  return _time / 60.0;
101  }
102 
104  int Minute(void) const
105  {
106  return int(Minutes()) % 60;
107  }
108 
110  double Hours(void) const
111  {
112  return _time / 3600.0;
113  }
114 
116  int Hour(void) const
117  {
118  return int(Hours()) % 24;
119  }
120 
122  double Days(void) const
123  {
124  return _time / (24*3600.0);
125  }
126 
128  int Day(void) const
129  {
130  return int(Days());
131  }
132 };
133 
136 {
137 private:
138  double _start, _past, _real_time, _curr_time, _prev_time, _pace; //[s]
139 public:
140  ExampleClock(double start = 0.0)
141  : _start(start)
142  , _past(start)
143  , _real_time(start)
144  , _curr_time(start)
145  , _prev_time(start)
146  , _pace(1.0)
147  { }
148 
150  void Update(double real_time)
151  {
152  _prev_time = _curr_time;
153  _real_time = real_time;
154  _curr_time = _past + (_real_time - _start) * _pace;
155  }
156 
158  void Advance(double seconds)
159  {
160  _prev_time = _curr_time;
161  _real_time += seconds;
162  _curr_time = _past + (_real_time - _start) * _pace;
163  }
164 
166  void Pace(double pace)
167  {
168  if(_pace != pace)
169  {
170  _start = _real_time;
171  _past = _curr_time;
172  _pace = pace;
173  }
174  }
175 
177  double Time(void) const
178  {
179  return this->Now().Seconds();
180  }
181 
184  {
185  return ExampleTimePeriod(_real_time);
186  }
187 
189  ExampleTimePeriod Now(void) const
190  {
191  return ExampleTimePeriod(_curr_time);
192  }
193 
196  {
197  return ExampleTimePeriod(_curr_time-_prev_time);
198  }
199 };
200 
201 class Example;
202 
205 {
206 public:
207  virtual ~ExampleThread(void)
208  { }
209 
211 
215  virtual void Cancel(void){ }
216 
218  virtual void Render(double /*time*/)
219  {
220  assert(!"Render must be overloaded by examples!");
221  }
222 
224  virtual void Render(const ExampleClock& clock)
225  {
226  this->Render(clock.Now().Seconds());
227  }
228 
230  virtual double RenderPart(
231  unsigned /*part_no*/,
232  const ExampleClock& clock
233  )
234  {
235  this->Render(clock);
236  return 1.0;
237  }
238 };
239 
240 std::unique_ptr<ExampleThread> makeExampleThread(
241  Example& example,
242  unsigned thread_id,
243  const ExampleParams& params
244 );
245 
247 class Example
248 {
249 public:
250  virtual ~Example(void)
251  { }
252 
254 
258  virtual void PrepareThread(unsigned, ExampleThread&)
259  {
260  }
261 
263 
267  virtual bool Continue(double duration)
268  {
269  return duration < 3.0; // [seconds]
270  }
271 
273 
277  virtual bool Continue(const ExampleClock& clock)
278  {
279  return this->Continue(clock.Now().Seconds());
280  }
281 
283  virtual void Reshape(GLuint width, GLuint height) = 0;
284 
286  virtual void MouseMoveNormalized(float x, float y, float aspect)
287  {
288  (void)(x+y+aspect);
289  }
290 
292  virtual void MouseMove(GLuint x, GLuint y, GLuint width, GLuint height)
293  {
294  return MouseMoveNormalized(
295  (float(x) - width * 0.5f) / (width * 0.5f),
296  (float(y) - height* 0.5f) / (height* 0.5f),
297  float(width)/height
298  );
299  }
300 
302  virtual void Render(double /*time*/)
303  {
304  assert(!"Render must be overloaded by examples!");
305  }
306 
308  virtual void Render(ExampleClock& clock)
309  {
310  this->Render(clock.Now().Seconds());
311  }
312 
314  virtual double RenderPart(unsigned /*part_no*/, ExampleClock& clock)
315  {
316  this->Render(clock);
317  return 1.0;
318  }
319 
321  virtual double ScreenshotTime(void) const
322  {
323  return 1.0; // [s]
324  }
325 
327  virtual double FrameTime(void) const
328  {
329  return 1.0/25.0;
330  }
331 
333  virtual GLuint HeatUpFrames(void) const
334  {
335  return 5;
336  }
337 
339  virtual double HeatUpTime(void) const
340  {
342  }
343 };
344 
345 std::unique_ptr<Example> makeExample(const ExampleParams& params);
346 
347 } // namespace oglplus
348 
349 #endif // include guard
int Hour(void) const
The current minute of the period <0-24)
Definition: example.hpp:116
Base class for OGLplus example offscreen rendering threads.
Definition: example.hpp:204
virtual double HeatUpTime(void) const
The screenshot capture heat-up sequence start time.
Definition: example.hpp:339
void Update(double real_time)
Update the clock by providing real time.
Definition: example.hpp:150
virtual void MouseMove(GLuint x, GLuint y, GLuint width, GLuint height)
Mouse move event handler.
Definition: example.hpp:292
char ** argv
the arguments strings
Definition: example.hpp:34
virtual double ScreenshotTime(void) const
The time of the default screenshot.
Definition: example.hpp:321
virtual void Render(double)
Rendering procedure with simple timing.
Definition: example.hpp:218
virtual void Render(ExampleClock &clock)
Rendering procedure with advanced timing.
Definition: example.hpp:308
virtual double RenderPart(unsigned, ExampleClock &clock)
Rendering procedure split into several parts.
Definition: example.hpp:314
double Minutes(void) const
The length of the periods in minutes.
Definition: example.hpp:98
double Hours(void) const
The length of the periods in hours.
Definition: example.hpp:110
int Second(void) const
The current second of the period <0-60)
Definition: example.hpp:92
virtual bool Continue(double duration)
Hint for the main function whether to continue rendering.
Definition: example.hpp:267
virtual void Render(const ExampleClock &clock)
Rendering procedure with advanced timing.
Definition: example.hpp:224
Run-time parameters for example instances.
Definition: example.hpp:28
ExampleTimePeriod RealTime(void) const
Returns the real time elapsed since the start of the example.
Definition: example.hpp:183
virtual void MouseMoveNormalized(float x, float y, float aspect)
Mouse move event handler.
Definition: example.hpp:286
double Days(void) const
The length of the period in days.
Definition: example.hpp:122
virtual void PrepareThread(unsigned, ExampleThread &)
Place to do additional thread-related initialization of example.
Definition: example.hpp:258
int Minute(void) const
The current minute of the period <0-60)
Definition: example.hpp:104
virtual GLuint HeatUpFrames(void) const
The number of heat-up sequence frames.
Definition: example.hpp:333
std::set< unsigned > compat_context_threads
The set of threads for which a compatibility context should be created.
Definition: example.hpp:49
float quality
The quality of rendered image (0.0 = low, 0.5 = default, 1.0 = high)
Definition: example.hpp:37
ExampleTimePeriod Now(void) const
Returns the simulation time elapsed since the start of the example.
Definition: example.hpp:189
A class measuring a time period in the examples.
Definition: example.hpp:74
void Pace(double pace)
Set the pace by which the sim. time advances compared to real-time.
Definition: example.hpp:166
double Seconds(void) const
The length of the periods in seconds.
Definition: example.hpp:86
virtual void Cancel(void)
Notifies the thread that the example is closing.
Definition: example.hpp:215
virtual double RenderPart(unsigned, const ExampleClock &clock)
Rendering procedure split into several parts.
Definition: example.hpp:230
virtual void Reshape(GLuint width, GLuint height)=0
Reshape event handler.
unsigned num_gpus
The number of available GPUs.
Definition: example.hpp:40
Class measuring the simulation time of an Example.
Definition: example.hpp:135
int argc
the number of arguments passed on command-line
Definition: example.hpp:31
virtual double FrameTime(void) const
Single frame time in the screenshot capture sequence.
Definition: example.hpp:327
double Time(void) const
Equivalent to Now().Seconds()
Definition: example.hpp:177
ExampleTimePeriod Interval(void) const
Returns the time elapsed between the last and the previous Update.
Definition: example.hpp:195
void Advance(double seconds)
Advances the clock.
Definition: example.hpp:158
virtual void Render(double)
Rendering procedure with simple timing.
Definition: example.hpp:302
Base class for OGLplus examples.
Definition: example.hpp:247
unsigned num_threads
The number of offscreen rendering threads (not counting the main thread)
Definition: example.hpp:43
unsigned max_threads
The maximum number of threads.
Definition: example.hpp:46
virtual bool Continue(const ExampleClock &clock)
Hint for the main function whether to continue rendering.
Definition: example.hpp:277
int Day(void) const
The current day of the period.
Definition: example.hpp:128

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).