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
48
50 {
51 position_is_center = true;
52 default_secondary_color = rgba(0.5f, 0.5f, 0.5f, 1.0f);
53 default_border_color = rgba(0.0f, 0.0f, 0.0f, 1.0f);
56 border_mode = RBM_MIN;
57 pixel_blend = 0.0f;
58 texture_mode = RTM_REPLACE;
60 blend_rectangles = false;
61 //is_blend = GL_FALSE;
62 //blend_src = blend_dst = 0;
63 }
64 rectangle_renderer::rectangle_renderer() {
65 has_extents = false;
67 has_border_colors = false;
68 has_border_infos = false;
69 has_translations = false;
70 has_rotations = false;
71 has_texcoords = false;
72 has_depth_offsets = false;
73 y_view_angle = 45;
74 }
75
76 void rectangle_renderer::set_y_view_angle(float _y_view_angle)
77 {
78 y_view_angle = _y_view_angle;
79 }
80
81 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)
82 {
84 set_attribute_array(ctx, "secondary_color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
85 }
86 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)
87 {
88 has_border_colors = true;
89 set_attribute_array(ctx, "border_color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
90 }
91 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)
92 {
93 has_border_infos = true;
94 set_attribute_array(ctx, "border_info", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
95 }
97 has_extents = false;
98 remove_attribute_array(ctx, "extent");
99 }
101 has_secondary_colors = false;
102 remove_attribute_array(ctx, "secondary_color");
103 }
105 has_border_colors = false;
106 remove_attribute_array(ctx, "border_color");
107 }
109 has_border_infos = false;
110 remove_attribute_array(ctx, "border_info");
111 }
113 has_depth_offsets = false;
114 remove_attribute_array(ctx, "depth_offset");
115 }
117 has_translations = false;
118 remove_attribute_array(ctx, "translation");
119 }
121 has_rotations = false;
122 remove_attribute_array(ctx, "rotation");
123 }
125 {
127 if (has_attribute(ctx, "extent"))
128 has_extents = true;
129 if (has_attribute(ctx, "secondary_color"))
131 if (has_attribute(ctx, "border_color"))
132 has_border_colors = true;
133 if (has_attribute(ctx, "border_info"))
134 has_border_infos = true;
135 if (has_attribute(ctx, "translation"))
136 has_translations = true;
137 if (has_attribute(ctx, "rotation"))
138 has_rotations = true;
139 if (has_attribute(ctx, "depth_offset"))
140 has_depth_offsets = true;
141 }
142
155 {
156 has_positions = true;
157 has_extents = true;
158 has_texcoords = true;
160 ref_prog().set_attribute(ctx, "position", tcr.rectangle.get_min_pnt());
161 ref_prog().set_attribute(ctx, "extent", tcr.rectangle.get_max_pnt());
162 ref_prog().set_attribute(ctx, "texcoord", tcr.texcoords);
163 }
164
166 {
167 return prog.build_program(ctx, "rectangle.glpr", true, defines);
168 }
170 {
171 bool res = renderer::init(ctx);
172 return res;
173 }
174 void rectangle_renderer::set_position_is_center(bool _position_is_center)
175 {
176 get_style<rectangle_render_style>().position_is_center = _position_is_center;
177 }
179 {
180 const surface_render_style& srs = get_style<surface_render_style>();
182 if(!has_extents) {
183 ctx.error("rectangle_renderer::enable() extent attribute not set");
184 res = false;
185 }
186 return res;
187 }
189 {
191 return false;
192 const rectangle_render_style& rrs = get_style<rectangle_render_style>();
194 ref_prog().set_attribute(ctx, "depth_offset", rrs.default_depth_offset);
195 int sci = ref_prog().get_attribute_location(ctx, "secondary_color");
196 if(!has_secondary_colors && sci != -1)
197 ref_prog().set_attribute(ctx, "secondary_color", rrs.default_secondary_color);
199 ref_prog().set_attribute(ctx, "border_color", rrs.default_border_color);
200 if (!has_border_infos)
201 ref_prog().set_attribute(ctx, "border_info", vec3(rrs.border_width_in_pixel, rrs.percentual_border_width, float(rrs.border_mode)));
202 // configure opengl
203 if (rrs.blend_rectangles) {
204 ctx.push_blend_state();
205 ctx.enable_blending();
207 }
208 ref_prog().set_uniform(ctx, "has_rotations", has_rotations);
209 ref_prog().set_uniform(ctx, "has_translations", has_translations);
210 ref_prog().set_uniform(ctx, "position_is_center", get_style<rectangle_render_style>().position_is_center);
211 ref_prog().set_uniform(ctx, "use_texture", has_texcoords);
212 ref_prog().set_uniform(ctx, "viewport_height", (float)ctx.get_height());
213 ref_prog().set_uniform(ctx, "texture_mode", (const int&)rrs.texture_mode);
214 ref_prog().set_uniform(ctx, "pixel_blend", rrs.pixel_blend);
215 ref_prog().set_uniform(ctx, "border_width_in_pixel", rrs.border_width_in_pixel);
216 ref_prog().set_uniform(ctx, "percentual_border_width", rrs.percentual_border_width);
217 ref_prog().set_uniform(ctx, "border_mode", (const int&)rrs.border_mode);
218 return true;
219 }
221 {
222 if(!attributes_persist()) {
223 has_extents = false;
224 has_secondary_colors = false;
225 has_border_colors = false;
226 has_border_infos = false;
227 has_rotations = false;
228 has_translations = false;
229 has_depth_offsets = false;
230 }
231 const rectangle_render_style& rrs = get_style<rectangle_render_style>();
232 if (rrs.blend_rectangles)
233 ctx.pop_blend_state();
234 return surface_renderer::disable(ctx);
235 }
236 void rectangle_renderer::draw(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
237 {
238 draw_impl(ctx, PT_POINTS, start, count, false, false, -1);
239 }
240 bool rectangle_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
241 {
242 return
243 rh.reflect_base(*static_cast<surface_render_style*>(this)) &&
244 rh.reflect_member("pixel_blend", pixel_blend) &&
245 rh.reflect_member("texture_mode", texture_mode) &&
246 rh.reflect_member("blend_rectangles", blend_rectangles) &&
247 rh.reflect_member("border_width_in_pixel", border_width_in_pixel) &&
248 rh.reflect_member("percentual_border_width", percentual_border_width) &&
249 rh.reflect_member("border_mode", border_mode) &&
250 rh.reflect_member("default_depth_offset", default_depth_offset) &&
251 rh.reflect_member("default_secondary_color", default_secondary_color) &&
252 rh.reflect_member("default_border_color", default_border_color);
253 }
255 {
257 }
258 }
259}
260
261
262
263#include <cgv/gui/provider.h>
264
265namespace cgv {
266 namespace gui {
267
269 {
271 bool create(provider* p, const std::string& label,
272 void* value_ptr, const std::string& value_type,
273 const std::string& gui_type, const std::string& options, bool*)
274 {
276 return false;
277 cgv::render::rectangle_render_style* prs_ptr = reinterpret_cast<cgv::render::rectangle_render_style*>(value_ptr);
278 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
279
280 p->add_member_control(b, "Default Secondary Color", prs_ptr->default_secondary_color);
281
282 p->add_member_control(b, "Default Border Color", prs_ptr->default_border_color);
283 p->add_member_control(b, "Border Width in Pixel", prs_ptr->border_width_in_pixel, "value_slider", "min=-10;max=10;ticks=true");
284 p->add_member_control(b, "Percentual Border Width", prs_ptr->percentual_border_width, "value_slider", "min=-0.5;max=0.5;ticks=true");
285 p->add_member_control(b, "Border Mode", (cgv::type::DummyEnum&)prs_ptr->border_mode, "dropdown",
286 "enums='Separate=0,Width,Height,Minimum Width/Height'");
287 p->add_member_control(b, "Pixel Blend", prs_ptr->pixel_blend, "value_slider", "min=0.0;max=2;ticks=true");
288 p->add_member_control(b, "Texture Mode", (cgv::type::DummyEnum&)prs_ptr->texture_mode, "dropdown",
289 "enums='Replace,Replace Alpha,Multiply Color,Multiply Secondary Color,Multiply Border Color,"
290 "Mix Color and Secondary Color,Mix Color and Border Color,Mix Secondary Color and Color,Mix Border Color and Color,"
291 "Red Mix Color and Secondary Color,Red Mix Color and Border Color,Red Mix Secondary Color and Color,Red Mix Border Color and Color'");
292 p->add_member_control(b, "Blend", prs_ptr->blend_rectangles, "toggle");
293 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");
294 p->add_member_control(b, "Position is Center", prs_ptr->position_is_center, "toggle");
295
296 if(p->begin_tree_node("Surface", prs_ptr->use_group_color, false, "level=3")) {
297 p->align("\a");
298 p->add_gui("surface_render_style", *static_cast<cgv::render::surface_render_style*>(prs_ptr));
299 p->align("\b");
300 p->end_tree_node(prs_ptr->use_group_color);
301 }
302 return true;
303 }
304 };
305
306#include "gl/lib_begin.h"
307
308 CGV_API cgv::gui::gui_creator_registration<rectangle_render_style_gui_creator> rectangle_rs_gc_reg("rectangle_render_style_gui_creator");
309
310 }
311}
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:621
void set_blend_func_back_to_front()
set the default blend function for back to front blending
Definition context.cxx:1752
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:1716
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1720
virtual unsigned int get_height() const =0
return the height of the window
virtual void enable_blending()
enable blending
Definition context.cxx:1756
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
bool build_shader_program(context &ctx, shader_program &prog, const shader_define_map &defines)
build rectangle program
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
render_style * create_render_style() const
overload to allow instantiation of rectangle_renderer
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:23
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
Definition renderer.h:60
virtual bool init(context &ctx)
call init() once before using renderer
Definition renderer.cxx:173
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:75
bool has_positions
track whether position attribute is defined
Definition renderer.h:85
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:66
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:253
a shader program combines several shader code fragments to a complete definition of the shading pipel...
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...
bool build_program(const context &ctx, const std::string &file_name, bool show_error=false, const shader_define_map &defines=shader_define_map())
successively calls create, attach_program and link.
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.
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
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::media::color< float, cgv::media::RGB, cgv::media::OPACITY > rgba
declare rgba color type with 32 bit components
Definition color.h:855
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:669
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...
rectangle_render_style()
default constructor initializes members as specified in member comments
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...
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:47
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54