Wrong projection parameters when using Custom Projection Matrix

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

Wrong projection parameters when using Custom Projection Matrix

Post 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 ?
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post 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
ChicChic
Kobold
Posts: 26
Joined: Tue Sep 05, 2017 10:19 am
x 7

Re: Wrong projection parameters when using Custom Projection Matrix

Post 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
		}
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by paroj »

actually it was 0.5 before: viewtopic.php?f=25&t=91630&p=533845
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by dark_sylinc »

Interesting! Thanks Pavel!

I will look into this
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by dark_sylinc »

Fixed! (2.1 branch's commit)

Thanks for the report!
ChicChic
Kobold
Posts: 26
Joined: Tue Sep 05, 2017 10:19 am
x 7

Re: Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic »

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

Glad to have helped (half :wink: )
ChicChic
Kobold
Posts: 26
Joined: Tue Sep 05, 2017 10:19 am
x 7

Re: Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic »

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 ????
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Wrong projection parameters when using Custom Projection Matrix

Post by dark_sylinc »

I'm afraid I'm clueless.

I tried to look for a possible cause when you posted, but didn't see anything that stood out.

The closest suspect I can see is that perhaps PSSMShadowCameraSetup::getShadowCamera uses a camera without custom projection applied?

Our vertex shader code performs:

Code: Select all

@property( hlms_pssm_splits )outVs.depth = outVs_Position.w;@end
and thus outVs.depth will be used by the pixel shader to select the split. Which is what's malfunctioning.

I can only guess your that your custom proj matrix is breaking our assumption that outVs_Position.w (i.e. the .w component after transforming position by the worldViewProj matrix) contains the perpendicular distance from camera to the object being rendered, thus a different method may be needed to calculate the split location.

Without knowing what your custom projection matrix is doing, I have no way to tell.
ChicChic
Kobold
Posts: 26
Joined: Tue Sep 05, 2017 10:19 am
x 7

Re: Wrong projection parameters when using Custom Projection Matrix

Post by ChicChic »

Hi dark_sylinc,

First thank you for having a look at that problem, I had the same idea about the shadow camera parameters but I was not able to go back to the problem...

As I told you in precedent post, The actual test only take the builtin Ogre projection parameters as a custom projection parameters, so it basically does :

Code: Select all

ogreCamera_->setCustomProjectionMatrix(true, ogreCamera_->getProjectionMatrix() )
Without any modifications, nothing more. And the camera's settings are set with the default camera parameters, so I don't think that I made any crazy code...

Alright, I will continue to have a look by myself for any problematic code in Ogre as I honestly don't think that the problem comes from me, but who knows... I will disable my new feature for now because I prefer to have good shadows than having that feature which is not used a lot for now in our product.

I will let you know if I found anything : patch or just a hint.

Thank you
Post Reply