我開發了一個簡單的3D引擎(不使用任何API),成功地將我的場景轉換爲世界和視圖空間,但無法使用視角投影我的場景(從視圖空間)投影矩陣(OpenGL樣式)。我不確定fov,near和far的值,我得到的場景被扭曲了。 我希望如果有人能指導我如何用示例代碼適當地構建和使用透視投影矩陣。預先感謝您的幫助。如何構建透視投影矩陣(無API)
基質構建:
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...
}
http://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix 我也推薦以前的課程(投影點和3D查看)。 – user18490