cgv
gl_context.h
1 #pragma once
2 
3 #include <stack>
4 #include <cgv/render/context.h>
5 #include <cgv/render/shader_program.h>
6 #include <cgv_gl/gl/gl.h>
7 
8 #include "lib_begin.h"
9 
10 namespace cgv {
12  namespace render {
14  namespace gl {
15 
17 extern CGV_API void set_gl_format(texture& tex, GLuint gl_format, const std::string& component_format_description);
18 
20 extern CGV_API GLuint get_gl_format(const texture& tex);
21 
22 extern CGV_API GLuint map_to_gl(PrimitiveType pt);
23 
24 extern CGV_API GLuint map_to_gl(MaterialSide ms);
25 
27 extern CGV_API void set_material(const cgv::media::illum::phong_material& mat, MaterialSide ms, float alpha);
28 
29 
32 class CGV_API gl_context : public render::context
33 {
34 private:
35  int query_integer_constant(ContextIntegerConstant cic) const;
36  GLuint texture_bind(TextureType tt, GLuint tex_id) const;
37  void texture_unbind(TextureType tt, GLuint tmp_id) const;
38  GLuint texture_generate(texture_base& tb) const;
39  /*
40  void frame_buffer_bind(const frame_buffer_base& fbb, void*& user_data) const;
41  void frame_buffer_unbind(const frame_buffer_base& fbb, void*& user_data) const;
42  void frame_buffer_bind(frame_buffer_base& fbb) const;
43  void frame_buffer_unbind(frame_buffer_base& fbb) const;
44  */
45 protected:
46  shader_program progs[4];
47  mutable cgv::type::uint64_type max_nr_indices, max_nr_vertices;
48  void ensure_configured() const;
49  void destruct_render_objects();
50  void put_id(void* handle, void* ptr) const;
51  void draw_elements_void(GLenum mode, size_t count, GLenum type, size_t type_size, const void* indices) const;
52  template <typename T>
53  void draw_elements(GLenum mode, size_t count, const T* indices) const {
55  error("called draw_elements with invalid index type");
56  else
57  draw_elements_void(mode, count, gl_traits<T>::type, sizeof(T), indices);
58  }
59 
60  cgv::data::component_format texture_find_best_format(const cgv::data::component_format& cf, render_component& rc, const std::vector<cgv::data::data_view>* palettes = 0) const;
61  bool texture_create(texture_base& tb, cgv::data::data_format& df) const;
62  bool texture_create(texture_base& tb, cgv::data::data_format& target_format, const cgv::data::const_data_view& data, int level, int cube_side = -1, int num_array_layers = 0, const std::vector<cgv::data::data_view>* palettes = 0) const;
63  bool texture_create_from_buffer(texture_base& tb, cgv::data::data_format& df, int x, int y, int level) const;
64  bool texture_replace(texture_base& tb, int x, int y, int z_or_cube_side, const cgv::data::const_data_view& data, int level, const std::vector<cgv::data::data_view>* palettes = 0) const;
65  bool texture_replace_from_buffer(texture_base& tb, int x, int y, int z_or_cube_side, int x_buffer, int y_buffer, unsigned int width, unsigned int height, int level) const;
66  bool texture_generate_mipmaps(texture_base& tb, unsigned int dim) const;
67  bool texture_destruct(texture_base& tb) const;
68  bool texture_set_state(const texture_base& tb) const;
69  bool texture_enable(texture_base& tb, int tex_unit, unsigned int nr_dims) const;
70  bool texture_disable(texture_base& tb, int tex_unit, unsigned int nr_dims) const;
71 
72  bool render_buffer_create(render_component& rc, cgv::data::component_format& cf, int& _width, int& _height) const;
73  bool render_buffer_destruct(render_component& rc) const;
74 
75  bool frame_buffer_create(frame_buffer_base& fbb) const;
76  bool frame_buffer_attach(frame_buffer_base& fbb, const render_component& rb, bool is_depth, int i) const;
77  bool frame_buffer_attach(frame_buffer_base& fbb, const texture_base& t, bool is_depth, int level, int i, int z) const;
78  bool frame_buffer_is_complete(const frame_buffer_base& fbb) const;
79  bool frame_buffer_enable(frame_buffer_base& fbb);
80  bool frame_buffer_disable(frame_buffer_base& fbb);
81  bool frame_buffer_destruct(frame_buffer_base& fbb) const;
82  int frame_buffer_get_max_nr_color_attachments() const;
83  int frame_buffer_get_max_nr_draw_buffers() const;
84 
85  bool shader_code_create(render_component& sc, ShaderType st, const std::string& source) const;
86  bool shader_code_compile(render_component& sc) const;
87  void shader_code_destruct(render_component& sc) const;
88 
89  bool shader_program_create(shader_program_base& spb) const;
90  void shader_program_attach(shader_program_base& spb, const render_component& sc) const;
91  bool shader_program_link(shader_program_base& spb) const;
92  bool shader_program_set_state(shader_program_base& spb) const;
93  bool shader_program_enable(shader_program_base& spb);
94  bool shader_program_disable(shader_program_base& spb);
95  void shader_program_detach(shader_program_base& spb, const render_component& sc) const;
96  bool shader_program_destruct(shader_program_base& spb);
97 
98  int get_uniform_location(const shader_program_base& spb, const std::string& name) const;
99  bool set_uniform_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const;
100  bool set_uniform_array_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr, size_t nr_elements) const;
101  int get_attribute_location(const shader_program_base& spb, const std::string& name) const;
102  bool set_attribute_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const;
103 
104  bool attribute_array_binding_create(attribute_array_binding_base& aab) const;
105  bool attribute_array_binding_destruct(attribute_array_binding_base& aab);
106  bool attribute_array_binding_enable(attribute_array_binding_base& aab);
107  bool attribute_array_binding_disable(attribute_array_binding_base& aab);
108  bool set_attribute_array_void(attribute_array_binding_base* aab, int loc, type_descriptor value_type, const vertex_buffer_base* vbb, const void* ptr, size_t nr_elements, unsigned stride_in_bytes) const;
109  bool set_element_array(attribute_array_binding_base* aab, const vertex_buffer_base* vbb) const;
110  bool enable_attribute_array(attribute_array_binding_base* aab, int loc, bool do_enable) const;
111  bool is_attribute_array_enabled(const attribute_array_binding_base* aab, int loc) const;
112 
113  bool vertex_buffer_bind(const vertex_buffer_base& vbb, VertexBufferType _type) const;
114  bool vertex_buffer_create(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const;
115  bool vertex_buffer_replace(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, const void* array_ptr) const;
116  bool vertex_buffer_copy(const vertex_buffer_base& src, size_t src_offset, vertex_buffer_base& target, size_t target_offset, size_t size_in_bytes) const;
117  bool vertex_buffer_copy_back(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, void* array_ptr) const;
118  bool vertex_buffer_destruct(vertex_buffer_base& vbb) const;
119 
120  bool check_gl_error(const std::string& where, const cgv::render::render_component* rc = 0) const;
121  bool check_texture_support(TextureType tt, const std::string& where, const cgv::render::render_component* rc = 0) const;
122  bool check_shader_support(ShaderType st, const std::string& where, const cgv::render::render_component* rc = 0) const;
123  bool check_fbo_support(const std::string& where, const cgv::render::render_component* rc = 0) const;
129  void draw_textual_info();
131  bool show_help;
132  bool show_stats;
134  void rotate_vector_to_target(const dvec3& vector, const dvec3& target);
135 public:
137  gl_context();
139  bool configure_gl();
140  void resize_gl();
142  float get_current_font_size() const;
144  media::font::font_face_ptr get_current_font_face() const;
146  void perform_screen_shot();
148  RenderAPI get_render_api() const;
150  void init_render_pass();
152  void finish_render_pass();
155  void set_color(const rgba& clr);
160  void enable_material(textured_material& mat);
162  void disable_material(textured_material& mat);
164  shader_program& ref_default_shader_program(bool texture_support = false);
166  shader_program& ref_surface_shader_program(bool texture_support = false);
168  void enumerate_program_uniforms(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr = 0, std::vector<int>* sizes_ptr = 0, std::vector<int>* types_ptr = 0, bool show = false) const;
170  void enumerate_program_attributes(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr = 0, std::vector<int>* sizes_ptr = 0, std::vector<int>* types_ptr = 0, bool show = false) const;
172  void on_lights_changed();
174  void tesselate_arrow(double length = 1, double aspect = 0.1, double rel_tip_radius = 2.0, double tip_aspect = 0.3, int res = 25, bool edges = false);
176  void tesselate_arrow(const dvec3& start, const dvec3& end, double aspect = 0.1f, double rel_tip_radius = 2.0f, double tip_aspect = 0.3f, int res = 25, bool edges = false);
178  void draw_light_source(const cgv::media::illum::light_source& l, float intensity_scale, float light_scale);
180  void announce_external_viewport_change(ivec4& cgv_viewport_storage);
183  void recover_from_external_viewport_change(const ivec4& cgv_viewport_storage);
185  void announce_external_frame_buffer_change(void*& cgv_fbo_storage);
187  void recover_from_external_frame_buffer_change(void* cgv_fbo_storage);
188 
191  void push_pixel_coords();
194  void pop_pixel_coords();
196  virtual bool read_frame_buffer(
197  data::data_view& dv,
198  unsigned int x = 0, unsigned int y = 0,
199  FrameBufferType buffer_type = FB_BACK,
202  int w = -1, int h = -1);
204 
207  bool prepare_attributes(std::vector<vec3>& P, std::vector<vec3>& N, std::vector<vec2>& T, unsigned nr_vertices,
209  const float* vertices, const float* normals, const float* tex_coords,
210  const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals) const;
212  bool release_attributes(const float* normals, const float* tex_coords, const int* normal_indices, const int* tex_coord_indices) const;
214  void draw_edges_of_faces(
215  const float* vertices, const float* normals, const float* tex_coords,
216  const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
217  int nr_faces, int face_degree, bool flip_normals = false) const;
219  void draw_edges_of_strip_or_fan(
220  const float* vertices, const float* normals, const float* tex_coords,
221  const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
222  int nr_faces, int face_degree, bool is_fan, bool flip_normals = false) const;
224  void draw_faces(
225  const float* vertices, const float* normals, const float* tex_coords,
226  const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
227  int nr_faces, int face_degree, bool flip_normals) const;
229  void draw_strip_or_fan(
230  const float* vertices, const float* normals, const float* tex_coords,
231  const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
232  int nr_faces, int face_degree, bool is_fan, bool flip_normals) const;
234 
237  dmat4 get_modelview_matrix() const;
240  void set_modelview_matrix(const dmat4& V);
242  dmat4 get_projection_matrix() const;
244  void set_projection_matrix(const dmat4& P);
245 
247  void pop_window_transformation_array();
249  unsigned get_max_window_transformation_array_size() const;
250 protected:
251  void update_window_transformation_array();
252 public:
254  void set_viewport(const ivec4& viewport, int array_index = -1);
256  void set_depth_range(const dvec2& depth_range = dvec2(0, 1), int array_index = -1);
257 
258  // return homogeneous 4x4 projection matrix, which transforms from clip to device space
259  // dmat4 get_device_matrix() const;
261  double get_window_z(int x_window, int y_window) const;
263 };
264 
265  }
266  }
267 }
268 
269 #include <cgv/config/lib_end.h>
cgv::render::gl::get_gl_format
GLuint get_gl_format(const texture &tex)
return the texture format used for a given texture. If called before texture has been created,...
Definition: gl_context.cxx:98
cgv::media::illum::light_source
>simple class to hold the properties of a light source
Definition: light_source.hh:17
cgv::render::render_component
base interface for all render components
Definition: context.h:246
cgv::data::component_format
Definition: component_format.h:58
cgv::render::gl::gl_context::info_font_size
float info_font_size
font size to draw textual info
Definition: gl_context.h:127
cgv::math::fmat
matrix of fixed size dimensions
Definition: fmat.h:23
cgv::data::CF_RGB
@ CF_RGB
color format with two components R and G
Definition: component_format.h:29
cgv::render::type_descriptor
compact type description of data that can be sent to the context; convertible to int
Definition: context.h:36
cgv::render::attribute_array_binding_base
base class for attribute_array_bindings
Definition: context.h:338
cgv::render::ContextIntegerConstant
ContextIntegerConstant
integer constants that can be queried from context
Definition: context.h:414
cgv::render::MaterialSide
MaterialSide
different sides of a material
Definition: context.h:113
cgv::render::ShaderType
ShaderType
different shader types
Definition: context.h:391
cgv::render::VertexBufferType
VertexBufferType
different vertex buffer types
Definition: context.h:349
cgv::type::info::TypeId
TypeId
ids for the different types and type constructs
Definition: type_id.h:12
cgv::math::fvec< double, 3 >
cgv::render::gl::gl_context
Definition: gl_context.h:33
cgv::data::const_data_view
Definition: data_view.h:211
cgv::render::gl::set_gl_format
void set_gl_format(texture &tex, GLuint gl_format, const std::string &component_format_description)
set a very specific texture format. This should be called after the texture is constructed and before...
Definition: gl_context.cxx:91
cgv::render::texture_base
base interface for a texture
Definition: context.h:269
cgv::data::ref_ptr< font_face >
cgv::render::vertex_buffer_base
base interface for a vertex buffer
Definition: context.h:365
cgv::media::illum::phong_material
>simple class to hold the material properties of a phong material
Definition: phong_material.hh:12
cgv::render::textured_material
class that extends obj_material with the management of textures
Definition: textured_material.h:13
cgv::render::shader_program
Definition: shader_program.h:25
cgv::media::color< float, cgv::media::RGB, cgv::media::OPACITY >
cgv::render::FrameBufferType
FrameBufferType
different frame buffer types which can be combined together with or
Definition: context.h:394
cgv::media::illum::surface_material
simple class to hold the material properties of a phong material
Definition: surface_material.h:26
cgv::data::data_view
Definition: data_view.h:155
cgv::render::frame_buffer_base
base interface for framebuffer
Definition: context.h:378
cgv::render::gl::gl_traits
Definition: gl.h:16
cgv::data::data_format
Definition: data_format.h:18
cgv::render::gl::gl_context::info_font_face
cgv::media::font::font_face_ptr info_font_face
font used to draw textual info
Definition: gl_context.h:125
cgv::type::info::TI_UINT8
@ TI_UINT8
signed integer stored in 64 bits
Definition: type_id.h:23
cgv::render::shader_program_base
base interface for shader programs
Definition: context.h:290
cgv::data::ComponentFormat
ComponentFormat
Definition: component_format.h:18
cgv::render::TextureType
TextureType
different texture types
Definition: context.h:155
cgv::render::RenderAPI
RenderAPI
enumeration of rendering APIs which can be queried from the context
Definition: context.h:59
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::render::context
Definition: context.h:525
cgv::render::PrimitiveType
PrimitiveType
different primitive types
Definition: context.h:177
cgv::render::gl::set_material
CGV_API void set_material(const cgv::media::illum::phong_material &mat, MaterialSide ms, float alpha)
set material in opengl state to given material
cgv::type::uint64_type
unsigned long long uint64_type
this type provides an 64 bit unsigned integer type
Definition: standard_types.h:22