Debian Codegraph @

cvd/gles1_helpers.h

raw symbols SCIP
1 #ifndef CVD_GLES1_HELPERS_H
2 #define CVD_GLES1_HELPERS_H
3
4 #include <iostream>
5 #include <map>
6 #include <utility>
7
8 #include <cvd/byte.h>
9 #include <cvd/config.h>
10 #include <cvd/image.h>
11 #include <cvd/image_ref.h>
12 #include <cvd/rgb.h>
13 #include <cvd/rgb8.h>
14 #include <cvd/rgba.h>
15 // #include <OpenGLES/ES1/gl.h>
16 #include <cvd/internal/gles1_types.h>
17
18 #ifdef CVD_HAVE_TOON
19 #include <TooN/TooN.h>
20 #include <TooN/se2.h>
21 #include <TooN/se3.h>
22 #include <TooN/so2.h>
23 #include <TooN/so3.h>
24 #endif
25
26 namespace CVD
27 {
28
29 /// add a translation specified by an ImageRef
30 /// @param v the translation ImageRef
31 /// @ingroup gGL
32 inline void glTranslate(const ImageRef& v)
33 {
34 glTranslatef(v.x, v.y, 0);
35 }
36
37 /// add a translation specified from the first three coordinates of a vector
38 /// @param v the translation vector
39 /// @ingroup gGL
40 template <int N, typename P, typename A>
41 inline void glTranslate(const TooN::Vector<N, P, A>& v)
42 {
43 glTranslatef(v[0], v[1], v[2]);
44 }
45
46 /// add a translation specified from the first two coordinates of a 2-vector
47 /// z is set to zero here
48 /// @param v the translation vector
49 /// @ingroup gGL
50 template <typename P, typename A>
51 inline void glTranslate(const TooN::Vector<2, P, A>& v)
52 {
53 glTranslatef(v[0], v[1], 0);
54 }
55
56 /// add a translation specified from the first coordinate of a 1-vector
57 /// Y and Z are zero here
58 /// @param v the translation vector
59 /// @ingroup gGL
60 template <typename P, typename A>
61 inline void glTranslate(const TooN::Vector<1, P, A>& v)
62 {
63 glTranslatef(v[0], 0, 0);
64 }
65
66 /// multiply a TooN matrix onto the current matrix stack. Works for matrizes
67 /// of size n >= 4 and uses the upper left 4x4 submatrix. The matrix is also
68 /// transposed to account for GL's column major format.
69 /// @param m the transformation matrix
70 /// @ingroup gGL
71 template <int N, class P, class A>
72 inline void glMultMatrix(const TooN::Matrix<N, N, P, A>& m)
73 {
74 GLfloat glm[16];
75 glm[0] = m[0][0];
76 glm[1] = m[1][0];
77 glm[2] = m[2][0];
78 glm[3] = m[3][0];
79 glm[4] = m[0][1];
80 glm[5] = m[1][1];
81 glm[6] = m[2][1];
82 glm[7] = m[3][1];
83 glm[8] = m[0][2];
84 glm[9] = m[1][2];
85 glm[10] = m[2][2];
86 glm[11] = m[3][2];
87 glm[12] = m[0][3];
88 glm[13] = m[1][3];
89 glm[14] = m[2][3];
90 glm[15] = m[3][3];
91 glMultMatrixf(glm);
92 }
93
94 /// multiply a TooN 3x3 matrix onto the current matrix stack. The GL matrix
95 /// last column and row are set to 0 with the lower right element to 1.
96 /// The matrix is also transposed to account for GL's column major format.
97 /// @param m the transformation matrix
98 /// @ingroup gGL
99 template <class P, class A>
100 inline void glMultMatrix(const TooN::Matrix<3, 3, P, A>& m)
101 {
102 GLfloat glm[16];
103 glm[0] = m[0][0];
104 glm[1] = m[1][0];
105 glm[2] = m[2][0];
106 glm[3] = 0;
107 glm[4] = m[0][1];
108 glm[5] = m[1][1];
109 glm[6] = m[2][1];
110 glm[7] = 0;
111 glm[8] = m[0][2];
112 glm[9] = m[1][2];
113 glm[10] = m[2][2];
114 glm[11] = 0;
115 glm[12] = 0;
116 glm[13] = 0;
117 glm[14] = 0;
118 glm[15] = 1;
119 glMultMatrixf(glm);
120 }
121
122 /// multiply a TooN 2x2 matrix onto the current matrix stack. The TooN matrix
123 /// will only occupy the upper left hand block, the remainder will be from the
124 /// identity matrix. The matrix is also transposed to account for GL's column major format.
125 /// @param m the transformation matrix
126 /// @ingroup gGL
127 template <class P, class A>
128 inline void glMultMatrix(const TooN::Matrix<2, 2, P, A>& m)
129 {
130 GLfloat glm[16];
131 glm[0] = m[0][0];
132 glm[1] = m[1][0];
133 glm[2] = 0;
134 glm[3] = 0;
135 glm[4] = m[0][1];
136 glm[5] = m[1][1];
137 glm[6] = 0;
138 glm[7] = 0;
139 glm[8] = 0;
140 glm[9] = 0;
141 glm[10] = 1;
142 glm[11] = 0;
143 glm[12] = 0;
144 glm[13] = 0;
145 glm[14] = 0;
146 glm[15] = 1;
147 glMultMatrixf(glm);
148 }
149
150 /// multiplies a SO3 onto the current matrix stack
151 /// @param so3 the SO3
152 /// @ingroup gGL
153 template <typename P>
154 inline void glMultMatrix(const TooN::SO3<P>& so3)
155 {
156 glMultMatrix(so3.get_matrix());
157 }
158
159 /// multiplies a SE3 onto the current matrix stack. This multiplies
160 /// the SO3 and the translation in order.
161 /// @param se3 the SE3
162 /// @ingroup gGL
163 template <typename P>
164 inline void glMultMatrix(const TooN::SE3<P>& se3)
165 {
166 glTranslate(se3.get_translation());
167 glMultMatrix(se3.get_rotation());
168 }
169
170 /// multiplies a SO2 onto the current matrix stack
171 /// @param so2 the SO2
172 /// @ingroup gGL
173 template <typename P>
174 inline void glMultMatrix(const TooN::SO2<P>& so2)
175 {
176 glMultMatrix(so2.get_matrix());
177 }
178
179 /// multiplies a SE2 onto the current matrix stack. This multiplies
180 /// the SO2 and the translation in order.
181 /// @param se3 the SE2
182 /// @ingroup gGL
183 template <typename P>
184 inline void glMultMatrix(const TooN::SE2<P>& se2)
185 {
186 glTranslate(se2.get_translation());
187 glMultMatrix(se2.get_rotation());
188 }
189
190 /// Sets up an ortho projection suitable for drawing onto individual pixels of a
191 /// gl window (or video image.) glVertex2f(0.0,0.0) will be the top left pixel and
192 /// glVertex2f(xsize-1.0, ysize-1.0) will be the bottom right pixel. Depth is set
193 /// from -1 to 1.
194 /// n.b. You first need to set up the matrix environment yourself,
195 /// e.g. glMatrixMode(GL_PROJECTION); glLoadIdentity();
196 /// @param size ImageRef containing the size of the GL window.
197 template <typename P>
198 inline void glOrtho(const CVD::ImageRef& size, const P nearPlane = -1.0, const P farPlane = 1.0)
199 {
200 ::glOrthof(-0.375, size.x - 0.375, size.y - 0.375, -0.375, nearPlane, farPlane);
201 }
202
203 /// Sets up an ortho projection from a simple Vector<6>
204 /// n.b. You first need to set up the matrix environment yourself,
205 /// e.g. glMatrixMode(GL_PROJECTION); glLoadIdentity();
206 /// @param param 6-vector containing the parameters of the projection
207 template <typename P, typename A>
208 inline void glOrtho(const TooN::Vector<6, P, A>& param)
209 {
210 ::glOrthof(param[0], param[1], param[2], param[3], param[4], param[5]);
211 }
212
213 /// sets a gl frustum from the linear camera parameters, image size and near and far plane.
214 /// The camera will be in OpenGL style with camera center in the origin and the viewing direction
215 /// down the negative z axis, with y pointing upwards and x pointing to the left and the image plane
216 /// at z=-1.
217 /// Images coordinates need to be rotated around the x axis to make sense here, because typically
218 /// the camera is described as y going down (pixel lines) and image plane at z=1.
219 /// @param params vector containing fu, fv, pu, pv as in the linear part of camera parameters
220 /// @param width width of the image plane in pixels, here the viewport for example
221 /// @param height height of the image plane in pixels, here the viewport for example
222 /// @param near near clipping plane
223 /// @param far far clipping plane
224 /// @ingroup gGL
225 template <typename P, typename A>
226 inline void glFrustum(const TooN::Vector<4, P, A>& params, const float width, const float height, const float nearPlane = 0.1, const float farPlane = 100)
227 {
228 GLfloat left, right, bottom, top;
229 left = -nearPlane * params[2] / params[0];
230 top = nearPlane * params[3] / params[1];
231 right = nearPlane * (width - params[2]) / params[0];
232 bottom = -nearPlane * (height - params[3]) / params[1];
233 ::glFrustumf(left, right, bottom, top, nearPlane, farPlane);
234 }
235
236 /// sets a gl frustum taking the first 4 parameters from the camera model. see @see glFrustum for
237 /// details on the created frustum.
238 /// @param camera camera supplying the parameters for the frustum
239 /// @param width width of the image plane in pixels, here the viewport for example
240 /// @param height height of the image plane in pixels, here the viewport for example
241 /// @param near near clipping plane
242 /// @param far far clipping plane
243 /// @ingroup gGL
244 template <class CAMERA>
245 inline void glFrustum(const CAMERA& camera, const float width, const float height, const float nearPlane = 0.1, const float farPlane = 100)
246 {
247 glFrustum(camera.get_parameters().template slice<0, 4>(), width, height, nearPlane, farPlane);
248 }
249
250 /// Sets up an ortho projection from a simple Vector<6>
251 /// n.b. You first need to set up the matrix environment yourself,
252 /// e.g. glMatrixMode(GL_PROJECTION); glLoadIdentity();
253 /// @param param 6-vector containing the parameters of the projection
254 template <typename P, typename A>
255 inline void glFrustum(const TooN::Vector<6, P, A>& param)
256 {
257 ::glFrustumf(param[0], param[1], param[2], param[3], param[4], param[5]);
258 }
259
260 /// Set the new colour to the red, green and blue components given in the Vector
261 /// (where 0.0 represents zero intensity and 1.0 full intensity)
262 /// @param v The new colour
263 /// @ingroup gGL
264 template <typename P, typename A>
265 inline void glColor(const TooN::Vector<3, P, A>& v)
266 {
267 glColor4f(v[0], v[1], v[2], 1);
268 }
269
270 /// Set the new colour to the red, green, blue and alpha components given in the Vector
271 /// (where 0.0 represents zero intensity and 1.0 full intensity)
272 /// @param v The new colour
273 ///@ingroup gGL
274 template <typename P, typename A>
275 inline void glColor(const TooN::Vector<4, P, A>& v)
276 {
277 glColor4f(v[0], v[1], v[2], v[3]);
278 }
279
280 /// Set the new clear colour to the red, green, blue and alpha components given in the Vector
281 /// (where 0.0 represents zero intensity and 1.0 full intensity)
282 /// @param v The new colour
283 ///@ingroup gGL
284 template <typename P, typename A>
285 inline void glClearColor(const TooN::Vector<4, P, A>& v)
286 {
287 ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], (GLclampf)v[3]);
288 }
289
290 /// Set the new clear colour to the red, green, blue components given in the Vector
291 /// alpha is set to 1
292 /// @param v The new colour
293 ///@ingroup gGL
294 template <typename P, typename A>
295 inline void glClearColor(const TooN::Vector<3, P, A>& v)
296 {
297 ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], 1);
298 }
299
300 /// glColor version for dynamic TooN::Vector, will test for 3 or 4 components
301 /// @param v The new colour
302 /// @ingroup gGL
303 template <typename P, typename A>
304 inline void glColor(const TooN::Vector<-1, P, A>& v)
305 {
306 switch(v.size())
307 {
308 case 3:
309 glColor4f(v[0], v[1], v[2], 1);
310 break;
311 case 4:
312 glColor4f(v[0], v[1], v[2], v[3]);
313 break;
314 }
315 }
316
317 /// Set the new colour to the red, green, blue components given
318 /// (where 0 represents zero intensity and 255 full intensity)
319 /// @param c The new colour
320 ///@ingroup gGL
321 inline void glColor(const CVD::Rgb<byte>& c)
322 {
323 glColor4ub(c.red, c.green, c.blue, 255);
324 }
325
326 /// Set the new colour to the red, green and blue components given
327 /// (where 0.0 represents zero intensity and 1.0 full intensity)
328 /// @param c The new colour
329 ///@ingroup gGL
330 inline void glColor(const CVD::Rgb<float>& c)
331 {
332 glColor4f(c.red, c.green, c.blue, 1);
333 }
334
335 /// Set the new colour to the red, green and blue components given
336 /// (where 0.0 represents zero intensity and 255 full intensity). The Rgb8::dummy member is ignored
337 /// @param c The new colour
338 ///@ingroup gGL
339 inline void glColor3(const CVD::Rgb8& c)
340 {
341 glColor4ub(c.red, c.green, c.blue, 255);
342 }
343
344 /// Set the new colour to the red, green, blue and alpha components given
345 /// (where 0.0 represents zero intensity and 255 full intensity)
346 /// @param c The new colour
347 ///@ingroup gGL
348 inline void glColor4(const CVD::Rgb8& c)
349 {
350 glColor4ub(c.red, c.green, c.blue, c.dummy);
351 }
352
353 /// Set the new colour to the red, green, blue and alpha components given
354 /// (where 0 represents zero intensity and 255 full intensity)
355 /// @param c The new colour
356 ///@ingroup gGL
357 inline void glColor(const CVD::Rgba<unsigned char>& c)
358 {
359 glColor4ub(c.red, c.green, c.blue, c.alpha);
360 }
361
362 /// Set the new colour to the red, green, blue and alpha components given
363 /// (where 0.0 represents zero intensity and 1.0 full intensity)
364 /// @param c The new colour
365 ///@ingroup gGL
366 inline void glColor(const CVD::Rgba<float>& c)
367 {
368 glColor4f(c.red, c.green, c.blue, c.alpha);
369 }
370
371 /// Read the current image from the colour buffer specified by glReadBuffer
372 /// @param i The image to write the image data into. This must already be initialised to be an BasicImage (or Image) of the right size.
373 /// @param origin The window co-ordinate of the first pixel to be read from the frame buffer
374 ///@ingroup gGL
375 template <class C>
376 inline void glReadPixels(BasicImage<C>& i, const ImageRef origin = ImageRef(0, 0))
377 {
378 ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
379 }
380
381 /// Read the current image from the colour buffer specified by glReadBuffer
382 /// @param size The size of the area to read.
383 /// @param origin The window co-ordinate of the first pixel to be read from the frame buffer
384 ///@ingroup gGL
385 template <class C>
386 inline Image<C> glReadPixels(ImageRef size, const ImageRef origin = ImageRef(0, 0))
387 {
388 Image<C> i(size);
389 ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
390 return i;
391 }
392
393 /// Sets an image as a texture sub region.
394 /// note the reordering of the various parameters to make better use of default parameters
395 /// @param i the image to set as texture
396 /// @ingroup gGL
397 template <class C>
398 inline void glTexBasicImage2D(const BasicImage<C>& i, GLint xoffset = 0, GLint yoffset = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
399 {
400 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
401 ::glTexBasicImage2D(target, level, xoffset, yoffset, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
402 }
403
404 /// Sets an image as a texture.
405 /// note the reordering of the various parameters to make better use of default parameters
406 /// @param i the image to set as texture
407 /// @ingroup gGL
408 template <class C>
409 inline void glTexImage2D(const BasicImage<C>& i, GLint border = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
410 {
411 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
412 ::glTexImage2D(target, level, gl::data<C>::format, i.size().x, i.size().y, border, gl::data<C>::format, gl::data<C>::type, i.data());
413 }
414
415 /// Prints the current errors on the gl error stack
416 ///@ingroup gGL
417 inline void glPrintErrors(void)
418 {
419 GLenum code;
420 while((code = glGetError()) != GL_NO_ERROR)
421 {
422 std::cout << "GL:" << code << std::endl;
423 }
424 }
425
426 /// @defgroup gGLText OpenGL text rendering
427 /// @ingroup gGL
428 /// @{
429
430 /// sets the font to use for future font rendering commands. currently sans, serif and mono are available.
431 /// @param fontname string containing font name
432 void glSetFont(const std::string& fontname);
433
434 /// returns the name of the currently active font
435 const std::string& glGetFont();
436
437 /// different style for font rendering
438 enum TEXT_STYLE
439 {
440 FILL = 0, ///< renders glyphs as filled polygons
441 OUTLINE = 1, ///< renders glyphs as outlines with GL_LINES
442 NICE = 2 ///< renders glyphs filled with antialiased outlines
443 };
444
445 /// renders a string in GL using the current settings.
446 /// Font coordinates are +X along the line and +Y along the up direction of glyphs.
447 /// The origin is at the top baseline at the left of the first character. Characters have a maximum size of 1.
448 /// linefeed is interpreted as a new line and the start is offset in -Y direction by @ref spacing . Individual characters
449 /// are separated by @ref kerning + plus their individual with.
450 /// @param text string to be rendered, unknown characters are replaced with '?'
451 /// @param style rendering style
452 /// @param spacing distance between individual text lines
453 /// @param kerning distance between characters
454 std::pair<double, double> glDrawText(const std::string& text, enum TEXT_STYLE style = NICE, double spacing = 1.5, double kerning = 0.1);
455
456 /// returns the size of the bounding box of a text to be rendered, similar to @ref glDrawText but without any visual output
457 std::pair<double, double> glGetExtends(const std::string& text, double spacing = 1.5, double kerning = 0.1);
458
459 ///@}
460
461 };
462
463 #endif