MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
context.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef OGL_CONTEXT_HEADER
11#define OGL_CONTEXT_HEADER
12
13#include <algorithm>
14
15#include "ogl/defines.h"
16#include "ogl/events.h"
17#include "ogl/camera.h"
19#include "ogl/camera_2d.h"
20
22
36{
37public:
38 virtual ~Context (void);
39
41 void init (void);
43 void resize (int new_width, int new_height);
45 void paint (void);
46
51 virtual bool mouse_event (MouseEvent const& event);
52
57 virtual bool keyboard_event (KeyboardEvent const& event);
58
60 int get_width (void) const;
62 int get_height (void) const;
63
64protected:
66 virtual void init_impl (void) = 0;
68 virtual void resize_impl (int old_width, int old_height) = 0;
70 virtual void paint_impl (void) = 0;
71
72protected:
73 int width;
74 int height;
75};
76
77/* ---------------------------------------------------------------- */
78
79template <typename CTRL> class CameraContext;
82
101template <typename CTRL>
102class CameraContext : public Context
103{
104protected:
107
108protected:
109 void resize_impl (int old_width, int old_height);
110 void update_camera (void);
111
112public:
113 CameraContext (void);
114 ~CameraContext (void);
115 bool keyboard_event (KeyboardEvent const& event);
116 bool mouse_event (MouseEvent const& event);
117};
118
119/* ------------------------- Implementation ----------------------- */
120
121inline
122Context::~Context (void)
123{
124}
125
126inline void
127Context::init (void)
128{
129 this->init_impl();
130}
131
132inline void
133Context::resize (int new_width, int new_height)
134{
135 std::swap(new_width, this->width);
136 std::swap(new_height, this->height);
137 this->resize_impl(new_height, new_height);
138}
139
140inline void
141Context::paint (void)
142{
143 this->paint_impl();
144}
145
146inline bool
147Context::mouse_event (MouseEvent const& event)
148{
150 return true;
151}
152
153inline bool
154Context::keyboard_event (KeyboardEvent const& event)
155{
157 return true;
158}
159
160inline int
161Context::get_width (void) const
162{
163 return this->width;
164}
165
166inline int
167Context::get_height (void) const
168{
169 return this->height;
170}
171
172/* ---------------------------------------------------------------- */
173
174template <typename CTRL>
175inline
177{
178 this->controller.set_camera(&this->camera);
179}
180
181template <typename CTRL>
182inline
186
187template <typename CTRL>
188void
189CameraContext<CTRL>::resize_impl (int /*old_width*/, int /*old_height*/)
190{
191 /* Always use full viewport. */
192 glViewport(0, 0, this->width, this->height);
193 this->camera.width = this->width;
194 this->camera.height = this->height;
195
196 /* Calculate proper top/bottom and left/right planes. */
197 float aspect = (float)this->width / (float)this->height;
198 float minside = 0.05f;
199 if (this->width > this->height)
200 {
201 this->camera.top = minside;
202 this->camera.right = minside * aspect;
203 }
204 else
205 {
206 this->camera.top = minside / aspect;
207 this->camera.right = minside;
208 }
209
210 /* Make sure the camera gets recent values. */
211 this->camera.update_proj_mat();
212 this->camera.update_inv_proj_mat();
213}
214
215template <typename CTRL>
216void
218{
219 /* Set camera viewing matrix values. */
220 this->camera.pos = this->controller.get_campos();
221 this->camera.viewing_dir = this->controller.get_viewdir();
222 this->camera.up_vec = this->controller.get_upvec();
223 this->camera.update_view_mat();
224 this->camera.update_inv_view_mat();
225 //this->camera.update_matrices();
226}
227
228template <typename CTRL>
229bool
231{
232 bool is_handled = this->controller.consume_event(event);
233 this->update_camera();
234 return is_handled;
235}
236
237template <typename CTRL>
238bool
240{
241 bool is_handled = this->controller.consume_event(event);
242 this->update_camera();
243 return is_handled;
244}
245
247
248#endif /* OGL_CONTEXT_HEADER */
A simple context that does some of the common annoying work.
Definition context.h:103
A camera class that manages viewing and projection matrices.
Definition camera.h:33
Abstraction of a rendering context/viewport that displays renderings.
Definition context.h:36
virtual void paint_impl(void)=0
Overwrite to define actions on paint.
virtual void init_impl(void)=0
Overwrite to define actions on init.
virtual void resize_impl(int old_width, int old_height)=0
Overwrite to define actions on resize.
void event_debug_print(ogl::MouseEvent const &event)
Prints debug information for mouse event 'e' to STDOUT.
Definition events.cc:18
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478
#define OGL_NAMESPACE_END
Definition defines.h:14
#define OGL_NAMESPACE_BEGIN
Definition defines.h:13
Keyboard event.
Definition events.h:57
Mouse event.
Definition events.h:40