void ShaderShadowMapEngine::updatePointLightRenderTargets(SSMEngineData *data) { if(data->getMFRenderTargets()->empty() == true) { for(UInt16 i = 0; i < 6; ++i) { FrameBufferObjectUnrecPtr newTarget = FrameBufferObject::createLocal(); newTarget->setWidth (this->getWidth ( )); newTarget->setHeight (this->getHeight ( )); newTarget->setDepthAttachment(data->getShadowTexBuffers(i)); data->editMFRenderTargets()->push_back(newTarget); } } else { for(UInt16 i = 0; i < 6; ++i) { FrameBufferObject *target = data->getRenderTargets(i); if((target->getWidth () != this->getWidth ()) || (target->getHeight() != this->getHeight()) ) { target->setWidth (this->getWidth ()); target->setHeight(this->getHeight()); } } } }
FrameBufferObjectTransitPtr FBOBuilder::operator()( UInt32 width, UInt32 height) const { // // Setup the FBO // FrameBufferObjectUnrecPtr fbo = FrameBufferObject::create(); // // multiple color buffers // for (UINT32 idx = 0; idx < _buffers.size(); ++idx) { // // use textures? // if (_buffers[idx].enable) { ImageUnrecPtr texImg = (_buffers[idx].image == nullptr ? Image::create() : _buffers[idx].image); TextureObjChunkUnrecPtr texObj = (_buffers[idx].texObj == nullptr ? TextureObjChunk::create() : _buffers[idx].texObj); TextureBufferUnrecPtr texBuf = TextureBuffer::create(); if (_buffers[idx].image == nullptr) texImg->set(_buffers[idx].pixel_format, width, height, 1, 1, 1, 0.f, nullptr, _buffers[idx].type, _buffers[idx].main_memory); texObj->setImage(texImg); texBuf->setTexture(texObj); fbo->setColorAttachment(texBuf, idx); } else // // no, then use simple render buffer // { RenderBufferUnrecPtr renBuf = RenderBuffer::create(); renBuf->setInternalFormat(_buffers[idx].pixel_format); fbo->setColorAttachment(renBuf, idx); } fbo->editMFDrawBuffers()->push_back(GL_COLOR_ATTACHMENT0_EXT + idx); } // // a sole depth buffer // if (_depth && !_stencil) { // // use textures? // if (_ds_buffer.enable) { ImageUnrecPtr texImg = (_ds_buffer.image == nullptr ? Image::create() : _ds_buffer.image); TextureObjChunkUnrecPtr texObj = (_ds_buffer.texObj == nullptr ? TextureObjChunk::create() : _ds_buffer.texObj); TextureBufferUnrecPtr texBuf = TextureBuffer::create(); if (_ds_buffer.image == nullptr) texImg->set(_ds_buffer.pixel_format, width, height, 1, 1, 1, 0.f, nullptr, _ds_buffer.type, _ds_buffer.main_memory); texObj->setImage(texImg); if (_ds_buffer.texObj == nullptr) { texObj->setInternalFormat(GL_DEPTH_COMPONENT24); texObj->setExternalFormat(GL_DEPTH_COMPONENT24); } texBuf->setTexture(texObj); fbo->setDepthAttachment(texBuf); } else // // no, then use simple render buffer // { RenderBufferUnrecPtr renBuf = RenderBuffer::create(); renBuf->setInternalFormat(GL_DEPTH_COMPONENT24); fbo->setDepthAttachment(renBuf); } } else // // or a combined depth/stencil buffer // if (_depth && _stencil) { // // use textures? // if (_ds_buffer.enable) { ImageUnrecPtr texImg = (_ds_buffer.image == nullptr ? Image::create() : _ds_buffer.image); TextureObjChunkUnrecPtr texObj = (_ds_buffer.texObj == nullptr ? TextureObjChunk::create() : _ds_buffer.texObj); TextureBufferUnrecPtr texBuf = TextureBuffer::create(); if (_ds_buffer.image == nullptr) texImg->set(GL_DEPTH_STENCIL_EXT, width, height, 1, 1, 1, 0.f, nullptr, GL_UNSIGNED_INT_24_8, _ds_buffer.main_memory); texObj->setImage(texImg); texObj->setInternalFormat(GL_DEPTH24_STENCIL8_EXT); texObj->setExternalFormat(GL_DEPTH_STENCIL_EXT); texBuf->setTexture(texObj); fbo->setDepthAttachment(texBuf); fbo->setStencilAttachment(texBuf); } else // // no, then use simple render buffer // { RenderBufferUnrecPtr renBuf = RenderBuffer::create(); renBuf->setInternalFormat(GL_DEPTH24_STENCIL8); fbo->setDepthAttachment(renBuf); fbo->setStencilAttachment(renBuf); } } fbo->setWidth (width ); fbo->setHeight(height); return FrameBufferObjectTransitPtr(fbo); }
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); }
// // FBO solution // static void writeHiResScreenShotFBO(const char* name, UInt32 width, UInt32 height) { size_t num_ports = win->getMFPort()->size(); if (num_ports == 0) return; // // calc image dimensions // UInt32 winWidth = win->getWidth(); UInt32 winHeight = win->getHeight(); if (width < winWidth ) width = winWidth; if (height < winHeight) height = winHeight; Real32 a = Real32(winWidth) / Real32(winHeight); width = UInt32(a*height); // // output stream for writing the final image // std::ofstream stream(name, std::ios::binary); if (stream.good() == false) return; // // Setup the FBO // FrameBufferObjectUnrecPtr fbo = FrameBufferObject::create(); // // We use two render buffers. One for the color buffer and one for the depth and // stencil buffer. This example does not take credit of the stencil buffer. There- // fore a depth buffer would suffice. However, the use of the combined depth and // stencil buffer is useful in other contextes and hence used. // RenderBufferUnrecPtr colBuf = RenderBuffer::create(); RenderBufferUnrecPtr dsBuf = RenderBuffer::create(); // // As we would like to read back the FBO color buffer, we must provide a fitting // image. // ImageUnrecPtr buffer_image = Image::create(); buffer_image->set(Image::OSG_RGBA_PF, winWidth, winHeight); colBuf->setImage(buffer_image); // // We must setup the internal image formats of the two render buffers accordingly. // colBuf->setInternalFormat(GL_RGBA); dsBuf ->setInternalFormat(GL_DEPTH24_STENCIL8_EXT); // // we must inform the FBO about the actual used color render buffers. // fbo->editMFDrawBuffers()->push_back(GL_COLOR_ATTACHMENT0_EXT); // // The FBO takes responsibility of the render buffers. Notice, that the shared // depth/stencil buffer is provided twice. As the depth render buffer and as the // stencil render buffer. // fbo->setColorAttachment (colBuf, 0); fbo->setDepthAttachment (dsBuf); fbo->setStencilAttachment(dsBuf); // // Also the FBO must be sized correctly. // fbo->setWidth (winWidth ); fbo->setHeight(winHeight); // // In order to read the color buffer back next two statements are necessary. // fbo->setPostProcessOnDeactivate(true); fbo->getColorAttachments(0)->setReadBack(true); // // We tile the final image and render each tile with the screen resolution // into the FBO. The more tiles we use the bigger the resolution of the // final image gets with respect to a provided measure of length. // typedef boost::tuple<TileCameraDecoratorUnrecPtr, bool, SimpleStageUnrecPtr, ViewportUnrecPtr> TupleT; std::vector<TupleT> decorators; decorators.resize(num_ports); // // Remember the stage viewports for later cleanup // std::stack<ViewportUnrecPtr> stage_viewports; // // Setup the tile camera decorators for each viewport of the window and // disable the tile property of tileable viewport backgrounds. // for (size_t i = 0; i < num_ports; ++i) { Viewport* vp = win->getPort(i); TileCameraDecoratorUnrecPtr decorator = TileCameraDecorator::create(); decorator->setFullSize (width, height); decorator->setDecoratee(vp->getCamera()); vp->setCamera(decorator); bool bTiled = false; TileableBackground* tbg = dynamic_cast<TileableBackground*>(vp->getBackground()); if (tbg) { bTiled = tbg->getTile(); tbg->setTile(false); } // // The scene manager root node does not provide the illumination of the // scene. This is governed internally by the manager. However, to take // credit of the illumination we scan to the final parent of the scene // graph. // Node* internalRoot = rootNode(mgr->getRoot()); // // We would like to render the scene but won't detach it from its parent. // The VisitSubTree allows just that. // VisitSubTreeUnrecPtr visitor = VisitSubTree::create(); visitor->setSubTreeRoot(internalRoot); NodeUnrecPtr visit_node = makeNodeFor(visitor); // // We clone the camera of the first viewport and do not swap the buffer on later // rendering. This way the image generation process is not noticable in the // window. // CameraUnrecPtr camera = dynamic_pointer_cast<Camera>(vp->getCamera()->shallowCopy()); // // The stage object does provide a render target for the frame buffer attachment. // SimpleStage has a camera, a background and the left, right, top, bottom // fields to let you restrict rendering to a sub-rectangle of your FBO, i.e. // they give you a viewport. // SimpleStageUnrecPtr stage = SimpleStage::create(); stage->setRenderTarget(fbo); stage->setCamera (decorator); stage->setBackground (vp->getBackground()); // // Give the stage core a place to live // NodeUnrecPtr stage_node = makeNodeFor(stage); stage_node->addChild(visit_node); // // root // | // +- SimpleStage // | // +- VisitSubTree -> ApplicationScene // NodeUnrecPtr root = makeCoredNode<Group>(); root->addChild(stage_node); // // Give the root node a place to live, i.e. create a passive // viewport and add it to the window. // ViewportUnrecPtr stage_viewport = PassiveViewport::create(); stage_viewport->setRoot (root); stage_viewport->setBackground(vp->getBackground()); stage_viewport->setCamera (camera); win->addPort(stage_viewport); // // remember the decorator, the background tile prop setting and the stage setup // decorators[i] = boost::make_tuple(decorator, bTiled, stage, stage_viewport); } // // We write the image in simple ppm format. This one starts with a description // header which we output once on first write. // bool write_header = true; // // Calc the max y start position (width). We process the tiles from bottom // up and from left tp right as determined by the image format. // UInt32 yPosLast = 0; for (; yPosLast < height-winHeight; yPosLast += winHeight); // // Process from bottom to top // for (Int32 yPos = yPosLast; yPos >= 0; yPos -= winHeight) { UInt32 ySize = std::min(winHeight, height - yPos); // // Collect the tile images for each row, i.e. we write the // image in row manner to disk. This way the main memory is // only moderately stressed. // std::vector<ImageUnrecPtr> vecColImages; // // Process from left to right // for (UInt32 xPos = 0; xPos < width; xPos += winWidth) { UInt32 xSize = std::min(winWidth, width - xPos); // // The current tile image // ImageUnrecPtr col_image = Image::create(); col_image->set(Image::OSG_RGBA_PF, xSize, ySize); // // Adapt the tile camera decorator boxes to the current tile // for (size_t i = 0; i < num_ports; ++i) { // // this tile does not fill the whole FBO - adjust to only render // to a part of it // decorators[i].get<2>()->setLeft (0.f); decorators[i].get<2>()->setRight (xSize / float(winWidth)); decorators[i].get<2>()->setBottom(0.f); decorators[i].get<2>()->setTop (ySize / float(winHeight)); TileCameraDecorator* decorator = decorators[i].get<0>(); decorator->setSize( xPos / float(width), yPos / float(height), (xPos + xSize) / float(width), (yPos + ySize) / float(height) ); } // // render the tile // mgr->update(); win->renderNoFinish(mgr->getRenderAction()); win->frameExit(); win->deactivate (); // // Copy the image into the tile image stored for later processing // if(fbo) { RenderBuffer* grabber = dynamic_cast<RenderBuffer*>(fbo->getColorAttachments(0)); if(grabber) { grabber->getImage()->subImage(0, 0, 0, xSize, ySize, 1, col_image); } } vecColImages.push_back(col_image); } // // Write the image format header once // if (write_header) { write_header = false; if (!writePNMImagesHeader(vecColImages, width, height, stream)) break; } // // Write the current column // if (!writePNMImagesData(vecColImages, stream)) break; // // Forget the current column images // vecColImages.clear(); } // // restore window and cleanup // for (size_t i = 0; i < num_ports; ++i) { win->subPortByObj(decorators[i].get<3>()); Viewport* vp = win->getPort(i); vp->setCamera(decorators[i].get<0>()->getDecoratee()); vp->setSize(0, 0, 1, 1); TileableBackground* tbg = dynamic_cast<TileableBackground*>(vp->getBackground()); if (tbg) tbg->setTile(decorators[i].get<1>()); } }
void VRShadowEngine::doLightPass(Light *pLight, RenderAction *pAction, EngineDataPtr pEngineData) { this->pushPartition(pAction); RenderPartition *pPart = pAction ->getActivePartition(); Viewarea *pArea = pAction ->getViewarea (); Background *pBack = pAction ->getBackground (); FrameBufferObject *pTarget = pEngineData->getRenderTarget(); if(pTarget == NULL) { FrameBufferObjectUnrecPtr pFBO = FrameBufferObject::createLocal(); pFBO->setWidth (this->getWidth ()); pFBO->setHeight(this->getHeight()); pEngineData->setRenderTarget(pFBO); pTarget = pFBO; } TextureObjChunk *pTexChunk = pEngineData->getTexChunk(); TextureBufferUnrecPtr pTexBuffer = pEngineData->getTexBuffer(); if(pTexBuffer == NULL) { pTexBuffer = TextureBuffer::createLocal(); pEngineData->setTexBuffer (pTexBuffer); pTexBuffer->setTexture (pTexChunk ); pTarget ->setDepthAttachment(pTexBuffer); } PolygonChunkUnrecPtr pPoly = pEngineData->getPolyChunk(); if(pPoly == NULL) { pPoly = PolygonChunk::createLocal(); pPoly->setOffsetBias (this->getOffsetBias ()); pPoly->setOffsetFactor(this->getOffsetFactor()); pPoly->setOffsetFill (true ); pEngineData->setPolyChunk(pPoly); } pPart->setRenderTarget(pTarget); if(pArea != NULL) { Camera *pCam = pEngineData->getCamera(); pPart->setWindow (pAction->getWindow()); pPart->calcViewportDimension(0.f, 0.f, 1.f, 1.f, pTarget->getWidth (), pTarget->getHeight ()); Matrix m, t; // set the projection pCam->getProjection (m, pPart->getViewportWidth (), pPart->getViewportHeight()); pCam->getProjectionTranslation(t, pPart->getViewportWidth (), pPart->getViewportHeight()); pPart->setupProjection(m, t); pCam->getViewing(m, pPart->getViewportWidth (), pPart->getViewportHeight()); pPart->setupViewing(m); pPart->setNear (pCam->getNear()); pPart->setFar (pCam->getFar ()); pPart->calcFrustum (); pPart->setBackground(pBack); } Node *pActNode = pAction->getActNode(); pAction->overrideMaterial(_pLightPassMat, pActNode); pAction->pushState(); UInt32 uiPolySlot = pPoly->getClassId(); pAction->addOverride (uiPolySlot, pPoly); // lightRenderEnter(pLight, pAction); pAction->useNodeList(false); this->recurseFrom(pAction, pLight); pAction->popState(); pAction->overrideMaterial(NULL, pActNode); this->popPartition(pAction); }