3 #include <osg/Geometry>
5 #include <osg/Texture2D>
6 #include <osg/Texture3D>
7 #include <osgViewer/CompositeViewer>
10 #include <osg/BoundingBox>
11 #include <osg/MatrixTransform>
13 namespace osgCloudyDay
22 osg::FrameBufferObject* fbo;
23 osg::FrameBufferAttachment *fba;
28 ReadBufferCallback(osg::Texture3D *_tex,
int slicex,
int slicey,
int layer) : m_layer(layer), m_slicex(slicex), m_slicey(slicey)
31 image =
new osg::Image();
32 fbo =
new osg::FrameBufferObject();
33 fba =
new osg::FrameBufferAttachment(tex, layer);
34 fbo->setAttachment(osg::Camera::COLOR_BUFFER, *fba);
37 virtual void operator() (osg::RenderInfo& renderInfo)
const
39 fbo->apply(*(renderInfo.getState()), osg::FrameBufferObject::READ_FRAMEBUFFER);
40 image->readPixels(0, 0, m_slicex, m_slicey, GL_RGBA, GL_FLOAT);
42 float* height = (
float*)image->data();
44 osg::Vec4 sum = osg::Vec4(0.f, 0.f, 0.f, 0.f);
45 for(
int i = 0; i < m_slicex*m_slicey; i++)
47 for(
int j = 0; j < 4; j++)
48 m_data[m_layer*m_slicey*m_slicex*4+i*4+j] = height[i*4+j];
50 sum.x() += height[i*4+0];
51 sum.y() += height[i*4+1];
52 sum.z() += height[i*4+2];
53 sum.w() += height[i*4+3];
56 std::cout <<
"SUM: " << sum.x() <<
" " << sum.y() <<
" " << sum.z() <<
" " << sum.z() << std::endl;
71 m_transformMatrix.makeIdentity();
75 virtual void apply( osg::Geode &geode )
82 osg::BoundingBox bbox;
87 for(
unsigned int i = 0; i < geode.getNumDrawables(); ++i ){
91 bbox.expandBy( geode.getDrawable( i )->getBound());
97 osg::BoundingBox bboxTrans;
99 for(
unsigned int i = 0; i < 8; ++i )
101 osg::Vec3 xvec = bbox.corner( i ) * m_transformMatrix;
102 bboxTrans.expandBy( xvec );
108 m_boundingBox.expandBy( bboxTrans );
117 virtual void apply( osg::MatrixTransform &node )
124 m_transformMatrix *= node.getMatrix();
125 std::cout <<
"TRANSFORM" << std::endl;
134 virtual void apply( osg::Billboard &node ){
153 osg::BoundingBox &getBoundBox() {
return m_boundingBox; }
156 osg::BoundingBox m_boundingBox;
157 osg::Matrix m_transformMatrix;
164 typedef std::map<GLint, osg::Texture3D*> t_tex3DsByUnit;
167 VoxelizeMesh(std::string path,
int slice_width,
int slice_height,
int slice_depth);
171 osg::Vec4 GetData(
int x,
int y,
int z);
173 osg::ref_ptr<osg::Texture3D> voxels_tex;
175 osg::BoundingBox m_boundingbox;
178 void Read3DTexture();
180 osg::Texture3D* setupTexture3D(
const char *name,
const GLenum internalFormat,
const GLenum pixelFormat,
const GLenum dataType,
const int width,
const int height,
const int depth, osg::Image *image=0);
182 void Render3D(osg::Vec4 color,
bool clear);
185 void cleanUp(osgViewer::CompositeViewer *viewer);
186 void dirtyTargets(t_tex3DsByUnit &targets3D);
187 osg::GraphicsContext *setupContext();
189 osg::Camera *setupCamera(
const int orderNum,
bool clear);
190 osg::Image *getLayerFrom3DImage(osg::Image *source,
const int layer);
191 osg::Group *setupGroup(osgViewer::CompositeViewer *viewer);
193 osg::ref_ptr<osg::Program> m_shader;
203 t_tex3DsByUnit targets3D;
204 osgViewer::CompositeViewer *viewer;
205 osg::ref_ptr<osg::Group> group;