summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Camera.cpp
blob: a2953a1942a5cd25b2b3a0df597e05ed127a48d9 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Camera Class - Position and Point of View
*/

#include "MemDebug.h"
#include "Camera.h"

// +--------------------------------------------------------------------+

Camera::Camera(double x, double y, double z)
    : pos(x,y,z)
{ }

Camera::~Camera()
{ }

// +--------------------------------------------------------------------+

void
Camera::MoveTo(double x, double y, double z)
{
    pos.x = x;
    pos.y = y;
    pos.z = z;
}

void
Camera::MoveTo(const Point& p)
{
    pos.x = p.x;
    pos.y = p.y;
    pos.z = p.z;
}

// +--------------------------------------------------------------------+

void
Camera::MoveBy(double dx, double dy, double dz)
{
    pos.x += dx;
    pos.y += dz;
    pos.z += dy;
}

void
Camera::MoveBy(const Point& p)
{
    pos.x += p.x;
    pos.y += p.y;
    pos.z += p.z;
}

// +--------------------------------------------------------------------+

void
Camera::Clone(const Camera& cam)
{
    pos         = cam.pos;
    orientation = cam.orientation;
}

// +--------------------------------------------------------------------+

void
Camera::LookAt(const Point& target, const Point& eye, const Point& up)
{
    Point zaxis = target - eye;         zaxis.Normalize();
    Point xaxis = up.cross(zaxis);      xaxis.Normalize();
    Point yaxis = zaxis.cross(xaxis);   yaxis.Normalize();

    orientation(0,0)  = xaxis.x;
    orientation(0,1)  = xaxis.y;
    orientation(0,2)  = xaxis.z;

    orientation(1,0)  = yaxis.x;
    orientation(1,1)  = yaxis.y;
    orientation(1,2)  = yaxis.z;

    orientation(2,0)  = zaxis.x;
    orientation(2,1)  = zaxis.y;
    orientation(2,2)  = zaxis.z;

    pos               = eye;
}

// +--------------------------------------------------------------------+

void
Camera::LookAt(const Point& target)
{
    // No navel gazing:
    if (target == Pos())
    return;

    Point tgt, tmp = target - Pos();

    // Rotate into the view orientation:
    tgt.x = (tmp * vrt());
    tgt.y = (tmp * vup());
    tgt.z = (tmp * vpn());

    if (tgt.z == 0) {
        Pitch(0.5);
        Yaw(0.5);
        LookAt(target);
        return;
    }

    double az = atan(tgt.x/tgt.z);
    double el = atan(tgt.y/tgt.z);

    // if target is behind, offset by 180 degrees:
    if (tgt.z < 0)
    az -= PI;

    Pitch(-el);
    Yaw(az);

    // roll to upright position:
    double deflection = vrt().y;
    while  (fabs(deflection) > 0.001) {
        double theta = asin(deflection/vrt().length());
        Roll(-theta);

        deflection = vrt().y;
    }
}


// +--------------------------------------------------------------------+

bool
Camera::Padlock(const Point& target, double alimit, double e_lo, double e_hi)
{
    // No navel gazing:
    if (target == Pos())
    return false;

    Point tgt, tmp = target - Pos();

    // Rotate into the view orientation:
    tgt.x = (tmp * vrt());
    tgt.y = (tmp * vup());
    tgt.z = (tmp * vpn());

    if (tgt.z == 0) {
        Yaw(0.1);

        tgt.x = (tmp * vrt());
        tgt.y = (tmp * vup());
        tgt.z = (tmp * vpn());

        if (tgt.z == 0)
        return false;
    }

    bool   locked  = true;
    double az      = atan(tgt.x/tgt.z);
    double orig    = az;

    // if target is behind, offset by 180 degrees:
    if (tgt.z < 0)
    az -= PI;

    while (az >  PI) az -= 2*PI;
    while (az < -PI) az += 2*PI;

    if (alimit > 0) {
        if (az < -alimit) {
            az = -alimit;
            locked = false;
        }
        else if (az > alimit) {
            az = alimit;
            locked = false;
        }
    }

    Yaw(az);

    // Rotate into the new view orientation:
    tgt.x = (tmp * vrt());
    tgt.y = (tmp * vup());
    tgt.z = (tmp * vpn());

    double el      = atan(tgt.y/tgt.z);

    if (e_lo > 0 && el < -e_lo) {
        el = -e_lo;
        locked = false;
    }

    else if (e_hi > 0 && el > e_hi) {
        el = e_hi;
        locked = false;
    }

    Pitch(-el);

    return locked;
}