Location>code7788 >text

OSG Development Notes (XXXIII): Multi-view Simultaneous Observation of Objects from Different Angles from Camera Technique

Popularity:804 ℃/2024-11-21 10:40:04

preamble

  The previous camera hud can display graphics separately, continue to delve into the camera hud, the technology is the subview, the direct technology to realize the function is from the camera technology.
  This description osg from camera technology

 

Demo

  请添加图片描述

  请添加图片描述

 

Key calls to the camera viewport

Whether to clear the color depth cache (clear)

pCamera->setClearMask(GL_DEPTH_BUFFER_BIT);

  If the color cache is not cleared, the rendered window will display the content rendered by other windows to the current window if there is no content in the rendered window.

Setting the rendering order (render last)

// Set the POST rendering order (render last)
pCamera->setRenderOrder(osg::Camera::POST_RENDER);

  Post-rendering has a higher priority (displayed last, display has the highest priority).

Set whether the event is accepted (not accepted)

// Set it to not receive events, so it never gets focus.
pCamera->setAllowEventFocus(false);

Setting the viewport size

// The viewport is the area of the engine in 3D, but note the difference in coordinate system from the screen (the screen is upper left at 0,0, while the 3D engine is lower left at 0,0)
pSlaveFrontCamera->setViewport(0,
                             0,
                             rect().width() / 4,
                             rect().height() / 4);
 

Setting up the slave camera

Step 1: New Camera

osg::ref_ptr<osg::Camera> pSlaveFrontCamera = new osg::Camera;

Step 2: Setting the Context

pSlaveFrontCamera->setGraphicsContext(_pViewer->getWindow());

Step 3: Setting the view area

// The viewport is the area of the engine in 3D, but note the difference in coordinate system from the screen (the screen is upper left at 0,0, while the 3D engine is lower left at 0,0)
pSlaveFrontCamera->setViewport(0,
                             0,
                             rect().width() / 4,
                             rect().height() / 4);

Step 4: Setting the Rendering Order

pSlaveFrontCamera->setRenderOrder(osg::Camera::POST_RENDER);

Step 5: Key Steps to Add from the Camera

  The second parameter is the scaling matrix and the third parameter is the rotation matrix

_pViewer->addSlave(pSlaveFrontCamera,
                  osg::Matrix(),
                  osg::Matrix::rotate(osg::DegreesToRadians(0.0), 0.0, 0.0, 0.0),
                  true);
 

Demo key source code

osg::ref_ptr<osg::Node> OsgWidget::getMulViewCameraNode()
{
    // Hide the global button panel of the entire demo (those that don't use the buttons are directly hidden and don't affect the demo).
{
        ui->groupBox_pannel->setVisible(false);
        ui->label_cursor->setVisible(false);
        ui->label_cursor_2->setVisible(false);
        ui->label_msg->setVisible(false);
        ui->label_state->setVisible(false);
    }

    osg::ref_ptr<osg::Group> pGroup = new osg::Group;
    // Drawing boxes (cubes, rectangles)
{
        osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
        // Create a class osg::TessellationHints that specifies the fineness and set the corresponding fineness.
        osg::ref_ptr<osg::TessellationHints> pHints = new osg::TessellationHints;
        pHints->setDetailRatio(0.5);
        // Draw geometry types (geometries)
        qreal width= 5.0f;
        // Function 1
        pGeode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 0, 0), width), pHints));
#if 1
        // Set the light off: OFF, while the rotation is visible (light off, normal vectors don't work)
{
            osg::StateSet *pStateSet = pGeode->getOrCreateStateSet();
            pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
//            pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
        }
#endif
        pGroup->addChild(pGeode);
    }
    // Create a multi-viewport camera
{
#if 0
        // Here we use the one already created by our own window, which is scrapped, but kept, the basic core idea is the same.
        osg::ref_ptr<osg::GraphicsContext::WindowingSystemInterface> pWindowingSystemInterface
                = osg::GraphicsContext::getWindowingSystemInterface();
        if(!pWindowingSystemInterface.get())
        {
            LOG << "if(!())";
            return pGroup.get();
        }
        unsigned int width = 0;
        unsigned int height = 0;
        pWindowingSystemInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0),
                                                       width,
                                                       height);
        osg::ref_ptr<osg::GraphicsContext::Traits> pTraits = new osg::GraphicsContext::Traits;
        {
            pTraits->x = 0;
            pTraits->y = 0;
            pTraits->width = width;
            pTraits->height = height;
            pTraits->windowDecoration = false;
            pTraits->doubleBuffer = true;
            pTraits