summaryrefslogtreecommitdiff
path: root/src/mvp.maxima
blob: 8ec7d355c5298a3cf1ffa4fb8953c968fddf54f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Copyright 2022 Benjamin Barenblat

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License. */

/* Computes the model-view-projection matrix for glplanet. Variables are as follows:

  - beta is the desired latitude in radians.

  - gamma is the desired longitude in radians _west_ of the prime meridian (the
    opposite of the usual convention).

  - d is the distance to the center of the Earth (i.e., the z-coordinate of the
    translated Earth), in Earth radii. Since this transformation also switches
    from a right-handed model coordinate system to a left-handed scene
    coordinate system, this should always be a positive value.

  - phix and phiy are the camera field of view, in radians, in the x and y
    directions.

  - zn and zf are the near and far clip planes, again in Earth radii. These
    should both be positive, and zf should be more than zn. */

/* Rotation around the model z axis (turning to longitude) */
Z : matrix([cos(gamma), -sin(gamma), 0, 0],
           [sin(gamma),  cos(gamma), 0, 0],
           [    0,           0,      1, 0],
           [    0,           0,      0, 1])$

/* Rotation around the model y axis (tilting to latitude) */
Y : matrix([ cos(beta), 0, sin(beta), 0],
           [     0,     1,     0,     0],
           [-sin(beta), 0, cos(beta), 0],
           [     0,     0,     0,     1])$

/* Transformation from model coordinates to view coordinates */
T : matrix([ 0, 1, 0, 0],
           [ 0, 0, 1, 0],
           [-1, 0, 0, z],
           [ 0, 0, 0, 1])$

/* Perspective transformation */
P : matrix([1 / tan(phix / 2),         0,                    0,                      0           ],
           [        0,         1 / tan(phiy / 2),            0,                      0           ],
           [        0,                 0,          (zn + zf) / (zf - zn), 2 * zn * zf / (zn - zf)],
           [        0,                 0,                    1,                      0           ])$

P . T . Y . Z;