cgv
Loading...
Searching...
No Matches
line_renderer.cxx
1#include "line_renderer.h"
2#include <cgv_reflect_types/media/color.h>
3#include <cgv_gl/gl/gl.h>
4#include <cgv_gl/gl/gl_tools.h>
5
6namespace cgv {
7 namespace render {
8
9 line_renderer& ref_line_renderer(context& ctx, int ref_count_change)
10 {
11 static int ref_count = 0;
12 static line_renderer r;
13 r.manage_singleton(ctx, "line_renderer", ref_count, ref_count_change);
14 return r;
15 }
20
21 line_render_style::line_render_style() : default_color(0, 1, 1, 1)
22 {
23 default_normal = vec3(0.0f,0.0f,1.0f);
24 default_color = rgba(1.0f);
26 default_line_width = 1.0f;
27 blend_lines = false;
28 halo_color = rgba(0.0f,0.0f,0.0f,1.0);
31 screen_aligned = true;
33 reference_line_width = 0.001f;
36 }
39 {
41 if (has_attribute(ctx, "normal"))
42 has_normals = true;
43 if (has_attribute(ctx, "line_width"))
44 has_line_widths = true;
45 if (has_attribute(ctx, "depth_offset"))
46 has_depth_offsets = true;
47 }
50 {
52 has_normals = false;
53 has_line_widths = false;
54 has_depth_offsets = false;
55 }
57 has_normals = false;
58 remove_attribute_array(ctx, "normal");
59 }
61 has_line_widths = false;
62 remove_attribute_array(ctx, "line_width");
63 }
65 has_depth_offsets = false;
66 remove_attribute_array(ctx, "depth_offset");
67 }
69 {
70 has_normals = false;
71 has_line_widths = false;
72 has_depth_offsets = false;
73 }
75 {
76 return prog.build_program(ctx, "line.glpr", true, defines);
77 }
78
80 {
81 bool res = renderer::init(ctx);
82 int li;
83 li = get_prog_attribute_location(ctx, "normal", false);
84 if (li != -1)
85 ref_prog().set_attribute(ctx, li, vec3(0.0f,0.0f,1.0f));
86 li = get_prog_attribute_location(ctx, "depth_offset", false);
87 if (li != -1)
88 ref_prog().set_attribute(ctx, li, 0.0f);
89 li = get_prog_attribute_location(ctx, "line_width", false);
90 if (li != -1)
91 ref_prog().set_attribute(ctx, li, 1.0f);
92 return res;
93 }
94
96 {
97 const line_render_style& lrs = get_style<line_render_style>();
98 if (!group_renderer::enable(ctx))
99 return false;
100 // set program attributes
101 if (!has_colors)
102 ctx.set_color(lrs.default_color);
103 if (!has_normals) {
104 int li = get_prog_attribute_location(ctx, "normal", false);
105 if (li != -1)
106 ref_prog().set_attribute(ctx, li, lrs.default_normal);
107 }
108 if (!has_depth_offsets) {
109 int li = get_prog_attribute_location(ctx, "depth_offset", false);
110 if (li != -1)
112 }
113 if (!has_line_widths) {
114 int li = get_prog_attribute_location(ctx, "line_width", false);
115 if (li != -1)
117 }
118 // configure opengl
119 if (lrs.blend_lines) {
120 lrs.is_blend = glIsEnabled(GL_BLEND);
121 glGetIntegerv(GL_BLEND_DST, reinterpret_cast<GLint*>(&lrs.blend_dst));
122 glGetIntegerv(GL_BLEND_SRC, reinterpret_cast<GLint*>(&lrs.blend_src));
123 glEnable(GL_BLEND);
124 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125 }
126
127 // set program uniforms
128 ref_prog().set_uniform(ctx, "halo_color", lrs.halo_color);
129 ref_prog().set_uniform(ctx, "halo_width_in_pixel", lrs.halo_width_in_pixel);
130 ref_prog().set_uniform(ctx, "percentual_halo_width", 0.01f * lrs.percentual_halo_width);
131 ref_prog().set_uniform(ctx, "screen_aligned", lrs.screen_aligned);
132 ref_prog().set_uniform(ctx, "measure_line_width_in_pixel", lrs.measure_line_width_in_pixel);
133 ref_prog().set_uniform(ctx, "reference_line_width", lrs.reference_line_width);
134 ref_prog().set_uniform(ctx, "blend_width_in_pixel", lrs.blend_width_in_pixel);
135 ref_prog().set_uniform(ctx, "viewport_height", (float)ctx.get_height());
136 ref_prog().set_uniform(ctx, "halo_color_strength", lrs.halo_color_strength);
137 return true;
138 }
139
141 {
142 if (!attributes_persist()) {
143 has_normals = false;
144 has_line_widths = false;
145 has_depth_offsets = false;
146 }
147 const line_render_style& lrs = get_style<line_render_style>();
148 if (lrs.blend_lines) {
149 if (!lrs.is_blend)
150 glDisable(GL_BLEND);
151 glBlendFunc(lrs.blend_src, lrs.blend_dst);
152 }
153 return group_renderer::disable(ctx);
154 }
155
156 void line_renderer::draw(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
157 {
158 draw_impl(ctx, use_strips ? (use_adjacency ? PT_LINE_STRIP_ADJACENCY : PT_LINE_STRIP) : (use_adjacency ? PT_LINES_ADJACENCY : PT_LINES), start, count, use_strips, use_adjacency, strip_restart_index);
159 }
160
161 bool line_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
162 {
163 return
164 rh.reflect_base(*static_cast<cgv::render::group_render_style*>(this)) &&
165 rh.reflect_member("default_normal", default_normal) &&
166 rh.reflect_member("default_color", default_color) &&
167 rh.reflect_member("default_line_width", default_line_width) &&
168 rh.reflect_member("default_depth_offset", default_depth_offset) &&
169 rh.reflect_member("blend_lines", blend_lines) &&
170 rh.reflect_member("halo_color", halo_color) &&
171 rh.reflect_member("halo_width_in_pixel", halo_width_in_pixel) &&
172 rh.reflect_member("percentual_halo_width", percentual_halo_width) &&
173 rh.reflect_member("screen_aligned", screen_aligned) &&
174 rh.reflect_member("measure_line_width_in_pixel", measure_line_width_in_pixel) &&
175 rh.reflect_member("reference_line_width", reference_line_width) &&
176 rh.reflect_member("blend_width_in_pixel", blend_width_in_pixel) &&
177 rh.reflect_member("halo_color_strength", halo_color_strength);
178 }
179
181 {
183 }
184 }
185}
186
187#include <cgv/gui/provider.h>
188
189namespace cgv {
190 namespace gui {
191
193 {
195 bool create(provider* p, const std::string& label,
196 void* value_ptr, const std::string& value_type,
197 const std::string& gui_type, const std::string& options, bool*)
198 {
200 return false;
201 cgv::render::line_render_style* lrs_ptr = reinterpret_cast<cgv::render::line_render_style*>(value_ptr);
202 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
203 p->add_gui("Default Normal", lrs_ptr->default_normal, "direction", "options='min=-1;max=1;ticks=true'");
204 p->add_member_control(b, "Default Color", lrs_ptr->default_color);
205 p->add_member_control(b, "Screen Aligned", lrs_ptr->screen_aligned, "toggle");
206 p->add_member_control(b, "Default Line Width", lrs_ptr->default_line_width, "value_slider", "min=1;max=20;log=true;ticks=true;w=130", "");
207 p->add_member_control(b, "px", lrs_ptr->measure_line_width_in_pixel, "toggle", "w=16", "");
208 p->add_member_control(b, "Blend", lrs_ptr->blend_lines, "toggle", "w=50");
209 p->add_member_control(b, "Reference Line Width", lrs_ptr->reference_line_width, "value_slider", "min=0.0000001;max=1;step=0.00000001;log=true;ticks=true");
210 p->add_member_control(b, "Default Depth Offset", lrs_ptr->default_depth_offset, "value_slider", "min=-0.001;max=0.001;step=0.00000001;log=true;ticks=true");
211 bool show = p->begin_tree_node("Halo", lrs_ptr->halo_color, false, "options='w=120';level=3;align=''");
212 p->add_member_control(b, "Color", lrs_ptr->halo_color, "", "w=50");
213 if (show) {
214 p->align("\a");
215 p->add_member_control(b, "Halo Color Strength", lrs_ptr->halo_color_strength, "value_slider", "min=0;max=1;ticks=true");
216 p->add_member_control(b, "Halo Width in Pixel", lrs_ptr->halo_width_in_pixel, "value_slider", "min=-10;max=10;ticks=true");
217 p->add_member_control(b, "Percentual Halo Width", lrs_ptr->percentual_halo_width, "value_slider", "min=-100;max=100;ticks=true");
218 p->add_member_control(b, "Blend Width in Pixel", lrs_ptr->blend_width_in_pixel, "value_slider", "min=0;max=3;ticks=true");
219 p->align("\b");
220 p->end_tree_node(lrs_ptr->halo_color);
221 }
222 if (p->begin_tree_node("Use of Group Information", lrs_ptr->use_group_color, false, "level=3")) {
223 p->align("\a");
224 p->add_gui("group render style", *static_cast<cgv::render::group_render_style*>(lrs_ptr));
225 p->align("\b");
226 p->end_tree_node(lrs_ptr->use_group_color);
227 }
228 return true;
229 }
230 };
231
232#include "gl/lib_begin.h"
233
234CGV_API cgv::gui::gui_creator_registration<line_render_style_gui_creator> line_rs_gc_reg("line_render_style_gui_creator");
235
236 }
237}
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
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
virtual void set_color(const rgba &clr)
set the current color
Definition context.cxx:1617
virtual unsigned int get_height() const =0
return the height of the window
bool disable(context &ctx)
disable renderer
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 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 enable(context &ctx)
overload to activate group style
bool init(context &ctx)
call init() once before using renderer
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
render_style * create_render_style() const
virtual method that creates a default render style
bool build_shader_program(context &ctx, shader_program &prog, const shader_define_map &defines)
build line program
line_renderer()
construct line rendering
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 remove_depth_offset_array(const context &ctx)
remove the depth offset attribute
bool enable(context &ctx)
overload to activate group style
bool disable(context &ctx)
disable renderer
void remove_normal_array(const context &ctx)
remove the normal attribute
void remove_line_width_array(const context &ctx)
remove the line width attribute
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 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_colors
track whether color attribute is defined
Definition renderer.h:83
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,...
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.
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
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 reflection traits implementation is used for external self_reflect implementations of instances ...
render style used for group information
bool use_group_color
whether to use group colors indexed through group index, defaults to false
float blend_width_in_pixel
blend with in pixels used for line smoothing
float default_depth_offset
default depth offset for case when "depth_offset" attribute is not set
float default_line_width
default line width for case when "line_width" attribute is not set
vec3 default_normal
default normal for case when "normal" attribute is not set
float halo_width_in_pixel
halo width in pixel
bool measure_line_width_in_pixel
whether to measure line width in pixels - otherwise in eye space relative to reference_line_width
float reference_line_width
reference line width multiplied to line width if measure_line_width_in_pixel is false
bool screen_aligned
whether to span line splat in screen aligned coordinate system
float halo_color_strength
parameter in [0,1] to mix line color with halo color
float percentual_halo_width
halo width in percent of line width
rgba default_color
default color for case when "color" attribute is not set
line_render_style()
construct with default values
base class for all render styles
Definition renderer.h:16
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54