Radium Engine  1.5.0
VolumeObject.cpp
1 #include <Engine/Data/VolumeObject.hpp>
2 
3 #include <Core/Geometry/MeshPrimitives.hpp>
4 #include <Core/Geometry/Volume.hpp>
5 #include <Core/Types.hpp>
6 #include <Core/Utils/Log.hpp>
7 #include <Engine/Data/ShaderProgram.hpp>
8 #include <Engine/OpenGL.hpp>
9 
10 namespace Ra {
11 namespace Engine {
12 namespace Data {
13 VolumeObject::VolumeObject( const std::string& name ) :
14  Displayable( name ), m_tex( {} ), m_mesh( name + "_internal" ) {}
15 
16 VolumeObject::~VolumeObject() {}
17 
18 void VolumeObject::loadGeometry( Core::Geometry::AbstractVolume* volume, const Core::Aabb& aabb ) {
19  if ( volume != nullptr && volume->isDense() ) {
20 
21  m_mesh.loadGeometry( Core::Geometry::makeSharpBox( aabb ) );
22 
23  Core::Vector3Array tex_coords;
24  tex_coords.resize( 24 );
25  tex_coords.getMap() <<
26  // R
27  Scalar( 1 ),
28  Scalar( 1 ), Scalar( 0 ), Scalar( 0 ), // Bottom
29  Scalar( 1 ), Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), // Top
30  Scalar( 1 ), Scalar( 1 ), Scalar( 1 ), Scalar( 1 ), // Right
31  Scalar( 0 ), Scalar( 0 ), Scalar( 0 ), Scalar( 0 ), // Left
32  Scalar( 1 ), Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), // Floor
33  Scalar( 1 ), Scalar( 1 ), Scalar( 0 ), Scalar( 0 ), // Ceil
34  // G
35  Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), Scalar( 0 ), // Bottom
36  Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), // Top
37  Scalar( 1 ), Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), // Right
38  Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), Scalar( 0 ), // Left
39  Scalar( 0 ), Scalar( 0 ), Scalar( 0 ), Scalar( 0 ), // Floor
40  Scalar( 1 ), Scalar( 1 ), Scalar( 1 ), Scalar( 1 ), // Ceil
41  // B
42  Scalar( 0 ), Scalar( 0 ), Scalar( 0 ), Scalar( 0 ), // Bottom
43  Scalar( 1 ), Scalar( 1 ), Scalar( 1 ), Scalar( 1 ), // Top
44  Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), // Right
45  Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), // Left
46  Scalar( 0 ), Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), // Floor
47  Scalar( 0 ), Scalar( 1 ), Scalar( 1 ), Scalar( 0 ); // Ceil
48  m_mesh.addAttrib( Ra::Core::Geometry::getAttribName( Ra::Core::Geometry::VERTEX_TEXCOORD ),
49  tex_coords );
50 
51  Core::Geometry::VolumeGrid* grid = static_cast<Core::Geometry::VolumeGrid*>( volume );
52  m_volume = std::unique_ptr<Core::Geometry::AbstractVolume>( volume );
53 
54  auto dim = grid->size();
55  TextureParameters texparam { getName(),
56  GL_TEXTURE_3D,
57  size_t( dim( 0 ) ),
58  size_t( dim( 1 ) ),
59  size_t( dim( 2 ) ),
60  GL_RED,
61  GL_R32F,
62  GL_SCALAR,
63  GL_CLAMP_TO_BORDER,
64  GL_CLAMP_TO_BORDER,
65  GL_CLAMP_TO_BORDER,
66  GL_LINEAR,
67  GL_LINEAR,
68  grid->data().data() };
69  m_tex.setParameters( texparam );
70 
71  m_isDirty = true;
72  }
73 }
74 
75 void VolumeObject::loadGeometry( Core::Geometry::AbstractVolume* volume ) {
76  loadGeometry( volume, volume->computeAabb() );
77 }
78 
79 void VolumeObject::updateGL() {
80  if ( m_isDirty ) {
81  m_mesh.updateGL();
82  GL_CHECK_ERROR;
83  m_tex.initializeGL();
84  GL_CHECK_ERROR;
85  m_isDirty = false;
86  }
87 }
88 
89 void VolumeObject::render( const ShaderProgram* prog ) {
90  GL_CHECK_ERROR;
91  // Cull faces
92 
93  GLboolean cullEnable = glIsEnabled( GL_CULL_FACE );
94  int culledFaces;
95  glGetIntegerv( GL_CULL_FACE_MODE, &culledFaces );
96  int frontFaces;
97  glGetIntegerv( GL_FRONT_FACE, &frontFaces );
98  glCullFace( GL_BACK );
99  glEnable( GL_CULL_FACE );
100 
101  m_mesh.render( prog );
102 
103  glCullFace( gl::GLenum( culledFaces ) );
104  glFrontFace( gl::GLenum( frontFaces ) );
105  if ( !cullEnable ) glDisable( GL_CULL_FACE );
106  GL_CHECK_ERROR;
107 }
108 
109 } // namespace Data
110 } // namespace Engine
111 } // namespace Ra
const Vector3i & size() const
return the size (number of bins ni each dimension) of the volume
Definition: Volume.hpp:115
bool isDense() const
Return true if the volume is dense (implies isDiscrete to be true)
Definition: Volume.cpp:20
Discrete volume data storing values in a regular grid.
Definition: Volume.hpp:182
const Container & data() const
Direct access to the managed data.
Definition: Volume.hpp:204
Definition: Cage.cpp:3
virtual Aabb computeAabb() const =0
Compute bounding box.