Page 1 of 1

Wrong projection parameters when using Custom Projection Matrix

Posted: Tue Aug 06, 2019 1:23 pm
by ChicChic
Hi,

I recently used Ogre::Camera::setCustomProjectionMatrix to add Camera skew to my project by modifying slightly the Projection matrix generated by Ogre.
When I give back that custom matrix to Ogre there is a problem concerning the rendering of my shader based skybox that wasn't there before.
(Ogre 2.1 branch, under linux with, of course, OpenGL 3+ and glsl shader)

After investigation I found that Ogre::Frustum::calcProjectionParameters seems to be wrong when a custom projection matrix is used :

Code: Select all

        if (mCustomProjMatrix)
        {
            // Convert clipspace corners to camera space
            Matrix4 invProj = mProjMatrix.inverse();
            Vector3 topLeft(-1.0f, 1.0f, 0.0f);
            Vector3 bottomRight(1.0f, -1.0f, 0.0f);

            topLeft = invProj * topLeft;
            bottomRight = invProj * bottomRight;

            left = topLeft.x;
            top = topLeft.y;
            right = bottomRight.x;
            bottom = bottomRight.y;

        }
should have been in fact :

Code: Select all

        if (mCustomProjMatrix)
        {
            // Convert clipspace corners to camera space
            Matrix4 invProj = mProjMatrix.inverse();
            Vector3 topLeft(-0.5f, 0.5f, 0.0f);
            Vector3 bottomRight(0.5f, -0.5f, 0.0f);

            topLeft = invProj * topLeft;
            bottomRight = invProj * bottomRight;

            left = topLeft.x;
            top = topLeft.y;
            right = bottomRight.x;
            bottom = bottomRight.y;

        }
Because rendering coordinate goes from -0.5 to 0.5 in width and height to have a length of 1.0 (instead of -1.0 to 1.0).
Thanks to this modification, my skybox was corrected.

To verify it, simply check the result of left, top, right and bottom values for custom projection matrix by calling

Code: Select all

myCamera->setCustomProjectionMatrix(true, myCamera->getProjectionMatrix() );
with and without the custom matrix and see that values are different.

Could someone merge this modification ?

Re: Wrong projection parameters when using Custom Projection Matrix

Posted: Thu Aug 08, 2019 1:20 am
by dark_sylinc
I'm afraid it is your matrix that is wrong, not the code.
Clip space is in range [-1; 1] in x and y axis.
For the Z axis, it is [-1;1] for Ogre's agnostic coordinate system, [1;0] if using reverse Z (Ogre 2.2), and the API-specific are [-1;1] for OpenGL, [0; 1] for the rest of the APIs.

You can fix your matrix before passing it to us though:

Code: Select all

const Matrix4 fix( 2.0f, 0.0f, 0.0f, 0.0f,
                   0.0f, 2.0f, 0.0f, 0.0f,
                   0.0f, 0.0f, 1.0f, 0.0f,
                   0.0f, 0.0f, 0.0f, 1.0f );
newCustomMat = fix * oldCustomMat;
UPDATE:
Mmm... I just noticed we don't divide by w. Does your problem go away if instead you change Ogre's code to do this?:

Code: Select all

// Convert clipspace corners to camera space
Matrix4 invProj = mProjMatrix.inverse();
Vector4 topLeft(-1.0f, 1.0f, 0.0f, 1.0f);
Vector4 bottomRight(1.0f, -1.0f, 0.0f, 1.0f);

topLeft = invProj * topLeft;
bottomRight = invProj * bottomRight;

left = topLeft.x / topLeft.w;
top = topLeft.y / topLeft.w;
right = bottomRight.x / bottomRight.w;
bottom = bottomRight.y / bottomRight.w;
Cheers
Matias

Re: Wrong projection parameters when using Custom Projection Matrix

Posted: Thu Aug 08, 2019 12:59 pm
by ChicChic
Hi dark_sylinc,

I'm sorry to insist but even your new trick doesn't do the job.

Here is my code :

Code: Select all

void Ogre3DCamera::setIntrinsic(const double &fu, const double &fv, const double &u0, const double &v0, const double &alpha)
{
  ogreCamera_->setCustomProjectionMatrix(false);

  const double W = viewport_->width();
  const double H = viewport_->height();
  ogreCamera_->setFrustumOffset(-(u0 - 0.5 * W) / fu,
                                (v0 - 0.5 * H) / fv);
  ogreCamera_->setFOVy(Ogre::Radian(2. * std::atan(0.5 * H / fv)));
  ogreCamera_->setAspectRatio( (fv * W) / (fu * H) );
  Ogre::Matrix4 mat = ogreCamera_->getProjectionMatrix();	// new feature
  mat[0][1] = alpha / W; // new feature
  ogreCamera_->setCustomProjectionMatrix(true, mat); // new feature
}
As you can see, I just add a skew parameter (alpha) to the projection matrix computed by Ogre.

When I disable the "new feature" simply by commenting the 3 last lines and the first : everything is OK
When I enable it, even if the "alpha" value is 0, the values computed by Frustum::calcProjectionParameters provide wrong parameters for the engine.

Even if I remove everything except something like that (as I said in my first post) :

Code: Select all

void Ogre3DCamera::setIntrinsic(const double &fu, const double &fv, const double &u0, const double &v0, const double &alpha)
{
ogreCamera_->setCustomProjectionMatrix(true, ogreCamera_->getProjectionMatrix());
}
the projections parameters are badly computed.

Some numerical values debugged out of Frustum::calcProjectionParameters :
When disabling my new feature (the 3 last lines), printing the values gives (left, right, top, bottom) :

Code: Select all

-0.0405009 0.0405009 0.0227817 -0.0227817
and with just the "ogreCamera_->setCustomProjectionMatrix(true, ogreCamera_->getProjectionMatrix());" trick :

Code: Select all

-0.0810013 0.0810013 0.0455632 -0.0455632
Which is twice the values...
(Your updated code provide the exact same values than the original code)

The problem is that the rest of the scene is rendered exactly as expected with or without the "problem", just the shader skybox is badly rendered with this sort of custom matrix that is not really in fact...
The other possibility, which is a little bit crazy, is that the custom projection computation is right and the "regular" computation (with Frustrum::mCustomProjMatrix = false) is wrong !?!?

PS : the skybox is rendered thanks to a very simple shader with a :

Code: Select all

		pass render_quad
		{
			quad_normals camera_direction
			material SkyPostprocess
		}

Re: Wrong projection parameters when using Custom Projection Matrix

Posted: Thu Aug 08, 2019 3:10 pm
by paroj
actually it was 0.5 before: viewtopic.php?f=25&t=91630&p=533845

Re: Wrong projection parameters when using Custom Projection Matrix

Posted: Thu Aug 08, 2019 3:35 pm
by dark_sylinc
Interesting! Thanks Pavel!

I will look into this

Re: Wrong projection parameters when using Custom Projection Matrix

Posted: Thu Aug 08, 2019 4:16 pm
by dark_sylinc
Fixed! (2.1 branch's commit)

Thanks for the report!

Re: Wrong projection parameters when using Custom Projection Matrix

Posted: Thu Aug 08, 2019 5:02 pm
by ChicChic
Ok, that brand new method is good for me too ! Fixed !!

Glad to have helped (half :wink: )