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"
38 int current_prog_render_count = 10;
41 std::set<int> enabled_attribute_arrays;
47 const void* indices =
nullptr;
53 size_t index_count = 0;
60 bool has_aam()
const {
return aam_ptr != 0 && aam_ptr != &default_aam; }
63 return aam_ptr->
has_attribute(ctx, get_prog_attribute_location(ctx, name,
false));
88 const T&
get_style()
const {
return *
static_cast<const T*
>(get_style_ptr()); }
90 mutable bool has_colors =
false;
92 mutable bool has_positions =
false;
94 int get_prog_attribute_location(
const context& ctx,
const std::string & name,
bool error_check =
true) {
95 int loc = ref_prog().get_attribute_location(ctx, name);
97 if(loc < 0 && error_check)
98 std::cerr <<
"Warning: cgv_gl::renderer attribute " << name <<
" not supported by current shader program" << std::endl;
103 template <
typename T>
104 bool set_attribute_array(
const context& ctx,
const std::string& name,
const T& array) {
105 int loc = get_prog_attribute_location(ctx, name);
109 return aam_ptr->set_attribute_array(ctx, loc, array);
110 enabled_attribute_arrays.insert(loc);
111 return attribute_array_binding::set_global_attribute_array(ctx, loc, array);
113 template <
typename T>
114 bool set_attribute_array(
const context& ctx,
const std::string& name,
const T* array_ptr,
size_t nr_elements,
unsigned stride) {
115 int loc = get_prog_attribute_location(ctx, name);
119 return aam_ptr->set_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
120 enabled_attribute_arrays.insert(loc);
121 return attribute_array_binding::set_global_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
123 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);
126 template <
typename C,
typename T>
128 int loc = get_prog_attribute_location(ctx, name);
132 return aam_ptr->set_composed_attribute_array(ctx, loc, array_ptr, nr_elements, elem);
133 enabled_attribute_arrays.insert(loc);
134 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements,
sizeof(C));
137 template <
typename C,
typename T>
139 int loc = get_prog_attribute_location(ctx, name);
140 int loc_ref = get_prog_attribute_location(ctx, name_ref);
141 if(loc < 0 || loc_ref < 0)
144 return aam_ptr->ref_composed_attribute_array(ctx, loc, loc_ref, array_ptr, nr_elements, elem);
145 enabled_attribute_arrays.insert(loc);
146 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements,
sizeof(C));
148 bool remove_attribute_array(
const context& ctx,
const std::string& name);
151 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);
153 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);
159 void manage_singleton(
context& ctx,
const std::string& renderer_name,
int& ref_count,
int ref_count_change);
165 DEPRECATED(
"deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
175 virtual
bool init(
context& ctx);
177 template <typename T>
178 void set_position(const
context& ctx, const T& position) { has_positions =
true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx,
"position"), position); }
180 template <
typename T>
181 void set_position_array(
const context& ctx,
const std::vector<T>& positions) { has_positions =
true; set_attribute_array(ctx,
"position", positions); }
183 template <
typename T>
184 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); }
188 template <
typename T>
189 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); }
191 void remove_position_array(
const context& ctx);
193 template <
typename T>
194 void set_color(
const context& ctx,
const T& color) { has_colors =
true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx,
"color"), color); }
196 template <
typename T>
197 void set_color_array(
const context& ctx,
const std::vector<T>& colors) { has_colors =
true; set_attribute_array(ctx,
"color", colors); }
199 template <
typename T>
200 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); }
202 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);
204 template <
typename T>
205 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); }
207 void remove_color_array(
const context& ctx);
218 template <
typename T>
220 this->indices = &indices.front();
221 index_buffer_ptr = 0;
222 index_count = indices.size();
224 if (!keep_on_cpu && aam_ptr)
225 return aam_ptr->set_indices(ctx, indices);
239 template <
typename T>
240 bool set_indices(
const context& ctx,
const T* indices,
size_t nr_indices,
bool keep_on_cpu =
false) {
241 this->indices = indices;
242 index_buffer_ptr = 0;
243 index_count = nr_indices;
245 if (!keep_on_cpu && aam_ptr)
246 return aam_ptr->set_indices(ctx, indices, nr_indices);
260 template <
typename T>
262 index_buffer_ptr = &vbo;
267 aam_ptr->remove_indices(ctx);
274 return index_count > 0;
277 void remove_indices(
const context& ctx);
282 return aam.
get_buffer_ptr(get_prog_attribute_location(ctx, attr_name));
291 virtual bool validate_attributes(
const context& ctx)
const;
293 bool validate_and_enable(
context& ctx);
297 virtual bool enable(
context& ctx);
310 virtual void draw(
context& ctx,
size_t start,
size_t count,
311 bool use_strips =
false,
bool use_adjacency =
false, uint32_t strip_restart_index = -1);
313 virtual bool disable(
context& ctx);
323 virtual bool render(
context& ctx,
size_t start,
size_t count,
324 bool use_strips =
false,
bool use_adjacency =
false, uint32_t strip_restart_index = -1);
326 virtual void clear(
const context& ctx);
331#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
shader_compile_options & ref_shader_options()
access to shader program compile options to update settings not handled by render style
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...
virtual void update_shader_program_options(shader_compile_options &options) const
overload to update the shader program compile options based on the current render style; only called ...
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
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
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
virtual render_style * create_render_style() const =0
implement this method to create 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
virtual std::string get_default_prog_name() const =0
implement this method to return the name of the default shader program; return an empty string if the...
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...
Stores preprocessor options used for conditionally compiling shader programs.
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.
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...