cgv
Loading...
Searching...
No Matches
cgv::render::renderer Class Referenceabstract

abstract base class for all renderers that handles a shader program and position / color attribute More...

#include <renderer.h>

Inheritance diagram for cgv::render::renderer:
cgv::render::group_renderer cgv::render::volume_renderer cgv::render::line_renderer cgv::render::point_renderer cgv::render::slab_renderer cgv::render::surface_renderer cgv::render::box_wire_renderer cgv::render::normal_renderer cgv::render::arrow_renderer cgv::render::box_renderer cgv::render::cone_renderer cgv::render::ellipsoid_renderer cgv::render::rectangle_renderer cgv::render::sphere_renderer cgv::render::spline_tube_renderer cgv::render::surfel_renderer

Public Member Functions

shader_define_mapref_defines ()
 access to shader define map to update defines not handled by render style
 
shader_programref_prog ()
 derived renderer classes have access to shader program
 
void set_prog (shader_program &one_shot_prog)
 set external shader program up to next call to disable() or render()
 
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)
 default implementation of draw method with support for indexed rendering and different primitive types
 
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)
 default implementation of instanced draw method with support for indexed rendering and different primitive types
 
 renderer ()
 construct and init attribute tracking flags
 
virtual ~renderer ()
 destructor deletes default renderer style
 
void manage_singleton (context &ctx, const std::string &renderer_name, int &ref_count, int ref_count_change)
 used by derived classes to manage singletons
 
virtual void enable_attribute_array_manager (const context &ctx, attribute_array_manager &aam)
 call this before setting attribute arrays to manage attribute array in given manager
 
virtual void disable_attribute_array_manager (const context &ctx, attribute_array_manager &aam)
 call this after last render/draw call to ensure that no other users of renderer change attribute arrays of given manager
 
virtual void set_attribute_array_manager (const context &ctx, attribute_array_manager *_aam_ptr=0)
 this function is deprecated, please use enable_attribute_array_manager() and disable_attribute_manager() instead
 
void set_render_style (const render_style &rs)
 reference given render style
 
bool build_program (context &ctx, shader_program &prog, const render_style &rs)
 build shader program for specific render style
 
virtual bool init (context &ctx)
 call init() once before using renderer
 
template<typename T >
void set_position (const context &ctx, const T &position)
 templated method to set the position attribute from a single position of type T
 
template<typename T >
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
 
template<typename T >
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
 
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)
 method to set the position attribute from a vertex buffer object
 
template<typename T >
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 given as explicit template parameter
 
void remove_position_array (const context &ctx)
 remove the position attribute
 
template<typename T >
void set_color (const context &ctx, const T &color)
 templated method to set the color attribute from a single color of type T
 
template<typename 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
 
template<typename 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
 
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)
 method to set the color attribute from a vertex buffer object, the element type must be given as explicit template parameter
 
template<typename T >
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 given as explicit template parameter
 
void remove_color_array (const context &ctx)
 remove the color attribute
 
template<typename T >
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.
 
template<typename T >
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.
 
template<typename T >
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_indices () const
 return whether indices have been defined
 
void remove_indices (const context &ctx)
 remove previously set indices
 
const vertex_bufferget_vertex_buffer_ptr (const context &ctx, const attribute_array_manager &aam, const std::string &attr_name)
 
const vertex_bufferget_index_buffer_ptr (const attribute_array_manager &aam)
 
virtual bool validate_attributes (const context &ctx) const
 call to validate, whether essential position attribute is defined
 
bool validate_and_enable (context &ctx)
 validate attributes and if successful, enable renderer
 
virtual bool enable (context &ctx)
 enables renderer
 
virtual void draw (context &ctx, size_t start, size_t count, bool use_strips=false, bool use_adjacency=false, uint32_t strip_restart_index=-1)
 Draw a range of vertices or indexed elements.
 
virtual bool disable (context &ctx)
 disable renderer
 
virtual bool render (context &ctx, size_t start, size_t count, bool use_strips=false, bool use_adjacency=false, uint32_t strip_restart_index=-1)
 Convenience function that draws vertex or indexed element with this renderer.
 
virtual void clear (const context &ctx)
 the clear function destructs the shader program
 

Protected Member Functions

bool has_aam () const
 check for attribute array manager
 
bool has_attribute (const context &ctx, const std::string &name)
 check for attribute
 
const render_styleget_style_ptr () const
 access to render style
 
bool attributes_persist () const
 return whether attributes persist after a call to disable
 
virtual void update_defines (shader_define_map &defines)
 overload to update the shader defines based on the current render style; only called if internal shader program is used
 
virtual bool build_shader_program (context &ctx, shader_program &prog, const shader_compile_options &options)
 overload to build shader program based on the passed defines
 
template<typename T >
const T & get_style () const
 access to style
 
virtual render_stylecreate_render_style () const =0
 virtual method that creates a default render style
 
int get_prog_attribute_location (const context &ctx, const std::string &name, bool error_check=true)
 
template<typename T >
bool set_attribute_array (const context &ctx, const std::string &name, const T &array)
 
template<typename T >
bool set_attribute_array (const context &ctx, const std::string &name, const T *array_ptr, size_t nr_elements, unsigned stride)
 
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)
 
template<typename C , typename T >
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_composed_attribute_array() for all others
 
template<typename C , typename T >
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 first and this function for all others
 
bool remove_attribute_array (const context &ctx, const std::string &name)
 

Protected Attributes

bool has_colors
 track whether color attribute is defined
 
bool has_positions
 track whether position attribute is defined
 

Detailed Description

abstract base class for all renderers that handles a shader program and position / color attribute

Definition at line 21 of file renderer.h.

Constructor & Destructor Documentation

◆ renderer()

cgv::render::renderer::renderer ( )

construct and init attribute tracking flags

Definition at line 9 of file renderer.cxx.

References has_colors, and has_positions.

◆ ~renderer()

cgv::render::renderer::~renderer ( )
virtual

destructor deletes default renderer style

Definition at line 47 of file renderer.cxx.

Member Function Documentation

◆ attributes_persist()

◆ build_program()

bool cgv::render::renderer::build_program ( context ctx,
shader_program prog,
const render_style rs 
)

build shader program for specific render style

Definition at line 146 of file renderer.cxx.

References build_shader_program(), cgv::render::shader_compile_options::defines, and update_defines().

◆ build_shader_program()

◆ clear()

void cgv::render::renderer::clear ( const context ctx)
virtual

◆ create_render_style()

◆ disable()

◆ disable_attribute_array_manager()

◆ draw()

void cgv::render::renderer::draw ( context ctx,
size_t  start,
size_t  count,
bool  use_strips = false,
bool  use_adjacency = false,
uint32_t  strip_restart_index = -1 
)
virtual

Draw a range of vertices or indexed elements.

Call this function only successful enabeling via validate_and_enable() or enable(). Capsulates glDrawArrays and glDrawElements calls. Overloaded implementations of specific renderers choose the to be used gl primitive type and whether to use an instanced draw call.

See also
render()
Parameters
countnumber of to be drawn vertices/elements
startindex of first to be drawn vertex/element
use_stripswhether to generate primitives in strips (only for line or triangle primitives)
use_adjacencywhether to specify adjacency information (only for line or triangle primitives)
strip_restart_indexextraorindary index used to mark end of strips (only for strip based drawing) Strip based drawing can be combined with the use of adjacency information. Default implementation uses triangle primitives.

Reimplemented in cgv::render::arrow_renderer, cgv::render::box_renderer, cgv::render::box_wire_renderer, cgv::render::cone_renderer, cgv::render::ellipsoid_renderer, cgv::render::line_renderer, cgv::render::normal_renderer, cgv::render::point_renderer, cgv::render::rectangle_renderer, cgv::render::slab_renderer, cgv::render::sphere_renderer, cgv::render::spline_tube_renderer, cgv::render::surfel_renderer, and cgv::render::volume_renderer.

Definition at line 341 of file renderer.cxx.

Referenced by cgv::render::render_data_base< RendererType, RenderStyleType, ColorType >::draw().

◆ draw_impl()

void cgv::render::renderer::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 
)

◆ draw_impl_instanced()

void cgv::render::renderer::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 
)

default implementation of instanced draw method with support for indexed rendering and different primitive types

Definition at line 299 of file renderer.cxx.

References cgv::type::info::get_type_size(), and cgv::render::VBT_INDICES.

◆ enable()

◆ enable_attribute_array_manager()

◆ get_index_buffer_ptr()

const vertex_buffer * cgv::render::renderer::get_index_buffer_ptr ( const attribute_array_manager aam)
inline

Returns a pointer to the vertex buffer of type element buffer holding the indices for indexed rendering as managed by the attribute array manager. Returns nullptr if the buffer or attribute array manager does not exist. Take caution when manipulating the buffer.

Definition at line 282 of file renderer.h.

References cgv::render::attribute_array_manager::get_buffer_ptr().

◆ get_prog_attribute_location()

int cgv::render::renderer::get_prog_attribute_location ( const context ctx,
const std::string &  name,
bool  error_check = true 
)
inlineprotected

Definition at line 89 of file renderer.h.

◆ get_style()

template<typename T >
const T & cgv::render::renderer::get_style ( ) const
inlineprotected

access to style

Definition at line 81 of file renderer.h.

◆ get_style_ptr()

const render_style * cgv::render::renderer::get_style_ptr ( ) const
protected

access to render style

Definition at line 158 of file renderer.cxx.

References create_render_style().

Referenced by cgv::render::group_renderer::validate_attributes().

◆ get_vertex_buffer_ptr()

const vertex_buffer * cgv::render::renderer::get_vertex_buffer_ptr ( const context ctx,
const attribute_array_manager aam,
const std::string &  attr_name 
)
inline

Returns a pointer to the vertex buffer of the given attribute name as managed by the attribute array manager. Returns nullptr if the buffer or attribute array manager does not exist. Take caution when manipulating the buffer.

Definition at line 276 of file renderer.h.

References cgv::render::attribute_array_manager::get_buffer_ptr().

◆ has_aam()

bool cgv::render::renderer::has_aam ( ) const
inlineprotected

check for attribute array manager

Definition at line 58 of file renderer.h.

◆ has_attribute()

◆ has_indices()

bool cgv::render::renderer::has_indices ( ) const
inline

return whether indices have been defined

Definition at line 266 of file renderer.h.

References cgv::render::attribute_array_manager::has_index_buffer().

Referenced by remove_indices().

◆ init()

bool cgv::render::renderer::init ( context ctx)
virtual

call init() once before using renderer

creates default render style and builds shader program based on defines that can be configured with ref_defines() before calling init(). Reconfiguring defines after init() causes rebuild of shader program in enable() function.

Reimplemented in cgv::render::line_renderer, cgv::render::rectangle_renderer, and cgv::render::volume_renderer.

Definition at line 173 of file renderer.cxx.

References build_shader_program(), cgv::render::context_config::core_profile, create_render_style(), cgv::render::shader_compile_options::defines, and update_defines().

Referenced by cgv::app::transformation_gizmo::init(), cgv::app::navigator::init(), cgv::render::line_renderer::init(), cgv::render::rectangle_renderer::init(), cgv::render::volume_renderer::init(), and manage_singleton().

◆ manage_singleton()

◆ ref_composed_attribute_array()

template<typename C , typename T >
bool cgv::render::renderer::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 
)
inlineprotected

in case that several attributes are stored interleaved, call set_composed_attribute_array() for the first and this function for all others

Definition at line 133 of file renderer.h.

◆ ref_defines()

shader_define_map & cgv::render::renderer::ref_defines ( )
inline

access to shader define map to update defines not handled by render style

Definition at line 73 of file renderer.h.

◆ ref_prog()

◆ remove_attribute_array()

bool cgv::render::renderer::remove_attribute_array ( const context ctx,
const std::string &  name 
)
protected

Definition at line 89 of file renderer.cxx.

◆ remove_color_array()

void cgv::render::renderer::remove_color_array ( const context ctx)

remove the color attribute

Definition at line 115 of file renderer.cxx.

References has_colors.

◆ remove_indices()

void cgv::render::renderer::remove_indices ( const context ctx)

remove previously set indices

Definition at line 119 of file renderer.cxx.

References has_indices().

◆ remove_position_array()

void cgv::render::renderer::remove_position_array ( const context ctx)

remove the position attribute

Definition at line 106 of file renderer.cxx.

References has_positions.

◆ render()

bool cgv::render::renderer::render ( context ctx,
size_t  start,
size_t  count,
bool  use_strips = false,
bool  use_adjacency = false,
uint32_t  strip_restart_index = -1 
)
virtual

Convenience function that draws vertex or indexed element with this renderer.

This function effectively calls validate_and_enable(), draw() and disable(), passes its parameters to draw and returns the result of validate_and_enable(). draw() and disable() are only executed if validate_and_enable() succeeds. For performance reasons this function should not be used for several successive draw calls due to the unnecessary enabling and disabling between render calls. Typically, this function does not need to be overloaded by specific renderers.

See also
draw()
validate_and_enable()

Definition at line 346 of file renderer.cxx.

Referenced by vr_test::draw(), cgv::render::gl::gl_implicit_surface_drawable_base::draw_implicit_surface(), and vr_view_interactor::draw_vr_kits().

◆ set_attribute_array() [1/3]

template<typename T >
bool cgv::render::renderer::set_attribute_array ( const context ctx,
const std::string &  name,
const T &  array 
)
inlineprotected

Definition at line 99 of file renderer.h.

◆ set_attribute_array() [2/3]

template<typename T >
bool cgv::render::renderer::set_attribute_array ( const context ctx,
const std::string &  name,
const T *  array_ptr,
size_t  nr_elements,
unsigned  stride 
)
inlineprotected

Definition at line 109 of file renderer.h.

◆ set_attribute_array() [3/3]

bool cgv::render::renderer::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 
)
protected

Definition at line 79 of file renderer.cxx.

◆ set_attribute_array_manager()

void cgv::render::renderer::set_attribute_array_manager ( const context ctx,
attribute_array_manager _aam_ptr = 0 
)
virtual

this function is deprecated, please use enable_attribute_array_manager() and disable_attribute_manager() instead

Definition at line 72 of file renderer.cxx.

References disable_attribute_array_manager(), and enable_attribute_array_manager().

◆ set_color()

template<typename T >
void cgv::render::renderer::set_color ( const context ctx,
const T &  color 
)
inline

templated method to set the color attribute from a single color of type T

Definition at line 189 of file renderer.h.

◆ set_color_array() [1/4]

template<typename T >
void cgv::render::renderer::set_color_array ( const context ctx,
const std::vector< T > &  colors 
)
inline

template method to set the color attribute from a vector of colors of type T

Definition at line 192 of file renderer.h.

Referenced by vr_test::draw(), and vr_view_interactor::draw_vr_kits().

◆ set_color_array() [2/4]

template<typename T >
void cgv::render::renderer::set_color_array ( const context ctx,
const T *  colors,
size_t  nr_elements,
unsigned  stride_in_bytes = 0 
)
inline

template method to set the color attribute from a vector of colors of type T

Definition at line 195 of file renderer.h.

◆ set_color_array() [3/4]

template<typename T >
void cgv::render::renderer::set_color_array ( const context ctx,
const vertex_buffer vbo,
size_t  offset_in_bytes,
size_t  nr_elements,
unsigned  stride_in_bytes = 0 
)
inline

template method to set the color attribute from a vertex buffer object, the element type must be given as explicit template parameter

Definition at line 200 of file renderer.h.

References set_color_array().

Referenced by set_color_array().

◆ set_color_array() [4/4]

void cgv::render::renderer::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 
)

method to set the color attribute from a vertex buffer object, the element type must be given as explicit template parameter

Definition at line 110 of file renderer.cxx.

References has_colors.

◆ set_composed_attribute_array()

template<typename C , typename T >
bool cgv::render::renderer::set_composed_attribute_array ( const context ctx,
const std::string &  name,
const C *  array_ptr,
size_t  nr_elements,
const T &  elem 
)
inlineprotected

in case that several attributes are stored interleaved, call this function for the first and ref_composed_attribute_array() for all others

Definition at line 122 of file renderer.h.

◆ set_indices() [1/3]

template<typename T >
bool cgv::render::renderer::set_indices ( const context ctx,
const std::vector< T > &  indices,
bool  keep_on_cpu = false 
)
inline

Set the indices for indexed rendering from a vector.

If an attribute array manager is enabled and keep_on_cpu is false (default), create GPU index buffer and transfer indices into it.

Template Parameters
Tindex type must be uint8_t, uint16_t, or uint32_t
Parameters
ctxopengl context in which indexed rendering takes place
indicesvector of indices
keep_on_cpuflag whether indices should be kept in CPU memory
Returns
this can only fail if indices cannot be copied to GPU buffer

Definition at line 214 of file renderer.h.

◆ set_indices() [2/3]

template<typename T >
bool cgv::render::renderer::set_indices ( const context ctx,
const T *  indices,
size_t  nr_indices,
bool  keep_on_cpu = false 
)
inline

Set the indices for indexed rendering from an array given as a pointer.

If an attribute array manager is enabled and keep_on_cpu is false (default), create GPU index buffer and transfer indices into it.

Template Parameters
Tindex type must be uint8_t, uint16_t, or uint32_t
Parameters
ctxopengl context in which indexed rendering takes place
indicespointer to array containing the indices
nr_indicesnumber of indices in the array
keep_on_cpuflag whether indices should be kept in CPU memory
Returns
this can only fail if indices cannot be copied to GPU buffer

Definition at line 235 of file renderer.h.

◆ set_indices() [3/3]

template<typename T >
bool cgv::render::renderer::set_indices ( const context ctx,
const vertex_buffer vbo,
size_t  count 
)
inline

Set the indices for indexed rendering from a GPU buffer.

If an attribute array manager is enabled its index buffer is removed through this call.

Template Parameters
Tindex type must be uint8_t, uint16_t, or uint32_t
Parameters
ctxopengl context in which indexed rendering takes place
vboGPU buffer
countnumber of indices in the GPU buffer
Returns
in current implementation this succeeds always

Definition at line 256 of file renderer.h.

◆ set_position()

template<typename T >
void cgv::render::renderer::set_position ( const context ctx,
const T &  position 
)
inline

templated method to set the position attribute from a single position of type T

Definition at line 173 of file renderer.h.

◆ set_position_array() [1/4]

template<typename T >
void cgv::render::renderer::set_position_array ( const context ctx,
const std::vector< T > &  positions 
)
inline

templated method to set the position attribute from a vector of positions of type T

Definition at line 176 of file renderer.h.

Referenced by cgv::render::gl::gl_implicit_surface_drawable_base::draw_implicit_surface(), and cgv::render::volume_renderer::init().

◆ set_position_array() [2/4]

template<typename T >
void cgv::render::renderer::set_position_array ( const context ctx,
const T *  positions,
size_t  nr_elements,
unsigned  stride_in_bytes = 0 
)
inline

templated method to set the position attribute from a vector of positions of type T

Definition at line 179 of file renderer.h.

◆ set_position_array() [3/4]

template<typename T >
void cgv::render::renderer::set_position_array ( const context ctx,
const vertex_buffer vbo,
size_t  offset_in_bytes,
size_t  nr_elements,
unsigned  stride_in_bytes = 0 
)
inline

template method to set the position attribute from a vertex buffer object, the element type must be given as explicit template parameter

Definition at line 184 of file renderer.h.

References set_position_array().

Referenced by set_position_array().

◆ set_position_array() [4/4]

void cgv::render::renderer::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 
)

method to set the position attribute from a vertex buffer object

Definition at line 101 of file renderer.cxx.

References has_positions.

◆ set_prog()

void cgv::render::renderer::set_prog ( shader_program one_shot_prog)

set external shader program up to next call to disable() or render()

set external shader program for successive draw call only

Definition at line 168 of file renderer.cxx.

◆ set_render_style()

void cgv::render::renderer::set_render_style ( const render_style rs)

◆ update_defines()

virtual void cgv::render::renderer::update_defines ( shader_define_map defines)
inlineprotectedvirtual

overload to update the shader defines based on the current render style; only called if internal shader program is used

Reimplemented in cgv::render::box_renderer, cgv::render::cone_renderer, and cgv::render::volume_renderer.

Definition at line 68 of file renderer.h.

Referenced by build_program(), enable(), and init().

◆ validate_and_enable()

bool cgv::render::renderer::validate_and_enable ( context ctx)

validate attributes and if successful, enable renderer

Definition at line 198 of file renderer.cxx.

References enable(), and validate_attributes().

Referenced by vr_test::draw().

◆ validate_attributes()

Member Data Documentation

◆ has_colors

◆ has_positions

bool cgv::render::renderer::has_positions
mutableprotected

The documentation for this class was generated from the following files: