Debian Codegraph @

cvd/gl_helpers.h

raw symbols SCIP
1 #ifndef CVD_GL_HELPERS_H
2 #define CVD_GL_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 #ifdef WIN32
16 #define NOMINMAX
17 #include <windows.h>
18 #endif
19
20 #ifdef __APPLE__
21 #include <OpenGL/gl.h>
22 #include <OpenGL/glu.h>
23 #else
24 #include <GL/gl.h>
25 #include <GL/glu.h>
26 #endif
27
28 #include <cvd/internal/gl_types.h>
29
30 #ifdef CVD_HAVE_TOON
31 #include <TooN/TooN.h>
32 #include <TooN/se2.h>
33 #include <TooN/se3.h>
34 #include <TooN/so2.h>
35 #include <TooN/so3.h>
36 #endif
37
38 namespace CVD
39 {
40
41 /// Specify the (x,y) co-ordinates of a vertex
42 /// @param i The vertex location
43 ///@ingroup gGL
44 inline void glVertex(const ImageRef& i)
45 {
46 glVertex2i(i.x, i.y);
47 }
48
49 /// Specify the (s,t) texture co-ordinates
50 /// @param i The texture coordinates
51 ///@ingroup gGL
52 inline void glTexCoord(const ImageRef& i)
53 {
54 glTexCoord2i(i.x, i.y);
55 }
56
57 #ifdef GL_GLEXT_PROTOTYPES
58 /// Specify the (s,t) texture co-ordinates for a specific texture unit
59 /// @param i The texture coordinates
60 /// @param enum The texture unit
61 ///@ingroup gGL
62 inline void glMultiTexCoord(GLenum unit, const ImageRef& i)
63 {
64 glMultiTexCoord2i(unit, i.x, i.y);
65 }
66 #endif
67 /// Specify the (x,y) co-ordinates of the current raster position
68 /// @param i The raster position
69 ///@ingroup gGL
70 inline void glRasterPos(const ImageRef& i)
71 {
72 glRasterPos2i(i.x, i.y);
73 }
74
75 /// Draws a rectangle by specifing two opposing vertices
76 /// @param p the first vertex
77 /// @param q the second vertex
78 /// @ingroup gGL
79 inline void glRect(const ImageRef& p, const ImageRef& q)
80 {
81 glRecti(p.x, p.y, q.x, q.y);
82 }
83
84 #ifdef CVD_HAVE_TOON
85 /// Specify the (x,y) co-ordinates of a vertex
86 /// @param v The vertex location
87 ///@ingroup gGL
88 template <class B>
89 inline void glVertex(const TooN::Vector<2, double, B>& v)
90 {
91 glVertex2d(v[0], v[1]);
92 }
93
94 /// Specify the (x,y) co-ordinates of a vertex
95 /// @param v The vertex location
96 ///@ingroup gGL
97 template <class B>
98 inline void glVertex(const TooN::Vector<2, float, B>& v)
99 {
100 glVertex2f(v[0], v[1]);
101 }
102
103 /// Specify the (x,y) co-ordinates of a vertex
104 /// @param v The vertex location
105 ///@ingroup gGL
106 template <class B>
107 inline void glVertex(const TooN::Vector<2, int, B>& v)
108 {
109 glVertexi(v[0], v[1]);
110 }
111
112 /// Specify the (x,y,z) co-ordinates of a vertex
113 /// @param v The vertex location
114 ///@ingroup gGL
115 template <class B>
116 inline void glVertex(const TooN::Vector<3, double, B>& v)
117 {
118 glVertex3d(v[0], v[1], v[2]);
119 }
120
121 /// Specify the (x,y,z) co-ordinates of a vertex
122 /// @param v The vertex location
123 ///@ingroup gGL
124 template <class B>
125 inline void glVertex(const TooN::Vector<3, float, B>& v)
126 {
127 glVertex3f(v[0], v[1], v[2]);
128 }
129
130 /// Specify the (x,y,z) co-ordinates of a vertex
131 /// @param v The vertex location
132 ///@ingroup gGL
133 template <class B>
134 inline void glVertex(const TooN::Vector<3, int, B>& v)
135 {
136 glVertex3i(v[0], v[1], v[2]);
137 }
138
139 /// Specify the (x,y,z,w) co-ordinates of a vertex
140 /// @param v The vertex location
141 ///@ingroup gGL
142 template <class B>
143 inline void glVertex(const TooN::Vector<4, double, B>& v)
144 {
145 glVertex4d(v[0], v[1], v[2], v[3]);
146 }
147
148 /// Specify the (x,y,z,w) co-ordinates of a vertex
149 /// @param v The vertex location
150 ///@ingroup gGL
151 template <class B>
152 inline void glVertex(const TooN::Vector<4, float, B>& v)
153 {
154 glVertex4f(v[0], v[1], v[2], v[3]);
155 }
156
157 /// Specify the (x,y,z,w) co-ordinates of a vertex
158 /// @param v The vertex location
159 ///@ingroup gGL
160 template <class B>
161 inline void glVertex(const TooN::Vector<4, int, B>& v)
162 {
163 glVertex4i(v[0], v[1], v[2], v[3]);
164 }
165
166 /// Specify the (s,t) texture coordinates
167 /// @param v The texture coordinates
168 ///@ingroup gGL
169 inline void glTexCoord(const TooN::Vector<2>& v)
170 {
171 glTexCoord2d(v[0], v[1]);
172 }
173
174 /// Specify the (s,t,r) texture coordinates
175 /// @param v The texture coordinates
176 ///@ingroup gGL
177 inline void glTexCoord(const TooN::Vector<3>& v)
178 {
179 glTexCoord3d(v[0], v[1], v[2]);
180 }
181
182 /// Specify the (s,t,r,q) texture coordinates
183 /// @param v The texture coordinates
184 ///@ingroup gGL
185 inline void glTexCoord(const TooN::Vector<4>& v)
186 {
187 glTexCoord4d(v[0], v[1], v[2], v[3]);
188 }
189
190 /// Draws a rectangle by specifing two opposing vertices
191 /// @param p the first vertex
192 /// @param q the second vertex
193 /// @ingroup gGL
194 inline void glRect(const TooN::Vector<2>& p, const TooN::Vector<2>& q)
195 {
196 glRectd(p[0], p[1], q[0], q[1]);
197 }
198
199 #ifdef GL_GLEXT_PROTOTYPES
200 /// Specify the (s,t) texture coordinates for a specific texture unit
201 /// @param v The texture coordinates
202 /// @param unit The texture unit
203 ///@ingroup gGL
204 inline void glMultiTexCoord(GLenum unit, const TooN::Vector<2>& v)
205 {
206 glMultiTexCoord2d(unit, v[0], v[1]);
207 }
208
209 /// Specify the (s,t,r) texture coordinates for a specific texture unit
210 /// @param v The texture coordinates
211 /// @param unit The texture unit
212 ///@ingroup gGL
213 inline void glMultiTexCoord(GLenum unit, const TooN::Vector<3>& v)
214 {
215 glMultiTexCoord3d(unit, v[0], v[1], v[2]);
216 }
217
218 /// Specify the (s,t,r,q) texture coordinates for a specific texture unit
219 /// @param v The texture coordinates
220 /// @param unit The texture unit
221 ///@ingroup gGL
222 inline void glMultiTexCoord(GLenum unit, const TooN::Vector<4>& v)
223 {
224 glMultiTexCoord4d(unit, v[0], v[1], v[2], v[3]);
225 }
226 #endif
227
228 /// Specify the (x,y) co-ordinates of the current raster position
229 /// @param v The raster position
230 ///@ingroup gGL
231 inline void glRasterPos(const TooN::Vector<2>& v)
232 {
233 glRasterPos2d(v[0], v[1]);
234 }
235
236 /// Specify the (x,y,z) co-ordinates of the current raster position
237 /// @param v The raster position
238 ///@ingroup gGL
239 inline void glRasterPos(const TooN::Vector<3>& v)
240 {
241 glRasterPos3d(v[0], v[1], v[2]);
242 }
243
244 /// Specify the (x,y,z,w) co-ordinates of the current raster position
245 /// @param v The raster position
246 ///@ingroup gGL
247 inline void glRasterPos(const TooN::Vector<4>& v)
248 {
249 glRasterPos4d(v[0], v[1], v[2], v[3]);
250 }
251
252 /// Specify the current vertex normal
253 /// @param n The normal vector
254 ///@ingroup gGL
255 inline void glNormal(const TooN::Vector<3>& n)
256 {
257 glNormal3d(n[0], n[1], n[2]);
258 }
259
260 /// add a translation specified by an ImageRef
261 /// @param v the translation ImageRef
262 /// @ingroup gGL
263 inline void glTranslate(const ImageRef& v)
264 {
265 glTranslatef(static_cast<GLfloat>(v.x), static_cast<GLfloat>(v.y), 0);
266 }
267
268 /// add a translation specified from the first three coordinates of a vector
269 /// @param v the translation vector
270 /// @ingroup gGL
271 template <int N>
272 inline void glTranslate(const TooN::Vector<N>& v)
273 {
274 glTranslated(v[0], v[1], v[2]);
275 }
276
277 /// add a translation specified from the first two coordinates of a 2-vector
278 /// z is set to zero here
279 /// @param v the translation vector
280 /// @ingroup gGL
281 template <>
282 inline void glTranslate(const TooN::Vector<2>& v)
283 {
284 glTranslated(v[0], v[1], 0);
285 }
286
287 /// add a translation specified from the first coordinate of a 1-vector
288 /// Y and Z are zero here
289 /// @param v the translation vector
290 /// @ingroup gGL
291 template <>
292 inline void glTranslate(const TooN::Vector<1>& v)
293 {
294 glTranslated(v[0], 0, 0);
295 }
296
297 /// multiply a TooN matrix onto the current matrix stack. Works for matrizes
298 /// of size n >= 4 and uses the upper left 4x4 submatrix. The matrix is also
299 /// transposed to account for GL's column major format.
300 /// @param m the transformation matrix
301 /// @ingroup gGL
302 template <int N, class P, class A>
303 inline void glMultMatrix(const TooN::Matrix<N, N, P, A>& m)
304 {
305 GLdouble glm[16];
306 glm[0] = m[0][0];
307 glm[1] = m[1][0];
308 glm[2] = m[2][0];
309 glm[3] = m[3][0];
310 glm[4] = m[0][1];
311 glm[5] = m[1][1];
312 glm[6] = m[2][1];
313 glm[7] = m[3][1];
314 glm[8] = m[0][2];
315 glm[9] = m[1][2];
316 glm[10] = m[2][2];
317 glm[11] = m[3][2];
318 glm[12] = m[0][3];
319 glm[13] = m[1][3];
320 glm[14] = m[2][3];
321 glm[15] = m[3][3];
322 glMultMatrixd(glm);
323 }
324
325 /// multiply a TooN 3x3 matrix onto the current matrix stack. The GL matrix
326 /// last column and row are set to 0 with the lower right element to 1.
327 /// The matrix is also transposed to account for GL's column major format.
328 /// @param m the transformation matrix
329 /// @ingroup gGL
330 template <class P, class A>
331 inline void glMultMatrix(const TooN::Matrix<3, 3, P, A>& m)
332 {
333 GLdouble glm[16];
334 glm[0] = m[0][0];
335 glm[1] = m[1][0];
336 glm[2] = m[2][0];
337 glm[3] = 0;
338 glm[4] = m[0][1];
339 glm[5] = m[1][1];
340 glm[6] = m[2][1];
341 glm[7] = 0;
342 glm[8] = m[0][2];
343 glm[9] = m[1][2];
344 glm[10] = m[2][2];
345 glm[11] = 0;
346 glm[12] = 0;
347 glm[13] = 0;
348 glm[14] = 0;
349 glm[15] = 1;
350 glMultMatrixd(glm);
351 }
352
353 /// multiply a TooN 2x2 matrix onto the current matrix stack. The TooN matrix
354 /// will only occupy the upper left hand block, the remainder will be from the
355 /// identity matrix. The matrix is also transposed to account for GL's column major format.
356 /// @param m the transformation matrix
357 /// @ingroup gGL
358 template <class P, class A>
359 inline void glMultMatrix(const TooN::Matrix<2, 2, P, A>& m)
360 {
361 GLdouble glm[16];
362 glm[0] = m[0][0];
363 glm[1] = m[1][0];
364 glm[2] = 0;
365 glm[3] = 0;
366 glm[4] = m[0][1];
367 glm[5] = m[1][1];
368 glm[6] = 0;
369 glm[7] = 0;
370 glm[8] = 0;
371 glm[9] = 0;
372 glm[10] = 1;
373 glm[11] = 0;
374 glm[12] = 0;
375 glm[13] = 0;
376 glm[14] = 0;
377 glm[15] = 1;
378 glMultMatrixd(glm);
379 }
380
381 /// multiplies a SO3 onto the current matrix stack
382 /// @param so3 the SO3
383 /// @ingroup gGL
384 template <typename P>
385 inline void glMultMatrix(const TooN::SO3<P>& so3)
386 {
387 glMultMatrix(so3.get_matrix());
388 }
389
390 /// multiplies a SE3 onto the current matrix stack. This multiplies
391 /// the SO3 and the translation in order.
392 /// @param se3 the SE3
393 /// @ingroup gGL
394 template <typename P>
395 inline void glMultMatrix(const TooN::SE3<P>& se3)
396 {
397 glTranslate(se3.get_translation());
398 glMultMatrix(se3.get_rotation());
399 }
400
401 /// multiplies a SO2 onto the current matrix stack
402 /// @param so2 the SO2
403 /// @ingroup gGL
404 template <typename P>
405 inline void glMultMatrix(const TooN::SO2<P>& so2)
406 {
407 glMultMatrix(so2.get_matrix());
408 }
409
410 /// multiplies a SE2 onto the current matrix stack. This multiplies
411 /// the SO2 and the translation in order.
412 /// @param se3 the SE2
413 /// @ingroup gGL
414 template <typename P>
415 inline void glMultMatrix(const TooN::SE2<P>& se2)
416 {
417 glTranslate(se2.get_translation());
418 glMultMatrix(se2.get_rotation());
419 }
420
421 /// Sets up an ortho projection suitable for drawing onto individual pixels of a
422 /// gl window (or video image.) glVertex2f(0.0,0.0) will be the top left pixel and
423 /// glVertex2f(xsize-1.0, ysize-1.0) will be the bottom right pixel. Depth is set
424 /// from -1 to 1.
425 /// n.b. You first need to set up the matrix environment yourself,
426 /// e.g. glMatrixMode(GL_PROJECTION); glLoadIdentity();
427 /// @param size ImageRef containing the size of the GL window.
428 inline void glOrtho(const CVD::ImageRef& size, const double nearPlane = -1.0, const double farPlane = 1.0)
429 {
430 ::glOrtho(-0.375, size.x - 0.375, size.y - 0.375, -0.375, nearPlane, farPlane);
431 }
432
433 /// Sets up an ortho projection from a simple Vector<6>
434 /// n.b. You first need to set up the matrix environment yourself,
435 /// e.g. glMatrixMode(GL_PROJECTION); glLoadIdentity();
436 /// @param param 6-vector containing the parameters of the projection
437 inline void glOrtho(const TooN::Vector<6>& param)
438 {
439 ::glOrtho(param[0], param[1], param[2], param[3], param[4], param[5]);
440 }
441
442 /// sets a gl frustum from the linear camera parameters, image size and near and far plane.
443 /// The camera will be in OpenGL style with camera center in the origin and the viewing direction
444 /// down the negative z axis, with y pointing upwards and x pointing to the left and the image plane
445 /// at z=-1.
446 /// Images coordinates need to be rotated around the x axis to make sense here, because typically
447 /// the camera is described as y going down (pixel lines) and image plane at z=1.
448 /// @param params vector containing fu, fv, pu, pv as in the linear part of camera parameters
449 /// @param width width of the image plane in pixels, here the viewport for example
450 /// @param height height of the image plane in pixels, here the viewport for example
451 /// @param near near clipping plane
452 /// @param far far clipping plane
453 /// @ingroup gGL
454 template <typename P, typename A>
455 inline void glFrustum(const TooN::Vector<4, P, A>& params, double width, double height, double nearPlane = 0.1, double farPlane = 100)
456 {
457 GLdouble left, right, bottom, top;
458 left = -nearPlane * params[2] / params[0];
459 top = nearPlane * params[3] / params[1];
460 right = nearPlane * (width - params[2]) / params[0];
461 bottom = -nearPlane * (height - params[3]) / params[1];
462 ::glFrustum(left, right, bottom, top, nearPlane, farPlane);
463 }
464
465 /// sets a gl frustum taking the first 4 parameters from the camera model. see @see glFrustum for
466 /// details on the created frustum.
467 /// @param camera camera supplying the parameters for the frustum
468 /// @param width width of the image plane in pixels, here the viewport for example
469 /// @param height height of the image plane in pixels, here the viewport for example
470 /// @param near near clipping plane
471 /// @param far far clipping plane
472 /// @ingroup gGL
473 template <class CAMERA>
474 inline void glFrustum(const CAMERA& camera, double width, double height, double nearPlane = 0.1, double farPlane = 100)
475 {
476 glFrustum(camera.get_parameters().template slice<0, 4>(), width, height, nearPlane, farPlane);
477 }
478
479 /// Sets up an ortho projection from a simple Vector<6>
480 /// n.b. You first need to set up the matrix environment yourself,
481 /// e.g. glMatrixMode(GL_PROJECTION); glLoadIdentity();
482 /// @param param 6-vector containing the parameters of the projection
483 inline void glFrustum(const TooN::Vector<6>& param)
484 {
485 ::glFrustum(param[0], param[1], param[2], param[3], param[4], param[5]);
486 }
487
488 /// Set the new colour to the red, green and blue components given in the Vector
489 /// (where 0.0 represents zero intensity and 1.0 full intensity)
490 /// @param v The new colour
491 ///@ingroup gGL
492 inline void glColor(const TooN::Vector<3>& v)
493 {
494 glColor3d(v[0], v[1], v[2]);
495 }
496
497 /// Set the new colour to the red, green, blue and alpha components given in the Vector
498 /// (where 0.0 represents zero intensity and 1.0 full intensity)
499 /// @param v The new colour
500 ///@ingroup gGL
501 inline void glColor(const TooN::Vector<4>& v)
502 {
503 glColor4d(v[0], v[1], v[2], v[3]);
504 }
505
506 /// Set the new clear colour to the red, green, blue and alpha components given in the Vector
507 /// (where 0.0 represents zero intensity and 1.0 full intensity)
508 /// @param v The new colour
509 ///@ingroup gGL
510 inline void glClearColor(const TooN::Vector<4>& v)
511 {
512 ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], (GLclampf)v[3]);
513 }
514
515 /// Set the new clear colour to the red, green, blue components given in the Vector
516 /// alpha is set to 0
517 /// @param v The new colour
518 ///@ingroup gGL
519 inline void glClearColor(const TooN::Vector<3>& v)
520 {
521 ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], 0);
522 }
523
524 /// glColor version for dynamic TooN::Vector, will test for 3 or 4 components
525 /// @param v The new colour
526 /// @ingroup gGL
527 inline void glColor(const TooN::Vector<-1>& v)
528 {
529 switch(v.size())
530 {
531 case 3:
532 glColor3d(v[0], v[1], v[2]);
533 break;
534 case 4:
535 glColor4d(v[0], v[1], v[2], v[3]);
536 break;
537 }
538 }
539 #endif
540
541 /// draws a line from x1 to x2
542 /// any type that is accepted by glVertex is possible
543 /// @ingroup gGL
544 template <class P1, class P2>
545 inline void glLine(const P1& x1, const P2& x2)
546 {
547 glBegin(GL_LINES);
548 glVertex(x1);
549 glVertex(x2);
550 glEnd();
551 }
552
553 /// sets a whole list of vertices stored in a std::vector. It uses the various
554 /// glVertex helpers defined in this header file.
555 /// @param list the list of vertices
556 /// @ingroup gGL
557 template <class C>
558 inline void glVertex(const C& list)
559 {
560 for(typename C::const_iterator v = list.begin(); v != list.end(); ++v)
561 glVertex(*v);
562 }
563
564 /// Set the new colour to the red, green, blue components given
565 /// (where 0 represents zero intensity and 255 full intensity)
566 /// @param c The new colour
567 ///@ingroup gGL
568 inline void glColor(const CVD::Rgb<byte>& c)
569 {
570 glColor3ub(c.red, c.green, c.blue);
571 }
572
573 /// Set the new colour to the red, green and blue components given
574 /// (where 0.0 represents zero intensity and 1.0 full intensity)
575 /// @param c The new colour
576 ///@ingroup gGL
577 inline void glColor(const CVD::Rgb<float>& c)
578 {
579 glColor3f(c.red, c.green, c.blue);
580 }
581
582 /// Set the new colour to the red, green and blue components given
583 /// (where 0.0 represents zero intensity and 1.0 full intensity). The Rgb8::dummy member is ignored
584 /// @param c The new colour
585 ///@ingroup gGL
586 inline void glColor3(const CVD::Rgb8& c)
587 {
588 glColor3ub(c.red, c.green, c.blue);
589 }
590
591 /// Set the new colour to the red, green, blue and alpha components given
592 /// (where 0.0 represents zero intensity and 1.0 full intensity)
593 /// @param c The new colour
594 ///@ingroup gGL
595 inline void glColor4(const CVD::Rgb8& c)
596 {
597 glColor4ub(c.red, c.green, c.blue, c.dummy);
598 }
599
600 /// Set the new colour to the red, green, blue and alpha components given
601 /// (where 0 represents zero intensity and 255 full intensity)
602 /// @param c The new colour
603 ///@ingroup gGL
604 inline void glColor(const CVD::Rgba<unsigned char>& c)
605 {
606 glColor4ub(c.red, c.green, c.blue, c.alpha);
607 }
608
609 /// Set the new colour to the red, green, blue and alpha components given
610 /// (where 0.0 represents zero intensity and 1.0 full intensity)
611 /// @param c The new colour
612 ///@ingroup gGL
613 inline void glColor(const CVD::Rgba<float>& c)
614 {
615 glColor4f(c.red, c.green, c.blue, c.alpha);
616 }
617
618 namespace Internal
619 {
620 static inline int cvdalignof(const void* ptr)
621 {
622 size_t p = (size_t)ptr;
623
624 if(p & 3)
625 if(p & 1)
626 return 1;
627 else
628 return 2;
629 else if(p & 4)
630 return 4;
631 else
632 return 8;
633 }
634 }
635
636 /// Draw an image to the frame buffer at the current raster position.
637 /// Use glRasterPos to set the current raster position
638 /// @param i The image to draw
639 ///@ingroup gGL
640 template <class C>
641 inline void glDrawPixels(const BasicImage<C>& i)
642 {
643 ::glPixelStorei(GL_UNPACK_ALIGNMENT, std::min(Internal::cvdalignof(i[0]), Internal::cvdalignof(i[1])));
644 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
645 ::glDrawPixels(i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
646 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
647 }
648
649 /// Read the current image from the colour buffer specified by glReadBuffer
650 /// @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.
651 /// @param origin The window co-ordinate of the first pixel to be read from the frame buffer
652 ///@ingroup gGL
653 template <class C>
654 inline void glReadPixels(BasicImage<C>& i, ImageRef origin = ImageRef(0, 0))
655 {
656 ::glPixelStorei(GL_PACK_ALIGNMENT, 1);
657 ::glPixelStorei(GL_PACK_ROW_LENGTH, i.row_stride());
658 ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
659 }
660
661 /// Read the current image from the colour buffer specified by glReadBuffer
662 /// @param size The size of the area to read.
663 /// @param origin The window co-ordinate of the first pixel to be read from the frame buffer
664 ///@ingroup gGL
665 template <class C>
666 inline Image<C> glReadPixels(ImageRef size, ImageRef origin = ImageRef(0, 0))
667 {
668 Image<C> i(size);
669 glReadPixels(i, origin);
670 return i;
671 }
672
673 /// Sets an image as a texture sub region.
674 /// note the reordering of the various parameters to make better use of default parameters
675 /// @param i the image to set as texture
676 /// @ingroup gGL
677 template <class C>
678 inline void glTexSubImage2D(const BasicImage<C>& i, GLint xoffset = 0, GLint yoffset = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
679 {
680 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
681 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
682 ::glTexSubImage2D(target, level, xoffset, yoffset, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
683 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
684 }
685
686 /// Sets an image as a texture.
687 /// note the reordering of the various parameters to make better use of default parameters
688 /// @param i the image to set as texture
689 /// @ingroup gGL
690 template <class C>
691 inline void glTexImage2D(const BasicImage<C>& i, GLint border = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
692 {
693 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
694 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
695 ::glTexImage2D(target, level, gl::data<C>::format, i.size().x, i.size().y, border, gl::data<C>::format, gl::data<C>::type, i.data());
696 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
697 }
698
699 /// Prints the current errors on the gl error stack
700 ///@ingroup gGL
701 inline void glPrintErrors(void)
702 {
703 GLenum code;
704 while((code = glGetError()) != GL_NO_ERROR)
705 {
706 std::cout << "GL:" << code << std::endl;
707 }
708 }
709
710 /// @defgroup gGLText OpenGL text rendering
711 /// @ingroup gGL
712 /// @{
713
714 /// sets the font to use for future font rendering commands. currently sans, serif and mono are available.
715 /// @param fontname string containing font name
716 void glSetFont(const std::string& fontname);
717
718 /// returns the name of the currently active font
719 const std::string& glGetFont();
720
721 /// different style for font rendering
722 enum TEXT_STYLE
723 {
724 FILL = 0, ///< renders glyphs as filled polygons
725 OUTLINE = 1, ///< renders glyphs as outlines with GL_LINES
726 NICE = 2 ///< renders glyphs filled with antialiased outlines
727 };
728
729 /// renders a string in GL using the current settings.
730 /// Font coordinates are +X along the line and +Y along the up direction of glyphs.
731 /// The origin is at the top baseline at the left of the first character. Characters have a maximum size of 1.
732 /// linefeed is interpreted as a new line and the start is offset in -Y direction by @ref spacing . Individual characters
733 /// are separated by @ref kerning + plus their individual with.
734 /// @param text string to be rendered, unknown characters are replaced with '?'
735 /// @param style rendering style
736 /// @param spacing distance between individual text lines
737 /// @param kerning distance between characters
738 std::pair<double, double> glDrawText(const std::string& text, enum TEXT_STYLE style = NICE, double spacing = 1.5, double kerning = 0.1);
739
740 /// returns the size of the bounding box of a text to be rendered, similar to @ref glDrawText but without any visual output
741 std::pair<double, double> glGetExtends(const std::string& text, double spacing = 1.5, double kerning = 0.1);
742
743 ///@}
744
745 };
746
747 #endif