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
9
10 void set_material_uniform(cgv::render::shader_program& prog, cgv::render::context& ctx, const std::string& name, const cgv::media::illum::surface_material& material)
11 {
12 prog.set_uniform(ctx, name + ".brdf_type", (int)material.get_brdf_type());
13 prog.set_uniform(ctx, name + ".diffuse_reflectance", material.get_diffuse_reflectance());
14 prog.set_uniform(ctx, name + ".roughness", material.get_roughness());
15 prog.set_uniform(ctx, name + ".ambient_occlusion", material.get_ambient_occlusion());
16 prog.set_uniform(ctx, name + ".emission", material.get_emission());
17 prog.set_uniform(ctx, name + ".specular_reflectance", material.get_specular_reflectance());
18 prog.set_uniform(ctx, name + ".roughness_anisotropy", material.get_roughness_anisotropy());
19 prog.set_uniform(ctx, name + ".roughness_orientation", material.get_roughness_orientation());
20 prog.set_uniform(ctx, name + ".propagation_slow_down", cgv::math::fvec<float, 2>(material.get_propagation_slow_down().real(), material.get_propagation_slow_down().imag()));
21 prog.set_uniform(ctx, name + ".transparency", material.get_transparency());
22 prog.set_uniform(ctx, name + ".metalness", material.get_metalness());
23 }
24
25 surface_render_style::surface_render_style() : material("default")
26 {
27 surface_color = cgv::media::illum::surface_material::color_type(0.4f, 0.1f, 0.7f);
28 surface_opacity = 1.0f;
29 culling_mode = CM_OFF;
30 illumination_mode = IM_ONE_SIDED;
31 map_color_to_material = CM_COLOR;
32 material.ref_brdf_type() = cgv::media::illum::BrdfType(cgv::media::illum::BT_STRAUSS_DIFFUSE + cgv::media::illum::BT_STRAUSS);
33 }
34
35 surface_renderer::surface_renderer()
36 {
37 cull_per_primitive = true;
38 has_normals = false;
39 has_texcoords = false;
40 }
42 void surface_renderer::enable_attribute_array_manager(const context& ctx, attribute_array_manager& aam)
43 {
44 group_renderer::enable_attribute_array_manager(ctx, aam);
45 if (has_attribute(ctx, "normal"))
46 has_normals = true;
47 if (has_attribute(ctx, "texcoord"))
48 has_texcoords = true;
49 }
51 void surface_renderer::disable_attribute_array_manager(const context& ctx, attribute_array_manager& aam)
52 {
53 group_renderer::disable_attribute_array_manager(ctx, aam);
54 has_normals = false;
55 has_texcoords = false;
56 }
57 void surface_renderer::remove_normal_array(const context& ctx) {
58 has_normals = false;
59 remove_attribute_array(ctx, "normal");
60 }
61 void surface_renderer::remove_texcoord_array(const context& ctx) {
62 has_texcoords = false;
63 remove_attribute_array(ctx, "texcoord");
64 }
65 void set_gl_material_color(GLenum side, const cgv::media::illum::phong_material::color_type& c, float alpha, GLenum type)
66 {
67 GLfloat v[4] = { c[0], c[1], c[2], c[3] * alpha };
68 glMaterialfv(side, type, v);
69 }
70
71 void set_gl_material(const cgv::media::illum::phong_material& mat, MaterialSide ms, float alpha = 1.0f)
72 {
73 if (ms == MS_NONE)
74 return;
75 unsigned side = gl::map_to_gl(ms);
76 set_gl_material_color(side, mat.get_ambient(), alpha, GL_AMBIENT);
77 set_gl_material_color(side, mat.get_diffuse(), alpha, GL_DIFFUSE);
78 set_gl_material_color(side, mat.get_specular(), alpha, GL_SPECULAR);
79 set_gl_material_color(side, mat.get_emission(), alpha, GL_EMISSION);
80 glMaterialf(side, GL_SHININESS, mat.get_shininess());
81 }
82
84 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)
85 {
86 has_normals = true;
87 set_attribute_array(ctx, "normal", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
88
89 }
91 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)
92 {
93 has_texcoords = true;
94 set_attribute_array(ctx, "texcoord", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
95
96 }
97 bool surface_renderer::enable(context& ctx)
98 {
99 bool res = group_renderer::enable(ctx);
100 const surface_render_style& srs = get_style<surface_render_style>();
101 if (cull_per_primitive) {
102 if (srs.culling_mode == CM_OFF) {
103 glDisable(GL_CULL_FACE);
104 }
105 else {
106 glCullFace(srs.culling_mode == CM_FRONTFACE ? GL_FRONT : GL_BACK);
107 glEnable(GL_CULL_FACE);
108 }
109 }
110 if (ref_prog().is_linked()) {
111 if (!has_colors)
113 ctx.set_material(srs.material);
114 ref_prog().set_uniform(ctx, "map_color_to_material", int(srs.map_color_to_material));
115 ref_prog().set_uniform(ctx, "culling_mode", int(srs.culling_mode));
116 ref_prog().set_uniform(ctx, "illumination_mode", int(srs.illumination_mode));
117 }
118 else
119 res = false;
120 return res;
121 }
122
123 bool surface_renderer::disable(context& ctx)
124 {
125 const surface_render_style& srs = get_style<surface_render_style>();
126 if (cull_per_primitive) {
127 if (srs.culling_mode != CM_OFF)
128 glDisable(GL_CULL_FACE);
129 }
130 if (!attributes_persist()) {
131 has_normals = false;
132 has_texcoords = false;
133 }
134 return group_renderer::disable(ctx);
135 }
136 bool surface_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
137 {
138 return
139 rh.reflect_base<group_render_style>(*this) &&
140 rh.reflect_member("culling_mode", culling_mode) &&
141 rh.reflect_member("illumination_mode", illumination_mode) &&
142 rh.reflect_member("map_color_to_material", map_color_to_material) &&
143 rh.reflect_member("surface_color", surface_color) &&
144 rh.reflect_member("material", material);
145 }
146
148 {
150 }
151 }
152}
153
154#include <cgv/gui/provider.h>
155
156namespace cgv {
157 namespace gui {
158
160 {
162 bool create(provider* p, const std::string& label,
163 void* value_ptr, const std::string& value_type,
164 const std::string& gui_type, const std::string& options, bool*)
165 {
167 return false;
168 cgv::render::surface_render_style* srs_ptr = reinterpret_cast<cgv::render::surface_render_style*>(value_ptr);
169 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
170 if (p->begin_tree_node("Color Mapping", srs_ptr->map_color_to_material, false, "level=3")) {
171 p->align("\a");
172 p->add_gui("Map Color to Material", srs_ptr->map_color_to_material, "bit_field_control",
173 "enums='Color Front=1,Color Back=2,Opacity Front=4,Opacity Back=8'");
174 p->align("\b");
176 }
177 p->add_member_control(b, "Illumination Mode", srs_ptr->illumination_mode, "dropdown", "enums='Off,One-Sided,Two-Sided'");
178 p->add_member_control(b, "Culling Mode", srs_ptr->culling_mode, "dropdown", "enums='Off,Backface,Frontface'");
179 if (p->begin_tree_node("Color and Materials", srs_ptr->surface_color, false, "level=3")) {
180 p->align("\a");
181 p->add_member_control(b, "Surface Color", srs_ptr->surface_color);
182 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");
183 if (p->begin_tree_node("Material", srs_ptr->material, false, "level=3")) {
184 p->align("\a");
185 p->add_gui("front_material", srs_ptr->material);
186 p->align("\b");
187 p->end_tree_node(srs_ptr->material);
188 }
189 p->align("\b");
190 p->end_tree_node(srs_ptr->surface_color);
191 }
192 if (p->begin_tree_node("Use of Group Information", srs_ptr->illumination_mode, false, "level=3")) {
193 p->align("\a");
194 p->add_gui("group render style", *static_cast<cgv::render::group_render_style*>(srs_ptr));
195 p->align("\b");
196 p->end_tree_node(srs_ptr->illumination_mode);
197 }
198 return true;
199 }
200 };
201
202#include "gl/lib_begin.h"
203
204CGV_API cgv::gui::gui_creator_registration<surface_render_style_gui_creator> surface_rs_gc_reg("surface_render_style_gui_creator");
205
206 }
207}
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
A vector with zero based index.
Definition fvec.h:26
>simple class to hold the material properties of a phong material
simple class to hold the material properties of a phong material
color< float, RGB > color_type
used color type
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 void set_material(const cgv::media::illum::surface_material &mat)
set the current material
Definition context.cxx:1632
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,...
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, set to CM_OFF in constructor
float surface_opacity
default value for the surface opacity when map color to material is used
ColorMapping map_color_to_material
material side[s] where color is to be mapped to the diffuse material component, defaults to MS_FRONT_...
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:47
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54