cgv
Loading...
Searching...
No Matches
mesh_render_info.h
1#pragma once
2
3#include "render_info.h"
4#include <cgv/media/mesh/simple_mesh.h>
5
6#include "lib_begin.h"
7
8namespace cgv {
9 namespace render {
15class CGV_API mesh_render_info : public render_info
16{
17public:
25 typedef std::map<attribute_type, uint32_t> attribute_map;
26protected:
28 std::vector<std::string> primitive_names;
30 std::vector<idx3_type> material_primitive_start;
56 cgv::media::ColorType ct;
57
58 /*@name support for flexible interleaving and dynamic updates of vertex attributes */
60
62 {
63 bool is_dynamic = false;
65 uint32_t stride = 0;
66 };
68 std::vector<vbo_configuration> vbo_config;
71 {
72 uint32_t vbo_index = 0;
73 uint32_t offset = 0;
74 };
76 std::map<attribute_type, attribute_configuration> per_attribute_vbo_config;
78 std::vector<idx4_type> unique_quadruples;
80 bool attribute_is_used(attribute_type at) const;
82 bool configure_vbos(cgv::render::context& ctx, const cgv::media::mesh::simple_mesh_base& mesh,
83 const attribute_map& attribute_to_vbo_index_map, const std::vector<int>& dynamic_vbo_idx);
85
98 void construct_index_buffers(cgv::render::context& ctx, const cgv::media::mesh::simple_mesh_base& mesh,
99 std::vector<idx4_type>& unique_quadruples, std::vector<idx_type>& per_corner_vertex_index,
100 std::vector<idx_type>& edges_element_buffer, std::vector<idx_type>& triangles_element_buffer);
110 void construct_element_vbo(cgv::render::context& ctx, const std::vector<idx_type>& edge_element_buffer, const std::vector<idx_type>& triangle_element_buffer);
112 void construct_draw_calls(cgv::render::context& ctx);
113public:
117 bool is_constructed() const { return get_vbos().size() > 0; }
119 bool is_bound() const { return get_aas().size() > 0; }
130 template <typename T>
132 std::vector<idx_type>* tuple_pos_indices = nullptr,
133 std::vector<idx_type>* tuple_normal_indices = nullptr,
134 int* num_floats_in_vertex = nullptr)
135 {
136 element_size = sizeof(T);
138 typename cgv::media::mesh::simple_mesh<T>::vec3_type>::get_type_descriptor(mesh.position(0));
140 get_type_descriptor(typename cgv::media::mesh::simple_mesh<T>::vec2_type());
141 attribute_map va_vbo_idx = { { attribute_type::position, 0 } };
142 if (mesh.has_tex_coords())
143 va_vbo_idx[attribute_type::texcoords] = 0;
144 if (mesh.has_normals())
145 va_vbo_idx[attribute_type::normal] = 0;
146 if (mesh.has_tangents())
147 va_vbo_idx[attribute_type::tangent] = 0;
148 if (mesh.has_colors())
149 va_vbo_idx[attribute_type::color] = 0;
150 configure_vbos(ctx, mesh, va_vbo_idx, {});
151 // construct render buffers
152 std::vector<idx_type> vertex_indices, edge_element_buffer, triangle_element_buffer;
153 std::vector<idx4_type> unique_quadruples;
154 construct_index_buffers(ctx, mesh, unique_quadruples, vertex_indices, edge_element_buffer, triangle_element_buffer);
155 // record which attributes each unique tuple was referencing
156 for (const auto& tuple : unique_quadruples) {
157 if (tuple_pos_indices)
158 tuple_pos_indices->push_back(tuple[0]);
159 if (tuple_normal_indices)
160 tuple_normal_indices->push_back(tuple[2]);
161 }
162 std::vector<uint8_t> attrib_buffer;
163 mesh.extract_vertex_attribute_buffer_base(unique_quadruples, vbo_config.front().attributes, attrib_buffer);
164 ref_vbos().push_back(new cgv::render::vertex_buffer(cgv::render::VBT_VERTICES));
165 ref_vbos().back()->create(ctx, attrib_buffer);
166 construct_element_vbo(ctx, edge_element_buffer, triangle_element_buffer);
167 construct_draw_calls(ctx);
168 }
170 template <typename T>
172 const attribute_map& va_vbo_idx, const std::vector<int>& dynamic_vbo_idx)
173 {
174 element_size = sizeof(T);
176 typename cgv::media::mesh::simple_mesh<T>::vec3_type>::get_type_descriptor(mesh.position(0));
178 get_type_descriptor(typename cgv::media::mesh::simple_mesh<T>::vec2_type());
179 configure_vbos(ctx, mesh, va_vbo_idx, dynamic_vbo_idx);
180 bool keep_unique_quadrupels = !dynamic_vbo_idx.empty();
181 // construct unique attribute index tupels that form vertices in the render data together with
182 // vector of per corner vertex indices, vertex index pairs forming edges and vertex index triples forming triangles
183 std::vector<idx4_type> tmp_unique_quadruples;
184 std::vector<idx4_type>& unique_quadruples_ref = keep_unique_quadrupels ? unique_quadruples : tmp_unique_quadruples;
185 std::vector<idx_type> per_corner_vertex_index, edge_element_buffer, triangle_element_buffer;
186 construct_index_buffers(ctx, mesh, unique_quadruples_ref, per_corner_vertex_index, edge_element_buffer, triangle_element_buffer);
187 //
188 for (const auto& vc : vbo_config) {
189 std::vector<uint8_t> attrib_buffer;
190 auto vca = vc.attributes;
191 mesh.extract_vertex_attribute_buffer_base(unique_quadruples_ref, vca, attrib_buffer);
192 ref_vbos().push_back(new cgv::render::vertex_buffer(cgv::render::VBT_VERTICES));
193 ref_vbos().back()->create(ctx, attrib_buffer);
194 }
195 construct_element_vbo(ctx, edge_element_buffer, triangle_element_buffer);
196 construct_draw_calls(ctx);
197 }
199 template <typename T>
201 {
202 const auto& vc = vbo_config[vbo_index];
203 std::vector<uint8_t> attrib_buffer;
204 auto vca = vc.attributes;
205 mesh.extract_vertex_attribute_buffer_base(unique_quadruples, vca, attrib_buffer);
206 ref_vbos()[vbo_index]->replace(ctx, 0, &attrib_buffer[0], attrib_buffer.size());
207 }
209 void set_nr_instances(unsigned nr);
211 size_t get_nr_primitives() const { return primitive_names.size(); }
213 const std::string& get_primitive_name(size_t i) const { return primitive_names[i]; }
215 size_t get_nr_fragments() const { return material_primitive_start.size(); }
217 size_t get_material_index(size_t i) const { return material_primitive_start[i](0); }
219 size_t get_primitive_index(size_t i) const { return material_primitive_start[i](1); }
220
222 size_t get_nr_vertices() const { return nr_vertices; };
224 size_t get_nr_edge_elements() const { return nr_edge_elements; };
226 size_t get_nr_triangle_elements() const { return nr_triangle_elements; };
228 size_t get_element_size() const { return element_size; };
229
231 void draw_primitive(cgv::render::context& ctx, size_t primitive_index, bool skip_opaque = false, bool skip_blended = false, bool use_materials = true);
233 bool bind(context& ctx, shader_program& prog, bool force_success, int aa_index = -1);
235 bool bind_wireframe(context& ctx, shader_program& prog, bool force_success);
237 void draw_wireframe(cgv::render::context& ctx);
239 void destruct(cgv::render::context& ctx);
240};
241 }
242}
243
244#include <cgv/config/lib_end.h>
A vector with zero based index.
Definition fvec.h:26
bool has_colors() const
check whether colors have been allocated
coordinate type independent base class of simple mesh data structure that handles indices and colors.
Definition simple_mesh.h:27
idx_type extract_vertex_attribute_buffer_base(const std::vector< idx4_type > &unique_quadruples, AttributeFlags &flags, std::vector< uint8_t > &attrib_buffer) const
extract vertex attribute buffer for the given flags and return size of vertex in bytes
attribute_type
different mesh attributes
Definition simple_mesh.h:40
the simple_mesh class is templated over the coordinate type that defaults to float
bool has_tangents() const
access to tangents
bool has_tex_coords() const
access to texture coordinates
bool has_normals() const
access to normals
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
the mesh_render_info structure manages vertex buffer objects for attribute and element buffers as wel...
cgv::math::fvec< idx_type, 3 > idx3_type
index triple type
size_t get_nr_edge_elements() const
Returns how many edge-indices are recorded in the element buffer.
size_t get_material_index(size_t i) const
return material index of i-th fragment
std::vector< idx3_type > material_primitive_start
index triple storing the material index, the primitive index and the offset (element index) in the tr...
std::vector< idx4_type > unique_quadruples
in case that at least one vbo is replaceable, store per vertex the unique quadruple of attribute indi...
size_t get_nr_fragments() const
return number of mesh fragments that are of distinct primitive and material
void update_vbo(cgv::render::context &ctx, cgv::media::mesh::simple_mesh< T > &mesh, int vbo_index)
replace the content of one vbo from a mesh
bool is_bound() const
check whether attribute array binding is bound
std::map< attribute_type, attribute_configuration > per_attribute_vbo_config
for each mesh attribute that is used, map attribute type to the vbo index
std::vector< vbo_configuration > vbo_config
store for each vbo its configuration
size_t get_primitive_index(size_t i) const
return group index of i-th fragment
cgv::render::type_descriptor tex_coords_descr
type description of tex coordinate attribute
bool include_tex_coords
store whether tex coords are in vbo
size_t color_increment
color type size in bytes
bool include_normals
store whether normals are in vbo
bool include_tangents
store whether tangents are in vbo
cgv::render::type_descriptor position_descr
type description of position attribute
size_t nr_vertices
number of vertices
size_t get_nr_vertices() const
Returns how many vertices the mesh has.
cgv::media::mesh::simple_mesh_base::attribute_type attribute_type
redeclare attribute type for shorter name
cgv::media::ColorType ct
color type
const std::string & get_primitive_name(size_t i) const
return name of i-th mesh primitive
std::vector< std::string > primitive_names
store list of primitive names
size_t get_nr_triangle_elements() const
Returns how many triangle-indices are recorded in the element buffer.
size_t nr_edge_elements
number of edges in the wireframe representation
size_t get_element_size() const
Returns how many bytes on index value takes up in the element buffer.
cgv::math::fvec< idx_type, 4 > idx4_type
index quadruple type
size_t element_size
size of single coordinate
size_t get_nr_primitives() const
return number of mesh primitives
draw_call wire_draw_call
draw call for wireframe rendering
bool include_colors
store whether colors are in vbo
std::map< attribute_type, uint32_t > attribute_map
type of map from mesh attribute to vbo index
void construct_dynamic(cgv::render::context &ctx, cgv::media::mesh::simple_mesh< T > &mesh, const attribute_map &va_vbo_idx, const std::vector< int > &dynamic_vbo_idx)
Construct mesh render info from a given simple mesh and store vertex attributes flexibly in multiple ...
void construct(cgv::render::context &ctx, cgv::media::mesh::simple_mesh< T > &mesh, std::vector< idx_type > *tuple_pos_indices=nullptr, std::vector< idx_type > *tuple_normal_indices=nullptr, int *num_floats_in_vertex=nullptr)
Construct mesh render info from a given simple mesh and store all vertex attributes in interleaved in...
size_t nr_triangle_elements
number of triangles in the triangulation
bool is_constructed() const
check whether vbos are constructed
the mesh_render_info structure manages vertex buffer objects for attribute and element buffers as wel...
Definition render_info.h:76
a shader program combines several shader code fragments to a complete definition of the shading pipel...
a vertex buffer is an unstructured memory block on the GPU.
@ VBT_VERTICES
The buffer contains vertices and will be bound to GL_ARRAY_BUFFER.
Definition context.h:416
the cgv namespace
Definition print.h:11
information needed per mesh attribute to know where it is stored in vbos
compact type description of data that can be sent to the context; convertible to int
Definition context.h:47