将屏幕位置转换为图像空间

时间:2017-12-17 18:04:08

标签: c++ opengl graphics ogre

我试图获得2D屏幕位置,因此我通过以下功能将3D点投影到2D屏幕坐标:

Ogre::Vector2 SpaceTransformHelper::convertWorldToScreenPosition( const Ogre::Vector3& worldPosition )
{
    Stages::AStage* currStage = Stages::StageManager::getSingleton()->getActiveStage();
    Core::Resolution screenRes = Core::EnvironmentInformation::getSingleton()->getScreenResolution();

    // Get worldviewprojection from camera.
    Ogre::Matrix4 worldViewProjection = currStage->getActiveCamera()->getCameraViewProjectionMatrix();

    // Transform world coordinates.
    Ogre::Vector3 transformedWorldPosition = worldViewProjection * worldPosition;

    // Calculate screen coordinates from world coordinates.
    Ogre::Vector2 screenPosition;
    screenPosition.x = (int) ( ( ( transformedWorldPosition.x + 1 ) / 2.0 ) * screenRes.width + 0.5 );
    screenPosition.y = (int) ( ( ( 1 - transformedWorldPosition.y ) / 2.0 ) * screenRes.height + 0.5 );

    return screenPosition;
}

现在我想将screenPosition转换为Image空间来裁剪图像:

Core::Resolution screenRes = Core::EnvironmentInformation::getSingleton()->getScreenResolution();
float screenPositionX = (screenPosition.x / screenRes.width) * (pixelBuffer->getWidth());
float screenPositionY =  (screenPosition.y / screenRes.height) * (pixelBuffer->getHeight());

enter image description here

当我点击绿色LED上的移动屏幕时,我得到了裁剪的图像..但我想要的只是裁剪那个绿色的led ..变换有问题吗?

   float screenPositionX = (screenPosition.x / screenRes.width) * ((int)pixelBuffer->getWidth());
        float screenPositionY= (screenPosition.y / screenRes.height) * ((int)pixelBuffer->getHeight());

       int  min_x = std::max( 0,(int)screenPosition.x - radius);
       int  min_y = std::max( 0,(int)screenPosition.y - radius);
       int  max_x = std::min( (int)cameraRes.width, (int) screenPosition.x + radius );
       int  max_y = std::min( (int)cameraRes.height, (int) screenPosition.y+ radius );

         // Get the pixelbox within the radius.
        pixelBuffer->lock(Ogre::HardwareBuffer::HBL_READ_ONLY);
        const Ogre::PixelBox &readrefpb = pixelBuffer->getCurrentLock();

        unsigned char *readrefdata = static_cast<unsigned char*>(readrefpb.data);

        Ogre::Image img;
        img = img.loadDynamicImage (readrefdata, pixelBuffer->getWidth(),
                                    pixelBuffer->getHeight(), pixelBuffer->getFormat());

        Ogre::Image *out = cropImage(img,  min_x, min_y,  max_x,  max_y);

0 个答案:

没有答案