cgv
Loading...
Searching...
No Matches
rectangle_renderer.h
1#pragma once
2
3#include "surface_renderer.h"
4#include <cgv/reflect/reflect_enum.h>
5#include <cgv/render/textured_rectangle.h>
6
7#include "gl/lib_begin.h"
8
9namespace cgv {
10 namespace render {
11 class CGV_API rectangle_renderer;
12
14
17 extern CGV_API rectangle_renderer& ref_rectangle_renderer(context& ctx, int ref_count_change = 0);
18
21 {
22 RTM_REPLACE = 0, // result = texture
23 RTM_REPLACE_ALPHA, // result = vec4(color.rgb, texture.r)
24 RTM_MULTIPLY_COLOR, // result = color * texture
25 RTM_MULTIPLY_SECONDARY_COLOR, // result = secondary_color * texture
26 RTM_MULTIPLY_BORDER_COLOR, // result = border_color * texture
27 RTM_MIX_COLOR_AND_SECONDARY_COLOR, // result = mix(color,secondary_color,texture)
28 RTM_MIX_COLOR_AND_BORDER_COLOR, // result = mix(color,border_color,texture)
29 RTM_MIX_SECONDARY_COLOR_AND_COLOR, // result = mix(secondary_color,color,texture)
30 RTM_MIX_BORDER_COLOR_AND_COLOR, // result = mix(border_color,color,texture)
31 RTM_RED_MIX_COLOR_AND_SECONDARY_COLOR, // result = mix(color,secondary_color,texture.r)
32 RTM_RED_MIX_COLOR_AND_BORDER_COLOR, // result = mix(color,border_color,texture.r)
33 RTM_RED_MIX_SECONDARY_COLOR_AND_COLOR, // result = mix(secondary_color,color,texture.r)
34 RTM_RED_MIX_BORDER_COLOR_AND_COLOR // result = mix(border_color,color,texture.r)
35 };
38 {
39 RBM_SEPARATE = 0, // border_width.xy = percentual_border_width*extent.xy
40 RBM_WIDTH, // border_width.xy = vec2(percentual_border_width*extent.x)
41 RBM_HEIGHT, // border_width.xy = vec2(percentual_border_width*extent.y)
42 RBM_MIN // border_width.xy = vec2(percentual_border_width*min(extent.x, extent.y))
43 };
47 extern CGV_API cgv::reflect::enum_reflection_traits<RectangleBoderMode> get_reflection_traits(const RectangleBoderMode&);
52 {
54
60 mutable bool position_is_center = true;
62 rgba default_secondary_color = { 0.5f, 0.5f, 0.5f, 1.0f };
64 rgba default_border_color = { 0.0f, 0.0f, 0.0f, 1.0f };
66 float border_width_in_pixel = 0.0f;
68 float percentual_border_width = 0.0f;
70 RectangleBoderMode border_mode = RBM_MIN;
72 float pixel_blend = 0.0f;
74 RectangleTextureMode texture_mode = RTM_REPLACE;
76
78 float default_depth_offset = 0.0f;
80 bool blend_rectangles = false;
81 };
82
84 class CGV_API rectangle_renderer : public surface_renderer
85 {
86 protected:
88 bool has_extents = false;
90 bool has_secondary_colors = false;
92 bool has_border_colors = false;
94 bool has_border_infos = false;
96 bool has_translations = false;
98 bool has_rotations = false;
100 bool has_depth_offsets = false;
101 float y_view_angle = 45.0f;
103 std::string get_default_prog_name() const override { return "rectangle.glpr"; }
105 render_style* create_render_style() const override { return new rectangle_render_style(); }
106 public:
108 void set_y_view_angle(float y_view_angle);
110 void enable_attribute_array_manager(const context& ctx, attribute_array_manager& aam) override;
112 void disable_attribute_array_manager(const context& ctx, attribute_array_manager& aam) override;
114 bool init(context& ctx) override;
116 void set_position_is_center(bool _position_is_center);
118 template <typename T>
119 void set_extent(const context& ctx, const cgv::math::fvec<T, 2U>& extent) { has_extents = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "extent"), extent); }
121 template <typename T>
122 void set_extent_array(const context& ctx, const std::vector<cgv::math::fvec<T, 2U>>& extents) { has_extents = true; set_attribute_array(ctx, "extent", extents); }
124 template <typename T>
125 void set_extent_array(const context& ctx, const cgv::math::fvec<T, 2U>* extents, size_t nr_elements, unsigned stride_in_bytes = 0) { has_extents = true; set_attribute_array(ctx, "extent", extents, nr_elements, stride_in_bytes); }
127 void remove_extent_array(const context& ctx);
129 template <typename T>
131 has_positions = true;
132 has_extents = true;
133 set_position_is_center(false);
134 ref_prog().set_attribute(ctx, "position", box.get_min_pnt());
135 ref_prog().set_attribute(ctx, "extent", box.get_max_pnt());
136 }
138 template <typename T>
139 void set_rectangle_array(const context& ctx, const std::vector<cgv::media::axis_aligned_box<T, 2> >& boxes) {
140 set_composed_attribute_array(ctx, "position", &boxes.front(), boxes.size(), boxes[0].get_min_pnt());
141 ref_composed_attribute_array(ctx, "extent", "position", &boxes.front(), boxes.size(), boxes[0].get_max_pnt());
142 has_positions = true;
143 has_extents = true;
144 set_position_is_center(false);
145 }
147 template <typename T>
148 void set_rectangle_array(const context& ctx, const cgv::media::axis_aligned_box<T, 2>* boxes, size_t count) {
149 set_composed_attribute_array(ctx, "position", boxes, count, boxes[0].get_min_pnt());
150 ref_composed_attribute_array(ctx, "extent", "position", boxes, count, boxes[0].get_max_pnt());
151 has_positions = true;
152 has_extents = true;
153 set_position_is_center(false);
154 }
156 void set_textured_rectangle(const context& ctx, const textured_rectangle& tcr);
158 void set_textured_rectangle_array(const context& ctx, const std::vector<textured_rectangle>& tc_rects) {
159 set_composed_attribute_array(ctx, "position", &tc_rects.front(), tc_rects.size(), tc_rects[0].rectangle.get_min_pnt());
160 ref_composed_attribute_array(ctx, "extent", "position", &tc_rects.front(), tc_rects.size(), tc_rects[0].rectangle.get_max_pnt());
161 ref_composed_attribute_array(ctx, "texcoord", "position", &tc_rects.front(), tc_rects.size(), tc_rects[0].texcoords);
162 has_positions = true;
163 has_extents = true;
164 has_texcoords = true;
165 set_position_is_center(false);
166 }
168 void set_textured_rectangle_array(const context& ctx, const textured_rectangle* tc_rects, size_t count) {
169 set_composed_attribute_array(ctx, "position", tc_rects, count, tc_rects[0].rectangle.get_min_pnt());
170 ref_composed_attribute_array(ctx, "extent", "position", tc_rects, count, tc_rects[0].rectangle.get_max_pnt());
171 ref_composed_attribute_array(ctx, "texcoord", "position", tc_rects, count, tc_rects[0].texcoords);
172 has_positions = true;
173 has_extents = true;
174 has_texcoords = true;
175 set_position_is_center(false);
176 }
178 template <typename T>
179 void set_secondary_color(const context& ctx, const T& color) { has_secondary_colors = true; ref_prog().set_attribute(ctx, "secondary_color", color); }
181 template <typename T>
182 void set_secondary_color_array(const context& ctx, const std::vector<T>& colors) { has_secondary_colors = true; set_attribute_array(ctx, "secondary_color", colors); }
184 template <typename T>
185 void set_secondary_color_array(const context& ctx, const T* colors, size_t nr_elements, unsigned stride_in_bytes = 0) { has_secondary_colors = true; set_attribute_array(ctx, "secondary_color", colors, nr_elements, stride_in_bytes); }
187 void set_secondary_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);
189 template <typename T>
190 void set_secondary_color_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_secondary_color_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
192 void remove_secondary_color_array(const context& ctx);
193
195 template <typename T>
196 void set_border_color(const context& ctx, const T& color) { has_border_colors = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "border_color"), color); }
198 template <typename T>
199 void set_border_color_array(const context& ctx, const std::vector<T>& colors) { has_border_colors = true; set_attribute_array(ctx, "border_color", colors); }
201 template <typename T>
202 void set_border_color_array(const context& ctx, const T* colors, size_t nr_elements, unsigned stride_in_bytes = 0) { has_border_colors = true; set_attribute_array(ctx, "border_color", colors, nr_elements, stride_in_bytes); }
204 void set_border_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);
206 template <typename T>
207 void set_border_color_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_border_color_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
209 void remove_border_color_array(const context& ctx);
211 template <typename T>
212 void set_border_info(const context& ctx, const cgv::math::fvec<T, 3>& border_info) { has_border_infos = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "border_info"), border_info); }
214 template <typename T>
215 void set_border_info_array(const context& ctx, const std::vector<T>& border_infos) { has_border_infos = true; set_attribute_array(ctx, "border_info", border_infos); }
217 template <typename T>
218 void set_border_info_array(const context& ctx, const T* border_infos, size_t nr_elements, unsigned stride_in_bytes = 0) { has_border_infos = true; set_attribute_array(ctx, "border_info", border_infos, nr_elements, stride_in_bytes); }
220 void set_border_info_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);
222 template <typename T>
223 void set_border_info_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_border_info_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
225 void remove_border_info_array(const context& ctx);
226
228 template <typename T>
229 void set_depth_offset(const context& ctx, const T& depth_offset) { has_depth_offsets = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "depth_offset"), depth_offset); }
231 template <typename T = float>
232 void set_depth_offset_array(const context& ctx, const std::vector<T>& depth_offsets) { has_depth_offsets = true; set_attribute_array(ctx, "depth_offset", depth_offsets); }
234 void remove_depth_offset_array(const context& ctx);
236 template <typename T>
237 void set_translation(const context& ctx, const T& translation) { has_translations = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "translation"), translation); }
239 template <typename T>
240 void set_translation_array(const context& ctx, const std::vector<T>& translations) { has_translations = true; set_attribute_array(ctx, "translation", translations); }
242 template <typename T>
243 void set_translation_array(const context& ctx, const T* translations, size_t nr_elements, unsigned stride) { has_translations = true; set_attribute_array(ctx, "translation", translations, nr_elements, stride); }
245 void remove_translation_array(const context& ctx);
247 template <typename T>
248 void set_rotation(const context& ctx, const T& rotation) { has_rotations = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "rotation"), rotation); }
250 template <typename T>
251 void set_rotation_array(const context& ctx, const std::vector<T>& rotations) { has_rotations = true; set_attribute_array(ctx, "rotation", rotations); }
253 template <typename T>
254 void set_rotation_array(const context& ctx, const T* rotations, size_t nr_elements, unsigned stride = 0) { has_rotations = true; set_attribute_array(ctx, "rotation", rotations, nr_elements, stride); }
256 void remove_rotation_array(const context& ctx);
258 bool validate_attributes(const context& ctx) const override;
260 bool enable(context& ctx) override;
262 bool disable(context& ctx) override;
264 void draw(context& ctx, size_t start, size_t count,
265 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1) override;
266 };
268 {
269 bool self_reflect(cgv::reflect::reflection_handler& rh);
270 };
272 }
273}
274
275#include <cgv/config/lib_end.h>
A vector with zero based index.
Definition fvec.h:27
An axis aligned box, defined by to points: min and max.
const fpnt_type & get_max_pnt() const
return a const reference to corner 7
const fpnt_type & get_min_pnt() const
return a const reference to corner 0
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
attribute array manager used to upload arrays to gpu
base class for all drawables, which is independent of the used rendering API.
Definition context.h:627
renderer that supports plane rendering
void set_secondary_color(const context &ctx, const T &color)
templated method to set the secondary color attribute from a single color of type T
void set_border_color_array(const context &ctx, const T *colors, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the border color attribute from a vector of colors of type T
void set_depth_offset_array(const context &ctx, const std::vector< T > &depth_offsets)
set per rectangle depth offsets
void set_border_color_array(const context &ctx, const std::vector< T > &colors)
template method to set the border color attribute from a vector of colors of type T
void set_secondary_color_array(const context &ctx, const T *colors, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the secondary color attribute from a vector of colors of type T
void set_translation_array(const context &ctx, const T *translations, size_t nr_elements, unsigned stride)
template method to set the translations from a vector of vectors of type T, which should have 3 compo...
void set_extent_array(const context &ctx, const cgv::math::fvec< T, 2U > *extents, size_t nr_elements, unsigned stride_in_bytes=0)
extent array specifies plane side lengths from origin to edge
void set_textured_rectangle_array(const context &ctx, const std::vector< textured_rectangle > &tc_rects)
specify rectangle array directly. This sets position_is_center to false as well as position and exten...
render_style * create_render_style() const override
create and return the default render style
void set_rectangle(const context &ctx, const cgv::media::axis_aligned_box< T, 2 > &box)
specify a single rectangle without array. This sets position_is_center to false as well as position a...
void set_border_info_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 border_info attribute from a vertex buffer object, the element type must b...
void set_rotation_array(const context &ctx, const std::vector< T > &rotations)
template method to set the rotation from a vector of quaternions of type T, which should have 4 compo...
void set_textured_rectangle_array(const context &ctx, const textured_rectangle *tc_rects, size_t count)
specify ractangle array directly. This sets position_is_center to false as well as position and exten...
void set_rectangle_array(const context &ctx, const cgv::media::axis_aligned_box< T, 2 > *boxes, size_t count)
specify ractangle array directly. This sets position_is_center to false as well as position and exten...
void set_translation_array(const context &ctx, const std::vector< T > &translations)
template method to set the translations from a vector of vectors of type T, which should have 3 compo...
void set_border_info_array(const context &ctx, const std::vector< T > &border_infos)
templated method to set the border_info attribute array from a vector of border_infos of type T,...
void set_rotation(const context &ctx, const T &rotation)
set single rotation for all rectangles from a quaternion of type T, which has 4 components
void set_border_info_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 border_info attribute from a vertex buffer object, the element type must be given a...
void set_border_info(const context &ctx, const cgv::math::fvec< T, 3 > &border_info)
specify a single border_info for all lines
void set_secondary_color_array(const context &ctx, const std::vector< T > &colors)
template method to set the secondary color attribute from a vector of colors of type T
void set_border_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 border color attribute from a vertex buffer object, the element type must ...
void set_translation(const context &ctx, const T &translation)
template method to set translation for all rectangles from a vector type T, which should have 3 compo...
void set_rotation_array(const context &ctx, const T *rotations, size_t nr_elements, unsigned stride=0)
template method to set the rotation from a vector of quaternions of type T, which should have 4 compo...
void set_depth_offset(const context &ctx, const T &depth_offset)
specify a single depth_offset for all lines
void set_secondary_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 secondary color attribute from a vertex buffer object, the element type mu...
void set_extent_array(const context &ctx, const std::vector< cgv::math::fvec< T, 2U > > &extents)
extent array specifies plane side lengths from origin to edge
void set_border_color(const context &ctx, const T &color)
templated method to set the border color attribute from a single color of type T
void set_border_info_array(const context &ctx, const T *border_infos, size_t nr_elements, unsigned stride_in_bytes=0)
templated method to set the border_info attribute from an array of border_infos of type T,...
void set_extent(const context &ctx, const cgv::math::fvec< T, 2U > &extent)
specify a single extent for all boxes
std::string get_default_prog_name() const override
return the default shader program name
void set_rectangle_array(const context &ctx, const std::vector< cgv::media::axis_aligned_box< T, 2 > > &boxes)
specify rectangle array directly. This sets position_is_center to false as well as position and exten...
base classes for renderers that support surface rendering
a vertex buffer is an unstructured memory block on the GPU.
rectangle_renderer & ref_rectangle_renderer(context &ctx, int ref_count_change)
reference to a singleton plane renderer that can be shared among drawables
RectangleTextureMode
different modes to use texture during rectangle rendering
RectangleBoderMode
different modes to compute relative border with for rectangle rendering
the cgv namespace
Definition print.h:11
this type specific reflection traits class is used by the reflect_enum function to reflect enum types
this reflection traits implementation is used for external self_reflect implementations of instances ...
configuration of rectangle renderer
base class for all render styles
Definition renderer.h:16
This type provides a simple helper class to store rectangles with texture coordinates.
compact type description of data that can be sent to the context; convertible to int
Definition context.h:56