I'm trying to get a 2D screen position so I unproject a point from 3D into 2D screen coordinates by that following function:
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;
}
Now I want to convert that screenPosition into Image space to crop an image:
Core::Resolution screenRes = Core::EnvironmentInformation::getSingleton()->getScreenResolution();
float screenPositionX = (screenPosition.x / screenRes.width) * (pixelBuffer->getWidth());
float screenPositionY = (screenPosition.y / screenRes.height) * (pixelBuffer->getHeight());
When I click on the mobile screen on the Green LED I get that cropped image.. but what I want is to only crop that green led.. Is there a problem with the transformation ?
const Core::Resolution cameraRes = Core::EnvironmentInformation::getSingleton()->getCameraResolutionUpLoad( plane );
Ogre::HardwarePixelBufferSharedPtr pixelBuffer = mTexture[plane]->getBuffer( 0, 0 );
viewarassert( screenPosition.x < cameraRes.width || screenPosition.x >= 0 || screenPosition.y < cameraRes.height || screenPosition.y >= 0 );
Core::Resolution screenRes = Core::EnvironmentInformation::getSingleton()->getScreenResolution();
std::pair< Ogre::Vector2, Ogre::Vector2 > uvBounds = calculateSingleViewTextureCoordinates();
float screenPositionX = cameraRes.height - (screenPosition.x / screenRes.width) * ((int)pixelBuffer->getHeight());
float screenPositionY= cameraRes.width - (screenPosition.y / screenRes.height) * ((int)pixelBuffer->getWidth());
int min_x = std::max( 0,(int)screenPositionX - radius);
int min_y = std::max( 0,(int)screenPositionY - radius);
int max_x = std::min( (int)cameraRes.width, (int) screenPositionX + radius );
int max_y = std::min( (int)cameraRes.height, (int) screenPositionY+ 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());
img = img.flipAroundY();
Ogre::Image *out = cropImage(img, min_x, min_y, max_x, max_y);