Jump to content
Kino

VEX :: Screen space to world space in SOPs

Recommended Posts

How can I get the word position of a point I'm seeing through a camera and on the camera's screen plane. Let's say the camera resolution is 1280x720 pixels ( 0,0 at top left and 1280,720 at bottom right), I want to know the world position of a point which will be at the pixel position of 300,200 at render output. Will this differ if I have Orthographic or Perspective camera? I think fromNDC / ptransform functions are the saviors here but I can't quite figure out how exactly.

In general I'd appreciate if anyone can explain the world space, object space and camera space in Houdini in comparison which conventions that OpenGL has for instance. I read here and there that in H world space is actually camera space (or is it only in Mantra and not SOPs?). Also in OpenGL NDC is [-1,1] in all three dimensions, how is this in H?

Cheers

Share this post


Link to post
Share on other sites

Use fromNDC(camera, position). Position consist of x and y in 0..1 range and z as depth in units.

 

Tests for Detail wrangle:

// Create point at the center of the camera view in 3 meters away.
addpoint(0, fromNDC("/obj/cam1", set(0.5, 0.5, -3)));
// Camera frustum. Set reasonable clipping distances like 0.2 to 10.
string cam = "/obj/cam1";
float near = -chf("/obj/cam1/near");
float far = -chf("/obj/cam1/far");

addpoint(0, fromNDC(cam, set(0.0, 0.0, near)));
addpoint(0, fromNDC(cam, set(1.0, 0.0, near)));
addpoint(0, fromNDC(cam, set(1.0, 1.0, near)));
addpoint(0, fromNDC(cam, set(0.0, 1.0, near)));
addpoint(0, fromNDC(cam, set(0.0, 0.0, far)));
addpoint(0, fromNDC(cam, set(1.0, 0.0, far)));
addpoint(0, fromNDC(cam, set(1.0, 1.0, far)));
addpoint(0, fromNDC(cam, set(0.0, 1.0, far)));

// Create quadrangle using points.
void quad(int pt1, pt2, pt3, pt4)
{
    int geo = geoself();
    int num = addprim(geo, "poly");
    addvertex(geo, num, pt1);
    addvertex(geo, num, pt2);
    addvertex(geo, num, pt3);
    addvertex(geo, num, pt4);
}
quad(3, 2, 1, 0);
quad(4, 5, 6, 7);
quad(1, 5, 4, 0);
quad(0, 4, 7, 3);
quad(3, 7, 6, 2);
quad(2, 6, 5, 1);

Share this post


Link to post
Share on other sites

Thanks a lot for the suggestion Shinjipierre but it really didn't work out for me. The use of the hi-rez grid and using its points' uv seemed to do the job in some cases but it's not a generic solution and it's an overkill IMO. What f1480187 suggested was more what I was after.

Thanks again f1480187 for helping me out, your solution did the job beautifully. 

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

×