3#include <cgv/render/context.h>
4#include <cgv/render/shader_program.h>
5#include <cgv_gl/gl/gl_context.h>
7#include "attribute_array_manager.h"
9#include "gl/lib_begin.h"
36 int current_prog_render_count;
39 std::set<int> enabled_attribute_arrays;
58 bool has_aam()
const {
return aam_ptr != 0 && aam_ptr != &default_aam; }
61 return aam_ptr->
has_attribute(ctx, get_prog_attribute_location(ctx, name,
false));
81 const T&
get_style()
const {
return *
static_cast<const T*
>(get_style_ptr()); }
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);
92 if(loc < 0 && error_check)
93 std::cerr <<
"Warning: cgv_gl::renderer attribute " << name <<
" not supported by current shader program" << std::endl;
99 bool set_attribute_array(
const context& ctx,
const std::string& name,
const T& array) {
100 int loc = get_prog_attribute_location(ctx, name);
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);
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);
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);
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);
121 template <
typename C,
typename T>
123 int loc = get_prog_attribute_location(ctx, name);
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));
132 template <
typename C,
typename T>
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)
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));
143 bool remove_attribute_array(
const context& ctx,
const std::string& name);
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);
154 void manage_singleton(
context& ctx,
const std::string& renderer_name,
int& ref_count,
int ref_count_change);
160 DEPRECATED(
"deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
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); }
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>
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);
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);
255 template <
typename T>
257 index_buffer_ptr = &vbo;
262 aam_ptr->remove_indices(ctx);
269 return index_count > 0;
272 void remove_indices(
const context& ctx);
277 return aam.
get_buffer_ptr(get_prog_attribute_location(ctx, attr_name));
286 virtual bool validate_attributes(
const context& ctx)
const;
288 bool validate_and_enable(
context& ctx);
292 virtual bool enable(
context& ctx);
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);
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);
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.
abstract base class for all renderers that handles a shader program and position / color attribute
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.
bool set_indices(const context &ctx, const vertex_buffer &vbo, size_t count)
Set the indices for indexed rendering from a GPU buffer.
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
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...
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...
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.
shader_program & ref_prog()
derived renderer classes have access to shader program
shader_define_map & ref_defines()
access to shader define map to update defines not handled by render style
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...
bool has_aam() const
check for attribute array manager
void set_color(const context &ctx, const T &color)
templated method to set the color attribute from a single color of type T
bool has_colors
track whether color attribute is defined
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
bool has_positions
track whether position attribute is defined
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
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
const vertex_buffer * get_vertex_buffer_ptr(const context &ctx, const attribute_array_manager &aam, const std::string &attr_name)
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
const vertex_buffer * get_index_buffer_ptr(const attribute_array_manager &aam)
bool attributes_persist() const
return whether attributes persist after a call to disable
bool has_indices() const
return whether indices have been defined
const T & get_style() const
access to style
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...
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
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...
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
PrimitiveType
different primitive types
TypeId
ids for the different types and type constructs
base class for all render styles
compact type description of data that can be sent to the context; convertible to int
template with a static member function get_id() of type TypeId returning the TypeId of the template a...