我开发了一个简单的3D引擎(没有使用任何API),已经成功地将我的场景转换到世界和视图空间,但是在使用透视投影矩阵(类似于OpenGL)将场景(从视图空间)投影时遇到了问题。我不确定fov、near和far值,而且得到的场景有畸变。我希望有人可以指导我如何正确构建和使用透视投影矩阵,并提供一些代码示例。非常感谢您提供的任何帮助。
矩阵构建:
矩阵构建:
double f = 1 / Math.Tan(fovy / 2);
return new double[,] {
{ f / Aspect, 0, 0, 0 },
{ 0, f, 0, 0 },
{ 0, 0, (Far + Near) / (Near - Far), (2 * Far * Near) / (Near - Far) },
{ 0, 0, -1, 0 }
};
矩阵的使用:
foreach (Point P in T.Points)
{
.
. // Transforming the point to homogen point matrix, to world space, and to view space (works fine)
.
// projecting the point with getProjectionMatrix() specified in the previous code :
double[,] matrix = MatrixMultiply( GetProjectionMatrix(Fovy, Width/Height, Near, Far) , viewSpacePointMatrix );
// translating to Cartesian coordinates (from homogen):
matrix [0, 0] /= matrix [3, 0];
matrix [1, 0] /= matrix [3, 0];
matrix [2, 0] /= matrix [3, 0];
matrix [3, 0] = 1;
P = MatrixToPoint(matrix);
// adjusting to the screen Y axis:
P.y = this.Height - P.y;
// Printing...
}