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 if (has_attribute(ctx, "normal"))
14 has_normals = true;
15 if (has_attribute(ctx, "texcoord"))
16 has_texcoords = true;
17 }
20 {
22 has_normals = false;
23 has_texcoords = false;
24 }
26 has_normals = false;
27 remove_attribute_array(ctx, "normal");
28 }
30 has_texcoords = false;
31 remove_attribute_array(ctx, "texcoord");
32 }
34 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)
35 {
36 has_normals = true;
37 set_attribute_array(ctx, "normal", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
38
39 }
41 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)
42 {
43 has_texcoords = true;
44 set_attribute_array(ctx, "texcoord", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
45
46 }
48 {
49 bool res = group_renderer::enable(ctx);
50 const surface_render_style& srs = get_style<surface_render_style>();
51 if (cull_per_primitive) {
52 if (srs.culling_mode == CM_OFF) {
53 glDisable(GL_CULL_FACE);
54 }
55 else {
56 glCullFace(srs.culling_mode == CM_FRONTFACE ? GL_FRONT : GL_BACK);
57 glEnable(GL_CULL_FACE);
58 }
59 }
60 if (ref_prog().is_linked()) {
61 if (!has_colors)
63 ctx.set_material(srs.material);
64 ref_prog().set_uniform(ctx, "map_color_to_material", int(srs.map_color_to_material));
65 ref_prog().set_uniform(ctx, "culling_mode", int(srs.culling_mode));
66 ref_prog().set_uniform(ctx, "illumination_mode", int(srs.illumination_mode));
67 }
68 else
69 res = false;
70 return res;
71 }
72
74 {
75 const surface_render_style& srs = get_style<surface_render_style>();
76 if (cull_per_primitive) {
77 if (srs.culling_mode != CM_OFF)
78 glDisable(GL_CULL_FACE);
79 }
80 if (!attributes_persist()) {
81 has_normals = false;
82 has_texcoords = false;
83 }
84 return group_renderer::disable(ctx);
85 }
86 bool surface_render_style_reflect::self_reflect(cgv::reflect::reflection_handler& rh)
87 {
88 return
90 rh.reflect_member("culling_mode", culling_mode) &&
91 rh.reflect_member("illumination_mode", illumination_mode) &&
92 rh.reflect_member("map_color_to_material", map_color_to_material) &&
93 rh.reflect_member("surface_color", surface_color) &&
94 rh.reflect_member("material", material);
95 }
96
98 {
100 }
101 }
102}
103
104#include <cgv/gui/provider.h>
105
106namespace cgv {
107 namespace gui {
108
110 {
112 bool create(provider* p, const std::string& label,
113 void* value_ptr, const std::string& value_type,
114 const std::string& gui_type, const std::string& options, bool*)
115 {
117 return false;
118 cgv::render::surface_render_style* srs_ptr = reinterpret_cast<cgv::render::surface_render_style*>(value_ptr);
119 cgv::base::base* b = dynamic_cast<cgv::base::base*>(p);
120 if (p->begin_tree_node("Color Mapping", srs_ptr->map_color_to_material, false, "level=3")) {
121 p->align("\a");
122 p->add_gui("Map Color to Material", srs_ptr->map_color_to_material, "bit_field_control",
123 "enums='Color Front=1,Color Back=2,Opacity Front=4,Opacity Back=8'");
124 p->align("\b");
126 }
127 p->add_member_control(b, "Illumination Mode", srs_ptr->illumination_mode, "dropdown", "enums='Off,One-Sided,Two-Sided'");
128 p->add_member_control(b, "Culling Mode", srs_ptr->culling_mode, "dropdown", "enums='Off,Backface,Frontface'");
129 if (p->begin_tree_node("Color and Materials", srs_ptr->surface_color, false, "level=3")) {
130 p->align("\a");
131 p->add_member_control(b, "Surface Color", srs_ptr->surface_color);
132 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");
133 if (p->begin_tree_node("Material", srs_ptr->material, false, "level=3")) {
134 p->align("\a");
135 p->add_gui("front_material", srs_ptr->material);
136 p->align("\b");
137 p->end_tree_node(srs_ptr->material);
138 }
139 p->align("\b");
140 p->end_tree_node(srs_ptr->surface_color);
141 }
142 if (p->begin_tree_node("Use of Group Information", srs_ptr->illumination_mode, false, "level=3")) {
143 p->align("\a");
144 p->add_gui("group render style", *static_cast<cgv::render::group_render_style*>(srs_ptr));
145 p->align("\b");
146 p->end_tree_node(srs_ptr->illumination_mode);
147 }
148 return true;
149 }
150 };
151
152#include "gl/lib_begin.h"
153
154CGV_API cgv::gui::gui_creator_registration<surface_render_style_gui_creator> surface_rs_gc_reg("surface_render_style_gui_creator");
155
156 }
157}
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:627
virtual void set_color(const rgba &clr)
set the current color
Definition context.cxx:1636
virtual void set_material(const cgv::media::illum::surface_material &mat)
set the current material
Definition context.cxx:1651
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 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.h:62
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:79
bool has_colors
track whether color attribute is defined
Definition renderer.h:90
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:70
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,...
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 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, 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 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:56
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54