Graphite  Version 3
An experimental 3D geometry processing program
rendering_context.h
Go to the documentation of this file.
1 /*
2  * OGF/Graphite: Geometry and Graphics Programming Library + Utilities
3  * Copyright (C) 2000 Bruno Levy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * If you modify this software, you should include a notice giving the
20  * name of the person performing the modification, the date of modification,
21  * and the reason for such modification.
22  *
23  * Contact: Bruno Levy
24  *
25  * levy@loria.fr
26  *
27  * ISA Project
28  * LORIA, INRIA Lorraine,
29  * Campus Scientifique, BP 239
30  * 54506 VANDOEUVRE LES NANCY CEDEX
31  * FRANCE
32  *
33  * Note that the GNU General Public License does not permit incorporating
34  * the Software into proprietary programs.
35  */
36 
37 
38 #ifndef H_OGF_RENDERER_CONTEXT_RENDERING_CONTEXT_H
39 #define H_OGF_RENDERER_CONTEXT_RENDERING_CONTEXT_H
40 
43 #include <OGF/renderer/context/overlay.h>
44 #include <geogram/image/image.h>
46 #include <geogram_gfx/basic/GL.h>
47 
53 namespace OGF {
54 
55  class Colormap;
56  class RenderArea;
57 
58 /*****************************************************************/
59 
67  class RENDERER_API RenderingContext : public Counted {
68  public:
69 
76  RenderingContext(GLUPcontext glup_context=nullptr);
77 
82 
88  return current_;
89  }
90 
91 
101  static bool is_currently_rendering();
102 
103 
113  static bool is_currently_picking();
114 
115 
120  index_t get_width() const {
121  return width_;
122  }
123 
128  index_t get_height() const {
129  return height_;
130  }
131 
139  bool initialized() const {
140  return initialized_;
141  }
142 
152  virtual void make_current();
153 
160  virtual void done_current();
161 
169  virtual void swap_buffers();
170 
175  void clear();
176 
184  void load_viewing_matrix(const mat4& m);
185 
195  void mul_viewing_matrix(const mat4& m);
196 
201  const mat4& viewing_matrix() const;
202 
208  const mat4& inverse_viewing_matrix() const;
209 
214  void load_projection_matrix(const mat4& m);
215 
221 
229  virtual void begin_frame();
230 
234  virtual void end_frame();
235 
236 
243  const Color& background_color() const;
244 
249  void set_background_color(const Color& c);
250 
251 
259  const Color& background_color_2() const;
260 
269 
278 
292  Memory::pointer ptr,
293  Image::ColorEncoding color_encoding,
294  Image::ComponentEncoding component_encoding,
295  index_t width, index_t height
296  );
297 
304  void set_lighting_matrix(const mat4& m);
305 
310  const mat4& lighting_matrix() const;
311 
318 
325  void set_clipping_matrix(const mat4& m);
326 
332  void set_clipping(bool x);
333 
339  bool get_clipping() const;
340 
346  void set_clipping_equation(const vec4& x) {
347  clipping_equation_ = x;
348  }
349 
356  return clipping_equation_;
357  }
358 
365  void set_clipping_viewer(bool x) {
366  clipping_viewer_ = x;
367  }
368 
375  bool get_clipping_viewer() const {
376  return clipping_viewer_;
377  }
378 
385  GLUPclipMode get_clipping_mode() const {
386  return clipping_mode_;
387  }
388 
395  void set_clipping_mode(GLUPclipMode mode) {
396  clipping_mode_ = mode;
397  }
398 
404  bool double_buffer() const {
405  return double_buffer_;
406  }
407 
413  bool stereo() const {
414  return stereo_;
415  }
416 
421  virtual void set_double_buffer(bool b);
422 
432  virtual void snapshot(
433  Image* image, bool make_current=true,
434  index_t x0=0, index_t y0=0,
435  index_t width=0, index_t height=0
436  );
437 
442  std::string gl_vendor() const;
443 
448  std::string gl_renderer() const;
449 
454  std::string gl_version() const;
455 
461  std::string gl_extensions() const;
462 
468  std::string get_gpu_information() const;
469 
474  std::string get_gpu_extensions() const;
475 
485 
493  return full_screen_effect_;
494  }
495 
503  virtual void resize(index_t w, index_t h);
504 
514 
523  void ndc_to_screen(const vec2& ndc, index_t& x, index_t& y) const;
524 
531  virtual void begin_picking(const vec2& ndc);
532 
537  virtual void end_picking();
538 
546  index_t picked_id() const {
547  return picked_id_;
548  }
549 
555  double picked_depth() const {
556  return picked_depth_;
557  }
558 
563  const vec3& picked_point() const {
564  return picked_point_;
565  }
566 
573  bool picked_background() const {
574  return picked_background_;
575  }
576 
582  vec3 unproject(const vec2& p_ndc, double depth) const;
583 
589  void check_gl() const;
590 
598  GLuint frame_buffer_id() const {
599  return frame_buffer_id_;
600  }
601 
611  bool contains_picking_image() const {
612  return last_frame_was_picking_;
613  }
614 
621  return overlay_;
622  }
623 
624  protected:
625 
634  virtual void draw_background();
635 
641 
647  void setup_projection_ortho(double zNear, double zFar);
648 
659  double zScreen, double zNear, double zFar,
660  double eye_offset=0.0
661  );
662 
668  void setup_modelview(double zScreen);
669 
674 
675 
682 
691  static const double* convert_matrix(const mat4& M);
692 
699  return background_texture_;
700  }
701 
711  void set_initialized(bool x = true) {
712  initialized_ = x;
713  }
714 
722  void set_width(index_t w) {
723  width_ = w;
724  }
725 
733  void set_height(index_t h) {
734  height_ = h;
735  }
736 
743 
744 
751 
752 
753  protected:
754 
755  static RenderingContext* current_;
756  bool initialized_;
757 
758  index_t width_;
759  index_t height_;
760  bool double_buffer_;
761 
762  int viewport_x_;
763  int viewport_y_;
764  int viewport_width_;
765  int viewport_height_;
766 
767  mat4 viewing_matrix_;
768  mutable mat4 inverse_viewing_matrix_;
769  mutable bool inverse_viewing_matrix_dirty_;
770 
771  bool lighting_;
772  mat4 lighting_matrix_;
773 
774  bool clipping_;
775  mat4 clipping_matrix_;
776  vec4 clipping_equation_;
777  bool clipping_viewer_;
778  GLUPclipMode clipping_mode_;
779 
780  Color background_color_;
781  Color background_color_2_;
782  Texture_var background_texture_;
783  FullScreenEffectImpl_var full_screen_effect_;
784 
785  bool perspective_;
786  bool stereo_;
787  bool stereo_odd_frame_;
788  double stereo_eye_dist_;
789  vec3 head_position_;
790  double head_tilt_;
791 
792 
793  bool picking_mode_;
794  vec2 picked_ndc_;
795  double picked_depth_;
796  index_t picked_id_;
797  vec3 picked_point_;
798  bool picked_background_;
799  bool last_frame_was_picking_;
800 
811  bool frame_buffer_id_init_;
812 
813  static bool geogram_gfx_initialized_;
814 
821 
828 
836  bool owns_glup_context_;
837 
838  bool use_core_profile_;
839  bool use_ES_profile_;
840 
841  bool transparent_;
842 
843  Overlay overlay_;
844 
845  friend class RenderArea;
846  };
847 
849 
850 /******************************************************************/
851 
852 }
853 
854 #endif
855 
856 
void * GLUPcontext
Opaque identifier of a GLUP context.
Definition: GLUP.h:90
Some utility functions for OpenGL graphics.
Base class for reference-counted objects.
Definition: counted.h:71
Implementation of full screen effects.
An image.
Definition: image.h:59
ComponentEncoding
Indicates the datatype used to encode each component of the colors.
Definition: image.h:74
ColorEncoding
Indicates how colors are encoded within the image.
Definition: image.h:66
A display list that memorizes simple graphic primitives to be displayed over the 3D rendering window.
Definition: overlay.h:53
Implementation of RenderArea using ImGui.
Definition: render_area.h:56
Helper class for OpenGL context management.
std::string get_gpu_information() const
Gets the information on the GPU.
FullScreenEffectImpl * get_full_screen_effect() const
Gets the current full screen effect.
vec3 unproject(const vec2 &p_ndc, double depth) const
Back-transforms a point given by its normalized device coordinates and depth.
void set_initialized(bool x=true)
Indicates whether this RenderingContext is initialized.
void setup_viewport()
Setups the viewport transform.
void set_height(index_t h)
Sets the height.
virtual void make_current()
Makes this RenderingContext the current one.
virtual void end_picking()
Exits picking mode.
virtual void resize(index_t w, index_t h)
Must be called whenever the rendering context is resized.
const mat4 & viewing_matrix() const
Gets the current viewing matrix.
virtual void draw_background()
Draws the background.
index_t picked_id() const
Gets the picked id that was encoded in the pixel color under the mouse pointer.
virtual void begin_picking(const vec2 &ndc)
Enters picking mode.
bool get_clipping_viewer() const
Tests whether clipping translation and rotation are applied in object bounding box or in viewer coord...
virtual void end_frame()
Terminates a frame.
void setup_modelview(double zScreen)
Setups the OpenGL model view transform from the viewing parameters.
void set_full_screen_effect(FullScreenEffectImpl *fse)
Sets the full screen effect.
~RenderingContext()
RenderingContext destructor.
void set_lighting_matrix(const mat4 &m)
Sets the lighting matrix.
virtual void begin_frame()
Begins a new frame.
void set_clipping_equation(const vec4 &x)
Sets the clipping equation.
static index_t nb_render_locks_
The number of RenderingContext instances that are currently rendering something, i....
void setup_projection_ortho(double zNear, double zFar)
Setups the OpenGL projection matrix in orthographic mode.
void set_background_color(const Color &c)
Sets the primary background color.
void setup_projection_perspective(double zScreen, double zNear, double zFar, double eye_offset=0.0)
Setups the OpenGL projection matrix in orthographic mode.
static bool is_currently_rendering()
Tests whether a rendering operation is occuring.
std::string gl_renderer() const
Gets the OpenGL renderer.
bool picked_background() const
Tests whether the background was picked.
const Color & background_color() const
Gets the primary background color.
const Color & background_color_2() const
Gets the secondary background color.
GLuint frame_buffer_id_
The identifier of the default frame buffer associated with this RenderingContext.
vec2 screen_to_ndc(index_t x, index_t y) const
Transforms screen coordinates to normalized device coordinates (viewport transform).
std::string gl_version() const
Gets the OpenGL version.
virtual void set_double_buffer(bool b)
Specifies whether double buffering should be used.
virtual void snapshot(Image *image, bool make_current=true, index_t x0=0, index_t y0=0, index_t width=0, index_t height=0)
void set_background_image(Image *image)
Sets the background image.
mat4 clipping_matrix() const
Gets the clipping matrix.
void load_viewing_matrix(const mat4 &m)
Sets the current viewing matrix.
GLUPclipMode get_clipping_mode() const
Gets the current clipping mode.
static RenderingContext * current()
Gets the current RenderingContext.
index_t get_width() const
Gets the width.
bool double_buffer() const
Tests whether this RenderingContext uses double buffering.
bool initialized() const
Tests whether this RenderingContext is initialized.
GLuint frame_buffer_id() const
Gets the default frame buffer id.
const vec4 & get_clipping_translation() const
Gets the clipping equation.
virtual void done_current()
This function should be called when client code is done with rendering to the current context.
static const double * convert_matrix(const mat4 &M)
Converts (transposes) a Graphite matrix for OpenGL use.
const vec3 & picked_point() const
Gets the picked point in world coordinates.
RenderingContext(GLUPcontext glup_context=nullptr)
RenderingContext constructor.
void ndc_to_screen(const vec2 &ndc, index_t &x, index_t &y) const
Transforms normalized device coordinates to screen coordinates (inverse viewport transform)....
void get_picked_point()
In picking mode, this function gets all the information about the picked point, by reading the depth ...
void get_view_parameters()
Queries the Graphite environment for global rendering parameters, e.g. stereo & perspective,...
void setup_lighting()
Setups OpenGL lighting parameters.
std::string get_gpu_extensions() const
Gets the GPU extensions.
void set_width(index_t w)
Sets the width.
void set_clipping_mode(GLUPclipMode mode)
Sets the current clipping mode.
bool stereo() const
Tests whether this RenderingContext uses stereo rendering.
void update_clipping()
Create a clipping plane perpendicular to the Z axis, transformed by the current clipping matrix and t...
const mat4 & lighting_matrix() const
Gets the lighting matrix.
const mat4 & inverse_viewing_matrix() const
Gets the inverse of the current viewing matrix.
void set_clipping_viewer(bool x)
Sets whether clipping translation and rotation are applied in object bounding box or in viewer coordi...
Overlay & overlay()
Gets the Overlay.
virtual void swap_buffers()
Meant to make the rendered frame visible.
std::string gl_extensions() const
Gets the OpenGL extension.
static bool is_currently_picking()
Tests whether a picking operation is occuring.
void update_background_image_from_data(Memory::pointer ptr, Image::ColorEncoding color_encoding, Image::ComponentEncoding component_encoding, index_t width, index_t height)
Updates the background image from raw data pointer.
void set_clipping_matrix(const mat4 &m)
Sets the lighting matrix.
bool contains_picking_image() const
Tests whether the image is a picking image.
mat4 projection_matrix() const
Gets the projection matrix.
double picked_depth() const
Gets the depth of the picked point in screen coordinates.
std::string gl_vendor() const
Gets the OpenGL vendor.
void load_projection_matrix(const mat4 &m)
Sets the projection matrix and sends it to OpenGL.
void mul_viewing_matrix(const mat4 &m)
Multiplies the current viewing matrix by another one.
Texture * background_texture() const
Gets the background texture.
void clear()
Clears this RenderingContext.
void set_clipping(bool x)
Activates or deactivates clipping.
void set_background_color_2(const Color &c)
Sets the secondary background color.
void check_gl() const
Tests whether there was any OpenGL error.
bool get_clipping() const
Tests whether clipping is active.
GLUPcontext glup_context_
The GLUP context.
index_t get_height() const
Gets the height.
static index_t nb_picking_locks_
The number of RenderingContext instances that are currently picking something, or that contain a pick...
An OpenGL texture.
Definition: texture.h:56
Low-level base class for full screen effects.
byte * pointer
Pointer to unsigned byte(s)
Definition: memory.h:104
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Global Graphite namespace.
Definition: common.h:76
Definitions common to all include files in the renderer library.
Helper class for manipulating OpenGL textures.