Jump to content

VEX :: Screen space to world space in SOPs


Kino

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

Link to comment
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);
Link to comment
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. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...