KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_opengl.h
Go to the documentation of this file.
1 
2 /* $Id: gr_opengl.h 18652 2015-03-16 16:27:26Z ksimek $ */
3 
4 #ifndef KJB_CPP_OPENGL_H
5 #define KJB_CPP_OPENGL_H
6 
7 /*******************************************************************
8  * A collection of functions and classes for using KJB c++ objects
9  * with opengl.
10  *
11  * In general, the functions below mimick native opengl functions,
12  * but use objects instead of opengl primitives.
13  *
14  * This doesn't include the OpenGL library headers; to get them
15  * include gr_cpp/gr_opengl_headers.h
16  *
17  *******************************************************************/
18 
19 #include <l_cpp/l_exception.h>
20 #include <string.h>
21 
22 //#include <gr_cpp/gr_opengl_headers.h>
23 
24 namespace kjb
25 {
26 
27 class Quaternion;
28 class Vector;
29 class Matrix;
30 class Int_matrix;
31 class Image;
32 struct PixelRGBA;
33 
34 template <size_t D>
35 class Vector_d;
36 typedef Vector_d<2> Vector2;
37 typedef Vector_d<3> Vector3;
39 
40 template <size_t M, size_t N, bool Transposed>
41 class Matrix_d;
43 
44 
45 namespace opengl
46 {
47 
48 /*-----------------------------------------------------------------------------
49  *
50  * Utility overloads
51  *
52  * These functions are overloaded versions of standard OpenGL functions.
53  *-----------------------------------------------------------------------------*/
54 
56 void glRotate(const kjb::Quaternion & q);
57 
59 void glTranslate(const kjb::Vector& p);
60 
62 void glTranslate(const kjb::Vector3& p);
63 
64 void glTranslate(const kjb::Vector2& p);
65 
67 void glVertex(const Vector& p);
68 
69 void glVertex(const Vector2& p);
70 
71 void glVertex(const Vector3& p);
72 
73 void glVertex(const Vector4& p);
74 
76 void glColor(const Vector& color);
77 
78 void glColor(const kjb::PixelRGBA& color);
79 
80 void glColor(const Vector3& color);
81 
82 void glColor(const Vector4& color);
83 
85 void glLoadMatrix(const kjb::Matrix& m);
86 
88 void glLoadMatrix(const kjb::Matrix4& m);
89 
91 void glMultMatrix(const kjb::Matrix& m);
92 
94 void glMultMatrix(const kjb::Matrix4& m);
95 
97 void gluBuild2DMipmaps(const Image& img);
98 
99 #ifdef KJB_HAVE_OPENGL
100 
106 void glTexImage2D(unsigned int target, int level, int border, const Image& img);
107 #endif
108 
110 void glTexImage2D(const Image& img);
111 
112 // Why doesn't this use kjb::Matrix's for modelview and projection? --Ernesto (2011/02/17)
113 //
114 // This is a thin wrapper for gluUnProject, which is intended to receive matrices received from
115 // a call to glGetDoublev(). This is called from the higher-level unproject() function.
116 // Please feel free to overload for kjb::Matrix. -- Kyle (2011/11/16)
117 //
119 Vector gluUnProject(const Vector& vertex, double* modelview, double* projection, int* viewport);
120 Vector3 gluUnProject(const Vector2& vertex, double* modelview, double* projection, int* viewport);
121 Vector3 gluUnProject(const Vector3& vertex, double* modelview, double* projection, int* viewport);
122 
124 void gluLookAt(Matrix& M, const Vector& eye, const Vector& target, const Vector& up);
125 
126 
133 void glOrtho(
134  Matrix& state,
135  double left,
136  double right,
137  double bottom,
138  double top,
139  double znear,
140  double zfar);
141 
142 
149 void glOrtho(
150  Matrix4& state,
151  double left,
152  double right,
153  double bottom,
154  double top,
155  double znear,
156  double zfar);
157 
158 
159 void glClearDepthBuffer();
160 
161 
162 void glcolormask(bool b1, bool b2, bool b3, bool b4);
163 
164 
165 void glEnableLineSmooth();
166 
167 
168 void glDisableLineSmooth();
169 
170 
171 
172 /*-----------------------------------------------------------------------------
173  * Non-standard OpenGL utility methods.
174  *-----------------------------------------------------------------------------*/
175 
177 void default_init_opengl(unsigned int width = 100, unsigned int height = 100);
178 
179 
180 
182 size_t get_viewport_width();
183 
184 
186 size_t get_viewport_height();
187 
188 
189 
192 
195 
200 Vector project(const Vector& x);
201 
202 Vector2 project(const Vector3& x);
203 
211 Vector unproject(const Vector& vertex);
212 
217 void draw_full_screen_quad(float z = 0.0);
218 
220 bool has_stencil_buffer();
221 
223 void set_framebuffer(const Image& img);
224 
231 void set_framebuffer(const Matrix& mat);
232 
239 void set_framebuffer(const Int_matrix& mat);
240 
249 Image get_framebuffer_as_image(size_t x, size_t y, size_t width, size_t height);
250 
255 
265 void get_framebuffer_as_matrix(size_t x, size_t y, size_t width, size_t height, Matrix& out);
266 
277 (
278  size_t x,
279  size_t y,
280  size_t width,
281  size_t height,
282  Matrix& result
283 );
284 
293 Matrix get_framebuffer_as_matrix(size_t x, size_t y, size_t width, size_t height);
294 
295 
304 Matrix get_depth_buffer_as_matrix(size_t x, size_t y, size_t width, size_t height);
305 
306 
307 
312 
313 
318 
319 
328 Int_matrix get_framebuffer_as_int_matrix(size_t x, size_t y, size_t width, size_t height);
329 
334 
335 
337 void bitmap_string
338 (
339  const std::string& s,
340  double x,
341  double y,
342  void* font = NULL // GLUT_BITMAP_8_BY_13
343 );
344 
346 void move_in_plane(kjb::Vector3& plane_pt, const kjb::Vector2& new_pt);
347 
349 void get_backprojection_line(const Vector2& pt, Vector3& line_pt, Vector3& line_dir);
350 
364 (
365  double xmin,
366  double xmax,
367  double zmin,
368  double zmax,
369  double csz = 1.0
370 );
371 
385 (
386  double xmin,
387  double xmax,
388  double ymin,
389  double ymax,
390  double csz = 1.0
391 );
392 
393 /*-----------------------------------------------------------------------------
394  * Interface classes
395  *-----------------------------------------------------------------------------*/
397 {
398 public:
399  virtual void gl_call() const = 0;
400 };
401 
402 /* --------------------------------------------------------------------------------
403  * ERROR HANDLING
404  * --------------------------------------------------------------------------------*/
405 
406 #ifdef KJB_HAVE_OPENGL
407 
415 #define GL_ETX() \
416  { \
417  GLenum error = glGetError(); \
418  if((error)) \
419  { \
420  KJB_THROW_2(::kjb::opengl::Opengl_error, error); \
421  } \
422  }
423 
430 #define GL_EPETE() \
431  { \
432  GLenum error = glGetError(); \
433  if((error)) \
434  { \
435  std:: cout << "Opengl error: " << gluErrorString(error) << std::endl; \
436  abort(); \
437  } \
438  }
439 
440 class Opengl_error : public Runtime_error
441 {
442 public:
446  Opengl_error(unsigned int error_code, const char* file, int line);
447 
448  Opengl_error(unsigned int error_code, const std::string& msg, const char* file, int line);
449 
453  Opengl_error(const std::string& msg, const char* file, int line);
454 
455  unsigned int get_code() { return code_; }
456 private:
457  unsigned int code_;
458 };
459 #endif /* #ifdef KJB_HAVE_OPENGL */
460 
461 
462 }
463 }
464 
465 #endif
Definition: gr_opengl.h:41
Vector_d< 4 > Vector4
Definition: gr_opengl.h:38
void set_framebuffer(const Image &img)
Puts the contents of an image into the framebuffer.
Definition: gr_opengl.cpp:750
Matrix_d< 4, 4, false > Matrix4
Definition: gr_opengl.h:41
size_t get_viewport_height()
Get current GL viewport height.
Definition: gr_opengl.cpp:587
Matrix get_projection_matrix()
Returns a copy of the current projection matrix.
Definition: gr_opengl.cpp:621
Vector_d< 3 > Vector3
Definition: g_quaternion.h:37
void glRotate(const kjb::Quaternion &q)
glRotate for a kjb::Quaternion.
Definition: gr_opengl.cpp:96
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
void glClearDepthBuffer()
Definition: gr_opengl.cpp:501
Definition: gr_opengl.h:396
height
Definition: APPgetLargeConnectedEdges.m:33
void gluBuild2DMipmaps(const Image &img)
NEEDS COMMENTING!!!.
Definition: gr_opengl.cpp:305
Vector unproject(const Vector &vertex)
Unproject a point from screen coordinates to world coordinates.
Definition: gr_opengl.cpp:680
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void default_init_opengl(unsigned int width, unsigned int height)
Initializes OpenGL in a standard way.
Definition: gr_opengl.cpp:560
void glMultMatrix(const Matrix &m)
glMultMatrix for a kjb::Matrix.
Definition: gr_opengl.cpp:290
Vector gluUnProject(const Vector &vertex, double *modelview, double *projection, int *viewport)
Wraps gluUnproject using kjb::Vertex.
Definition: gr_opengl.cpp:384
size_t get_viewport_width()
Get current GL viewport width.
Definition: gr_opengl.cpp:575
void glVertex(const Vector &p)
glVertex for a kjb::Vector.
Definition: gr_opengl.cpp:136
void render_xy_plane_grid(double xmin, double xmax, double ymin, double ymax, double csz)
Draw a grid representing the ground plane.
Definition: gr_opengl.cpp:1267
REAL xmin
Definition: triangle.c:649
void move_in_plane(kjb::Vector3 &plane_pt, const kjb::Vector2 &new_pt)
Translate a point so it projects to a screen point.
Definition: gr_opengl.cpp:1108
Definition: g_quaternion.h:40
void render_xz_plane_grid(double xmin, double xmax, double zmin, double zmax, double csz)
Draw a grid representing the ground plane.
Definition: gr_opengl.cpp:1233
x
Definition: APPgetLargeConnectedEdges.m:100
virtual void gl_call() const =0
void glEnableLineSmooth()
Definition: gr_opengl.cpp:542
void draw_full_screen_quad(float z)
Definition: gr_opengl.cpp:699
void glcolormask(bool b1, bool b2, bool b3, bool b4)
Definition: gr_opengl.cpp:510
Wrapped version of the C struct Pixel, with Alpha (opacity).
Definition: i_pixel.h:57
REAL ymax
Definition: triangle.c:649
void get_framebuffer_as_matrix(size_t x, size_t y, size_t width, size_t height, Matrix &result)
Definition: gr_opengl.cpp:973
void glTranslate(const kjb::Vector &p)
glTranslate for a kjb::Vector.
Definition: gr_opengl.cpp:108
void glTexImage2D(const Image &img)
Copy kjb::Image into an opengl texture.
Definition: gr_opengl.cpp:339
void glDisableLineSmooth()
Definition: gr_opengl.cpp:551
void glColor(const Vector &color)
glColor for a kjb::Vector.
Definition: gr_opengl.cpp:196
void glLoadMatrix(const Matrix &m)
glLoadMatrix for a kjb::Matrix.
Definition: gr_opengl.cpp:236
void glOrtho(Matrix &state, double left, double right, double bottom, double top, double znear, double zfar)
Definition: gr_opengl.cpp:475
REAL ymin
Definition: triangle.c:649
Int_matrix get_framebuffer_as_int_matrix()
Read pixels from opengl's whole back-buffer into a int matrix.
Definition: gr_opengl.cpp:1031
Definition: g_quaternion.h:37
Vector project(const Vector &x)
Computes the window coordinates of the given point using current GL state.
Definition: gr_opengl.cpp:632
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Matrix get_modelview_matrix()
Returns a copy of the current modelview matrix.
Definition: gr_opengl.cpp:610
for m
Definition: APPgetLargeConnectedEdges.m:64
void bitmap_string(const std::string &s, double x, double y, void *font)
Draws a bitmap string at the specified location.
Definition: gr_opengl.cpp:1081
Support for error handling exception classes in libKJB.
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
Image get_framebuffer_as_image()
Read pixels from opengl's whole back-buffer into an image.
Definition: gr_opengl.cpp:598
Vector_d< 2 > Vector2
Definition: gr_opengl.h:35
REAL xmax
Definition: triangle.c:649
void gluLookAt(Matrix &M, const Vector &eye, const Vector &target, const Vector &up)
Computes a gluLookAt matrix and multiplies it by the given matrix.
Definition: gr_opengl.cpp:400
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
void get_depth_buffer_as_matrix(size_t x, size_t y, size_t width, size_t height, Matrix &result)
Definition: gr_opengl.cpp:988
void get_backprojection_line(const Vector2 &pt, Vector3 &line_pt, Vector3 &line_dir)
Get backprojection line using current opengl state.
Definition: gr_opengl.cpp:1199
bool has_stencil_buffer()
Returns true if OpenGL version supports stencil buffer (right?).
Definition: gr_opengl.cpp:726