cgv
Loading...
Searching...
No Matches
cone_renderer.cxx
1#include "cone_renderer.h"
2#include <cgv_gl/gl/gl.h>
3#include <cgv_gl/gl/gl_tools.h>
4
5namespace cgv {
6 namespace render {
7 cone_renderer& ref_cone_renderer(context& ctx, int ref_count_change) {
8 static int ref_count = 0;
9 static cone_renderer r;
10 r.manage_singleton(ctx, "cone_renderer", ref_count, ref_count_change);
11 return r;
12 }
16 if(has_attribute(ctx, "radius"))
17 has_radii = true;
18 }
25 has_radii = false;
26 remove_attribute_array(ctx, "radius");
27 }
29 const cone_render_style& crs = get_style<cone_render_style>();
31 return res;
32 }
35 const cone_render_style& crs = get_style<cone_render_style>();
36 options.define_macro_if_true(crs.enable_texturing, "ENABLE_TEXTURE");
37 options.define_macro_if_true(crs.show_caps, "ENABLE_CAPS");
38 options.define_macro_if_true(crs.rounded_caps, "USE_ROUNDED_CONE");
39 options.define_macro_if_true(crs.enable_ambient_occlusion, "ENABLE_AMBIENT_OCCLUSION");
40 }
41 bool cone_renderer::set_albedo_texture(texture* tex) {
42 if(!tex || tex->get_nr_dimensions() != 2)
43 return false;
44 albedo_texture = tex;
45 return true;
46 }
47 bool cone_renderer::set_density_texture(texture* tex) {
48 if(!tex || tex->get_nr_dimensions() != 3)
49 return false;
50 density_texture = tex;
51 return true;
52 }
56 return false;
57
58 if(!ref_prog().is_linked())
59 return false;
60
61 const cone_render_style& crs = get_style<cone_render_style>();
62 if(!has_radii)
63 ref_prog().set_attribute(ctx, "radius", crs.radius);
64
65 ref_prog().set_uniform(ctx, "radius_scale", crs.radius_scale);
66
67 if(crs.enable_texturing && !albedo_texture)
68 return false;
69
70 if(crs.enable_ambient_occlusion && !density_texture)
71 return false;
72
73 if(crs.enable_ambient_occlusion) {
74 ref_prog().set_uniform(ctx, "ao_offset", crs.ao_offset);
75 ref_prog().set_uniform(ctx, "ao_distance", crs.ao_distance);
76 ref_prog().set_uniform(ctx, "ao_strength", crs.ao_strength);
77 ref_prog().set_uniform(ctx, "density_tex_offset", crs.tex_offset);
78 ref_prog().set_uniform(ctx, "density_tex_scaling", crs.tex_scaling);
79 ref_prog().set_uniform(ctx, "tex_coord_scaling", crs.tex_coord_scaling);
80 ref_prog().set_uniform(ctx, "texel_size", crs.texel_size);
81 ref_prog().set_uniform(ctx, "cone_angle_factor", crs.cone_angle_factor);
82 ref_prog().set_uniform_array(ctx, "sample_dirs", crs.sample_dirs);
83 }
84
85 if(albedo_texture) {
86 ref_prog().set_uniform(ctx, "texture_blend_factor", crs.texture_blend_factor);
87 ref_prog().set_uniform(ctx, "texture_offset", crs.texture_offset);
88 ref_prog().set_uniform(ctx, "texture_tiling", crs.texture_tiling);
89 ref_prog().set_uniform(ctx, "texture_reference_length", crs.texture_reference_length);
90 albedo_texture->enable(ctx, 0);
91 }
92 if(density_texture) density_texture->enable(ctx, 1);
93
94 return true;
95 }
98 if(albedo_texture) albedo_texture->disable(ctx);
99 if(density_texture) density_texture->disable(ctx);
100
101 if(!attributes_persist()) {
102 has_radii = false;
103 }
104 return surface_renderer::disable(ctx);
105 }
106
107 bool cone_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh) {
108 return
109 rh.reflect_base(*static_cast<surface_render_style*>(this)) &&
110 rh.reflect_member("radius", radius);
111 }
112
113 void cone_renderer::draw(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index) {
114 draw_impl(ctx, PT_LINES, start, count, use_strips, use_adjacency, strip_restart_index);
115 }
116
117 void cone_renderer::clear(const context& ctx) {
118 renderer::clear(ctx);
119 albedo_texture = nullptr;
120 density_texture = nullptr;
121 }
122
125 }
126 }
127}
128
129#include <cgv/gui/provider.h>
130
131namespace cgv {
132 namespace gui {
133
136 bool create(provider* p, const std::string& label,
137 void* value_ptr, const std::string& value_type,
138 const std::string& gui_type, const std::string& options, bool*) {
140 return false;
141 cgv::render::cone_render_style* crs_ptr = reinterpret_cast<cgv::render::cone_render_style*>(value_ptr);
142 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
143
144 p->add_member_control(b, "Default Radius", crs_ptr->radius, "value_slider", "min=0.001;step=0.0001;max=10.0;log=true;ticks=true");
145 p->add_member_control(b, "Radius Scale", crs_ptr->radius_scale, "value_slider", "min=0.01;step=0.0001;max=100.0;log=true;ticks=true");
146
147 p->add_member_control(b, "Show Caps", crs_ptr->show_caps, "check");
148 p->add_member_control(b, "Rounded Caps", crs_ptr->rounded_caps, "check");
149
150 if(p->begin_tree_node("Texturing", crs_ptr->enable_texturing, false, "level=3")) {
151 p->align("\a");
152 p->add_member_control(b, "Enable", crs_ptr->enable_texturing, "check");
153 p->add_member_control(b, "Blend Mode", crs_ptr->texture_blend_mode, "dropdown", "enums='Mix,Tint,Multiply,Inverse Multiply,Add'");
154 p->add_member_control(b, "Blend Factor", crs_ptr->texture_blend_factor, "value_slider", "min=0.0;step=0.0001;max=1.0;ticks=true");
155
156 p->add_member_control(b, "Tile from Center", crs_ptr->texture_tile_from_center, "check");
157
158 p->add_member_control(b, "Offset", crs_ptr->texture_offset[0], "value", "w=95;min=-1;max=1;step=0.001", " ");
159 p->add_member_control(b, "", crs_ptr->texture_offset[1], "value", "w=95;min=-1;max=1;step=0.001");
160 p->add_member_control(b, "", crs_ptr->texture_offset[0], "slider", "w=95;min=-1;max=1;step=0.001;ticks=true", " ");
161 p->add_member_control(b, "", crs_ptr->texture_offset[1], "slider", "w=95;min=-1;max=1;step=0.001;ticks=true");
162
163 p->add_member_control(b, "Tiling", crs_ptr->texture_tiling[0], "value", "w=95;min=-5;max=5;step=0.001", " ");
164 p->add_member_control(b, "", crs_ptr->texture_tiling[1], "value", "w=95;min=-5;max=5;step=0.001");
165 p->add_member_control(b, "", crs_ptr->texture_tiling[0], "slider", "w=95;min=-5;max=5;step=0.001;ticks=true", " ");
166 p->add_member_control(b, "", crs_ptr->texture_tiling[1], "slider", "w=95;min=-5;max=5;step=0.001;ticks=true");
167
168 p->add_member_control(b, "Use Reference Length", crs_ptr->texture_use_reference_length, "check");
169 p->add_member_control(b, "Reference Length", crs_ptr->texture_reference_length, "value_slider", "min=0.0;step=0.0001;max=5.0;log=true;ticks=true");
170
171 p->align("\b");
172 p->end_tree_node(crs_ptr->enable_texturing);
173 }
174
175 if(p->begin_tree_node("Ambient Occlusion", crs_ptr->enable_ambient_occlusion, false, "level=3")) {
176 p->align("\a");
177 p->add_member_control(b, "Enable", crs_ptr->enable_ambient_occlusion, "check");
178 p->add_member_control(b, "Offset", crs_ptr->ao_offset, "value_slider", "min=0.0;step=0.0001;max=0.2;log=true;ticks=true");
179 p->add_member_control(b, "Distance", crs_ptr->ao_distance, "value_slider", "min=0.0;step=0.0001;max=1.0;log=true;ticks=true");
180 p->add_member_control(b, "Strength", crs_ptr->ao_strength, "value_slider", "min=0.0;step=0.0001;max=10.0;log=true;ticks=true");
181 p->align("\b");
182 }
183
184 p->add_gui("surface_render_style", *static_cast<cgv::render::surface_render_style*>(crs_ptr));
185 return true;
186 }
187 };
188
189#include "gl/lib_begin.h"
190
191 CGV_API cgv::gui::gui_creator_registration<cone_render_style_gui_creator> cone_rs_gc_reg("cone_render_style_gui_creator");
192 }
193}
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
unsigned get_nr_dimensions() const
return the number of dimensions of the data set
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
renderer that supports raycasting of cones
void clear(const context &ctx) override
the clear function destructs the shader program and resets the texture pointers
void update_shader_program_options(shader_compile_options &options) const override
update shader program compile options based on render style
void draw(context &ctx, size_t start, size_t count, bool use_strips=false, bool use_adjacency=false, uint32_t strip_restart_index=-1) override
convenience function to render with default settings
bool enable(context &ctx) override
enables renderer
bool validate_attributes(const context &ctx) const override
call to validate, whether essential position attribute is defined
void disable_attribute_array_manager(const context &ctx, attribute_array_manager &aam) override
call this after last render/draw call to ensure that no other users of renderer change attribute arra...
bool disable(context &ctx) override
disable renderer
void enable_attribute_array_manager(const context &ctx, attribute_array_manager &aam) override
call this before setting attribute arrays to manage attribute array in given manager
void remove_radius_array(const context &ctx)
remove the radius attribute
base class for all drawables, which is independent of the used rendering API.
Definition context.h:672
bool validate_attributes(const context &ctx) const
check additionally the group attributes
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.cxx:48
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:129
virtual void clear(const context &ctx)
the clear function destructs the shader program
Definition renderer.cxx:348
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:120
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:252
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
void define_macro_if_true(bool predicate, const std::string &identifier)
Conditionally define identifier as a macro with replacement text 1.
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_uniform_array(const context &ctx, const std::string &name, const T &array)
set uniform array from array array where number elements can be derived from array through array_desc...
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 update_shader_program_options(shader_compile_options &options) const override
overload to update the shader program compile options based on the current render style; only called ...
void enable_attribute_array_manager(const context &ctx, attribute_array_manager &aam) override
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) override
call this after last render/draw call to ensure that no other users of renderer change attribute arra...
bool enable(context &ctx) override
overload to activate group style
bool disable(context &ctx) override
disable renderer
the texture class encapsulates all functionality independent of the rendering api.
Definition texture.h:15
bool disable(const context &ctx)
disable texture and restore state from before last enable call
Definition texture.cxx:774
bool enable(const context &ctx, int tex_unit=-1)
enable this texture in the given texture unit, -1 corresponds to the current unit.
Definition texture.cxx:763
cone_renderer & ref_cone_renderer(context &ctx, int ref_count_change)
reference to a singleton cone renderer that is shared among drawables
the cgv namespace
Definition print.h:11
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
interface for gui creators
Definition gui_creator.h:14
this reflection traits implementation is used for external self_reflect implementations of instances ...
float radius
default value assigned to radius attribute in enable method of cone renderer
float radius_scale
multiplied to the cone radii
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54