OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

dynamic cube map

Goto page Previous  1, 2
 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
Trajce Nikolov NICK
Guest





PostPosted: Wed Dec 06, 2017 6:42 am    Post subject:
dynamic cube map
Reply with quote

Hi Romulo,

have a look at the OpenIG project sources, it has it implemented with GLSL


https://github.com/CCSI-CSSI/MuseOpenIG/blob/master/Plugin-ModelComposition/IGPluginModelComposition.cpp

Look here for "environmental", starting from line 395. Here you use texture cache to set up the 6 textures and the #define for the shader composition


The GLSL part is here:
https://github.com/CCSI-CSSI/MuseOpenIG/blob/master/Resources/shaders/forwardplus_vs.glsl

https://github.com/CCSI-CSSI/MuseOpenIG/blob/master/Resources/shaders/forwardplus_ps.glsl

Again, it uses shader composition the code inside #if defined(ENVIRONMENTAL)


Shoot questions if you struggle


Hope this helps


Cheers,
Nick


On Wed, Dec 6, 2017 at 3:08 AM, Rômulo Cerqueira < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi Robert,

my idea is to compute the secondary reflections by using dynamic cubemap. I need to get the normal and depth from cube map reflections. Is it possible?

...

Thank you!

Cheers,
Rômulo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72541#72541





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org






--
trajce nikolov nick

------------------
Post generated by Mail2Forum
Back to top
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 41

PostPosted: Fri Dec 08, 2017 12:05 pm    Post subject:
Reply with quote

Thanks a lot, Nick.

I will have a look in this source code. If I have some doubts, I will ask.

...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 41

PostPosted: Sun Dec 10, 2017 5:38 am    Post subject:
Reply with quote

Hi Nick,

I have a deep look in your code and I have a doubt.

The cube mapping results provides the reflected color. Is it possible to get the normal and depth instead of the reflected color?

Best regards,

...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
Trajce Nikolov NICK
Guest





PostPosted: Sun Dec 10, 2017 1:29 pm    Post subject:
dynamic cube map
Reply with quote

Hi Romulo,

if the normal and the depth are encoded in the env texture I believe the answer is yes. Are you encoding the normal and the depth into texture? if so, can you provide snippet?


On Sun, Dec 10, 2017 at 6:38 AM, Rômulo Cerqueira < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi Nick,

I have a deep look in your code and I have a doubt.

The cube mapping results provides the reflected color. Is it possible to get the normal and depth instead of the reflected color?

Best regards,

...

Thank you!

Cheers,
Rômulo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72562#72562





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org






--
trajce nikolov nick

------------------
Post generated by Mail2Forum
Back to top
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 41

PostPosted: Mon Dec 11, 2017 6:15 pm    Post subject:
Reply with quote

Hi Nick,

how can I encode the normal and depth data in the env texture?

...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
Trajce Nikolov NICK
Guest





PostPosted: Mon Dec 11, 2017 7:21 pm    Post subject:
dynamic cube map
Reply with quote

Hi Romulo,

I guess you should do a render to texture and make sure the texture format is with texels as floating points. Then custom shaders that will render normals and depths into these textures.


I am on travel at the moment will help you more when I get back. Meanwhile you can do research. Start with the osgprerender example


On Dec 11, 2017 7:13 PM, "Rômulo Cerqueira" < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi Nick,

how can I encode the normal and depth data in the env texture?

...

Thank you!

Cheers,
Rômulo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72565#72565





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


------------------
Post generated by Mail2Forum
Back to top
Trajce Nikolov NICK
Guest





PostPosted: Mon Dec 11, 2017 10:22 pm    Post subject:
dynamic cube map
Reply with quote

Hi Romulo,

more proper example is osgprerendercubemap - how to setup the RTT for cubemaps. Then you can have a look again in the OpenIG project, in the Triton plugin, line 70 - 114, that is the shader to generate HeighMap, for your case instead of the world Z you put in the output Z from the camera (the depth), and same for the normals. Here is the link for the code snippet:


https://github.com/CCSI-CSSI/MuseOpenIG/blob/master/Plugin-Triton/IGPluginTriton.cpp



good luck Wink


On Mon, Dec 11, 2017 at 8:19 PM, Trajce Nikolov NICK < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi Romulo,

I guess you should do a render to texture and make sure the texture format is with texels as floating points. Then custom shaders that will render normals and depths into these textures.


I am on travel at the moment will help you more when I get back. Meanwhile you can do research. Start with the osgprerender example


On Dec 11, 2017 7:13 PM, "Rômulo Cerqueira" < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi Nick,

how can I encode the normal and depth data in the env texture?

...

Thank you!

Cheers,
Rômulo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=72565#72565





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org








--
trajce nikolov nick

------------------
Post generated by Mail2Forum
Back to top
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 41

PostPosted: Mon Dec 18, 2017 10:13 pm    Post subject:
Reply with quote

Hi Nick,

I still need help to get the normal/depth data from reflected objects. Could you have a look in my current code?

C++ code:
Code:

// OSG includes
#include <osgViewer/Viewer>
#include <osg/Texture>
#include <osg/TexGen>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/TextureCubeMap>
#include <osg/TexMat>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Camera>
#include <osg/TexGenNode>
#include <osgDB/FileUtils>

// C++ includes
#include <iostream>

#define SHADER_PATH_FRAG "normal_depth_map/shaders/normalDepthMap.frag"
#define SHADER_PATH_VERT "normal_depth_map/shaders/normalDepthMap.vert"

#define BOOST_TEST_MODULE "DynamicCubeMap_test"

using namespace osg;

unsigned int numTextures = 6;

enum TextureUnitTypes {
    TEXTURE_UNIT_DIFFUSE,
    TEXTURE_UNIT_NORMAL,
    TEXTURE_UNIT_CUBEMAP
};

osg::ref_ptr<osg::Group> _create_scene() {
    osg::ref_ptr<osg::Group> scene = new osg::Group;

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    scene->addChild(geode.get());

    const float radius = 0.8f;
    const float height = 1.0f;
    osg::ref_ptr<osg::ShapeDrawable> shape;

    // sphere
    shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-3.0f, 0.0f, 0.0f), radius));
    shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
    geode->addDrawable(shape.get());

    // box
    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius));
    shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
    geode->addDrawable(shape.get());

    // cone
    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, -3.0f), radius, height));
    shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
    geode->addDrawable(shape.get());

    // cylinder
    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, 3.0f), 2* radius));
    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
    geode->addDrawable(shape.get());

    return scene;
}

osg::NodePath createReflector() {
    Geode* node = new Geode;
    const float radius = 0.8f;
    ref_ptr<TessellationHints> hints = new TessellationHints;
    hints->setDetailRatio(2.0f);
    ShapeDrawable* shape = new ShapeDrawable(new Sphere(Vec3(0.0f, 0.0f, 0.0f), radius * 1.5f), hints.get());
    shape->setColor(Vec4(0.8f, 0.8f, 0.8f, 1.0f));
    node->addDrawable(shape);

    osg::NodePath nodeList;
    nodeList.push_back(node);

    return nodeList;
}

class UpdateCameraAndTexGenCallback : public osg::NodeCallback
{
    public:

        typedef std::vector< osg::ref_ptr<osg::Camera> >  CameraList;

        UpdateCameraAndTexGenCallback(osg::NodePath& reflectorNodePath, CameraList& Cameras):
            _reflectorNodePath(reflectorNodePath),
            _Cameras(Cameras)
        {
        }

        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            // first update subgraph to make sure objects are all moved into position
            traverse(node,nv);

            // compute the position of the center of the reflector subgraph
            osg::Matrixd worldToLocal = osg::computeWorldToLocal(_reflectorNodePath);
            osg::BoundingSphere bs = _reflectorNodePath.back()->getBound();
            osg::Vec3 position = bs.center();

            typedef std::pair<osg::Vec3, osg::Vec3> ImageData;
            const ImageData id[] =
            {
                ImageData( osg::Vec3( 1,  0,  0), osg::Vec3( 0, -1,  0) ), // +X
                ImageData( osg::Vec3(-1,  0,  0), osg::Vec3( 0, -1,  0) ), // -X
                ImageData( osg::Vec3( 0,  1,  0), osg::Vec3( 0,  0,  1) ), // +Y
                ImageData( osg::Vec3( 0, -1,  0), osg::Vec3( 0,  0, -1) ), // -Y
                ImageData( osg::Vec3( 0,  0,  1), osg::Vec3( 0, -1,  0) ), // +Z
                ImageData( osg::Vec3( 0,  0, -1), osg::Vec3( 0, -1,  0) )  // -Z
            };

            for(unsigned int i = 0; i < 6 && i < _Cameras.size(); ++i) {
                osg::Matrix localOffset;
                localOffset.makeLookAt(position,position+id[i].first,id[i].second);

                osg::Matrix viewMatrix = worldToLocal*localOffset;

                _Cameras[i]->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
                _Cameras[i]->setProjectionMatrixAsFrustum(-1.0,1.0,-1.0,1.0,1.0,10000.0);
                _Cameras[i]->setViewMatrix(viewMatrix);
            }
        }

    protected:

        virtual ~UpdateCameraAndTexGenCallback() {}

        osg::NodePath               _reflectorNodePath;
        CameraList                  _Cameras;
};

class TexMatCullCallback : public osg::NodeCallback
{
    public:

        TexMatCullCallback(osg::TexMat* texmat):
            _texmat(texmat)
        {
        }

        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            // first update subgraph to make sure objects are all moved into position
            traverse(node,nv);

            osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
            if (cv)
            {
            osg::Quat q = osg::Matrix::inverse(*cv->getModelViewMatrix()).getRotate();


            float yaw2 = asin(-2.0f*(q.x()*q.z() - q.w()*q.y()));
            osg::Matrixd mxY;
            mxY.makeRotate(yaw2,osg::Vec3(0,0,1));

            osg::Matrixd mx = mxY;
                _texmat->setMatrix(mx);
            }
        }

    protected:

        osg::ref_ptr<TexMat>    _texmat;
};

class UpdateCameraPosUniformCallback : public osg::Uniform::Callback
{
public:
   UpdateCameraPosUniformCallback(osg::Camera* camera)
      : mCamera(camera)
   {
   }

   virtual void operator () (osg::Uniform* u, osg::NodeVisitor*)
   {
      osg::Vec3 eye;
      osg::Vec3 center;
      osg::Vec3 up;
      mCamera->getViewMatrixAsLookAt(eye,center,up);

      u->set(eye);
   }
protected:
   osg::Camera* mCamera;
};

osg::Group* createShadowedScene(osg::Node* reflectedSubgraph, osg::NodePath reflectorNodePath, unsigned int unit, const osg::Vec4& clearColor, unsigned tex_width, unsigned tex_height, osg::Camera::RenderTargetImplementation renderImplementation, osg::Camera* camera = 0) {
    osg::Group* group = new osg::Group;

    osg::TextureCubeMap* texture = new osg::TextureCubeMap;
    texture->setTextureSize(tex_width, tex_height);
    texture->setInternalFormat(GL_RGB);
    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
    texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
    texture->setFilter(osg::TextureCubeMap::MIN_FILTER,osg::TextureCubeMap::LINEAR);
    texture->setFilter(osg::TextureCubeMap::MAG_FILTER,osg::TextureCubeMap::LINEAR);

    // set up the render to texture cameras.
    UpdateCameraAndTexGenCallback::CameraList Cameras;
    for(unsigned int i = 0; i < 6; ++i) {
        // create the camera
        osg::Camera* camera = new osg::Camera;

        camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        camera->setClearColor(clearColor);

        // set viewport
        camera->setViewport(0,0,tex_width,tex_height);

        // set the camera to render before the main camera.
        camera->setRenderOrder(osg::Camera::PRE_RENDER);

        // tell the camera to use OpenGL frame buffer object where supported.
        camera->setRenderTargetImplementation(renderImplementation);

        // attach the texture and use it as the color buffer.
        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, i);

        // add subgraph to render
        camera->addChild(reflectedSubgraph);

        group->addChild(camera);

        Cameras.push_back(camera);
    }

    // create the texgen node to project the tex coords onto the subgraph
    osg::TexGenNode* texgenNode = new osg::TexGenNode;
    texgenNode->getTexGen()->setMode(osg::TexGen::REFLECTION_MAP);
    texgenNode->setTextureUnit(unit);
    group->addChild(texgenNode);

    // set the reflected subgraph so that it uses the texture and tex gen settings.
   osg::Node* reflectorNode = reflectorNodePath.front();
    {

        group->addChild(reflectorNode);

        osg::StateSet* stateset = reflectorNode->getOrCreateStateSet();
      stateset->setTextureAttributeAndModes(unit,texture,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

        osg::TexMat* texmat = new osg::TexMat;
        stateset->setTextureAttributeAndModes(unit,texmat,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

        reflectorNode->setCullCallback(new TexMatCullCallback(texmat));
    }

   osg::StateSet* ss = reflectorNode->getOrCreateStateSet();
   ss->setTextureAttributeAndModes(unit,texture,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

   osg::Program* program = new osg::Program;
    osg::ref_ptr<osg::Shader> shaderVertex = osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile(SHADER_PATH_VERT));
    osg::ref_ptr<osg::Shader> shaderFragment = osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile(SHADER_PATH_FRAG));
    program->addShader(shaderFragment);
    program->addShader(shaderVertex);
    ss->setAttributeAndModes( program, osg::StateAttribute::ON );
   ss->addUniform( new osg::Uniform("cubeMap", unit) );

   osg::Uniform* u = new osg::Uniform("cameraPos",osg::Vec3());
   u->setUpdateCallback( new UpdateCameraPosUniformCallback( camera ) );
   ss->addUniform( u );

    // add the reflector scene to draw just as normal
    group->addChild(reflectedSubgraph);

    // set an update callback to keep moving the camera and tex gen in the right direction.
    group->setUpdateCallback(new UpdateCameraAndTexGenCallback(reflectorNodePath, Cameras));

    return group;
}


int main() {
    // construct the viewer.
    osgViewer::Viewer viewer;

    unsigned tex_width = 256;
    unsigned tex_height = 256;

    osg::Camera::RenderTargetImplementation renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT;

    osg::ref_ptr<osg::Group> scene = new osg::Group;
    osg::ref_ptr<osg::Group> reflectedSubgraph = _create_scene();
    if (!reflectedSubgraph.valid()) exit(0);

    osg::ref_ptr<osg::Group> reflectedScene = createShadowedScene(
         reflectedSubgraph.get(),
         createReflector(),
         TEXTURE_UNIT_CUBEMAP,
         viewer.getCamera()->getClearColor(),
            tex_width,
         tex_height,
         renderImplementation,
         viewer.getCamera());

    scene->addChild(reflectedScene.get());
    viewer.setSceneData(scene.get());
    viewer.setUpViewInWindow(0,0,600,600);
    viewer.run();
    return 0;
}



Vertex Shader:
Code:
#version 130

uniform mat4 osg_ViewMatrixInverse;
uniform vec3 cameraPos;

void main() {
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
    mat4 ModelWorld4x4 = osg_ViewMatrixInverse * gl_ModelViewMatrix;
    mat3 ModelWorld3x3 = mat3( ModelWorld4x4 );
    vec4 WorldPos = ModelWorld4x4 *  gl_Vertex;
    vec3 N = normalize( ModelWorld3x3 * gl_Normal );
    vec3 E = normalize( WorldPos.xyz - cameraPos.xyz );
    gl_TexCoord[1].xyz = reflect( E, N );
}


Fragment Shader:
Code:
#version 130

uniform samplerCube cubeMap;
uniform sampler2D colorMap;
const float reflect_factor = 0.5;

void main (void)                                                           
{
    vec3 base_color = texture2D(colorMap, gl_TexCoord[0].xy).rgb;
    vec3 cube_color = textureCube(cubeMap, gl_TexCoord[1].xyz).rgb;
    gl_FragColor = vec4( mix(cube_color, base_color, reflect_factor).rgb, 1.0);
}

...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 41

PostPosted: Tue Dec 19, 2017 10:49 pm    Post subject:
Reply with quote

Hi,

I got the depth data from reflected objects. I needed to render the depth buffer to texture. Follows my update code:

C++ code:
Code:

// OSG includes
#include <osgViewer/Viewer>
#include <osg/Texture>
#include <osg/TexGen>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/TextureCubeMap>
#include <osg/TexMat>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Camera>
#include <osg/TexGenNode>
#include <osgDB/FileUtils>

// C++ includes
#include <iostream>

#define SHADER_PATH_FRAG "normal_depth_map/shaders/normalDepthMap.frag"
#define SHADER_PATH_VERT "normal_depth_map/shaders/normalDepthMap.vert"

#define BOOST_TEST_MODULE "DynamicCubeMap_test"

using namespace osg;

unsigned int numTextures = 6;

enum TextureUnitTypes {
    TEXTURE_UNIT_DIFFUSE,
    TEXTURE_UNIT_NORMAL,
    TEXTURE_UNIT_CUBEMAP
};

osg::ref_ptr<osg::Group> _create_scene() {
    osg::ref_ptr<osg::Group> scene = new osg::Group;

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    scene->addChild(geode.get());

    const float radius = 0.8f;
    const float height = 1.0f;
    osg::ref_ptr<osg::ShapeDrawable> shape;

    // sphere
    shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-3.0f, 0.0f, 0.0f), radius));
    shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
    geode->addDrawable(shape.get());

    // box
    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius));
    shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
    geode->addDrawable(shape.get());

    // cone
    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, -3.0f), radius, height));
    shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
    geode->addDrawable(shape.get());

    // cylinder
    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, 3.0f), 2* radius));
    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
    geode->addDrawable(shape.get());

    return scene;
}

osg::NodePath createReflector() {
    Geode* node = new Geode;
    const float radius = 0.8f;
    ref_ptr<TessellationHints> hints = new TessellationHints;
    hints->setDetailRatio(2.0f);
    ShapeDrawable* shape = new ShapeDrawable(new Sphere(Vec3(0.0f, 0.0f, 0.0f), radius * 1.5f), hints.get());
    shape->setColor(Vec4(0.8f, 0.8f, 0.8f, 1.0f));
    node->addDrawable(shape);

    osg::NodePath nodeList;
    nodeList.push_back(node);

    return nodeList;
}

class UpdateCameraAndTexGenCallback : public osg::NodeCallback
{
    public:

        typedef std::vector< osg::ref_ptr<osg::Camera> >  CameraList;

        UpdateCameraAndTexGenCallback(osg::NodePath& reflectorNodePath, CameraList& Cameras):
            _reflectorNodePath(reflectorNodePath),
            _Cameras(Cameras)
        {
        }

        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            // first update subgraph to make sure objects are all moved into position
            traverse(node,nv);

            // compute the position of the center of the reflector subgraph
            osg::Matrixd worldToLocal = osg::computeWorldToLocal(_reflectorNodePath);
            osg::BoundingSphere bs = _reflectorNodePath.back()->getBound();
            osg::Vec3 position = bs.center();

            typedef std::pair<osg::Vec3, osg::Vec3> ImageData;
            const ImageData id[] =
            {
                ImageData( osg::Vec3( 1,  0,  0), osg::Vec3( 0, -1,  0) ), // +X
                ImageData( osg::Vec3(-1,  0,  0), osg::Vec3( 0, -1,  0) ), // -X
                ImageData( osg::Vec3( 0,  1,  0), osg::Vec3( 0,  0,  1) ), // +Y
                ImageData( osg::Vec3( 0, -1,  0), osg::Vec3( 0,  0, -1) ), // -Y
                ImageData( osg::Vec3( 0,  0,  1), osg::Vec3( 0, -1,  0) ), // +Z
                ImageData( osg::Vec3( 0,  0, -1), osg::Vec3( 0, -1,  0) )  // -Z
            };

            for(unsigned int i = 0; i < 6 && i < _Cameras.size(); ++i) {
                osg::Matrix localOffset;
                localOffset.makeLookAt(position,position+id[i].first,id[i].second);

                osg::Matrix viewMatrix = worldToLocal*localOffset;

                _Cameras[i]->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
                _Cameras[i]->setProjectionMatrixAsFrustum(-1.0,1.0,-1.0,1.0,1.0,10000.0);
                _Cameras[i]->setViewMatrix(viewMatrix);
            }
        }

    protected:

        virtual ~UpdateCameraAndTexGenCallback() {}

        osg::NodePath               _reflectorNodePath;
        CameraList                  _Cameras;
};

class TexMatCullCallback : public osg::NodeCallback
{
    public:

        TexMatCullCallback(osg::TexMat* texmat):
            _texmat(texmat)
        {
        }

        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            // first update subgraph to make sure objects are all moved into position
            traverse(node,nv);

            osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
            if (cv)
            {
            osg::Quat q = osg::Matrix::inverse(*cv->getModelViewMatrix()).getRotate();


            float yaw2 = asin(-2.0f*(q.x()*q.z() - q.w()*q.y()));
            osg::Matrixd mxY;
            mxY.makeRotate(yaw2,osg::Vec3(0,0,1));

            osg::Matrixd mx = mxY;
                _texmat->setMatrix(mx);
            }
        }

    protected:

        osg::ref_ptr<TexMat>    _texmat;
};

class UpdateCameraPosUniformCallback : public osg::Uniform::Callback
{
public:
   UpdateCameraPosUniformCallback(osg::Camera* camera)
      : mCamera(camera)
   {
   }

   virtual void operator () (osg::Uniform* u, osg::NodeVisitor*)
   {
      osg::Vec3 eye;
      osg::Vec3 center;
      osg::Vec3 up;
      mCamera->getViewMatrixAsLookAt(eye,center,up);

      u->set(eye);
   }
protected:
   osg::Camera* mCamera;
};

osg::Group* createShadowedScene(osg::Node* reflectedSubgraph, osg::NodePath reflectorNodePath, unsigned int unit, const osg::Vec4& clearColor, unsigned tex_width, unsigned tex_height, osg::Camera::RenderTargetImplementation renderImplementation, osg::Camera* camera = 0) {
    osg::Group* group = new osg::Group;

    osg::TextureCubeMap* texture = new osg::TextureCubeMap;
    texture->setTextureSize(tex_width, tex_height);
    texture->setInternalFormat(GL_DEPTH_COMPONENT);
    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
    texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
    texture->setFilter(osg::TextureCubeMap::MIN_FILTER,osg::TextureCubeMap::LINEAR);
    texture->setFilter(osg::TextureCubeMap::MAG_FILTER,osg::TextureCubeMap::LINEAR);

    // set up the render to texture cameras.
    UpdateCameraAndTexGenCallback::CameraList Cameras;
    for(unsigned int i = 0; i < 6; ++i) {
        // create the camera
        osg::Camera* camera = new osg::Camera;

        camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        camera->setClearColor(clearColor);

        // set viewport
        camera->setViewport(0,0,tex_width,tex_height);

        // set the camera to render before the main camera.
        camera->setRenderOrder(osg::Camera::PRE_RENDER);

        // tell the camera to use OpenGL frame buffer object where supported.
        camera->setRenderTargetImplementation(renderImplementation);

        // attach the texture and use it as the color buffer.
        camera->attach(osg::Camera::DEPTH_BUFFER, texture, 0, i);

        // add subgraph to render
        camera->addChild(reflectedSubgraph);

        group->addChild(camera);

        Cameras.push_back(camera);
    }

    // create the texgen node to project the tex coords onto the subgraph
    osg::TexGenNode* texgenNode = new osg::TexGenNode;
    texgenNode->getTexGen()->setMode(osg::TexGen::REFLECTION_MAP);
    texgenNode->setTextureUnit(unit);
    group->addChild(texgenNode);

    // set the reflected subgraph so that it uses the texture and tex gen settings.
   osg::Node* reflectorNode = reflectorNodePath.front();
    {

        group->addChild(reflectorNode);

        osg::StateSet* stateset = reflectorNode->getOrCreateStateSet();
      stateset->setTextureAttributeAndModes(unit,texture,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
        stateset->setTextureMode(unit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

        osg::TexMat* texmat = new osg::TexMat;
        stateset->setTextureAttributeAndModes(unit,texmat,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

        reflectorNode->setCullCallback(new TexMatCullCallback(texmat));
    }

   osg::StateSet* ss = reflectorNode->getOrCreateStateSet();
   ss->setTextureAttributeAndModes(unit,texture,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);

   osg::Program* program = new osg::Program;
    osg::ref_ptr<osg::Shader> shaderVertex = osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile(SHADER_PATH_VERT));
    osg::ref_ptr<osg::Shader> shaderFragment = osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile(SHADER_PATH_FRAG));
    program->addShader(shaderFragment);
    program->addShader(shaderVertex);
    ss->setAttributeAndModes( program, osg::StateAttribute::ON );
   ss->addUniform( new osg::Uniform("cubeMap", unit) );

   osg::Uniform* u = new osg::Uniform("cameraPos",osg::Vec3());
   u->setUpdateCallback( new UpdateCameraPosUniformCallback( camera ) );
   ss->addUniform( u );

    // add the reflector scene to draw just as normal
    group->addChild(reflectedSubgraph);

    // set an update callback to keep moving the camera and tex gen in the right direction.
    group->setUpdateCallback(new UpdateCameraAndTexGenCallback(reflectorNodePath, Cameras));

    return group;
}


int main() {
    // construct the viewer.
    osgViewer::Viewer viewer;

    unsigned tex_width = 256;
    unsigned tex_height = 256;

    osg::Camera::RenderTargetImplementation renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT;

    osg::ref_ptr<osg::Group> scene = new osg::Group;
    osg::ref_ptr<osg::Group> reflectedSubgraph = _create_scene();
    if (!reflectedSubgraph.valid()) exit(0);

    osg::ref_ptr<osg::Group> reflectedScene = createShadowedScene(
         reflectedSubgraph.get(),
         createReflector(),
         TEXTURE_UNIT_CUBEMAP,
         viewer.getCamera()->getClearColor(),
            tex_width,
         tex_height,
         renderImplementation,
         viewer.getCamera());

    scene->addChild(reflectedScene.get());
    viewer.setSceneData(scene.get());
    viewer.setUpViewInWindow(0,0,600,600);
    viewer.run();
    return 0;
}



Vertex Shader:
Code:
#version 130

uniform mat4 osg_ViewMatrixInverse;
uniform vec3 cameraPos;

void main() {
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
    mat4 ModelWorld4x4 = osg_ViewMatrixInverse * gl_ModelViewMatrix;
    mat3 ModelWorld3x3 = mat3( ModelWorld4x4 );
    vec4 WorldPos = ModelWorld4x4 *  gl_Vertex;
    vec3 N = normalize( ModelWorld3x3 * gl_Normal );
    vec3 E = normalize( WorldPos.xyz - cameraPos.xyz );
    gl_TexCoord[1].xyz = reflect( E, N );
}


Fragment Shader:
Code:
#version 130

uniform samplerCube cubeMap;
uniform sampler2D colorMap;
const float reflect_factor = 0.5;

void main (void)                                                           
{
    vec3 base_color = texture2D(colorMap, gl_TexCoord[0].xy).rgb;
    vec3 cube_color = textureCube(cubeMap, gl_TexCoord[1].xyz).rgb;
    gl_FragColor = vec4( mix(cube_color, base_color, reflect_factor).rgb, 1.0);
}


...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Appending dynamic 3d line ertanataman General 2 Thu Jan 11, 2018 2:23 pm View latest post
No new posts Normal Mapping using Dynamic Cubemap romulogcerqueira General 6 Tue Dec 19, 2017 2:10 am View latest post
No new posts Dynamic Cubemap with OSG and Shader romulogcerqueira General 0 Tue Dec 05, 2017 1:27 am View latest post
No new posts Dynamic linking of fbx plugin and fbx... Vladimir Chebaev Submission 5 Mon Nov 14, 2016 1:20 pm View latest post
No new posts Drawing a textured cube cbuchner1 General 2 Fri Apr 08, 2016 8:31 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP