Radium Engine  1.5.20
Loading...
Searching...
No Matches
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
10namespace Ra {
11namespace Engine {
12namespace Data {
13VolumeObject::VolumeObject( const std::string& name ) :
14 Displayable( name ), m_mesh( name + "_internal" ) {}
15
16VolumeObject::~VolumeObject() {}
17
18void 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 );
53
54 auto dim = grid->size();
55
57 // tmp hack create a shared ptr and copy to it.
58 std::shared_ptr<float[]> data( new float[grid->data().size()] );
59 auto itr = data.get();
60 for ( const auto& v : grid->data() ) {
61 *( itr++ ) = v;
62 }
63 TextureParameters texparam {
64 getName(),
65 { GL_CLAMP_TO_BORDER, GL_CLAMP_TO_BORDER, GL_CLAMP_TO_BORDER, GL_LINEAR, GL_LINEAR },
66 { GL_TEXTURE_3D,
67 size_t( dim( 0 ) ),
68 size_t( dim( 1 ) ),
69 size_t( dim( 2 ) ),
70 GL_RED,
71 GL_R32F,
72 GL_SCALAR,
73 false,
74 data } };
75 m_tex.setParameters( texparam );
76
77 m_isDirty = true;
78 }
79}
80
81void VolumeObject::loadGeometry( Core::Geometry::AbstractVolume* volume ) {
82 loadGeometry( volume, volume->computeAabb() );
83}
84
85void VolumeObject::updateGL() {
86 if ( m_isDirty ) {
87 m_mesh.updateGL();
88 GL_CHECK_ERROR;
89 m_tex.initialize();
90 GL_CHECK_ERROR;
91 m_isDirty = false;
92 }
93}
94
95void VolumeObject::render( const ShaderProgram* prog ) {
96 GL_CHECK_ERROR;
97 // Cull faces
98
99 GLboolean cullEnable = glIsEnabled( GL_CULL_FACE );
100 int culledFaces;
101 glGetIntegerv( GL_CULL_FACE_MODE, &culledFaces );
102 int frontFaces;
103 glGetIntegerv( GL_FRONT_FACE, &frontFaces );
104 glCullFace( GL_BACK );
105 glEnable( GL_CULL_FACE );
106
107 m_mesh.render( prog );
108
109 glCullFace( gl::GLenum( culledFaces ) );
110 glFrontFace( gl::GLenum( frontFaces ) );
111 if ( !cullEnable ) glDisable( GL_CULL_FACE );
112 GL_CHECK_ERROR;
113}
114
115} // namespace Data
116} // namespace Engine
117} // 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
T get(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T size(T... args)
virtual Aabb computeAabb() const =0
Compute bounding box.
Describes the sampler and image of a texture.
Definition Texture.hpp:99