cgv
Loading...
Searching...
No Matches
rectangle_renderer.cxx
1#include "rectangle_renderer.h"
2#include <cgv_gl/gl/gl.h>
3#include <cgv_gl/gl/gl_tools.h>
4
5namespace cgv {
6 namespace render {
9 {
11 "RTM_REPLACE=0,"
12 "RTM_REPLACE_ALPHA,"
13 "RTM_MULTIPLY_COLOR,"
14 "RTM_MULTIPLY_SECONDARY_COLOR,"
15 "RTM_MULTIPLY_BORDER_COLOR,"
16 "RTM_MIX_COLOR_AND_SECONDARY_COLOR,"
17 "RTM_MIX_COLOR_AND_BORDER_COLOR,"
18 "RTM_MIX_SECONDARY_COLOR_AND_COLOR,"
19 "RTM_MIX_BORDER_COLOR_AND_COLOR,"
20 "RTM_RED_MIX_COLOR_AND_SECONDARY_COLOR,"
21 "RTM_RED_MIX_COLOR_AND_BORDER_COLOR,"
22 "RTM_RED_MIX_SECONDARY_COLOR_AND_COLOR,"
23 "RTM_RED_MIX_BORDER_COLOR_AND_COLOR"
24 );
25 }
28 {
30 "RBM_SEPARATE=0,"
31 "RBM_WIDTH,"
32 "RBM_HEIGHT,"
33 "RBM_MIN"
34 );
35 }
37 {
38 static int ref_count = 0;
39 static rectangle_renderer r;
40 r.manage_singleton(ctx, "rectangle_renderer", ref_count, ref_count_change);
41 return r;
42 }
43 void rectangle_renderer::set_y_view_angle(float _y_view_angle)
44 {
45 y_view_angle = _y_view_angle;
46 }
47
48 void rectangle_renderer::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)
49 {
51 set_attribute_array(ctx, "secondary_color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
52 }
53 void rectangle_renderer::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)
54 {
55 has_border_colors = true;
56 set_attribute_array(ctx, "border_color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
57 }
58 void rectangle_renderer::rectangle_renderer::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)
59 {
60 has_border_infos = true;
61 set_attribute_array(ctx, "border_info", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
62 }
64 has_extents = false;
65 remove_attribute_array(ctx, "extent");
66 }
69 remove_attribute_array(ctx, "secondary_color");
70 }
72 has_border_colors = false;
73 remove_attribute_array(ctx, "border_color");
74 }
76 has_border_infos = false;
77 remove_attribute_array(ctx, "border_info");
78 }
80 has_depth_offsets = false;
81 remove_attribute_array(ctx, "depth_offset");
82 }
84 has_translations = false;
85 remove_attribute_array(ctx, "translation");
86 }
88 has_rotations = false;
89 remove_attribute_array(ctx, "rotation");
90 }
92 {
94 if (has_attribute(ctx, "extent"))
95 has_extents = true;
96 if (has_attribute(ctx, "secondary_color"))
98 if (has_attribute(ctx, "border_color"))
99 has_border_colors = true;
100 if (has_attribute(ctx, "border_info"))
101 has_border_infos = true;
102 if (has_attribute(ctx, "translation"))
103 has_translations = true;
104 if (has_attribute(ctx, "rotation"))
105 has_rotations = true;
106 if (has_attribute(ctx, "depth_offset"))
107 has_depth_offsets = true;
108 }
109
122 {
123 has_positions = true;
124 has_extents = true;
125 has_texcoords = true;
127 ref_prog().set_attribute(ctx, "position", tcr.rectangle.get_min_pnt());
128 ref_prog().set_attribute(ctx, "extent", tcr.rectangle.get_max_pnt());
129 ref_prog().set_attribute(ctx, "texcoord", tcr.texcoords);
130 }
132 {
133 // diretly call init of top-most base class to prevent calling init of surface_renderer
134 return renderer::init(ctx);
135 }
136 void rectangle_renderer::set_position_is_center(bool _position_is_center)
137 {
138 get_style<rectangle_render_style>().position_is_center = _position_is_center;
139 }
141 {
142 const surface_render_style& srs = get_style<surface_render_style>();
144 if(!has_extents) {
145 ctx.error("rectangle_renderer::enable() extent attribute not set");
146 res = false;
147 }
148 return res;
149 }
151 {
153 return false;
154 const rectangle_render_style& rrs = get_style<rectangle_render_style>();
156 ref_prog().set_attribute(ctx, "depth_offset", rrs.default_depth_offset);
157 int sci = ref_prog().get_attribute_location(ctx, "secondary_color");
158 if(!has_secondary_colors && sci != -1)
159 ref_prog().set_attribute(ctx, "secondary_color", rrs.default_secondary_color);
161 ref_prog().set_attribute(ctx, "border_color", rrs.default_border_color);
162 if (!has_border_infos)
163 ref_prog().set_attribute(ctx, "border_info", vec3(rrs.border_width_in_pixel, rrs.percentual_border_width, float(rrs.border_mode)));
164 // configure opengl
165 if (rrs.blend_rectangles) {
166 ctx.push_blend_state();
167 ctx.enable_blending();
169 }
170 ref_prog().set_uniform(ctx, "has_rotations", has_rotations);
171 ref_prog().set_uniform(ctx, "has_translations", has_translations);
172 ref_prog().set_uniform(ctx, "position_is_center", get_style<rectangle_render_style>().position_is_center);
173 ref_prog().set_uniform(ctx, "use_texture", has_texcoords);
174 ref_prog().set_uniform(ctx, "viewport_height", (float)ctx.get_height());
175 ref_prog().set_uniform(ctx, "texture_mode", (const int&)rrs.texture_mode);
176 ref_prog().set_uniform(ctx, "pixel_blend", rrs.pixel_blend);
177 ref_prog().set_uniform(ctx, "border_width_in_pixel", rrs.border_width_in_pixel);
178 ref_prog().set_uniform(ctx, "percentual_border_width", rrs.percentual_border_width);
179 ref_prog().set_uniform(ctx, "border_mode", (const int&)rrs.border_mode);
180 return true;
181 }
183 {
184 if(!attributes_persist()) {
185 has_extents = false;
186 has_secondary_colors = false;
187 has_border_colors = false;
188 has_border_infos = false;
189 has_rotations = false;
190 has_translations = false;
191 has_depth_offsets = false;
192 }
193 const rectangle_render_style& rrs = get_style<rectangle_render_style>();
194 if (rrs.blend_rectangles)
195 ctx.pop_blend_state();
196 return surface_renderer::disable(ctx);
197 }
198 void rectangle_renderer::draw(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
199 {
200 draw_impl(ctx, PT_POINTS, start, count, false, false, -1);
201 }
202 bool rectangle_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
203 {
204 return
205 rh.reflect_base(*static_cast<surface_render_style*>(this)) &&
206 rh.reflect_member("pixel_blend", pixel_blend) &&
207 rh.reflect_member("texture_mode", texture_mode) &&
208 rh.reflect_member("blend_rectangles", blend_rectangles) &&
209 rh.reflect_member("border_width_in_pixel", border_width_in_pixel) &&
210 rh.reflect_member("percentual_border_width", percentual_border_width) &&
211 rh.reflect_member("border_mode", border_mode) &&
212 rh.reflect_member("default_depth_offset", default_depth_offset) &&
213 rh.reflect_member("default_secondary_color", default_secondary_color) &&
214 rh.reflect_member("default_border_color", default_border_color);
215 }
217 {
219 }
220 }
221}
222
223
224
225#include <cgv/gui/provider.h>
226
227namespace cgv {
228 namespace gui {
229
231 {
233 bool create(provider* p, const std::string& label,
234 void* value_ptr, const std::string& value_type,
235 const std::string& gui_type, const std::string& options, bool*)
236 {
238 return false;
239 cgv::render::rectangle_render_style* prs_ptr = reinterpret_cast<cgv::render::rectangle_render_style*>(value_ptr);
240 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
241
242 p->add_member_control(b, "Default Secondary Color", prs_ptr->default_secondary_color);
243
244 p->add_member_control(b, "Default Border Color", prs_ptr->default_border_color);
245 p->add_member_control(b, "Border Width in Pixel", prs_ptr->border_width_in_pixel, "value_slider", "min=-10;max=10;ticks=true");
246 p->add_member_control(b, "Percentual Border Width", prs_ptr->percentual_border_width, "value_slider", "min=-0.5;max=0.5;ticks=true");
247 p->add_member_control(b, "Border Mode", (cgv::type::DummyEnum&)prs_ptr->border_mode, "dropdown",
248 "enums='Separate=0,Width,Height,Minimum Width/Height'");
249 p->add_member_control(b, "Pixel Blend", prs_ptr->pixel_blend, "value_slider", "min=0.0;max=2;ticks=true");
250 p->add_member_control(b, "Texture Mode", (cgv::type::DummyEnum&)prs_ptr->texture_mode, "dropdown",
251 "enums='Replace,Replace Alpha,Multiply Color,Multiply Secondary Color,Multiply Border Color,"
252 "Mix Color and Secondary Color,Mix Color and Border Color,Mix Secondary Color and Color,Mix Border Color and Color,"
253 "Red Mix Color and Secondary Color,Red Mix Color and Border Color,Red Mix Secondary Color and Color,Red Mix Border Color and Color'");
254 p->add_member_control(b, "Blend", prs_ptr->blend_rectangles, "toggle");
255 p->add_member_control(b, "Default Depth Offset", prs_ptr->default_depth_offset, "value_slider", "min=0.000001;max=0.1;step=0.0000001;log=true;ticks=true");
256 p->add_member_control(b, "Position is Center", prs_ptr->position_is_center, "toggle");
257
258 if(p->begin_tree_node("Surface", prs_ptr->use_group_color, false, "level=3")) {
259 p->align("\a");
260 p->add_gui("surface_render_style", *static_cast<cgv::render::surface_render_style*>(prs_ptr));
261 p->align("\b");
262 p->end_tree_node(prs_ptr->use_group_color);
263 }
264 return true;
265 }
266 };
267
268#include "gl/lib_begin.h"
269
270 CGV_API cgv::gui::gui_creator_registration<rectangle_render_style_gui_creator> rectangle_rs_gc_reg("rectangle_render_style_gui_creator");
271
272 }
273}
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
helper template for registration of gui creators
Definition gui_creator.h:32
derive from this class to provide a gui to the current viewer
Definition provider.h:64
bool add_gui(const std::string &label, T &value, const std::string &gui_type="", const std::string &options="")
Add a composed gui of the given gui_type for the given value.
Definition provider.h:247
void align(const std::string &_align)
send pure alignment information
Definition provider.cxx:36
bool begin_tree_node(const std::string &label, const T &value, bool initial_visibility=false, const std::string &options="", gui_group_ptr ggp=gui_group_ptr())
Begin a sub tree of a tree structured gui.
Definition provider.h:212
data::ref_ptr< control< T > > add_member_control(cgv::base::base *base_ptr, const std::string &label, T &value, const std::string &gui_type="", const std::string &options="", const std::string &align="\n")
add control with callback to cgv::base::on_set method on cgv::gui::control::value_change
Definition provider.h:137
void end_tree_node(const T &value)
template specialization that allows to specify value reference plus node_instance by using the result...
Definition provider.h:222
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.
bool reflect_base(B &base_ref)
reflect a base class with its members
bool reflect_member(const std::string &member_name, T &member_ref, bool hard_cast=false)
call this to reflect a member by member name and reference to the member.
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
void set_blend_func_back_to_front()
set the default blend function for back to front blending
Definition context.cxx:1771
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition context.cxx:219
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1735
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1739
virtual unsigned int get_height() const =0
return the height of the window
virtual void enable_blending()
enable blending
Definition context.cxx:1775
bool validate_attributes(const context &ctx) const
check additionally the group attributes
renderer that supports plane rendering
void draw(context &ctx, size_t start, size_t count, bool use_strips=false, bool use_adjacency=false, uint32_t strip_restart_index=-1)
convenience function to render with default settings
void remove_secondary_color_array(const context &ctx)
remove the secondary color attribute
bool has_extents
whether extent array has been specified
void remove_depth_offset_array(const context &ctx)
remove the depth offset attribute
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
bool has_border_colors
whether border color or color array was set
bool validate_attributes(const context &ctx) const
check additionally the group attributes
bool has_secondary_colors
whether secondary color or color array was set
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...
bool has_depth_offsets
whether depth offset array has been specified
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_border_info_array(const context &ctx)
remove the border info attribute
bool init(context &ctx)
call init() once before using renderer
bool enable(context &ctx)
overload to activate group style
void remove_rotation_array(const context &ctx)
remove the rotation attribute
void remove_translation_array(const context &ctx)
remove the translation attribute
bool has_border_infos
whether border info or info array was set
bool disable(context &ctx)
disable renderer
bool has_translations
whether translation array has been specified
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 remove_extent_array(const context &ctx)
remove the extent attribute
void remove_border_color_array(const context &ctx)
remove the border color attribute
void set_position_is_center(bool _position_is_center)
set the flag of the render style, whether the position is interpreted as the box center
bool has_rotations
whether rotation array has been specified
void set_textured_rectangle(const context &ctx, const textured_rectangle &tcr)
specify rectangle without array. This sets position_is_center to false as well as position and extent...
void manage_singleton(context &ctx, const std::string &renderer_name, int &ref_count, int ref_count_change)
used by derived classes to manage singletons
Definition renderer.cxx:10
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
Definition renderer.h:62
virtual bool init(context &ctx)
call init() once before using renderer
Definition renderer.cxx:167
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:79
bool has_positions
track whether position attribute is defined
Definition renderer.h:92
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:70
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 type...
Definition renderer.cxx:248
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
int get_attribute_location(const context &ctx, const std::string &name) const
query location index of an attribute
bool set_attribute(const context &ctx, const std::string &name, const T &value)
set constant default value of a vertex attribute by attribute name, if name does not specify an attri...
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...
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
bool disable(context &ctx)
disable renderer
bool enable(context &ctx)
overload to activate group style
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
DummyEnum
some enum to mark an integral parameter to be of enum type
the cgv namespace
Definition print.h:11
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:670
interface for gui creators
Definition gui_creator.h:14
bool create(provider *p, const std::string &label, void *value_ptr, const std::string &value_type, const std::string &gui_type, const std::string &options, bool *)
attempt to create a gui and return whether this was successful
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 ...
bool use_group_color
whether to use group colors indexed through group index, defaults to false
configuration of rectangle renderer
rgba default_secondary_color
default value of secondary color which is ignored if set_secondary_color_array() is used to set per r...
bool blend_rectangles
if true the renderer enables blending in the enable method and recovers previous blending mode on dis...
RectangleBoderMode border_mode
different modes of computing the width of the border (default: RBM_MIN)
float pixel_blend
number of pixels around the rectangle splat used for antialiasing (default: 0.0f)
RectangleTextureMode texture_mode
mode of using texture during rastrization (default: RTM_REPLACE)
bool position_is_center
flag whether position attribute is the rectangle center; otherwise position is lower left corner (def...
float border_width_in_pixel
border width measured in pixels (default: 0)
rgba default_border_color
default value for the border color attribute which is ignored when set_border_color_array() is used t...
float default_depth_offset
default depth offset added to depth value of fragment. (default: 0.0f)
float percentual_border_width
border width measured relative to rectangle extent computed according to current border_mode (default...
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
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54