2

I'm working with openGL but this is basically a math question. I'm trying to calculate the projection matrix, I have a point on the view plane R(x,y,z) and the Normal vector of that plane N(n1,n2,n3). I also know that the eye is at (0,0,0) which I guess in technical terms its the Perspective Reference Point. How can I arrive the perspective projection from this data? I know how to do it the regular way where you get the FOV, aspect ration and near and far planes.

Amit Ofer
  • 413
  • 1
  • 7
  • 26
  • 1
    That's not enough information. Imagine, for argument's sake, that your computer screen is the plane, `R` is the centre of your monitor, your real eye is at `(0, 0, 0)` and you know the normal at `R`. How big should the window be? In effect, what should the field of view be? – Tommy May 21 '15 at 14:16
  • I don't need to create an openGL perspective matrix with it. Basically its a math problem we got as an assignment in computer graphics course and I'm not sure how to approach it, the questions says: "Write the matrix that represents a perspective projection on a plane that goes through the point R(x,y,z) and has a normal vector N(n1,n2,n3). Assume that the point of view is (0,0,0)." I know that it might sound like something is missing but I would have knew it if it had a mistake. – Amit Ofer May 21 '15 at 14:26
  • 2
    I maintain that the question omits necessary information. You need to establish a vector basis at `R`. So e.g. which of the infinitely many tangent vectors to the plane represents 'up'? You can start with `N` being your z vector but then, regardless of maths, what information do you have to determine x and y? – Tommy May 21 '15 at 14:48
  • Check [this explanation](http://maverick.inria.fr/~Xavier.Decoret/resources/maths/plane-projection.pdf). – Nico Schertler May 21 '15 at 16:50
  • I haven't read it all but from the beginning of the document it seems exactly like what I was looking for, thanks! – Amit Ofer May 21 '15 at 17:34
  • 1
    see [Understanding Transform matrices](http://stackoverflow.com/a/28084380/2521214) , You need to construct the camera matrix not projection!!! Set projection matrix only with perspective matrix (set znear with min focusable distance, zfar with most far seeable distance and angle with 60 degrees, aspect is done from screen resolutions ...) the camera position and orientation is multiplied to Modelview not Projection !!! and as Inverse matrix ... – Spektre May 22 '15 at 07:41
  • Also the camera position is sometimes translated by Z near backwards prior to use to achieve the effect of `znear=0` ... – Spektre May 22 '15 at 07:44
  • I understand matrix transformation and what the camera matrix is, but its an assignment we got and it clearly says "write the matrix that represents the perspective projection..." – Amit Ofer May 22 '15 at 07:57
  • I've read the explanation that Nico put here, I still don't understand what I can do with the information I got in this question. Can someone try to help? if you think its a mistake and I'm supposed to create the camera matrix, what would it be? – Amit Ofer May 23 '15 at 22:38
  • @AmitOfer added answer make sure you read the linked page at the bottom. when you are commenting someone add `@nick` to star of comment so (s)he gets notified – Spektre May 24 '15 at 10:39
  • What you ask is in this class book _[Computer Graphics: Principles and Practice](http://www.amazon.com/Computer-Graphics-Principles-Practice-2nd/dp/0201848406/ref=sr_1_2?ie=UTF8&qid=1432471350&sr=8-2&keywords=computer+graphics)_ –  May 24 '15 at 12:43
  • Where? I think I have this book – Amit Ofer May 24 '15 at 13:10

2 Answers2

8

I think you created a bit of confusion by putting this question under the "opengl" tag. The problem is that in computer graphics, the term projection is not understood in a strictly mathematical sense.

In maths, a projection is defined (and the following is not the exact mathematical definiton, but just my own paraphrasing) as something which doesn't further change the results when applied twice. Think about it. When you project a point in 3d space to a 2d plane (which is still in that 3d space), each point's projection will end up on that plane. But points which already are on this plane aren't moving at all any more, so you can apply this as many times as you want without changing the outcome any further.

The classic "projection" matrices in computer graphics don't do this. They transfrom the space in a way that a general frustum is mapped to a cube (or cuboid). For that, you basically need all the parameters to describe the frustum, which typically is aspect ratio, field of view angle, and distances to near and far plane, as well as the projection direction and the center point (the latter two are typically implicitely defined by convention). For the general case, there are also the horizontal and vertical asymmetries components (think of it like "lens shift" with projectors). And all of that is what the typical projection matrix in computer graphics represents.

To construct such a matrix from the paramters you have given is not really possible, because you are lacking lots of parameters. Also - and I think this is kind of revealing - you have given a view plane. But the projection matrices discussed so far do not define a view plane - any plane parallel to the near or far plane and in front of the camera can be imagined as the viewing plane (behind the camere would also work, but the image would be mirrored), if you should need one. But in the strict sense, it would only be a "view plane" if all of the projected points would also end up on that plane - which the computer graphics perspective matrix explicitely does'nt do. It instead keeps their 3d distance information - which also means that the operation is invertible, while a classical mathematical projection typically isn't.

From all of that, I simply guess that what you are looking for is a perspective projection from 3D space onto a 2D plane, as opposed to a perspective transformation used for computer graphics. And all parameters you need for that are just the view point and a plane. Note that this is exactly what you have givent: The projection center shall be the origin and R and N define the plane.

Such a projection can also be expressed in terms of a 4x4 homogenous matrix. There is one thing that is not defined in your question: the orientation of the normal. I'm assuming standard maths convention again and assume that the view plane is defined as <N,x> + d = 0. From using R in that equation, we can get d = -N_x*R_x - N_y*R_y - N_z*R_z. So the projection matrix is just

(   1       0       0     0   )
(   0       1       0     0   )
(   0       0       1     0   )
(-N_x/d  -N_y/d  -N_z/d   0   )

There are a few properties of this matrix. There is a zero column, so it is not invertible. Also note that for every point (s*x, s*y, s*z, 1) you apply this to, the result (after division by resulting w, of course) is just the same no matter what s is - so every point on a line between the origin and (x,y,z) will result in the same projected point - which is what a perspective projection is supposed to do. And finally note that w=(N_x*x + N_y*y + N_z*z)/-d, so for every point fulfilling the above plane equation, w= -d/-d = 1 will result. In combination with the identity transform for the other dimensions, which just means that such a point is unchanged.

derhass
  • 43,833
  • 2
  • 57
  • 78
2
  1. Projection matrix must be at (0,0,0) and viewing in Z+ or Z- direction

    this is a must because many things in OpenGL depends on it like FOG,lighting ... So if your direction or position is different then you need to move this to camera matrix. Let assume your focal point is (0,0,0) as you stated and the normal vector is (0,0,+/-1)

  2. Z near

    is the distance between focal point and projection plane so znear is perpendicular distance of plane and (0,0,0). If assumption is correct then

    znear=R.z
    

    otherwise you need to compute that. I think you got everything you need for it

    • cast line from R with direction N
    • find closest point to focal point (0,0,0)
    • and then the z near is the distance of that point to R
  3. Z far

    is determined by the depth buffer bit width and z near

    zfar=znear*(1<<(cDepthBits-1))
    

    this is the maximal usable zfar (for mine purposes) if you need more precision then lower it a bit do not forget precision is higher near znear and much much worse near zfar. The zfar is usually set to the max view distance and znear computed from it or set to min focus range.

  4. view angle

    I use mostly 60 degree view. zang=60.0 [deg]

    Common males in my region can see up to 90 degrees but that is peripherial view included the 60 degree view is more comfortable to view.

    Females have a bit wider view ... but I did not heard any complains from them on 60 degree views ever so let assume its comfortable for them too...

  5. Aspect

    aspect ratio is determined by your OpenGL window dimensions xs,ys

    aspect=(xs/ys)
    

This is how I set the projection matrix:

glMatrixMode(GL_PROJECTION);   
glLoadIdentity();
gluPerspective(zang/aspect,aspect,znear,zfar);
 // gluPerspective has inacurate tangens so correct perspective matrix like this:
double perspective[16];
glGetDoublev(GL_PROJECTION_MATRIX,perspective);
perspective[ 0]=   1.0/tan(0.5*zang*deg);
perspective[ 5]=aspect/tan(0.5*zang*deg);
glLoadMatrixd(perspective);


deg = M_PI/180.0
perspective is projection matrix copy I use it for mouse position conversions etc ...

If you do not correct the matrix then you will be off when using advanced things like overlapping more frustrum to get high precision depth range. I use this to obtain <0.1m,1000AU> frustrum with 24bit depth buffer and the inaccuracy would cause the images will not fit perfectly ...

[Notes]

if the focal point is not really (0,0,0) or you are not viewing in Z axis (like you do not have camera matrix but instead use projection matrix for that) then on basic scenes/techniques you will see no problem. They starts with use of advanced graphics. If you use GLSL then you can handle this without problems but fixed OpenGL function can not handle this properly. This is also called PROJECTION_MATRIX abuse

[edit1] few links

If your view is standard frustrum then write the matrix your self gluPerspective otherwise look here Projections for some ideas how to construct it

[edit2]

From your comment I see it like this:

overview


f is your viewing point (axises are the global world axises)
f' is viewing point if R would be the center of screen

so create projection matrix for f' position (as explained above), create transform matrix to transform f' to f. The transformed f must have Z axis the same as in f' the other axises can be obtained by cross product and use that as camera or multiply booth together and use as abused Projection matrix

How to construct the matrix is explained in the Understanding transform matrices link from my earlier comments

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thanks for taking the time on this, but its actually not what I'm looking for. Its not an openGL question, its a theoretical math problem, so I need the actual matrix and not the openGL implementation. I'm still a bit confused about the matrix I'm looking for. – Amit Ofer May 24 '15 at 10:42
  • @AmitOfer draw image of your frustrum and knonws so it is clear what we are talking about – Spektre May 24 '15 at 10:45
  • @AmitOfer added edit1 without more info is hard to answer as I mentioned before draw an image ... that usually clears things up – Spektre May 24 '15 at 10:56
  • Thats what I don't understand, I will try to translate the question as accurate as I can because it wasn't written in English: "Write a matrix that represents the perspective projection on a plane that passes through point R0(X0,Y0,Z0) and has perpendicular vector N=(n1,n2,n3). Assume that the point of view is (0,0,0)." – Amit Ofer May 24 '15 at 10:58
  • your diagram in edit2 seems correct, I'm still not sure how I can calculate f' in order to get the translation right and how to set the projection after it. – Amit Ofer May 24 '15 at 13:08