VirtualSLSProjector.cpp
Go to the documentation of this file.
00001 /*
00002  * VirtualStructuredLightProjector.cpp
00003  *
00004  *  Created on: 06/02/2013
00005  *      Author: Miquel Massot
00006  *      Modified by: Javier Perez
00007  *
00008  */
00009 
00010 #include <uwsim/VirtualSLSProjector.h>
00011 
00012 class UpdateLMVPM : public osg::Uniform::Callback
00013 {
00014 public:
00015   UpdateLMVPM(osg::Camera* camera) :
00016       mCamera(camera)
00017   {
00018   }
00019   virtual void operator ()(osg::Uniform* u, osg::NodeVisitor*)
00020   {
00021     osg::Matrixd lmvpm = mCamera->getViewMatrix() * mCamera->getProjectionMatrix() * osg::Matrix::translate(1, 1, 1)
00022         * osg::Matrix::scale(0.5, 0.5, 0.5);
00023 
00024     u->set(lmvpm);
00025   }
00026 
00027 protected:
00028   osg::Camera* mCamera;
00029 };
00030 
00031 VirtualSLSProjector::VirtualSLSProjector()
00032 {
00033   osg::ref_ptr < osg::Node > node = new osg::Node;
00034   osg::ref_ptr < osg::Node > root = new osg::Node;
00035   std::string name = "SLSprojector";
00036   std::string image_name = "laser_texture.png";
00037   double range = 0; 
00038   double fov = 60.0;
00039   init(name, root, node, image_name, range, fov, 0);
00040 }
00041 
00042 VirtualSLSProjector::VirtualSLSProjector(std::string name, osg::Node *root, osg::Node *node, std::string image_name,
00043                                          double fov, bool laser)
00044 {
00045   double range = 0;
00046   init(name, root, node, image_name, range, fov, laser);
00047 }
00048 
00049 void VirtualSLSProjector::init(std::string name, osg::Node *root, osg::Node *node, std::string image_name, double range,
00050                                double fov, bool laser)
00051 {
00052   this->name = name;
00053   this->fov = fov;
00054   this->range = range;
00055   this->node = node;
00056   this->image_name = image_name;
00057   this->textureUnit = 3; // It shouldn't be fixed
00058 
00059   //Create projected texture
00060   osg::Texture2D* texture = new osg::Texture2D();
00061   osg::Image* texture_to_project = osgDB::readImageFile(this->image_name);
00062   assert(texture_to_project);
00063   texture->setImage(texture_to_project);
00064   texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER); // It makes texture not to repeat 
00065   texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER); // F.M.I: http://lucera-project.blogspot.com.es/2010/06/opengl-wrap.html
00066   texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER);
00067   texture->setBorderColor(osg::Vec4d(0.0, 0.0, 0.0, 0.0));
00068   root->getOrCreateStateSet()->setTextureAttributeAndModes(4, texture, osg::StateAttribute::ON);
00069 
00070   //Shadow camera
00071   camera = VirtualCamera(root->asGroup(), "slp_camera", node, texture_to_project->s(), texture_to_project->t(), fov,
00072                          texture_to_project->s() / (float)texture_to_project->t());
00073 
00074   //Create depth texture for shadow mapping test
00075   dbgDepthTexture = new osg::Texture2D;
00076   dbgDepthTexture->setTextureSize(texture_to_project->s(), texture_to_project->t()); //CHECK: Maybe we can use a smaller texture?
00077   dbgDepthTexture->setInternalFormat(GL_DEPTH_COMPONENT);
00078   dbgDepthTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
00079   dbgDepthTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
00080   root->getOrCreateStateSet()->setTextureAttributeAndModes(3, dbgDepthTexture, osg::StateAttribute::ON);
00081   camera.textureCamera->attach(osg::Camera::DEPTH_BUFFER, dbgDepthTexture);
00082 
00083   //Uniform to update texture
00084   osg::Matrixd lmvpm = camera.textureCamera->getViewMatrix() * camera.textureCamera->getProjectionMatrix()
00085       * osg::Matrix::translate(1, 1, 1) * osg::Matrix::scale(0.5, 0.5, 0.5);
00086   osg::Uniform* u = new osg::Uniform("LightModelViewProjectionMatrix", lmvpm);
00087   u->setUpdateCallback(new UpdateLMVPM(camera.textureCamera));
00088   root->getOrCreateStateSet()->addUniform(u);
00089 
00090   // add Laser uniform to change from laser to light behaviours
00091   osg::Uniform* laserUniform = new osg::Uniform("isLaser", laser);
00092   root->getOrCreateStateSet()->addUniform(laserUniform);
00093 
00094 }
00095 


uwsim
Author(s): Mario Prats
autogenerated on Mon Oct 6 2014 08:24:07