port perspective projection matrix from -js

This commit is contained in:
Ansis Brammanis
2015-08-14 12:41:18 +03:00
parent d513910712
commit 8d22e2bf01
4 changed files with 40 additions and 16 deletions

View File

@@ -132,8 +132,27 @@ void Painter::lineWidth(float line_width) {
}
void Painter::changeMatrix() {
// Initialize projection matrix
matrix::ortho(projMatrix, 0, state.getWidth(), state.getHeight(), 0, -1, 1);
double halfFov = std::atan(0.5 / state.getAltitude());
double topHalfSurfaceDistance = std::sin(halfFov) * state.getAltitude() /
std::sin(M_PI / 2.0f - state.getPitch() - halfFov);
// Calculate z value of the farthest fragment that should be rendered.
double farZ = std::cos(M_PI / 2.0f - state.getPitch()) * topHalfSurfaceDistance + state.getAltitude();
matrix::perspective(projMatrix, 2.0f * std::atan((state.getHeight() / 2.0f) / state.getAltitude()),
double(state.getWidth()) / state.getHeight(), 0.1, farZ);
matrix::translate(projMatrix, projMatrix, 0, 0, -state.getAltitude());
// After the rotateX, z values are in pixel units. Convert them to
// altitude unites. 1 altitude unit = the screen height.
matrix::scale(projMatrix, projMatrix, 1, -1, 1.0f / state.getHeight());
matrix::rotate_x(projMatrix, projMatrix, state.getPitch());
matrix::rotate_z(projMatrix, projMatrix, state.getAngle());
matrix::translate(projMatrix, projMatrix, state.pixel_x() - state.getWidth() / 2.0f,
state.pixel_y() - state.getWidth() / 2.0f, 0);
// The extrusion matrix.
matrix::identity(extrudeMatrix);