Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Technical 3D Perspective Projection Question

3D C# Perspective Projection Camera Matrix

  • Please log in to reply
No replies to this topic

#1 Belos


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C, C++, C#
  • Learning:C, C++, C#

Posted 20 June 2012 - 04:24 PM

I have a problem with perspective projection, In order to display a 3D object on screen, I have to do the following:
  • Perform model transformation matrix * p, resulting in pm.
  • Perform projection matrix * pm, resulting in pp.
  • Perform viewport transformation matrix * pp, resulting is ps: point on screen

I did and used the following code to get the perspective projection:

public static Matrix3D ProjectionMatrix(double angle, double aspect, double near, double far)
    double size = near * Math.Tan(MathUtils.DegreeToRadian(angle) / 2.0);
    double left = -size, right = size, bottom = -size / aspect, top = size / aspect;
    Matrix3D m = new Matrix3D(new double[,] {
    {2*near/(right-left),0,(right + left)/(right - left),0},
    {0,0,-(far+near)/(far-near),-(2 * far * near) / (far - near)},
    return m;

and used the following code to get the camera:

Matrix3D Camera
		    Vector3D cameraZAxis = -this.LookDirection;
		    Vector3D cameraXAxis = Vector3D.CrossProduct(this.UpDirection, cameraZAxis);
		    Vector3D cameraYAxis = Vector3D.CrossProduct(cameraZAxis, cameraXAxis);
		    Vector3D cameraPosition = (Vector3D)this.Position;
		    double offsetX = -Vector3D.DotProduct(cameraXAxis, cameraPosition);
		    double offsetY = -Vector3D.DotProduct(cameraYAxis, cameraPosition);
		    double offsetZ = -Vector3D.DotProduct(cameraZAxis, cameraPosition);
		    return new Matrix3D(new double[,]{{cameraXAxis.X, cameraYAxis.X, cameraZAxis.X, 0},
							    {cameraXAxis.Y, cameraYAxis.Y, cameraZAxis.Y, 0},
							    {cameraXAxis.Z, cameraYAxis.Z, cameraZAxis.Z, 0},
							    {offsetX, offsetY, offsetZ, 1}});

The default camera matrix is





after I multiply the three matrices(the third is the model matrix) by the point and the resulting W in the point is very huge like 13000 :scared: so I am very confused right now!!

the model matrix is





Can you tell me where I have gone wrong?
note: this is not xna

  • 0

Also tagged with one or more of these keywords: 3D, C#, Perspective Projection, Camera, Matrix

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download