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 }
34 const cone_render_style& crs = get_style<cone_render_style>();
35 options.define_macro_if_true(crs.enable_texturing, "ENABLE_TEXTURE");
36 options.define_macro_if_true(crs.show_caps, "ENABLE_CAPS");
37 options.define_macro_if_true(crs.rounded_caps, "USE_ROUNDED_CONE");
38 options.define_macro_if_true(crs.enable_ambient_occlusion, "ENABLE_AMBIENT_OCCLUSION");
39 }
40 bool cone_renderer::set_albedo_texture(texture* tex) {
41 if(!tex || tex->get_nr_dimensions() != 2)
42 return false;
43 albedo_texture = tex;
44 return true;
45 }
46 bool cone_renderer::set_density_texture(texture* tex) {
47 if(!tex || tex->get_nr_dimensions() != 3)
48 return false;
49 density_texture = tex;
50 return true;
51 }
55 return false;
56
57 if(!ref_prog().is_linked())
58 return false;
59
60 const cone_render_style& crs = get_style<cone_render_style>();
61 if(!has_radii)
62 ref_prog().set_attribute(ctx, "radius", crs.radius);
63
64 ref_prog().set_uniform(ctx, "radius_scale", crs.radius_scale);
65
66 if(crs.enable_texturing && !albedo_texture)
67 return false;
68
69 if(crs.enable_ambient_occlusion && !density_texture)
70 return false;
71
72 if(crs.enable_ambient_occlusion) {
73 ref_prog().set_uniform(ctx, "ao_offset", crs.ao_offset);
74 ref_prog().set_uniform(ctx, "ao_distance", crs.ao_distance);
75 ref_prog().set_uniform(ctx, "ao_strength", crs.ao_strength);
76 ref_prog().set_uniform(ctx, "density_tex_offset", crs.tex_offset);
77 ref_prog().set_uniform(ctx, "density_tex_scaling", crs.tex_scaling);
78 ref_prog().set_uniform(ctx, "tex_coord_scaling", crs.tex_coord_scaling);
79 ref_prog().set_uniform(ctx, "texel_size", crs.texel_size);
80 ref_prog().set_uniform(ctx, "cone_angle_factor", crs.cone_angle_factor);
81 ref_prog().set_uniform_array(ctx, "sample_dirs", crs.sample_dirs);
82 }
83
84 if(albedo_texture) {
85 ref_prog().set_uniform(ctx, "texture_blend_factor", crs.texture_blend_factor);
86 ref_prog().set_uniform(ctx, "texture_offset", crs.texture_offset);
87 ref_prog().set_uniform(ctx, "texture_tiling", crs.texture_tiling);
88 ref_prog().set_uniform(ctx, "texture_reference_length", crs.texture_reference_length);
89 albedo_texture->enable(ctx, 0);
90 }
91 if(density_texture) density_texture->enable(ctx, 1);
92
93 return true;
94 }
97 if(albedo_texture) albedo_texture->disable(ctx);
98 if(density_texture) density_texture->disable(ctx);
99
100 if(!attributes_persist()) {
101 has_radii = false;
102 }
103 return surface_renderer::disable(ctx);
104 }
105
106 bool cone_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh) {
107 return
108 rh.reflect_base(*static_cast<surface_render_style*>(this)) &&
109 rh.reflect_member("radius", radius);
110 }
111
112 void cone_renderer::draw(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index) {
113 draw_impl(ctx, PT_LINES, start, count, use_strips, use_adjacency, strip_restart_index);
114 }
115
116 void cone_renderer::clear(const context& ctx) {
117 renderer::clear(ctx);
118 albedo_texture = nullptr;
119 density_texture = nullptr;
120 }
121
124 }
125 }
126}
127
128#include <cgv/gui/provider.h>
129
130namespace cgv {
131 namespace gui {
132
135 bool create(provider* p, const std::string& label,
136 void* value_ptr, const std::string& value_type,
137 const std::string& gui_type, const std::string& options, bool*) {
139 return false;
140 cgv::render::cone_render_style* crs_ptr = reinterpret_cast<cgv::render::cone_render_style*>(value_ptr);
141 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
142
143 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");
144 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");
145
146 p->add_member_control(b, "Show Caps", crs_ptr->show_caps, "check");
147 p->add_member_control(b, "Rounded Caps", crs_ptr->rounded_caps, "check");
148
149 if(p->begin_tree_node("Texturing", crs_ptr->enable_texturing, false, "level=3")) {
150 p->align("\a");
151 p->add_member_control(b, "Enable", crs_ptr->enable_texturing, "check");
152 p->add_member_control(b, "Blend Mode", crs_ptr->texture_blend_mode, "dropdown", "enums='Mix,Tint,Multiply,Inverse Multiply,Add'");
153 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");
154
155 p->add_member_control(b, "Tile from Center", crs_ptr->texture_tile_from_center, "check");
156
157 p->add_member_control(b, "Offset", crs_ptr->texture_offset[0], "value", "w=95;min=-1;max=1;step=0.001", " ");
158 p->add_member_control(b, "", crs_ptr->texture_offset[1], "value", "w=95;min=-1;max=1;step=0.001");
159 p->add_member_control(b, "", crs_ptr->texture_offset[0], "slider", "w=95;min=-1;max=1;step=0.001;ticks=true", " ");
160 p->add_member_control(b, "", crs_ptr->texture_offset[1], "slider", "w=95;min=-1;max=1;step=0.001;ticks=true");
161
162 p->add_member_control(b, "Tiling", crs_ptr->texture_tiling[0], "value", "w=95;min=-5;max=5;step=0.001", " ");
163 p->add_member_control(b, "", crs_ptr->texture_tiling[1], "value", "w=95;min=-5;max=5;step=0.001");
164 p->add_member_control(b, "", crs_ptr->texture_tiling[0], "slider", "w=95;min=-5;max=5;step=0.001;ticks=true", " ");
165 p->add_member_control(b, "", crs_ptr->texture_tiling[1], "slider", "w=95;min=-5;max=5;step=0.001;ticks=true");
166
167 p->add_member_control(b, "Use Reference Length", crs_ptr->texture_use_reference_length, "check");
168 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");
169
170 p->align("\b");
171 p->end_tree_node(crs_ptr->enable_texturing);
172 }
173
174 if(p->begin_tree_node("Ambient Occlusion", crs_ptr->enable_ambient_occlusion, false, "level=3")) {
175 p->align("\a");
176 p->add_member_control(b, "Enable", crs_ptr->enable_ambient_occlusion, "check");
177 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");
178 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");
179 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");
180 p->align("\b");
181 }
182
183 p->add_gui("surface_render_style", *static_cast<cgv::render::surface_render_style*>(crs_ptr));
184 return true;
185 }
186 };
187
188#include "gl/lib_begin.h"
189
190 CGV_API cgv::gui::gui_creator_registration<cone_render_style_gui_creator> cone_rs_gc_reg("cone_render_style_gui_creator");
191 }
192}
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:627
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.h:62
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:79
virtual void clear(const context &ctx)
the clear function destructs the shader program
Definition renderer.cxx:344
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
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 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