Wrong projection parameters when using Custom Projection Matrix

Minor issues with the Ogre API that can be trivial to fix
Post Reply
ChicChic
Gnoblar
Posts: 16
Joined: Tue Sep 05, 2017 10:19 am
x 1

Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic » Tue Aug 06, 2019 1:23 pm

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 ?
0 x

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4066
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 228
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by dark_sylinc » Thu Aug 08, 2019 1:20 am

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
0 x

ChicChic
Gnoblar
Posts: 16
Joined: Tue Sep 05, 2017 10:19 am
x 1

Re: Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic » Thu Aug 08, 2019 12:59 pm

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
		}
0 x

paroj
OGRE Team Member
OGRE Team Member
Posts: 851
Joined: Sun Mar 30, 2014 2:51 pm
x 165
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by paroj » Thu Aug 08, 2019 3:10 pm

actually it was 0.5 before: viewtopic.php?f=25&t=91630&p=533845
1 x

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4066
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 228
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by dark_sylinc » Thu Aug 08, 2019 3:35 pm

Interesting! Thanks Pavel!

I will look into this
0 x

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4066
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 228
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by dark_sylinc » Thu Aug 08, 2019 4:16 pm

Fixed! (2.1 branch's commit)

Thanks for the report!
0 x

ChicChic
Gnoblar
Posts: 16
Joined: Tue Sep 05, 2017 10:19 am
x 1

Re: Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic » Thu Aug 08, 2019 5:02 pm

Ok, that brand new method is good for me too ! Fixed !!

Glad to have helped (half :wink: )
1 x

ChicChic
Gnoblar
Posts: 16
Joined: Tue Sep 05, 2017 10:19 am
x 1

Re: Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic » Tue Sep 10, 2019 4:26 pm

I'm reopening this subject because I have just found that the shadows are also affected by the use of custom projection matrix.

With the same procedure above, if I use the custom projection matrix my shadows are getting fade between two splits of the PSSM

Image

As you can see the shadow of the tree is splitted in 2 with a fade appearing.
Whereas when I don't use the custom projection matrix feature (but still with the same parameters)

Image

Shadows are correct...

I've searched the Ogre's source code for the problem but I haven't found anything yet, any suggestions someone ????
0 x

Post Reply