I'm trying to learn the fundamentals of 3d computer graphics by writing a toy framework in WebGL.

I'm currently working on mouse based camera controls. There are plenty of good resources in the ACM SIGGRAPH library to be found, but there are some aspects of this area that I can't find answers to.

A lot of orbit controls simply use the origin as the pivot point, but other implementations can orbit around any arbitrary point in a scene and the camera stays 'still'. I'm trying to implement the latter.

It seems that a good way to do this would be to simply get the cross product of the two vectors made by the pivot point and 3d coords of the corresponding pixels on the image plane, but I can't anything like this approach anywhere which leads me to believe I've misunderstood some fundamental aspect of image projection/image planes.

Is the image plane an abstract construct that doesn't really have 3d coords or can pixels in the image plane actually be mapped to world coords?

  • $\begingroup$ "other implementations can orbit around any arbitrary point in a scene and the camera stays 'still'." - please elaborate. $\endgroup$ – lightxbulb Sep 8 '19 at 21:52
  • $\begingroup$ Imagine several objects in a scene and all those objects are placed around the origin. The camera looks at the origin (which is in the center of the image plane). Normally, the camera orbits the origin (which is also the pivot point), but by picking a different pivot point, the camera remains looking at the origin, but all the objects rotate around the pivot point. $\endgroup$ – oorst Sep 9 '19 at 0:07
  • $\begingroup$ So you want a collection of objects revolving around some point in space? $\endgroup$ – lightxbulb Sep 9 '19 at 8:01
  • $\begingroup$ Amongst other things, yes. But I want to experiment with this stuff to help me understand. I guess what I'm really after is a way to determine the vector from a pixel on the image plane to any point in the scene and to know if that's even possible. $\endgroup$ – oorst Sep 9 '19 at 22:07
  • $\begingroup$ A collection of objects rotating around another object, can be done through hierarchical transformations. In your case T_pivot * R_pivot * T_model * pos. Transforming screen-space coords to world coords is an entirely different problem. There are many articles on this, here's one that includes it: mynameismjp.wordpress.com/2010/09/05/position-from-depth-3 $\endgroup$ – lightxbulb Sep 11 '19 at 8:29

What you are asking about is 'mouse picking'. If a user clicks a pixel on the screen how can you get corresponding world space coordinates of an object being clicked on. Imagine a ray from that pixel, through the view volume all the way to the far plane. That ray is a set of all possible points the user is clicking on. So we want to perform ray/poly ntersections with every object in the scene, and return the first intersect.

To transform the pixel into world space, you first have to turn your x,y point in screen space into NDC coordinates (-1 to 1) use -1 for your Z component and 1 for W to create a 4 float component vector and then multiply that vector by the inverse projection and then inverse view matrices. There are many examples of this process if you want to see code, but this is the basic process. Now we have a ray from the camera/eye position into the scene, we want to do intersection tests with our objects. There are many ways of achieving this, take your pick.

Hope this helps, good luck!

| improve this answer | |
  • $\begingroup$ Aha! That's exactly what I needed to hear. I had a feeling the -1 z plane of the NDC cube would be the screen, but I couldn't confirm it. Thanks $\endgroup$ – oorst Sep 13 '19 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.