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 {
9 static int ref_count = 0;
10 static cone_renderer r;
11 r.manage_singleton(ctx, "cone_renderer", ref_count, ref_count_change);
12 return r;
13 }
14
19
21 {
22 radius = 1.0f;
23 radius_scale = 1.0f;
24
25 show_caps = true;
26 rounded_caps = false;
27
28 enable_texturing = false;
29 texture_blend_mode = TBM_MIX;
30 texture_blend_factor = 1.0f;
31 texture_tile_from_center = false;
32 texture_offset = vec2(0.0f);
33 texture_tiling = vec2(1.0f);
34 texture_use_reference_length = false;
35 texture_reference_length = 1.0f;
36
37 enable_ambient_occlusion = false;
38 ao_offset = 0.04f;
39 ao_distance = 0.8f;
40 ao_strength = 1.0f;
41
42 tex_offset = vec3(0.0f);
43 tex_scaling = vec3(1.0f);
44 tex_coord_scaling = vec3(1.0f);
45 texel_size = 1.0f;
46
47 cone_angle_factor = 1.0f;
48 sample_dirs.resize(3);
49 sample_dirs[0] = vec3(0.0f, 1.0f, 0.0f);
50 sample_dirs[1] = vec3(0.0f, 1.0f, 0.0f);
51 sample_dirs[2] = vec3(0.0f, 1.0f, 0.0f);
52 }
53
55 {
56 has_radii = false;
58 albedo_texture = nullptr;
59 density_texture = nullptr;
60 }
61
64 {
66 if (has_attribute(ctx, "radius"))
67 has_radii = true;
68 }
76 has_radii = false;
77 remove_attribute_array(ctx, "radius");
78 }
80 {
81 const cone_render_style& crs = get_style<cone_render_style>();
83 return res;
84 }
86 {
87 const cone_render_style& crs = get_style<cone_render_style>();
88 shader_code::set_define(defines, "CAPS", crs.show_caps, true);
89 shader_code::set_define(defines, "CAP_TYPE", crs.rounded_caps, false);
90 shader_code::set_define(defines, "ENABLE_TEXTURING", crs.enable_texturing, false);
91 shader_code::set_define(defines, "TEXTURE_BLEND_MODE", crs.texture_blend_mode, cone_render_style::TBM_MIX);
92 shader_code::set_define(defines, "TEXTURE_TILE_FROM_CENTER", crs.texture_tile_from_center, false);
93 shader_code::set_define(defines, "TEXTURE_USE_REFERENCE_LENGTH", crs.texture_use_reference_length, false);
94 shader_code::set_define(defines, "ENABLE_AMBIENT_OCCLUSION", crs.enable_ambient_occlusion, false);
95 }
97 {
98 return prog.build_program(ctx, "cone.glpr", true, defines);
99 }
100 bool cone_renderer::set_albedo_texture(texture* tex)
101 {
102 if(!tex || tex->get_nr_dimensions() != 2)
103 return false;
104 albedo_texture = tex;
105 return true;
106 }
107 bool cone_renderer::set_density_texture(texture* tex)
108 {
109 if(!tex || tex->get_nr_dimensions() != 3)
110 return false;
111 density_texture = tex;
112 return true;
113 }
116 {
117 if (!surface_renderer::enable(ctx))
118 return false;
119
120 if(!ref_prog().is_linked())
121 return false;
122
123 const cone_render_style& crs = get_style<cone_render_style>();
124 if(!has_radii)
125 ref_prog().set_attribute(ctx, "radius", crs.radius);
126
127 ref_prog().set_uniform(ctx, "radius_scale", crs.radius_scale);
128
129 if(crs.enable_texturing && !albedo_texture)
130 return false;
131
132 if(crs.enable_ambient_occlusion && !density_texture)
133 return false;
134
135 if(crs.enable_ambient_occlusion) {
136 ref_prog().set_uniform(ctx, "ao_offset", crs.ao_offset);
137 ref_prog().set_uniform(ctx, "ao_distance", crs.ao_distance);
138 ref_prog().set_uniform(ctx, "ao_strength", crs.ao_strength);
139 ref_prog().set_uniform(ctx, "density_tex_offset", crs.tex_offset);
140 ref_prog().set_uniform(ctx, "density_tex_scaling", crs.tex_scaling);
141 ref_prog().set_uniform(ctx, "tex_coord_scaling", crs.tex_coord_scaling);
142 ref_prog().set_uniform(ctx, "texel_size", crs.texel_size);
143 ref_prog().set_uniform(ctx, "cone_angle_factor", crs.cone_angle_factor);
144 ref_prog().set_uniform_array(ctx, "sample_dirs", crs.sample_dirs);
145 }
146
147 if(albedo_texture) {
148 ref_prog().set_uniform(ctx, "texture_blend_factor", crs.texture_blend_factor);
149 ref_prog().set_uniform(ctx, "texture_offset", crs.texture_offset);
150 ref_prog().set_uniform(ctx, "texture_tiling", crs.texture_tiling);
151 ref_prog().set_uniform(ctx, "texture_reference_length", crs.texture_reference_length);
152 albedo_texture->enable(ctx, 0);
153 }
154 if(density_texture) density_texture->enable(ctx, 1);
155
156 return true;
157 }
160 {
161 if(albedo_texture) albedo_texture->disable(ctx);
162 if(density_texture) density_texture->disable(ctx);
163
164 if(!attributes_persist()) {
165 has_radii = false;
166 }
167 return surface_renderer::disable(ctx);
168 }
169
170 bool cone_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
171 {
172 return
173 rh.reflect_base(*static_cast<surface_render_style*>(this)) &&
174 rh.reflect_member("radius", radius);
175 }
176
177 void cone_renderer::draw(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
178 {
179 draw_impl(ctx, PT_LINES, start, count, use_strips, use_adjacency, strip_restart_index);
180 }
181
183 {
184 renderer::clear(ctx);
185 albedo_texture = nullptr;
186 density_texture = nullptr;
187 }
188
190 {
192 }
193 }
194}
195
196#include <cgv/gui/provider.h>
197
198namespace cgv {
199 namespace gui {
200
203 bool create(provider* p, const std::string& label,
204 void* value_ptr, const std::string& value_type,
205 const std::string& gui_type, const std::string& options, bool*) {
207 return false;
208 cgv::render::cone_render_style* crs_ptr = reinterpret_cast<cgv::render::cone_render_style*>(value_ptr);
209 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
210
211 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");
212 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");
213
214 p->add_member_control(b, "Show Caps", crs_ptr->show_caps, "check");
215 p->add_member_control(b, "Rounded Caps", crs_ptr->rounded_caps, "check");
216
217 if(p->begin_tree_node("Texturing", crs_ptr->enable_texturing, false, "level=3")) {
218 p->align("\a");
219 p->add_member_control(b, "Enable", crs_ptr->enable_texturing, "check");
220 p->add_member_control(b, "Blend Mode", crs_ptr->texture_blend_mode, "dropdown", "enums='Mix,Tint,Multiply,Inverse Multiply,Add'");
221 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");
222 //p->add_member_control(b, "Texcoord Offset", crs_ptr->texcoord_offset, "value_slider", "min=-1.0;step=0.0001;max=1.0;ticks=true");
223 //p->add_member_control(b, "Texcoord Scale", crs_ptr->texcoord_scale, "value_slider", "min=-10.0;step=0.0001;max=10.0;ticks=true");
224
225 p->add_member_control(b, "Tile from Center", crs_ptr->texture_tile_from_center, "check");
226
227 p->add_member_control(b, "Offset", crs_ptr->texture_offset[0], "value", "w=95;min=-1;max=1;step=0.001", " ");
228 p->add_member_control(b, "", crs_ptr->texture_offset[1], "value", "w=95;min=-1;max=1;step=0.001");
229 p->add_member_control(b, "", crs_ptr->texture_offset[0], "slider", "w=95;min=-1;max=1;step=0.001;ticks=true", " ");
230 p->add_member_control(b, "", crs_ptr->texture_offset[1], "slider", "w=95;min=-1;max=1;step=0.001;ticks=true");
231
232 p->add_member_control(b, "Tiling", crs_ptr->texture_tiling[0], "value", "w=95;min=-5;max=5;step=0.001", " ");
233 p->add_member_control(b, "", crs_ptr->texture_tiling[1], "value", "w=95;min=-5;max=5;step=0.001");
234 p->add_member_control(b, "", crs_ptr->texture_tiling[0], "slider", "w=95;min=-5;max=5;step=0.001;ticks=true", " ");
235 p->add_member_control(b, "", crs_ptr->texture_tiling[1], "slider", "w=95;min=-5;max=5;step=0.001;ticks=true");
236
237 p->add_member_control(b, "Use Reference Length", crs_ptr->texture_use_reference_length, "check");
238 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");
239
240 p->align("\b");
241 p->end_tree_node(crs_ptr->enable_texturing);
242 }
243
244 if(p->begin_tree_node("Ambient Occlusion", crs_ptr->enable_ambient_occlusion, false, "level=3")) {
245 p->align("\a");
246 p->add_member_control(b, "Enable", crs_ptr->enable_ambient_occlusion, "check");
247 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");
248 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");
249 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");
250 p->align("\b");
251 }
252
253 p->add_gui("surface_render_style", *static_cast<cgv::render::surface_render_style*>(crs_ptr));
254 return true;
255 }
256 };
257
258#include "gl/lib_begin.h"
259
260 CGV_API cgv::gui::gui_creator_registration<cone_render_style_gui_creator> cone_rs_gc_reg("cone_render_style_gui_creator");
261 }
262}
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
render_style * create_render_style() const
overload to allow instantiation of cone_renderer
void update_defines(shader_define_map &defines)
update shader defines based on render style
virtual void clear(const context &ctx)
the clear function destructs the shader program and resets the texture pointers
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 disable(context &ctx)
disable renderer
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
shader_define_map shader_defines
the shader defines used to build the shader, used to comapre against new defines to determine if the ...
bool build_shader_program(context &ctx, shader_program &prog, const shader_define_map &defines)
build cone program
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_radius_array(const context &ctx)
remove the radius attribute
cone_renderer()
initializes member variables
bool validate_attributes(const context &ctx) const
call to validate, whether essential position attribute is defined
bool enable(context &ctx)
enables renderer
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
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:23
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
Definition renderer.h:60
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:75
virtual void clear(const context &ctx)
the clear function destructs the shader program
Definition renderer.cxx:349
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_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...
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
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:756
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:745
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
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
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
Definition fvec.h:667
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:669
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 ...
cone_render_style()
construct with default values
float radius
default value assigned to radius attribute in enable method of cone renderer, set to 1 in constructor
float radius_scale
multiplied to the sphere radii, initialized to 1
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