Perspective Projections

Recommended Posts

Hi all,

Does anyone know how to build a matrix that will project the points of an object onto the viewing plane of a camera or light. I know how to transform the points from world space to the camera space but then how do you do the perspective projection onto the viewing plane using the FOV and the image res of the camera or light?

I've found various descriptions of how to do this but when I try to impliment them they don't work with Houdini's camera FOV's and image res.

I did find one formula that seemed to work but it wasn't a matrix, and so didn't allow me to transform z too, which is actually what I need to really do.

Ultimately what I want to do is build a cop that lets you input 2 images with Pz info, one from the point of view of a camera and one from a light and then by specifying where the light was it will add shadows to the image.

Of course if there is a cop that I don't know about that already does this then that would be handy too

Any help gratefully recieved.

Oh I should add that I can't do this with any built in space change functions. Firstly because you can't use them in cops and secondly because this is just a test for something that will eventually be done outside of Houdini, I just trying to figure out the maths for it.

Share on other sites

This stuff is covered in most introductory computer graphics programming material. Lots of links if you look up "perspective projection matrix".

For example, the lecture notes here:

http://www.student.cs.uwaterloo.ca/~cs488/

EDIT: Try bottom of page 60 if you download the lecture notes from above.

Share on other sites

Does this help? Look at the SOP method.

Velocity Rendering Tutorial

Share on other sites
Does this help? Look at the SOP method.

Velocity Rendering Tutorial

17265[/snapback]

Hi Jason, unfortunately this was exactly what I was trying to avoid, it uses those lovely built in space change functions, I want to know the math behind the functions.

Edward, that link looks like exactly what I want, I try it out later, sometimes it seems to be hard to track down the basics. All my CG books describe the process but none of them are very precise about what is going on. For some reason my google search completely failed to turn up anything like this. All I got was a bunch of papers on doing perspective shadow mapping or openGL implimentations without the explaination. That link looks really handy, ta.

Share on other sites

Ok you clever people now I'm really stuck.

Edwards link was great and when I implimented the code all looked good. However I had to use the tangent and not the cotangent to get sensible results.

BUT when I compare it with doing the same projection with the UVtexture sop I found they didn't match.

Digging a little deeper I find that according to the notes in Edwards link the Houdini projection is the incorrect way to do things. Take a look at the attached and you will see that the UVtexture sop distorts lines into curves whereas the method in the link does not.

This raises two questions.

1. Is the Houdini UVtexture sop wrong or just different, and does this have any implications else where. Does it actually matter? Surely though people use this method of textureing for matching to live action plates so surely the UVtexture sop must work?

2. I still don't know how to match the way Houdini does it, in the attached example I've had to fudge it by manually altering the FOV value so that it no longer matches the light that it's linked to.

If anyone has any answers to either of these questions I'd really appreciate it, in the meantime I'll keep digging.

test1000.zip

Share on other sites
<snip>

...smart people stuff...

<snip>

On a side note the toNDC vex function produces the same results as the UVTexture SOP.

```sop
ndcThingy(string cam = "")
{
P = toNDC(cam,P);
}```

Share on other sites
2. I still don't know how to match the way Houdini does it, in the attached example I've had to fudge it by manually altering the FOV value so that it no longer matches the light that it's linked to.

I took a quick peek at your vex setup and have one question...

The aperture of the light/camera also effects the view frustum but I don't see it being taken into account anywhere?

Share on other sites
I took a quick peek at your vex setup and have one question...

The aperture of the light/camera also effects the view frustum but I don't see it being taken into account anywhere?

17307[/snapback]

That's a good point, I hadn't even begun to think about that, all the notes I've read so far only talk about the FOV. Changing aperture just seems to create a linear scale, if this is true it shouldn't be too hard to incorporate.

By the way I've uploaded a new version as I made some errors in the original that I've already fixed. I'll totally confused at the moment and it's late so I'm off to bed. With any luck by the morning Edward and Mario will have sorted this mess out for me .....

One thing I'm thinking is that if you don't care about Z, and for texture mapping you mostly don't then the whole distortion thing doesn't matter it's only introduced by looking at the complete projection.

All I really want to know is what transform do Sesi use, I have most of it now but I'm still having to fudge the FOV value so something is definately missing.

Share on other sites

I don't really have time to delve into this but it seems like you're treating the focal length as the angle of view when it's not. See the bottom of this page:

TIP: If you combine the two sources of info, the atan/cot functions cancel out.

Share on other sites

Doh! That must be it. Thank you Edward, I'm not really up on camera descriptions, sure am learning a lot now. ta

Share on other sites

That did the trick! ta Ed.

Share on other sites

See? Nothing magical after all. It's all just triangles.

Share on other sites

In case anyone was wondering what all this was about. Here is the result so far. It's a cop that lets you add depth map shadows in post.

It's WIP - I still need to add a softness control.

If anyone knows what the best way to do this is please let me know. I'm still researching it and there seems to be an a lot of different approaches.

To see what it's doing go to the cop editor and bypass any of the file sops that are called light1* you should see the shadow for that object magically dissappear.

Depth_composite.zip

Share on other sites

That's cool. All you're missing now is an option to output an alpha channel instead you that we can blur it for soft shadows. :thumbsup:

Could you also use autogen shadow maps? I wonder what space those maps are in. I guess you could always just look at the IFD.

Share on other sites
That's cool. All you're missing now is an option to output an alpha channel instead you that we can blur it for soft shadows. :thumbsup:

Could you also use autogen shadow maps? I wonder what space those maps are in. I guess you could always just look at the IFD.

17445[/snapback]

I've added soft shadows too now and alpha channel. see attached

I think it will work with autogen shadow maps - they just store the depth like Pz deep rasters do.

Share on other sites

Are you multi-sampling for the soft shadows? I guess that's more accurate than blurring.

Share on other sites

Indeed the quality parameter is specifying number of samples, although I do it like a convolution filter where the dimension of the filter is actually (quality + 1) x (quality + 1)

This gives better results although slower because it actually jitter the points around in the light space.

If you just want to blur the hard edge result set the quality and the softness to 0 and then blur the alpha channel and use it in a composite.

Share on other sites

Ooops, I didn't check the alpha channel. .... Hmm .... it might more useful to output it as a mask (M) channel instead while retaining original alpha.

Share on other sites

I was just following your suggestion Easily changed though.

Actually not so easily changed. You can't create planes in Vex (unless I'm missing something) so I'm stuck with the alpha solution.

Edited by sibarrick

Share on other sites
Actually not so easily changed. You can't create planes in Vex (unless I'm missing something) so I'm stuck with the alpha solution.

17473[/snapback]

You can try connecting your image to a color COP, which should add an M channel.

then you can rename it with the rename COP

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account