cgv
Loading...
Searching...
No Matches
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
10namespace cgv {
12 namespace render {
14 namespace gl {
15
17extern CGV_API std::vector<int> get_context_creation_attrib_list(cgv::render::context_config& cc);
18
20extern CGV_API void set_gl_format(texture& tex, GLuint gl_format, const std::string& component_format_description);
21
23extern CGV_API GLuint get_gl_format(const texture& tex);
24
25extern CGV_API GLuint map_to_gl(PrimitiveType pt);
26
27extern CGV_API GLuint map_to_gl(MaterialSide ms);
28
29extern CGV_API GLuint map_to_gl(BlendFunction blend_function);
30
31extern CGV_API GLboolean map_to_gl(bool flag);
32
33extern CGV_API GLuint get_gl_id(const void* handle);
34
35extern CGV_API void* get_handle(GLuint id);
36
38extern CGV_API void set_material(const cgv::media::illum::phong_material& mat, MaterialSide ms, float alpha);
39
40
43class CGV_API gl_context : public render::context
44{
45private:
46 int query_integer_constant(ContextIntegerConstant cic) const override;
47 GLuint texture_bind(TextureType tt, GLuint tex_id) const;
48 void texture_unbind(TextureType tt, GLuint tmp_id) const;
49 GLuint texture_generate(texture_base& tb) const;
50 /*
51 void frame_buffer_bind(const frame_buffer_base& fbb, void*& user_data) const;
52 void frame_buffer_unbind(const frame_buffer_base& fbb, void*& user_data) const;
53 void frame_buffer_bind(frame_buffer_base& fbb) const;
54 void frame_buffer_unbind(frame_buffer_base& fbb) const;
55 */
56protected:
57 shader_program progs[4];
58 mutable cgv::type::uint64_type max_nr_indices, max_nr_vertices;
59 void ensure_configured() const;
60 void destruct_render_objects() override;
61 void put_id(void* handle, void* ptr) const override;
62 void draw_elements_void(GLenum mode, size_t count, GLenum type, size_t type_size, const void* indices) const;
63 template <typename T>
64 void draw_elements(GLenum mode, size_t count, const T* indices) const {
66 error("called draw_elements with invalid index type");
67 else
68 draw_elements_void(mode, count, gl_traits<T>::type, sizeof(T), indices);
69 }
70
71 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 override;
72 bool texture_create(texture_base& tb, cgv::data::data_format& df) const override;
73 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 override;
74 bool texture_create_from_buffer(texture_base& tb, cgv::data::data_format& df, int x, int y, int level) const override;
75 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 override;
76 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 override;
77 bool texture_create_mipmaps(texture_base& tb, cgv::data::data_format& df) const override;
78 bool texture_generate_mipmaps(texture_base& tb, unsigned int dim) const override;
79 bool texture_destruct(texture_base& tb) const override;
80 bool texture_set_state(const texture_base& tb) const override;
81 bool texture_enable(texture_base& tb, int tex_unit, unsigned int nr_dims) const override;
82 bool texture_disable(texture_base& tb, int tex_unit, unsigned int nr_dims) const override;
83 bool texture_bind_as_image(texture_base& tb, int tex_unit, int level, bool bind_array, int layer, AccessType access) const override;
84
85 bool render_buffer_create(render_buffer_base& rc, cgv::data::component_format& cf, int& _width, int& _height) const override;
86 bool render_buffer_destruct(render_buffer_base& rc) const override;
87
88 bool frame_buffer_create(frame_buffer_base& fbb) const override;
89 bool frame_buffer_attach(frame_buffer_base& fbb, const render_buffer_base& rb, bool is_depth, int i) const override;
90 bool frame_buffer_attach(frame_buffer_base& fbb, const texture_base& t, bool is_depth, int level, int i, int z) const override;
91 bool frame_buffer_is_complete(const frame_buffer_base& fbb) const override;
92 bool frame_buffer_enable(frame_buffer_base& fbb) override;
93 bool frame_buffer_disable(frame_buffer_base& fbb) override;
94 bool frame_buffer_destruct(frame_buffer_base& fbb) const override;
95 void frame_buffer_blit(const frame_buffer_base* src_fbb_ptr, const ivec4& S, frame_buffer_base* dst_fbb_ptr, const ivec4& _D, BufferTypeBits btbs, bool interpolate) const override;
96 int frame_buffer_get_max_nr_color_attachments() const override;
97 int frame_buffer_get_max_nr_draw_buffers() const override;
98
99 bool shader_code_create(render_component& sc, ShaderType st, const std::string& source) const override;
100 bool shader_code_compile(render_component& sc) const override;
101 void shader_code_destruct(render_component& sc) const override;
102
103 bool shader_program_create(shader_program_base& spb) const override;
104 void shader_program_attach(shader_program_base& spb, const render_component& sc) const override;
105 bool shader_program_link(shader_program_base& spb) const override;
106 bool shader_program_set_state(shader_program_base& spb) const override;
107 bool shader_program_enable(shader_program_base& spb) override;
108 bool shader_program_disable(shader_program_base& spb) override;
109 void shader_program_detach(shader_program_base& spb, const render_component& sc) const override;
110 bool shader_program_destruct(shader_program_base& spb) const override;
111
112 int get_uniform_location(const shader_program_base& spb, const std::string& name) const override;
113 bool set_uniform_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const override;
114 bool set_uniform_array_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr, size_t nr_elements) const override;
115 int get_attribute_location(const shader_program_base& spb, const std::string& name) const override;
116 bool set_attribute_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const override;
117
118 bool attribute_array_binding_create(attribute_array_binding_base& aab) const override;
119 bool attribute_array_binding_destruct(attribute_array_binding_base& aab);
120 bool attribute_array_binding_enable(attribute_array_binding_base& aab) override;
121 bool attribute_array_binding_disable(attribute_array_binding_base& aab) override;
122 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 override;
123 bool set_element_array(attribute_array_binding_base* aab, const vertex_buffer_base* vbb) const override;
124 bool enable_attribute_array(attribute_array_binding_base* aab, int loc, bool do_enable) const override;
125 bool is_attribute_array_enabled(const attribute_array_binding_base* aab, int loc) const override;
126
127 bool vertex_buffer_bind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const override;
128 bool vertex_buffer_unbind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const override;
129 bool vertex_buffer_create(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const override;
130 bool vertex_buffer_resize(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const override;
131 bool vertex_buffer_replace(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, const void* array_ptr) const override;
132 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 override;
133 bool vertex_buffer_copy_back(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, void* array_ptr) const override;
134 bool vertex_buffer_destruct(vertex_buffer_base& vbb) const override;
135
136 bool check_gl_error(const std::string& where, const cgv::render::render_component* rc = 0) const;
137 bool check_texture_support(TextureType tt, const std::string& where, const cgv::render::render_component* rc = 0) const;
138 bool check_shader_support(ShaderType st, const std::string& where, const cgv::render::render_component* rc = 0) const;
139 bool check_fbo_support(const std::string& where, const cgv::render::render_component* rc = 0) const;
145 void draw_textual_info() override;
147 bool show_help;
148 bool show_stats;
150 void rotate_vector_to_target(const dvec3& vector, const dvec3& target);
151public:
153 gl_context();
155 bool configure_gl();
156 void resize_gl();
157
159 void set_bg_color(vec4 rgba) override;
161 void set_bg_depth(float d) override;
163 void set_bg_stencil(int s) override;
165 void set_bg_accum_color(vec4 rgba) override;
167 void clear_background(bool color_flag, bool depth_flag, bool stencil_flag = false, bool accum_flag = false) override;
168
170 float get_info_font_size() const;
172 cgv::media::font::font_face_ptr get_info_font_face() const;
174 float get_current_font_size() const override;
176 media::font::font_face_ptr get_current_font_face() const override;
178 void perform_screen_shot() override;
180 RenderAPI get_render_api() const override;
182 void init_render_pass() override;
184 void finish_render_pass() override;
188 void set_color(const rgba& clr) override;
190 void set_material(const cgv::media::illum::surface_material& mat) override;
192 void enable_material(textured_material& mat) override;
194 void disable_material(textured_material& mat) override;
196 shader_program& ref_default_shader_program(bool texture_support = false) override;
198 shader_program& ref_surface_shader_program(bool texture_support = false) override;
200 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 override;
202 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 override;
204 void on_lights_changed() override;
206 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) override;
208 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) override;
210 void draw_light_source(const cgv::media::illum::light_source& l, float intensity_scale, float light_scale) override;
212
213 void announce_external_viewport_change(ivec4& cgv_viewport_storage) override;
215 void recover_from_external_viewport_change(const ivec4& cgv_viewport_storage) override;
217 void announce_external_frame_buffer_change(void*& cgv_fbo_storage) override;
219 void recover_from_external_frame_buffer_change(void* cgv_fbo_storage) override;
220
224 void push_pixel_coords() override;
226 void pop_pixel_coords() override;
228 virtual bool read_frame_buffer(
229 data::data_view& dv,
230 unsigned int x = 0, unsigned int y = 0,
231 FrameBufferType buffer_type = FB_BACK,
232 cgv::type::info::TypeId type = type::info::TI_UINT8,
233 data::ComponentFormat cf = data::CF_RGB,
234 int w = -1, int h = -1) override;
236
240 bool prepare_attributes(std::vector<vec3>& P, std::vector<vec3>& N, std::vector<vec2>& T, unsigned nr_vertices,
241 const float* vertices, const float* normals, const float* tex_coords,
242 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals) const;
244 bool release_attributes(const float* normals, const float* tex_coords, const int* normal_indices, const int* tex_coord_indices) const;
246 void draw_edges_of_faces(
247 const float* vertices, const float* normals, const float* tex_coords,
248 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
249 int nr_faces, int face_degree, bool flip_normals = false) const override;
251 void draw_edges_of_strip_or_fan(
252 const float* vertices, const float* normals, const float* tex_coords,
253 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
254 int nr_faces, int face_degree, bool is_fan, bool flip_normals = false) const override;
256 void draw_faces(
257 const float* vertices, const float* normals, const float* tex_coords,
258 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
259 int nr_faces, int face_degree, bool flip_normals) const override;
261 void draw_strip_or_fan(
262 const float* vertices, const float* normals, const float* tex_coords,
263 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
264 int nr_faces, int face_degree, bool is_fan, bool flip_normals) const override;
266
269
271 void set_depth_test_state(DepthTestState state) override;
273 void set_depth_func(CompareFunction func) override;
275 void enable_depth_test() override;
277 void disable_depth_test() override;
278
280 void set_cull_state(CullingMode culling_mode) override;
281
283 void set_blend_state(BlendState state) override;
285 void set_blend_func(BlendFunction src_factor, BlendFunction dst_factor) override;
287 void set_blend_func_separate(BlendFunction src_color_factor, BlendFunction dst_color_factor, BlendFunction src_alpha_factor, BlendFunction dst_alpha_factor) override;
289 void enable_blending() override;
291 void disable_blending() override;
292
294 void set_buffer_mask(BufferMask mask) override;
296 void set_depth_mask(bool flag) override;
298 void set_color_mask(bvec4 flags) override;
299
301
305 dmat4 get_modelview_matrix() const override;
307 void set_modelview_matrix(const dmat4& V) override;
309 dmat4 get_projection_matrix() const override;
311 void set_projection_matrix(const dmat4& P) override;
312
314 void pop_window_transformation_array() override;
316 unsigned get_max_window_transformation_array_size() const override;
318
319protected:
320 void update_window_transformation_array();
321public:
323 void set_viewport(const ivec4& viewport, int array_index = -1) override;
325 void set_depth_range(const dvec2& depth_range = dvec2(0, 1), int array_index = -1) override;
326
327 // return homogeneous 4x4 projection matrix, which transforms from clip to device space
328 // dmat4 get_device_matrix() const;
330 double get_window_z(int x_window, int y_window) const override;
332};
333
334 }
335 }
336}
337
338#include <cgv/config/lib_end.h>
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
the component format inherits the information of a packing_info and adds information on the component...
The const_data_view has the functionality of the data_view but uses a const pointer and therefore doe...
Definition data_view.h:221
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
matrix of fixed size dimensions
Definition fmat.h:23
>simple class to hold the properties of a light source
>simple class to hold the material properties of a phong material
simple class to hold the material properties of a phong material
base class for attribute_array_bindings
Definition context.h:403
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
base interface for framebuffer
Definition context.h:471
implementation of the context API for the OpenGL API excluding methods for font selection,...
Definition gl_context.h:44
float info_font_size
font size to draw textual info
Definition gl_context.h:143
cgv::media::font::font_face_ptr info_font_face
font used to draw textual info
Definition gl_context.h:141
base interface for a render_buffer
Definition context.h:324
base interface for all render components
Definition context.h:301
base interface for shader programs
Definition context.h:355
a shader program combines several shader code fragments to a complete definition of the shading pipel...
base interface for a texture
Definition context.h:332
class that extends obj_material with the management of textures
base interface for a vertex buffer
Definition context.h:450
ComponentFormat
define standard formats, which should be used to avoid wrong assignment of component names
CGV_API void set_material(const cgv::media::illum::phong_material &mat, MaterialSide ms, float alpha)
set material in opengl state to given material
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...
std::vector< int > get_context_creation_attrib_list(cgv::render::context_config &cc)
construct a 0 terminated list of context creation attribute definitions
GLuint get_gl_format(const texture &tex)
return the texture format used for a given texture. If called before texture has been created,...
RenderAPI
enumeration of rendering APIs which can be queried from the context
Definition context.h:70
CullingMode
different culling modes
Definition context.h:143
BlendFunction
different blend functions
Definition context.h:150
AccessType
different access types
Definition context.h:266
FrameBufferType
different frame buffer types which can be combined together with or
Definition context.h:488
MaterialSide
different sides of a material
Definition context.h:128
ContextIntegerConstant
integer constants that can be queried from context
Definition context.h:508
ShaderType
different shader types
Definition context.h:485
PrimitiveType
different primitive types
Definition context.h:225
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:414
BufferTypeBits
Bits for the selection of different buffer types.
Definition context.h:461
TextureType
different texture types
Definition context.h:201
CompareFunction
different comparison functions used for depth testing or texture comparisons
Definition context.h:254
TypeId
ids for the different types and type constructs
Definition type_id.h:12
the cgv namespace
Definition print.h:11
Represents a blend state used to configure fragment blending.
Definition context.h:645
Represents a buffer mask used to mask depth and color buffer outputs.
Definition context.h:660
Represents a depth test state used to configure depth testing.
Definition context.h:637
configuration object used to define context parameters that need to be set already at creation time
Definition context.h:525
traits structure that maps a fundamental type to the Type enum.
Definition gl.h:16
compact type description of data that can be sent to the context; convertible to int
Definition context.h:47