Friday, October 6, 2017

matrix - How can I calculate the orthographic projection needed to encompass an object with arbitrary rotation?


So I need to set up the dimensions of a orthographic projection that will encompass the entirety of a bounding box when viewed from any arbitrary position.


enter image description here


That's an eye (programmer art); and the vertical line next to it is my view. How can I work out what the size of this view should be so that it encompasses the furthest corners of my rotated rectangle (from the viewports perspective)?


I want to do this in 3D obviously, I thought 2D would explain it better.


So far, I've considered that if I have:



  1. The coordinates of each corner of my bounding box from it's model matrix perspective

  2. With this, I could easily work out what the furthest ones are IF I was able to multiply it with my view matrix. However, it's the view matrix I'm trying to calculate...




Answer



You can solve this by looking at the typical procedure followed when rendering a game world. When you render a 3D object, you typically apply three transformations:





This tranformation is applied to the model itself, disregarding anything else in the scene. It is used to scale, rotate (around its own center axis) and finally translate the object to its desired location. The resulting transformation will move the model to its desired location in the world. Therefore the coordinates are called world coordinates.





You typically want to view your world from a specific position, at a specific angle. The metaphor of a camera is often used for this. Therefore, a view transform is performed, to transform all objects in your game world as they would appear when viewed through the lens of a specific camera. It is interesting to note, that you are actually moving your objects with respect to a camera looking at the origin (0, 0, 0). You are not moving a camera through the world. Looking through a camera which is at position (5, 0, 0) is the equivalent of looking through a camera at position (0, 0, 0), after displacing all objects in your world by (-5, 0, 0). In the same way, you don't rotate your camera, but you rotate the entire world around your camera.






The final step is to apply a projection. The projection specifies how (parallel or converging projectors) and what volume (bounds of the view and/or field of view). Using parallel projectors means that no foreshortening is applied. This means that objects far away from the camera will appear equal in size as compared to nearby objects. An orthographic projection is an example of this. Converging projectors will cause distant objects to appear smaller than nearby ones, creating the illusion of perspective. The first approach is useful in applications where accuracy is key, whereas the latter is useful when realism is desired.


The viewing volume is also specified in this transformation. For example in an orthographic projection along the positive x-axis (after camera rotations have been applied via the view transformation), you would specify this is terms of left (z-min), right (z-max), bottom (y-min), top (y-max), near (x-min) and far (x-max). For a perspective projection, this is specified as a horizontal and vertical field of view along with near and far clipping planes.





From the above, you can see that deciding what volume is viewed, happens in the projection transformation. Depending on how you specified your camera, you can retrieve the bounds by looking at the Camera coordinates. Assuming your camera looks along the positive x-axis, simply use the minimum and maximum y and z values of the bounding box in camera coordinates.


To construct your view matrix, note that it is a concatenation of rotation and translation transforms. You are trying to calculate the transform that would place your camera in the origin, looking along the positive x-axis (can be another axis if you desire so).



  1. First find the translation that would place your camera in the origin. For example, if your camera is located at (5, 10, 15), this transformation would be (-5, -10, -15).

  2. Then find the rotation that would make your camera look at the positive x-axis. For example if the camera is looking 15 degrees to the left, and 25 degrees downward, your transformation would be 15 degrees to the right and 25 degrees upward. Be careful with the order of the rotations here. It depends on how you defined the angles your camera (typically you want to rotate around the z-axis first (= looking up/down) and around the y-axis (up-vector) next (= looking around left and right). To transform your camera to the origin, you need to apply these in reverse.





I may have made some mistakes concerning the terminology of the transformations and the coordinate systems, but this should give you the general idea.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...