Search code examples
mathopengl-esperspective

Ray trigonometry in Opengl


I am quite new to this, and iv'e heard that i need to get my inversed projection matrix and so on to create a ray from a 2D point to a 3D world point, however since im using OpenglES and there are not as many methods as there would be regulary to help me with this. (And i simply don't know how to do it) im using a trigenomeric formula for this insted.

For each time i iterate one step down the negative Z-axis i multiply the Y-position on the screen (-1 to 1) with

(-z / (cot(myAngle / 2))

And the X position likewise but with a koefficent equally to the aspect ratio.
myAngle is the frustum perspective angle.

This works really good for me and i get very accurate values, so what i wonder is: Why should i use the inverse of the projection matrix and multiply it with some stuff instead of using this?


Solution

  • Most of the time you have a matrix lying around for your OpenGl camera. Using an inverse matrix is simple when you already have a camera matrix on hand. It is also (oh so very slightly at computer speeds) faster to do a matrix multiply. And in cases where you are doing a bajillion of these calculations per frame, it can matter.

    Here is some good info on getting started on a camera class if you are interested: Camera Class

    And some matrix resources

    Depending on what you are working on, I wouldn't worry too much about the 'best way to do it.' You just want to make sure you understand what your code is doing then keep improving it.