1
\$\begingroup\$

I've been wanting to know how to do 3d projections.enter image description here

Can someone explain how i can use this to find the position for x and y on the 2d plane that is 600 * 600 pixels.

\$\endgroup\$
3
  • \$\begingroup\$ What do you mean by "get a position for x and y on a screen?" Are you asking how to get a point on the surface of 3D geometry, or how to project 3D geometry into 2D for rendering? \$\endgroup\$
    – jzx
    Aug 26, 2015 at 6:13
  • \$\begingroup\$ In other words, what would be the output you're looking for? A 3D coordinate? A color? Distance from the camera surface (depth)? \$\endgroup\$
    – jzx
    Aug 26, 2015 at 6:15
  • \$\begingroup\$ I meant the 2d plane that the 3d points are going to be projected onto. \$\endgroup\$
    – Funshine
    Aug 26, 2015 at 16:45

2 Answers 2

1
\$\begingroup\$

Assuming that you want to compute the position that an arbitrary 3D vertex (x,y,z,1) has on the view plane after the projection: You can use the given parameters to compute a projection matrix:

Matrix

where f = cotangent(fovy * 0.5)

This is also the matrix that will be set by a call to gluPerspective. Then, you can multiply this matrix with your vertex to obtain the projected vertex position.

\$\endgroup\$
9
  • \$\begingroup\$ In the vertex matrix would it contain [x,y,z,1] or just [x,y,z]? \$\endgroup\$
    – Funshine
    Aug 27, 2015 at 15:39
  • \$\begingroup\$ Also what is fovy? Is it the distance to the 2d plane? \$\endgroup\$
    – Funshine
    Aug 27, 2015 at 15:39
  • \$\begingroup\$ @Funshine For some mathematical reasons, the vertex must be a 4D point. You can just add "1" as the last component of the 3D point that you already have. And fovy is the "Field Of View", in Y-direction. Basically "the (vertical) opening angle" of the view frustum. \$\endgroup\$
    – Marco13
    Aug 27, 2015 at 15:43
  • \$\begingroup\$ How do i find the value of fovy? \$\endgroup\$
    – Funshine
    Aug 27, 2015 at 15:48
  • \$\begingroup\$ Would it be (tan^-1(D/(W/2)))*2 where D is the distance to the 2d plane and W is the width? \$\endgroup\$
    – Funshine
    Aug 27, 2015 at 16:01
0
\$\begingroup\$

Generally, you have to use a bunch of matrices. With the traditional OpenGL pipeline, you'd have to mutiply these matrices:

finalMatrix = modelMatrix
  *
  viewMatrix
  * 
  projectionMatrix
  * 
  viewport.windowMatrix;

To find the position of the vertex on your viewport, just multiply the vertex by the matrix:

onViewportVertex = in3dVertex * finalMatrix;

Of course, this implies that no shader modifies the vertex or the pixel.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .