cgv
Loading...
Searching...
No Matches
mesh_drawable.cxx
1#include <cgv/base/base.h>
2#include "mesh_drawable.h"
3#include <cgv/base/import.h>
4
5namespace cgv {
6 namespace render {
7 namespace gl {
8
11{
12 rebuild_mesh_info = false;
13}
14
17{
18 if (!rebuild_mesh_info)
19 return;
20
21 mesh_info.destruct(ctx);
22 mesh_info.construct(ctx, mesh);
23 mesh_info.bind(ctx, ctx.ref_surface_shader_program(true), true);
24}
25
28{
29 mesh_info.destruct(ctx);
30}
31
32void mesh_drawable::draw_mesh_primitive(context &ctx, unsigned pi, bool opaque, bool use_materials)
33{
34 mesh_info.draw_primitive(ctx, pi, !opaque, opaque, use_materials);
35}
36
37void mesh_drawable::draw_mesh(context &ctx, bool opaque, bool use_materials)
38{
39 mesh_info.draw_all(ctx, !opaque, opaque);
40}
41
43{
44 draw_mesh(ctx, true);
45}
46
48{
49 draw_mesh(ctx, false);
50}
51
52bool mesh_drawable::read_mesh(const std::string& _file_name)
53{
54 std::string fn = cgv::base::find_data_file(_file_name, "cpMD", "", model_path);
55 if (fn.empty()) {
56 std::cerr << "mesh file " << file_name << " not found" << std::endl;
57 return false;
58 }
59 file_name = _file_name;
60 mesh.clear();
61 if (mesh.read(file_name)) {
62 box = mesh.compute_box();
63 rebuild_mesh_info = true;
64 return true;
65 }
66 std::cerr << "could not read mesh " << file_name << std::endl;
67 return false;
68}
69
71{
73 if (!view_ptr)
74 return;
75 view_ptr->set_focus(box.get_center());
76 view_ptr->set_y_extent_at_focus(1.3*box.get_extent()(box.get_max_extent_coord_index()));
78}
81{
82 return box;
83}
84
85
86 }
87 }
88}
89
fpnt_type get_center() const
return the center of the box
unsigned get_max_extent_coord_index() const
return the index of the coordinte with maximum extend
fvec_type get_extent() const
return a vector with the extents in the different dimensions
box_type compute_box() const
compute the axis aligned bounding box
bool read(const std::string &file_name)
read simple mesh from file (currently only obj and stl are supported)
void clear()
clear simple mesh
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
virtual shader_program & ref_surface_shader_program(bool texture_support=false)=0
return a reference to the default shader program used to render surfaces
view * find_view_as_node(size_t view_idx=0) const
convenience function to find the view control in the current hierarchy
Definition drawable.cxx:49
void post_redraw()
posts a redraw event to the current context if one is available
Definition drawable.cxx:43
const box3 & get_box() const
return the axis aligned bounding box
void center_view()
call this to center the view after loading the mesh.
void draw_mesh_primitive(cgv::render::context &ctx, unsigned pi, bool opaque, bool use_materials=true)
draw all faces belonging to the given primitive, optionally turn off the specification of materials
void init_frame(cgv::render::context &ctx)
init textures if the mesh has been newly loaded
void clear(context &ctx)
clear all objects living in the context like textures or display lists
virtual bool read_mesh(const std::string &file_name)
read mesh from the given file_name which is extended by model path if it does not exist
void finish_draw(context &ctx)
default finish drawing implementation calls draw_mesh(ctx, false) to render transparent part
mesh_drawable()
construct from name which is necessary construction argument to node
std::string file_name
currently loaded file name
void draw_mesh(cgv::render::context &ctx, bool opaque, bool use_materials=true)
draw the complete mesh, optionally turn off the specification of materials
void draw(cgv::render::context &ctx)
default drawing implementation calls draw_mesh(ctx, true) to render opaque part
std::string model_path
default path to file names
box3 box
the bounding box of the mesh is computed in the read_mesh method
void draw_primitive(cgv::render::context &ctx, size_t primitive_index, bool skip_opaque=false, bool skip_blended=false, bool use_materials=true)
draw triangles of given mesh part or whole mesh in case part_index is not given (=-1)
bool bind(context &ctx, shader_program &prog, bool force_success, int aa_index=-1)
override to restrict bind function to first aa as second is used for wireframe rendering
void destruct(cgv::render::context &ctx)
destruct render mesh info and free vertex buffer objects
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...
void draw_all(context &ctx, bool skip_opaque=false, bool skip_blended=false, bool use_materials=true)
execute all draw calls
defines a symmetric view with the following quantities:
Definition view.h:22
std::string find_data_file(const std::string &file_name, const std::string &strategy, const std::string &sub_directory, const std::string &master_path)
Find a file with the given strategy and return the file name extended by the necessary path.
Definition import.cxx:59
the cgv namespace
Definition print.h:11