Example #1
0
    //------------------------------------------------------------------------------
    void UnitInMipmapOut::init()
    {
        // default initialization
        UnitInOut::init();

        // if we have to create mipmaps for an input texture
        if (mGenerateMipmapInputIndex >= 0)
        {
            setOutputTexture(getInputTexture(mGenerateMipmapInputIndex));
            assignOutputTexture();
        }

        // enable mipmap levels in the output texture
        enableMipmapGeneration();

        // if generating of mipmaps done in software, then generate drawables
        if (mUseShader)
        {
            // disable standard fbo
            //getOrCreateStateSet()->removeAttribute(mFBO.get());
            getOrCreateStateSet()->removeAttribute(mViewport.get());

            // this is the base level of mipmap generation
            // if the output texture is the same as input texture, then
            // we do not need to recompute the level 0, because it should 
            // stay the same.
            int baseLevel = (mGenerateMipmapInputIndex < 0 ? 0 : 1);

            // attach a drawable for each mipmap level
            for (int i = baseLevel; i < mNumLevels; i++)
            {
                // setup the drawable
                osg::Drawable* draw = mMipmapDrawable[i];
                osg::StateSet* ss = draw->getOrCreateStateSet();

                // setup drawable uniforms
                osg::Uniform* w = ss->getOrCreateUniform(OSGPPU_VIEWPORT_WIDTH_UNIFORM, osg::Uniform::FLOAT);
                osg::Uniform* h = ss->getOrCreateUniform(OSGPPU_VIEWPORT_HEIGHT_UNIFORM, osg::Uniform::FLOAT);
                w->set((float)mMipmapViewport[i]->width());
                h->set((float)mMipmapViewport[i]->height());

                osg::Uniform* iw = ss->getOrCreateUniform(OSGPPU_VIEWPORT_INV_WIDTH_UNIFORM, osg::Uniform::FLOAT);
                osg::Uniform* ih = ss->getOrCreateUniform(OSGPPU_VIEWPORT_INV_HEIGHT_UNIFORM, osg::Uniform::FLOAT);
                iw->set(1.0f / (float)mMipmapViewport[i]->width());
                ih->set(1.0f / (float)mMipmapViewport[i]->height());

                osg::Uniform* ln = ss->getOrCreateUniform(OSGPPU_MIPMAP_LEVEL_NUM_UNIFORM, osg::Uniform::FLOAT);
                ln->set((float)mNumLevels);

                osg::Uniform* le = ss->getOrCreateUniform(OSGPPU_MIPMAP_LEVEL_UNIFORM, osg::Uniform::FLOAT);
                le->set((float)i);
            }
        }
    }
Example #2
0
	void GausBlur::apply() {
		assert(m_inputTexure[0] && "GausBlur::calculate: Input texture not specified");

		//
		// First pass.
		//
		m_rai->setActiveShaderProgram(m_shaderProgram);
		m_rai->setActiveVertexArray(m_va);

		xdl::xdl_uint targets [] = {xdl::XDEVL_COLOR_TARGET0};

		m_frameBuffer->activate();

		m_frameBuffer->activateColorTargets(1, targets);
		m_frameBuffer->clearColorTargets(0.0f, 0.0f, 0.0f, 0.0f);

		m_shaderProgram->activate();
		m_shaderProgram->setUniformi(m_blurDirectionid, 0);
		m_shaderProgram->setUniform2v(m_blurSizeid,1, m_blurSize);

		m_shaderProgram->setUniformi(m_textureid, 0);
		m_inputTexure[0]->activate(0);

		m_rai->drawVertexArray(xdl::XDEVL_PRIMITIVE_TRIANGLES, 6);


		m_frameBuffer->deactivate();


		//
		// Second pass
		//

		m_frameBuffer2->activate();
		m_frameBuffer2->activateColorTargets(1, targets);
		m_frameBuffer2->clearColorTargets(1.0f, 0.0f, 0.0f, 0.0f);

		m_shaderProgram->activate();
		m_shaderProgram->setUniformi(m_blurDirectionid, 1);
		m_shaderProgram->setUniform2v(m_blurSizeid, 1, m_blurSize);

		m_shaderProgram->setUniformi(m_textureid, 0);
		m_frameBuffer->getTexture(0)->activate(0);

		m_rai->drawVertexArray(xdl::XDEVL_PRIMITIVE_TRIANGLES, 6);

		m_frameBuffer2->deactivate();



		// This is important. Set the output texture.
		setOutputTexture(0, m_frameBuffer2->getTexture(0));

	}