Another method that took a while to find was the one that calculates a perspective projection matrix from a given field of view, aspect ratio and near and far plane to work with OpenGL. This constructor for a Matrix object does it though:
public Matrix(float fov, float aspect, float near, float far) { Values = new float[4, 4]; float scale = (float)Math.Tan(fov.ToRadians() * 0.5f) * near; float right = aspect * scale; float left = -right; float top = scale; float bottom = -top; Values[0, 0] = 2f * near / (right - left); Values[1, 1] = 2f * near / (top - bottom); Values[0, 2] = (right + left) / (right - left); Values[1, 2] = (top + bottom) / (top - bottom); Values[2, 2] = -(far + near) / (far - near); Values[3, 2] = -1f; Values[2, 3] = -2f * far * near / (far - near); }
Note that this class uses row-major order, while OpenGL uses column-major order. When I pass my matrices to the glUniformMatrix4fv method, I set it's transpose parameter to True.
No comments:
Post a Comment