•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Technical 3D Perspective Projection Question

3D C# Perspective Projection Camera Matrix

No replies to this topic

### #1 Belos

Belos

CC Lurker

• Just Joined
• 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,2*near/(top-bottom),(top+bottom)/(top-bottom),0},
{0,0,-(far+near)/(far-near),-(2 * far * near) / (far - near)},
{0,0,-1,0}
});
return m;
}
```

and used the following code to get the camera:

```Matrix3D Camera
{
get
{
Vector3D cameraZAxis = -this.LookDirection;
cameraZAxis.Normalize();[/background]
Vector3D cameraXAxis = Vector3D.CrossProduct(this.UpDirection, cameraZAxis);
cameraXAxis.Normalize();
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

{-1,0,0,0},

{0,1,0,0},

{0,0,-1,0},

{0,0,0,1}

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 so I am very confused right now!!

the model matrix is

{0,0,0,100}

{0,0,0,100}

{0,0,0,100}

{0,0,0,1}

Can you tell me where I have gone wrong?
[background=transparent]
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