2#include "gl/gl_tools.h"
6 render_style::~render_style()
12 current_prog_render_count = 10;
15 rs = default_render_style = 0;
19 index_type = cgv::type::info::TI_UNDEF;
25 switch (ref_count_change) {
29 ctx.
error(std::string(
"unable to initialize ") + renderer_name +
" singleton");
37 ctx.
error(std::string(
"attempt to decrease reference count of ") + renderer_name +
" singleton below 0");
44 ctx.
error(std::string(
"invalid change reference count outside {-1,0,1} for ") + renderer_name +
" singleton");
49 if (default_render_style)
50 delete default_render_style;
51 default_render_style = 0;
68 aam_ptr = &default_aam;
79 bool 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)
81 int loc = get_prog_attribute_location(ctx, name);
85 return aam_ptr->set_attribute_array(ctx, loc, element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
86 enabled_attribute_arrays.insert(loc);
89 bool renderer::remove_attribute_array(
const context& ctx,
const std::string& name)
91 int loc = get_prog_attribute_location(ctx, name);
95 aam_ptr->remove_attribute_array(ctx, loc);
98 enabled_attribute_arrays.erase(loc);
104 set_attribute_array(ctx,
"position", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
108 remove_attribute_array(ctx,
"position");
113 set_attribute_array(ctx,
"color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
117 remove_attribute_array(ctx,
"color");
124 aam_ptr->remove_indices(ctx);
125 index_buffer_ptr = 0;
128 index_type = cgv::type::info::TI_UNDEF;
134 ctx.
error(
"renderer::enable() position attribute not set");
162 if (default_render_style)
163 return default_render_style;
165 return default_render_style;
170 prog_ptr = &one_shot_prog;
176 default_aam.init(ctx);
178 aam_ptr = &default_aam;
180 if (!default_render_style)
184 rs = default_render_style;
186 if (prog_ptr == &prog) {
190 last_defines = defines;
192 return default_render_style != 0;
205 if(prog_ptr == &prog) {
207 if(defines != last_defines) {
215 if(current_prog_render_count < 10)
216 std::cerr <<
"Performance warning: shader is rebuild! Consider using multiple instances of the renderer." << std::endl;
217 current_prog_render_count = 0;
219 ++current_prog_render_count;
221 last_defines = defines;
223 bool res = ref_prog().enable(ctx);
225 res = aam_ptr->enable(ctx);
233 res = aam_ptr->disable(ctx);
236 default_aam.disable(ctx);
237 for (
int loc : enabled_attribute_arrays)
238 res = default_aam.aab.disable_array(ctx, loc) && res;
241 for (
int loc : enabled_attribute_arrays)
242 res = attribute_array_binding::disable_global_array(ctx, loc) && res;
244 enabled_attribute_arrays.clear();
246 has_positions =
false;
249 res = ref_prog().disable(ctx) && res;
253 void renderer::draw_impl(
context& ctx,
PrimitiveType type,
size_t start,
size_t count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index)
255 if (use_strips && has_indices()) {
256 glPrimitiveRestartIndex(strip_restart_index);
257 glEnable(GL_PRIMITIVE_RESTART);
259 if (type == PT_LINES) {
262 type = PT_LINE_STRIP_ADJACENCY;
264 type = PT_LINES_ADJACENCY;
267 type = PT_LINE_STRIP;
269 else if (type == PT_TRIANGLES)
272 type = PT_TRIANGLE_STRIP_ADJACENCY;
274 type = PT_TRIANGLES_ADJACENCY;
277 type = PT_TRIANGLE_STRIP;
279 GLenum pt = gl::map_to_gl(type);
281 bool aam_has_index_buffer = aam_ptr && aam_ptr->has_index_buffer();
282 bool use_index_buffer = index_buffer_ptr && !aam_has_index_buffer;
283 bool use_indices = !index_buffer_ptr && !aam_has_index_buffer;
284 if (use_index_buffer)
286 const void* offset =
reinterpret_cast<const uint8_t*
>(use_indices ? indices : 0)
288 glDrawElements(pt, (GLsizei)count, gl::map_to_gl(index_type), offset);
289 if (use_index_buffer)
290 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
293 glDrawArrays(pt, (GLint)start, (GLsizei)count);
253 void renderer::draw_impl(
context& ctx,
PrimitiveType type,
size_t start,
size_t count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index) {
…}
295 void renderer::draw_impl_instanced(
context& ctx,
PrimitiveType type,
size_t start,
size_t count,
size_t instance_count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index)
297 if (use_strips && has_indices()) {
298 glPrimitiveRestartIndex(strip_restart_index);
299 glEnable(GL_PRIMITIVE_RESTART);
301 if (type == PT_LINES) {
304 type = PT_LINE_STRIP_ADJACENCY;
306 type = PT_LINES_ADJACENCY;
309 type = PT_LINE_STRIP;
311 else if (type == PT_TRIANGLES)
314 type = PT_TRIANGLE_STRIP_ADJACENCY;
316 type = PT_TRIANGLES_ADJACENCY;
319 type = PT_TRIANGLE_STRIP;
321 GLenum pt = gl::map_to_gl(type);
323 bool aam_has_index_buffer = aam_ptr && aam_ptr->has_index_buffer();
324 bool use_index_buffer = index_buffer_ptr && !aam_has_index_buffer;
325 bool use_indices = !index_buffer_ptr && !aam_has_index_buffer;
326 if (use_index_buffer)
328 const void* offset =
reinterpret_cast<const uint8_t*
>(use_indices ? indices : 0)
330 glDrawElementsInstanced(pt, (GLsizei)count, gl::map_to_gl(index_type), offset, (GLsizei)instance_count);
331 if (use_index_buffer)
332 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
335 glDrawArraysInstanced(pt, (GLint)start, (GLsizei)count, (GLsizei)instance_count);
295 void renderer::draw_impl_instanced(
context& ctx,
PrimitiveType type,
size_t start,
size_t count,
size_t instance_count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index) {
…}
337 void renderer::draw(
context& ctx,
size_t start,
size_t count,
338 bool use_strips,
bool use_adjacency, uint32_t strip_restart_index)
340 draw_impl(ctx, PT_TRIANGLES, start, count, use_strips, use_adjacency, strip_restart_index);
337 void renderer::draw(
context& ctx,
size_t start,
size_t count, {
…}
342 bool renderer::render(
context& ctx,
size_t start,
size_t count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index)
344 if (!validate_and_enable(ctx))
346 draw(ctx, start, count, use_strips, use_adjacency, strip_restart_index);
342 bool renderer::render(
context& ctx,
size_t start,
size_t count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index) {
…}
static bool set_global_attribute_array(const context &ctx, int loc, const vertex_buffer &vbo, type_descriptor td, size_t size, size_t offset, unsigned stride=0)
point array of vertex attribute at location loc to vertex buffer array array stored in CPU memory; in...
attribute array manager used to upload arrays to gpu
base class for all drawables, which is independent of the used rendering API.
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
virtual bool is_created() const
return whether component has been created
void manage_singleton(context &ctx, const std::string &renderer_name, int &ref_count, int ref_count_change)
used by derived classes to manage singletons
void remove_color_array(const context &ctx)
remove the color attribute
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
virtual bool enable(context &ctx)
enables renderer
bool validate_and_enable(context &ctx)
validate attributes and if successful, enable renderer
virtual bool init(context &ctx)
call init() once before using renderer
virtual ~renderer()
destructor deletes default renderer style
bool build_program(context &ctx, shader_program &prog, const render_style &rs)
build shader program for specific render style
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
void remove_position_array(const context &ctx)
remove the position attribute
virtual void clear(const context &ctx)
the clear function destructs the shader program
renderer()
construct and init attribute tracking flags
bool has_colors
track whether color attribute is defined
bool has_positions
track whether position attribute is defined
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 arra...
const render_style * get_style_ptr() const
access to render style
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
virtual bool validate_attributes(const context &ctx) const
call to validate, whether essential position attribute is defined
void set_render_style(const render_style &rs)
reference given render style
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
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_manage...
void set_prog(shader_program &one_shot_prog)
set external shader program up to next call to disable() or render()
bool has_indices() const
return whether indices have been defined
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
void remove_indices(const context &ctx)
remove previously set indices
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...
void destruct(const context &ctx)
destruct shader program
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
@ VBT_INDICES
The buffer contains indices and will be bound to GL_ELEMENT_ARRAY_BUFFER.
unsigned int get_type_size(TypeId tid)
function that returns the size of a type specified through TypeId
bool core_profile
default: true
base class for all render styles
compact type description of data that can be sent to the context; convertible to int