cgv
Loading...
Searching...
No Matches
surface_renderer.cxx
1#include "surface_renderer.h"
2#include <cgv_gl/gl/gl.h>
3#include <cgv_gl/gl/gl_tools.h>
4#include <cgv_gl/gl/gl_context.h>
5
6namespace cgv {
7 namespace render {
8
11 {
13 const auto& srs = get_style<surface_render_style>();
14 options.define_macro("MAX_NR_LIGHTS", srs.max_nr_lights);
15 }
18 {
19 bool res = group_renderer::build_shader_program(ctx, prog, options);
20 prog.allow_context_to_set_color(false);
21 return res;
22 }
23
26 {
28 if (has_attribute(ctx, "normal"))
29 has_normals = true;
30 if (has_attribute(ctx, "texcoord"))
31 has_texcoords = true;
32 }
35 {
37 has_normals = false;
38 has_texcoords = false;
39 }
41 has_normals = false;
42 remove_attribute_array(ctx, "normal");
43 }
45 has_texcoords = false;
46 remove_attribute_array(ctx, "texcoord");
47 }
49 void surface_renderer::set_normal_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)
50 {
51 has_normals = true;
52 set_attribute_array(ctx, "normal", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
53
54 }
56 void surface_renderer::set_texcoord_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)
57 {
58 has_texcoords = true;
59 set_attribute_array(ctx, "texcoord", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
60
61 }
63 {
64 bool res = group_renderer::enable(ctx);
65 const surface_render_style& srs = get_style<surface_render_style>();
66
67 if (cull_per_primitive) {
68 ctx.push_cull_state();
70 }
71 if (ref_prog().is_linked()) {
72 if (!has_colors) {
73 cgv::rgba col;
74 (cgv::rgb&)col = srs.surface_color;
75 col.opacity() = srs.surface_opacity;
76 ref_prog().set_attribute(ctx, "color", col);
77 }
78 ctx.set_material(srs.material);
79 ref_prog().set_uniform(ctx, "map_color_to_material", int(srs.map_color_to_material));
80 ref_prog().set_uniform(ctx, "culling_mode", int(srs.culling_mode));
81 ref_prog().set_uniform(ctx, "illumination_mode", int(srs.illumination_mode));
82 }
83 else
84 res = false;
85 return res;
86 }
87
89 {
90 const surface_render_style& srs = get_style<surface_render_style>();
91 if (cull_per_primitive) {
92 ctx.pop_cull_state();
93 }
94 if (!attributes_persist()) {
95 has_normals = false;
96 has_texcoords = false;
97 }
98 return group_renderer::disable(ctx);
99 }
100 bool surface_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
101 {
102 return
103 rh.reflect_base<group_render_style>(*this) &&
104 rh.reflect_member("culling_mode", culling_mode) &&
105 rh.reflect_member("illumination_mode", illumination_mode) &&
106 rh.reflect_member("map_color_to_material", map_color_to_material) &&
107 rh.reflect_member("surface_color", surface_color) &&
108 rh.reflect_member("max_nr_lights", max_nr_lights) &&
109 rh.reflect_member("material", material);
110 }
111
113 {
115 }
116 }
117}
118
119#include <cgv/gui/provider.h>
120
121namespace cgv {
122 namespace gui {
123
125 {
127 bool create(provider* p, const std::string& label,
128 void* value_ptr, const std::string& value_type,
129 const std::string& gui_type, const std::string& options, bool*)
130 {
132 return false;
133 cgv::render::surface_render_style* srs_ptr = reinterpret_cast<cgv::render::surface_render_style*>(value_ptr);
134 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
135 if (p->begin_tree_node("Color Mapping", srs_ptr->map_color_to_material, false, "level=3")) {
136 p->align("\a");
137 p->add_gui("Map Color to Material", srs_ptr->map_color_to_material, "bit_field_control",
138 "enums='Color Front=1,Color Back=2,Opacity Front=4,Opacity Back=8'");
139 p->align("\b");
141 }
142 p->add_member_control(b, "Illumination Mode", srs_ptr->illumination_mode, "dropdown", "enums='Off,One-Sided,Two-Sided'");
143 p->add_member_control(b, "Max Nr Light", srs_ptr->max_nr_lights, "value_slider", "min=1;max=8;ticks=true");
144 p->add_member_control(b, "Culling Mode", srs_ptr->culling_mode, "dropdown", "enums='Off,Backface,Frontface'");
145 if (p->begin_tree_node("Color and Materials", srs_ptr->surface_color, false, "level=3")) {
146 p->align("\a");
147 p->add_member_control(b, "Surface Color", srs_ptr->surface_color);
148 p->add_member_control(b, "Surface Opacity", srs_ptr->surface_opacity, "value_slider", "min=0.0;step=0.01;max=1.0;log=false;ticks=true");
149 if (p->begin_tree_node("Material", srs_ptr->material, false, "level=3")) {
150 p->align("\a");
151 p->add_gui("front_material", srs_ptr->material);
152 p->align("\b");
153 p->end_tree_node(srs_ptr->material);
154 }
155 p->align("\b");
156 p->end_tree_node(srs_ptr->surface_color);
157 }
158 if (p->begin_tree_node("Use of Group Information", srs_ptr->illumination_mode, false, "level=3")) {
159 p->align("\a");
160 p->add_gui("group render style", *static_cast<cgv::render::group_render_style*>(srs_ptr));
161 p->align("\b");
162 p->end_tree_node(srs_ptr->illumination_mode);
163 }
164 return true;
165 }
166 };
167
168#include "gl/lib_begin.h"
169
170CGV_API cgv::gui::gui_creator_registration<surface_render_style_gui_creator> surface_rs_gc_reg("surface_render_style_gui_creator");
171
172 }
173}
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:670
void push_cull_state()
push a copy of the current culling state onto the stack saved attributes: cull face enablement,...
Definition context.cxx:1805
virtual void set_material(const cgv::media::illum::surface_material &mat)
set the current material
Definition context.cxx:1738
virtual void set_cull_state(CullingMode culling_mode)
set the culling state
Definition context.cxx:1818
void pop_cull_state()
pop the top of the current culling state from the stack
Definition context.cxx:1809
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 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 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 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 bool build_shader_program(context &ctx, shader_program &prog, const shader_compile_options &options) const
overload to change default behaviour and build a custom shader program based on the passed options
Definition renderer.cxx:40
bool has_colors
track whether color attribute is defined
Definition renderer.h:140
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:120
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
void define_macro(const std::string &identifier)
Define a macro as identifier and no replacement text.
Definition shader_code.h:99
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...
void set_texcoord_array(const context &ctx, const std::vector< T > &texcoords)
templated method to set the texcoord attribute array from a vector of texcoords of type T
void set_normal_array(const context &ctx, const std::vector< T > &normals)
templated method to set the normal attribute array from a vector of normals of type T,...
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 ...
bool build_shader_program(context &ctx, shader_program &prog, const shader_compile_options &options) const override
overload to disable context from setting color in shader program
void remove_normal_array(const context &ctx)
remove the normal attribute
void remove_texcoord_array(const context &ctx)
remove the texcoord attribute
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
a vertex buffer is an unstructured memory block on the GPU.
the cgv namespace
Definition print.h:11
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
CullingMode culling_mode
culling mode for point splats, defaults to CM_OFF
float surface_opacity
default value for the surface opacity when map color to material is used
int max_nr_lights
maximum number of supported lights (change triggers recompilation of shader)
ColorMapping map_color_to_material
material side[s] where color is to be mapped to the diffuse material component, defaults to CM_COLOR ...
IlluminationMode illumination_mode
illumination mode defaults to IM_ONE_SIDED
cgv::media::illum::textured_surface_material material
material of surface
cgv::media::illum::surface_material::color_type surface_color
default value for color when map color to material is used
compact type description of data that can be sent to the context; convertible to int
Definition context.h:57
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54