cgv
Loading...
Searching...
No Matches
renderer.h
1#pragma once
2
3#include <cgv/render/context.h>
4#include <cgv/render/shader_program.h>
5#include <cgv_gl/gl/gl_context.h>
6
7#include "attribute_array_manager.h"
8
9#include "gl/lib_begin.h"
10
11namespace cgv { // @<
12 namespace render { // @<
13
15 struct CGV_API render_style
16 {
17 virtual ~render_style();
18 };
19
21 class CGV_API renderer
22 {
23 private:
25 shader_program prog;
27 shader_program* prog_ptr;
29 shader_define_map defines;
31 shader_define_map last_defines;
32//#ifdef _DEBUG
33 /* TODO: FIXME: Find out why _DEBUG is sometimes not set in CMake Ninja builds under Linux, causing crashes
34 due to inconsistent object layout in memory between different modules using the renderers */
36 int current_prog_render_count;
37//#endif
39 std::set<int> enabled_attribute_arrays;
41 mutable render_style* default_render_style;
43 const render_style* rs;
45 const void* indices;
47 const vertex_buffer* index_buffer_ptr;
49 cgv::type::info::TypeId index_type;
51 size_t index_count;
53 attribute_array_manager default_aam;
56 protected:
58 bool has_aam() const { return aam_ptr != 0 && aam_ptr != &default_aam; }
60 bool has_attribute(const context& ctx, const std::string& name) {
61 return aam_ptr->has_attribute(ctx, get_prog_attribute_location(ctx, name, false));
62 }
64 const render_style* get_style_ptr() const;
66 bool attributes_persist() const { return has_aam(); }
68 virtual void update_defines(shader_define_map& defines) {}
70 virtual bool build_shader_program(context& ctx, shader_program& prog, const shader_define_map& defines) { return false; }
71 public:
73 shader_define_map& ref_defines() { return defines; }
75 shader_program& ref_prog() { return *prog_ptr; }
77 void set_prog(shader_program& one_shot_prog);
78 protected:
80 template <typename T>
81 const T& get_style() const { return *static_cast<const T*>(get_style_ptr()); }
83 mutable bool has_colors;
85 mutable bool has_positions;
87 virtual render_style* create_render_style() const = 0;
88
89 int get_prog_attribute_location(const context& ctx, const std::string & name, bool error_check = true) {
90 int loc = ref_prog().get_attribute_location(ctx, name);
91#ifdef _DEBUG
92 if(loc < 0 && error_check)
93 std::cerr << "Warning: cgv_gl::renderer attribute " << name << " not supported by current shader program" << std::endl;
94#endif
95 return loc;
96 }
97
98 template <typename T>
99 bool set_attribute_array(const context& ctx, const std::string& name, const T& array) {
100 int loc = get_prog_attribute_location(ctx, name);
101 if(loc < 0)
102 return false;
103 if(aam_ptr)
104 return aam_ptr->set_attribute_array(ctx, loc, array);
105 enabled_attribute_arrays.insert(loc);
106 return attribute_array_binding::set_global_attribute_array(ctx, loc, array);
107 }
108 template <typename T>
109 bool set_attribute_array(const context& ctx, const std::string& name, const T* array_ptr, size_t nr_elements, unsigned stride) {
110 int loc = get_prog_attribute_location(ctx, name);
111 if(loc < 0)
112 return false;
113 if (aam_ptr)
114 return aam_ptr->set_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
115 enabled_attribute_arrays.insert(loc);
116 return attribute_array_binding::set_global_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
117 }
118 bool set_attribute_array(const context& ctx, const std::string& name, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
119 //bool set_attribute_array(const context& ctx, int loc, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
121 template <typename C, typename T>
122 bool set_composed_attribute_array(const context& ctx, const std::string& name, const C* array_ptr, size_t nr_elements, const T& elem) {
123 int loc = get_prog_attribute_location(ctx, name);
124 if(loc < 0)
125 return false;
126 if (aam_ptr)
127 return aam_ptr->set_composed_attribute_array(ctx, loc, array_ptr, nr_elements, elem);
128 enabled_attribute_arrays.insert(loc);
129 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
130 }
132 template <typename C, typename T>
133 bool ref_composed_attribute_array(const context& ctx, const std::string& name, const std::string& name_ref, const C* array_ptr, size_t nr_elements, const T& elem) {
134 int loc = get_prog_attribute_location(ctx, name);
135 int loc_ref = get_prog_attribute_location(ctx, name_ref);
136 if(loc < 0 || loc_ref < 0)
137 return false;
138 if (aam_ptr)
139 return aam_ptr->ref_composed_attribute_array(ctx, loc, loc_ref, array_ptr, nr_elements, elem);
140 enabled_attribute_arrays.insert(loc);
141 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
142 }
143 bool remove_attribute_array(const context& ctx, const std::string& name);
144 public:
146 void draw_impl(context& ctx, PrimitiveType pt, size_t start, size_t count, bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
148 void draw_impl_instanced(context& ctx, PrimitiveType type, size_t start, size_t count, size_t instance_count, bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
150 renderer();
152 virtual ~renderer();
154 void manage_singleton(context& ctx, const std::string& renderer_name, int& ref_count, int ref_count_change);
156 virtual void enable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
158 virtual void disable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
160 DEPRECATED("deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
161 virtual void set_attribute_array_manager(const context& ctx, attribute_array_manager* _aam_ptr = 0);
163 void set_render_style(const render_style& rs);
165 bool build_program(context& ctx, shader_program& prog, const render_style& rs);
167
170 virtual bool init(context& ctx);
172 template <typename T>
173 void set_position(const context& ctx, const T& position) { has_positions = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "position"), position); }
175 template <typename T>
176 void set_position_array(const context& ctx, const std::vector<T>& positions) { has_positions = true; set_attribute_array(ctx, "position", positions); }
178 template <typename T>
179 void set_position_array(const context& ctx, const T* positions, size_t nr_elements, unsigned stride_in_bytes = 0) { has_positions = true; set_attribute_array(ctx, "position", positions, nr_elements, stride_in_bytes); }
181 void set_position_array(const context& ctx, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
183 template <typename T>
184 void set_position_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_position_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
186 void remove_position_array(const context& ctx);
188 template <typename T>
189 void set_color(const context& ctx, const T& color) { has_colors = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "color"), color); }
191 template <typename T>
192 void set_color_array(const context& ctx, const std::vector<T>& colors) { has_colors = true; set_attribute_array(ctx, "color", colors); }
194 template <typename T>
195 void set_color_array(const context& ctx, const T* colors, size_t nr_elements, unsigned stride_in_bytes = 0) { has_colors = true; set_attribute_array(ctx, "color", colors, nr_elements, stride_in_bytes); }
197 void set_color_array(const context& ctx, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0);
199 template <typename T>
200 void set_color_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_color_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
202 void remove_color_array(const context& ctx);
213 template <typename T>
214 bool set_indices(const context& ctx, const std::vector<T>& indices, bool keep_on_cpu = false) {
215 this->indices = &indices.front();
216 index_buffer_ptr = 0;
217 index_count = indices.size();
219 if (!keep_on_cpu && aam_ptr)
220 return aam_ptr->set_indices(ctx, indices);
221 return true;
222 }
234 template <typename T>
235 bool set_indices(const context& ctx, const T* indices, size_t nr_indices, bool keep_on_cpu = false) {
236 this->indices = indices;
237 index_buffer_ptr = 0;
238 index_count = nr_indices;
240 if (!keep_on_cpu && aam_ptr)
241 return aam_ptr->set_indices(ctx, indices, nr_indices);
242 return true;
243 }
245
255 template <typename T>
256 bool set_indices(const context& ctx, const vertex_buffer& vbo, size_t count) {
257 index_buffer_ptr = &vbo;
258 indices = 0;
259 index_count = count;
261 if (aam_ptr)
262 aam_ptr->remove_indices(ctx);
263 return true;
264 }
266 bool has_indices() const {
267 if(aam_ptr && aam_ptr->has_index_buffer())
268 return true;
269 return index_count > 0;
270 }
272 void remove_indices(const context& ctx);
276 const vertex_buffer* get_vertex_buffer_ptr(const context& ctx, const attribute_array_manager& aam, const std::string& attr_name) {
277 return aam.get_buffer_ptr(get_prog_attribute_location(ctx, attr_name));
278 }
283 return aam.get_buffer_ptr(-1);
284 }
286 virtual bool validate_attributes(const context& ctx) const;
288 bool validate_and_enable(context& ctx);
290
292 virtual bool enable(context& ctx);
294
305 virtual void draw(context& ctx, size_t start, size_t count,
306 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
308 virtual bool disable(context& ctx);
310
318 virtual bool render(context& ctx, size_t start, size_t count,
319 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
321 virtual void clear(const context& ctx);
322 };
323 }
324}
325
326#include <cgv/config/lib_end.h>
attribute array manager used to upload arrays to gpu
bool has_index_buffer() const
whether aam contains an index buffer
const vertex_buffer * get_buffer_ptr(int loc) const
returns the pointer to the vertex buffer as managed by this attribute array manager if specified and ...
bool has_attribute(const context &ctx, int loc) const
check whether the given attribute is available
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
abstract base class for all renderers that handles a shader program and position / color attribute
Definition renderer.h:22
bool set_indices(const context &ctx, const T *indices, size_t nr_indices, bool keep_on_cpu=false)
Set the indices for indexed rendering from an array given as a pointer.
Definition renderer.h:235
bool set_indices(const context &ctx, const vertex_buffer &vbo, size_t count)
Set the indices for indexed rendering from a GPU buffer.
Definition renderer.h:256
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
Definition renderer.h:60
bool ref_composed_attribute_array(const context &ctx, const std::string &name, const std::string &name_ref, const C *array_ptr, size_t nr_elements, const T &elem)
in case that several attributes are stored interleaved, call set_composed_attribute_array() for the f...
Definition renderer.h:133
bool set_composed_attribute_array(const context &ctx, const std::string &name, const C *array_ptr, size_t nr_elements, const T &elem)
in case that several attributes are stored interleaved, call this function for the first and ref_comp...
Definition renderer.h:122
bool set_indices(const context &ctx, const std::vector< T > &indices, bool keep_on_cpu=false)
Set the indices for indexed rendering from a vector.
Definition renderer.h:214
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:75
shader_define_map & ref_defines()
access to shader define map to update defines not handled by render style
Definition renderer.h:73
void set_position_array(const context &ctx, const vertex_buffer &vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the position attribute from a vertex buffer object, the element type must be g...
Definition renderer.h:184
bool has_aam() const
check for attribute array manager
Definition renderer.h:58
void set_color(const context &ctx, const T &color)
templated method to set the color attribute from a single color of type T
Definition renderer.h:189
bool has_colors
track whether color attribute is defined
Definition renderer.h:83
void set_color_array(const context &ctx, const T *colors, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the color attribute from a vector of colors of type T
Definition renderer.h:195
bool has_positions
track whether position attribute is defined
Definition renderer.h:85
virtual render_style * create_render_style() const =0
virtual method that creates a default render style
void set_position_array(const context &ctx, const std::vector< T > &positions)
templated method to set the position attribute from a vector of positions of type T
Definition renderer.h:176
void set_color_array(const context &ctx, const std::vector< T > &colors)
template method to set the color attribute from a vector of colors of type T
Definition renderer.h:192
const vertex_buffer * get_vertex_buffer_ptr(const context &ctx, const attribute_array_manager &aam, const std::string &attr_name)
Definition renderer.h:276
void set_position_array(const context &ctx, const T *positions, size_t nr_elements, unsigned stride_in_bytes=0)
templated method to set the position attribute from a vector of positions of type T
Definition renderer.h:179
const vertex_buffer * get_index_buffer_ptr(const attribute_array_manager &aam)
Definition renderer.h:282
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:66
bool has_indices() const
return whether indices have been defined
Definition renderer.h:266
const T & get_style() const
access to style
Definition renderer.h:81
void set_color_array(const context &ctx, const vertex_buffer &vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the color attribute from a vertex buffer object, the element type must be give...
Definition renderer.h:200
virtual bool build_shader_program(context &ctx, shader_program &prog, const shader_define_map &defines)
overload to build shader program based on the passed defines
Definition renderer.h:70
virtual void update_defines(shader_define_map &defines)
overload to update the shader defines based on the current render style; only called if internal shad...
Definition renderer.h:68
a shader program combines several shader code fragments to a complete definition of the shading pipel...
a vertex buffer is an unstructured memory block on the GPU.
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
PrimitiveType
different primitive types
Definition context.h:225
TypeId
ids for the different types and type constructs
Definition type_id.h:12
the cgv namespace
Definition print.h:11
base class for all render styles
Definition renderer.h:16
compact type description of data that can be sent to the context; convertible to int
Definition context.h:47
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition type_id.h:86