CubeMapGeneratorStageDataTransitPtr CubeMapGenerator::setupStageData(
    RenderActionBase *pAction)
{
    CubeMapGeneratorStageDataTransitPtr returnValue = 
        CubeMapGeneratorStageData::createLocal();
    
    if(returnValue == NULL)
        return returnValue;

    FrameBufferObjectUnrecPtr pCubeTarget  = NULL;
    RenderBufferUnrecPtr      pDepthBuffer = NULL;

    if(this->getRenderTarget() == NULL)
    {
        pCubeTarget  = FrameBufferObject::createLocal();
        pDepthBuffer = RenderBuffer     ::createLocal();

        pDepthBuffer->setInternalFormat (GL_DEPTH_COMPONENT24);

        pCubeTarget ->setDepthAttachment(pDepthBuffer        );

        returnValue ->setRenderTarget   (pCubeTarget         );
    }
    else
    {
        pCubeTarget = this->getRenderTarget();
    }

    TextureObjChunkUnrecPtr   pCubeTex     = NULL;

    if(0x0000 != (_sfSetupMode.getValue() & SetupTexture))
    {
        pCubeTex = TextureObjChunk::createLocal();

        ImageUnrecPtr pImg = Image::createLocal();
    
        pImg->set(Image::OSG_RGB_PF, 
                  getWidth (), 
                  getHeight(),
                  1,
                  1,
                  1,
                  0.0,
                  0,
                  Image::OSG_UINT8_IMAGEDATA,
                  false,
                  6);
        
        pCubeTex   ->setImage         (pImg              ); 
        pCubeTex   ->setMinFilter     (GL_LINEAR         );
        pCubeTex   ->setMagFilter     (GL_LINEAR         );
        pCubeTex   ->setWrapS         (GL_CLAMP_TO_EDGE  );
        pCubeTex   ->setWrapT         (GL_CLAMP_TO_EDGE  );
        pCubeTex   ->setWrapR         (GL_CLAMP_TO_EDGE  );
        pCubeTex   ->setInternalFormat(getTextureFormat());
    }
    else
    {
        pCubeTex = _sfTexture.getValue();
    }

    TextureEnvChunkUnrecPtr pCubeTexEnv  = NULL;

    if(0x0000 != (_sfSetupMode.getValue() & SetupTexEnv))
    {
        pCubeTexEnv = TextureEnvChunk::createLocal();
        
        pCubeTexEnv->setEnvMode       (GL_REPLACE       );
    }

    TexGenChunkUnrecPtr           pCubeTexGen   = NULL;
    TextureTransformChunkUnrecPtr pCubeTexTrans = NULL;

    if(0x0000 != (_sfSetupMode.getValue() & SetupTexGen))
    {
        pCubeTexGen = TexGenChunk::createLocal();

        pCubeTexGen->setGenFuncS(GL_REFLECTION_MAP);
        pCubeTexGen->setGenFuncT(GL_REFLECTION_MAP);
        pCubeTexGen->setGenFuncR(GL_REFLECTION_MAP);

        pCubeTexTrans = TextureTransformChunk::createLocal();

        pCubeTexTrans->setUseCameraBeacon(true);
    }


    static GLenum targets[6] = 
    {
        GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
        GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
        GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
    };

    for(UInt32 i = 0; i < 6; ++i)
    {
        TextureBufferUnrecPtr pCubeTexBuffer = TextureBuffer::createLocal();
    
        pCubeTexBuffer->setTexture  (pCubeTex  );
        pCubeTexBuffer->setTexTarget(targets[i]);

        pCubeTarget->setColorAttachment(pCubeTexBuffer,    i);
    }

    pCubeTarget->setSize(getWidth (),
                         getHeight());

    if(0x0000 != (_sfSetupMode.getValue() & OverrideTex))
    {
        returnValue->addChunk(pCubeTex,
                              getTexUnit());
    }

    if(0x0000 != (_sfSetupMode.getValue() & SetupTexEnv))
    {
        returnValue->addChunk(pCubeTexEnv,
                              getTexUnit());
    }

    if(0x0000 != (_sfSetupMode.getValue() & SetupTexGen))
    {
        returnValue->addChunk(pCubeTexGen,
                              getTexUnit());
        returnValue->addChunk(pCubeTexTrans,
                              getTexUnit());

        returnValue->setTexTransform(pCubeTexTrans);
    }

    if(this->getCamera() == NULL)
    {
        PerspectiveCameraUnrecPtr pCam = PerspectiveCamera::createLocal();

        pCam->setNear(pAction->getCamera()->getNear());
        pCam->setFar (pAction->getCamera()->getFar ());
        
        pCam->setFov (osgDegree2Rad(90.f));

        returnValue->setCamera(pCam);
    }

    return returnValue;
}
Example #2
0
void VRShadowEngine::doFinalPass(Light         *pLight,
                                        RenderAction  *pAction,
                                        EngineDataPtr  pEngineData)
{
    this->pushPartition(pAction,
                        (RenderPartition::CopyViewing      |
                         RenderPartition::CopyProjection   |
                         RenderPartition::CopyWindow       |
                         RenderPartition::CopyViewportSize |
                         RenderPartition::CopyFrustum      |
                         RenderPartition::CopyNearFar      ));

    FrameBufferObject *pTarget = pEngineData->getRenderTarget();

    if(pTarget == NULL)
    {
        FrameBufferObjectUnrecPtr pFBO = FrameBufferObject::createLocal();

        pFBO->setWidth (this->getWidth ());
        pFBO->setHeight(this->getHeight());

        pEngineData->setRenderTarget(pFBO);

        pTarget = pFBO;
    }

    BlendChunkUnrecPtr pBlender      = pEngineData->getBlendChunk();

    if(pBlender == NULL)
    {
        pBlender = BlendChunk::createLocal();

        pBlender->setSrcFactor(GL_ONE);
        pBlender->setDestFactor(GL_ONE);
        pBlender->setAlphaFunc(GL_GEQUAL);
        pBlender->setAlphaValue(0.99f);

        pEngineData->setBlendChunk(pBlender);
    }


    Matrix4f projectionMatrix, viewMatrix, biasMatrix;

    biasMatrix.setIdentity();
    biasMatrix.setScale(0.5);
    biasMatrix.setTranslate(0.5,0.5,0.5);

    MatrixCamera *pCam = dynamic_cast<MatrixCamera *>(pEngineData->getCamera());
    pCam->getProjection(projectionMatrix, this->getWidth (), this->getHeight());
    pCam->getViewing(viewMatrix, this->getWidth (), this->getHeight());

    Matrix textureMatrix = biasMatrix;
    textureMatrix.mult(projectionMatrix);
    textureMatrix.mult(viewMatrix);

    textureMatrix.transpose();
    Vec4f ps = textureMatrix[0];
    Vec4f pt = textureMatrix[1];
    Vec4f pr = textureMatrix[2];
    Vec4f pq = textureMatrix[3];

    TexGenChunkUnrecPtr pTexGen = pEngineData->getTexGenChunk();

    if(pTexGen == NULL)
    {
        pTexGen = TexGenChunk::createLocal();

        pEngineData->setTexGenChunk(pTexGen);

        pTexGen->setEyeModelViewMode(TexGenChunk::EyeModelViewCamera);

        pTexGen->setGenFuncS(GL_EYE_LINEAR);
        pTexGen->setGenFuncT(GL_EYE_LINEAR);
        pTexGen->setGenFuncR(GL_EYE_LINEAR);
        pTexGen->setGenFuncQ(GL_EYE_LINEAR);
    }

    pTexGen->setGenFuncSPlane(ps);
    pTexGen->setGenFuncTPlane(pt);
    pTexGen->setGenFuncRPlane(pr);
    pTexGen->setGenFuncQPlane(pq);

    TextureObjChunkUnrecPtr pTexChunk = pEngineData->getTexChunk();

    if(pTexChunk == NULL)
    {
        pTexChunk = TextureObjChunk::createLocal();

        pEngineData->setTexChunk(pTexChunk);

        ImageUnrecPtr pImage = Image::createLocal();

            // creates a image without allocating main memory.

        pImage->set(Image::OSG_L_PF,
                    pTarget->getWidth (), pTarget->getHeight(), 1,
                    1, 1, 0, NULL,
                    Image::OSG_UINT8_IMAGEDATA, false);


        pTexChunk->setImage         (pImage);
        pTexChunk->setInternalFormat(GL_DEPTH_COMPONENT32);
        pTexChunk->setExternalFormat(GL_DEPTH_COMPONENT);
        pTexChunk->setMinFilter     (GL_LINEAR); // tried GL_LINEAR_MIPMAP_LINEAR
        pTexChunk->setMagFilter     (GL_LINEAR);
        pTexChunk->setWrapS         (GL_CLAMP_TO_EDGE); // was GL_CLAMP_TO_BORDER
        pTexChunk->setWrapT         (GL_CLAMP_TO_EDGE); // was GL_CLAMP_TO_BORDER
//        pTexChunk->setEnvMode       (GL_MODULATE);
        pTexChunk->setTarget        (GL_TEXTURE_2D);

        pTexChunk->setCompareMode(GL_COMPARE_R_TO_TEXTURE);
        pTexChunk->setCompareFunc(GL_LEQUAL);
        pTexChunk->setDepthMode  (GL_INTENSITY);
    }

    pAction->pushState();

    UInt32 uiBlendSlot  = pBlender ->getClassId();
    UInt32 uiTexSlot    = pTexChunk->getClassId();
    UInt32 uiTexGenSlot = pTexGen  ->getClassId();

    if(this->getForceTextureUnit() != -1)
    {
        uiTexSlot    += this->getForceTextureUnit();
        uiTexGenSlot += this->getForceTextureUnit();
    }
    else
    {
        uiTexSlot    += 3;
        uiTexGenSlot += 3;
    }

    pAction->addOverride(uiBlendSlot,  pBlender );
    pAction->addOverride(uiTexSlot,    pTexChunk);
    pAction->addOverride(uiTexGenSlot, pTexGen  );

    lightRenderEnter(pLight, pAction);

    pAction->useNodeList(false);

    this->recurseFrom(pAction, pLight);

    pAction->popState();

    this->popPartition(pAction);
}