Jump to content
  • Advertisement
Sign in to follow this  
Endemoniada

Off Center Projection ?

This topic is 1100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

 

Is is possible to make (an unrealistic) projection that appears as if the camera is offset even though it isn't ?

 

The image on the left is a regular projection, the look at point is the black object. I want it to be like the image on the right which I made by moving the look at point to the right (x+ axis) and then using Photoshop to move the cube back to the center.

 

Thanks.

 

offcentre_zpsztjvaad7.jpg

 

 

Share this post


Link to post
Share on other sites
Advertisement
Yup, you just apply translation and scaling transformations after the perspective transform


finalPerspectiveMatrix = Translate(offsetX, 0, 0) * perspectiveMatrix

one thing to note, the view space after the perspective transform is in the range form -1 to 1 in the x direction, so offset would likely be a value in that range. If you need to zoom in after translating you can apply scale transformation after the perspective transform

Share this post


Link to post
Share on other sites

Hi HappyCoder,

 

I don't know what you mean by applying a translation *after* the perspective transform.

 

I pass the WorldViewProjection to the shader and it does the transform:

 

world = DirectX::XMMatrixIdentity();
view = DirectX::XMMatrixLookAtLH(eye, look, up);
projection = DirectX::XMMatrixPerspectiveFovLH(pi / 4.0f, aspect_ratio, near_z, far_z);
 
// for shader
world_view = DirectX::XMMatrixMultiply(world, view);
world_view_projection = DirectX::XMMatrixMultiply(world_view, projection);

 

Thanks for helping.

Share this post


Link to post
Share on other sites

You just need to multiply your projection matrix by a translation matrix

world = DirectX::XMMatrixIdentity();
view = DirectX::XMMatrixLookAtLH(eye, look, up);
projection = DirectX::XMMatrixPerspectiveFovLH(pi / 4.0f, aspect_ratio, near_z, far_z);
// move the projection off center
projectionOffset = DirectX::XMMatrixTranslation(-offsetX, 0.0f, 0.0f);
projection = DirectX::XMMatrixMultiply(projection, projectionOffset);
 
// for shader
world_view = DirectX::XMMatrixMultiply(world, view);
world_view_projection = DirectX::XMMatrixMultiply(world_view, projection);
Since your code appears to be using row vectors, your world matrix is on the right left and projection on the right. When using row vectors, matrices are applied from left to right so any matrices to the right of the projection, in this case projectionOffset, will be applied after the projection.

Share this post


Link to post
Share on other sites
Is is possible to make (an unrealistic) projection that appears as if the camera is offset even though it isn't ?

 

The image on the left is a regular projection, the look at point is the black object. I want it to be like the image on the right which I made by moving the look at point to the right (x+ axis) and then using Photoshop to move the cube back to the center.

 

It's actually *really* easy.

 

Your vertex shader simply outputs normalized screen coordinates (from -1 to 1) multiplied by W.

 

If you want to simulate the same shift in screen coordinates that you would get by shifting in Photoshop, then you take the computed screen coordinates (X/W, Y/W) and add the necessary constant amount to each one.

 

So to sum up If you want to shift from (Xo, Yo) in screen coordinates, you do this (after you've done all the transforms !) :

output.position.x += Xo * output.position.w;

output.position.y += Yo * output.position.w;

 

(Note : z and w won't change obviously).

Edited by LeGreg

Share this post


Link to post
Share on other sites


You just need to multiply your projection matrix by a translation matrix

I would not perform 4x4  multiplication of perspective matrix with 4x4 simple x axis translation because perspective matrix performs the w component construction in 4th row, performing later unlinear operation of w division (you would obviosly alter the w component row).

 

You can write the translation in x and y safely into the perspective matrix, performing the shift of object on screen, but that will not make it projected any different.

 

The solution to see the cube as if the camera was moved to side, is to add the translation to view matrix position column, and then, put the projection space offset to move it back (only on x or y or both, not z).

 

But to calibrate the amount of offset in normalized projection space synchronized with camera translation is more tricky.

 

As I think of it, the projection space offset vector (x,y) can be evaluated as

-original matrix World*View*Proj(objectposition)=(a,b,c) - the projected point of object space (0,0,0) of cube

-altered view matrix with camera translation World*View1*Proj(objectposition)=(d,e,f) - the projected point of object space (0,0,0) of cube with moved camera

 

(x,y)=(a-d,b-e)

and then you just add x and y, or one of them, to fourth column first two components of projection matrix.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!