Terkadang kita ingin me-render image menjadi tekstur. Hal ini umumnya digunakan dalam menerapkan post process filters atau untuk membuat picture-in-picture dengan perspektif yang berbeda. Contoh ini akan mencakup penerapan RTT dalam pembuatan gambar dalam gambar (picture in picture). Contoh ini sangat menerapkan OSG. Untuk contoh yang lebih up-to-date dalam penggunaan Delta3D, silahkan lihat contoh program Delta3D berjudul "testPreRender" (2/19/09).
Untuk pengubahan ukuran, kita akan mulai dengan menentukan dimensi PIP (picture in picture).
float width = screenWidth * 0.35f; float height = screenHeight * 0.35f;
Kemudian, kita akan membuat tekstur yang akan di render.
// Create the texture to render to osg::Texture2D* renderTexture = new osg::Texture2D; renderTexture->setTextureSize(screenWidth, screenHeight); renderTexture->setInternalFormat(GL_RGBA); renderTexture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR); renderTexture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
osg::ref_ptr<osg::Geometry> screenQuad; screenQuad = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(width, 0.0, 0.0), osg::Vec3(0.0, height, 0.0));
Buat OSG Geode dan bentuk geometry pada progam.
osg::ref_ptr<osg::Geode> quadGeode = new osg::Geode; quadGeode->addDrawable(screenQuad.get());
osg::StateSet *quadState = quadGeode->getOrCreateStateSet(); quadState->setTextureAttributeAndModes(0, renderTexture,
osg::StateAttribute::ON);
Pertama kita buat Kamera yang akan me-Render ke dalam bentuk Tekstur.
osg::ref_ptr<osg::Camera> textureCamera = new osg::Camera; textureCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); textureCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); textureCamera->setViewport(0, 0, screenWidth, screenHeight); // Frame buffer objects are the best option textureCamera->setRenderTargetImplementation
(osg::Camera::FRAME_BUFFER_OBJECT); // We need to render to the texture BEFORE we render to the screen textureCamera->setRenderOrder(osg::Camera::PRE_RENDER); // The camera will render into the texture that we created earlier textureCamera->attach(osg::Camera::COLOR_BUFFER, renderTexture); // Add whatever children you want drawn to the texture textureCamera->addChild(mySceneGroupNode.get());
osg::ref_ptr<osg::Camera> orthoCamera = new osg::Camera; // We don't want to apply perspective, just overlay using orthographic orthoCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, width, 0, height)); orthoCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); orthoCamera->setViewMatrix(osg::Matrix::identity()); // Choose a good spot on the screen to overlay the quad float xPos = screenWidth * 0.635f; float yPos = screenHeight * 0.625f; orthoCamera->setViewport(xPos, yPos, width, height); // Make sure to render this after rendering the scene // in order to overlay the quad on top orthoCamera->setRenderOrder(osg::Camera::POST_RENDER); // Render only the quad orthoCamera->addChild(quadGeode.get());
sceneRoot->addChild(textureCamera.get()); sceneRoot->addChild(orthoCamera.get());
Tidak ada komentar:
Posting Komentar