summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/Projector.cpp
blob: 55f50b48d5f71002924baeff13831d70cdc768f5 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, 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
    ========
    3D Projection Camera class
*/

#ifndef NOMINMAX
#define NOMINMAX
#endif

#include <algorithm>

#include "Projector.h"
#include "Utils.h"

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

static const float   CLIP_PLANE_EPSILON   = 0.0001f;
static const double  Z_NEAR               = 1.0;

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

static Camera  emergency_cam;

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

Projector::Projector(Window* window, Camera* cam)
: camera(cam), infinite(0), depth_scale(1.0f), orthogonal(false), field_of_view(2)
{
    if (!camera)
    camera = &emergency_cam;

    UseWindow(window);
}

Projector::~Projector()
{ }

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

void
Projector::UseCamera(Camera* cam)
{
    if (cam)
    camera = cam;
    else
    camera = &emergency_cam;
}

void
Projector::UseWindow(Window* win)
{
    Rect r  = win->GetRect();
    width   = r.w;
    height  = r.h;

    xcenter = (width  / 2.0);
    ycenter = (height / 2.0);

    xclip0  = 0.0f;
    xclip1  = (float) width-0.5f;
    yclip0  = 0.0f;
    yclip1  = (float) height-0.5f;

    SetFieldOfView(field_of_view);
}

void
Projector::SetFieldOfView(double fov)
{
    field_of_view = fov;

    xscreenscale = width  / fov;
    yscreenscale = height / fov;

    maxscale     = std::max(xscreenscale, yscreenscale);

    xangle       = atan(2.0/fov * maxscale/xscreenscale);
    yangle       = atan(2.0/fov * maxscale/yscreenscale);
}

double
Projector::GetFieldOfView() const
{
    return field_of_view;
}

void
Projector::SetDepthScale(float scale)
{
    depth_scale = scale;
}

double
Projector::GetDepthScale() const
{
    return depth_scale;
}

int
Projector::SetInfinite(int i)
{
    int old = infinite;
    infinite = i;
    return old;
}

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

void
Projector::StartFrame()
{
    SetUpFrustum();
    SetWorldSpace();
}

// +--------------------------------------------------------------------+
// Transform a point from worldspace to viewspace.
// +--------------------------------------------------------------------+

void
Projector::Transform(Vec3& vec) const
{
    Vec3 tvert = vec;

    // Translate into a viewpoint-relative coordinate
    if (!infinite)
    tvert -= camera->Pos();

    // old method:
    vec.x = (tvert * camera->vrt());
    vec.y = (tvert * camera->vup());
    vec.z = (tvert * camera->vpn());

    // Rotate into the view orientation
    // vec = tvert * camera->Orientation();
}

// +--------------------------------------------------------------------+
// Transform a point from worldspace to viewspace.
// +--------------------------------------------------------------------+

void
Projector::Transform(Point& point) const
{
    Point tvert = point;

    // Translate into a viewpoint-relative coordinate
    if (!infinite)
    tvert -= camera->Pos();

    // old method:
    point.x = (tvert * camera->vrt());
    point.y = (tvert * camera->vup());
    point.z = (tvert * camera->vpn());

    // Rotate into the view orientation
    // point = tvert * camera->Orientation();
}

// +--------------------------------------------------------------------+
// APPARENT RADIUS OF PROJECTED OBJECT
// Project a viewspace point into screen coordinates.
// Use projected Z to determine apparent radius of object.
// +--------------------------------------------------------------------+

float
Projector::ProjectRadius(const Vec3& v, float radius) const
{
    return (float) fabs((radius * maxscale) / v.z);
}

// +--------------------------------------------------------------------+
// IN PLACE PROJECTION OF POINT
// Project a viewspace point into screen coordinates.
// Note that the y axis goes up in worldspace and viewspace, but
// goes down in screenspace.
// +--------------------------------------------------------------------+

void
Projector::Project(Vec3& v, bool clamp) const
{
    double  zrecip;

    if (orthogonal) {
        double scale = field_of_view/2;
        v.x  = (float)           (xcenter + scale * v.x);
        v.y  = (float) (height - (ycenter + scale * v.y));
        v.z  = (float) (0.0f);
    }

    else {
        //zrecip = 2 * (1.0e5 / (1.0e5-1)) / v.z;
        //zrecip = 2 * 0.97 / v.z; -- what the heck was this version used for?

        zrecip = 2 / v.z;
        v.x  = (float)           (xcenter + maxscale * v.x * zrecip);
        v.y  = (float) (height - (ycenter + maxscale * v.y * zrecip));
        v.z  = (float) (1 - zrecip);
    }

    // clamp the point to the viewport:
    if (clamp) {
        if (v.x < xclip0) v.x = xclip0;
        if (v.x > xclip1) v.x = xclip1;
        if (v.y < yclip0) v.y = yclip0;
        if (v.y > yclip1) v.y = yclip1;
    }
}

// +--------------------------------------------------------------------+
// IN PLACE PROJECTION OF POINT
// Project a viewspace point into screen coordinates.
// Note that the y axis goes up in worldspace and viewspace, but
// goes down in screenspace.
// +--------------------------------------------------------------------+

void
Projector::Project(Point& v, bool clamp) const
{
    double  zrecip;

    if (orthogonal) {
        double scale = field_of_view/2;
        v.x  =           (xcenter + scale * v.x);
        v.y  = (height - (ycenter + scale * v.y));
        v.z  = 0;
    }

    else {
        zrecip = 1 / v.z;
        v.x  =           (xcenter + 2 * maxscale * v.x * zrecip);
        v.y  = (height - (ycenter + 2 * maxscale * v.y * zrecip));
        v.z  = (1 - zrecip);
    }

    // clamp the point to the viewport:
    if (clamp) {
        if (v.x < xclip0) v.x = xclip0;
        if (v.x > xclip1) v.x = xclip1;
        if (v.y < yclip0) v.y = yclip0;
        if (v.y > yclip1) v.y = yclip1;
    }
}

// +--------------------------------------------------------------------+
// IN PLACE UN-PROJECTION OF POINT
// Convert a point in screen coordinates back to viewspace.
// Note that the y axis goes up in worldspace and viewspace, but
// goes down in screenspace.
// +--------------------------------------------------------------------+

void
Projector::Unproject(Point& v) const
{
    double  zrecip = 1 / v.z;

    /***
    * forward projection:
v.x  = (xcenter + maxscale * v.x * zrecip);
v.y  = (height - (ycenter + maxscale * v.y * zrecip));
v.z  = (1 - zrecip);
***/

    v.x  = (         v.x - xcenter) / (maxscale * zrecip);
    v.y  = (height - v.y - ycenter) / (maxscale * zrecip);
}

// +--------------------------------------------------------------------+
// IN PLACE PROJECTION OF RECTANGLE (FOR SPRITES)
// Project a viewspace point into screen coordinates.
// Note that the y axis goes up in worldspace and viewspace, but
// goes down in screenspace.
// +--------------------------------------------------------------------+

void
Projector::ProjectRect(Point& v, double& w, double& h) const
{
    double  zrecip;

    if (orthogonal) {
        double scale = field_of_view/2;
        v.x  =           (xcenter + scale * v.x);
        v.y  = (height - (ycenter + scale * v.y));
        v.z  = 0;
    }

    else {
        zrecip = 1 / v.z;
        v.x  =           (xcenter + 2 * maxscale * v.x * zrecip);
        v.y  = (height - (ycenter + 2 * maxscale * v.y * zrecip));
        v.z  = (1 - Z_NEAR*zrecip);

        w   *= maxscale * zrecip;
        h   *= maxscale * zrecip;
    }
}

// +--------------------------------------------------------------------+
// Set up a clip plane with the specified normal.
// +--------------------------------------------------------------------+

void
Projector::SetWorldspaceClipPlane(Vec3& normal, Plane& plane)
{
    // Rotate the plane normal into worldspace
    ViewToWorld(normal, plane.normal);
    plane.distance = (float) (camera->Pos() * plane.normal + CLIP_PLANE_EPSILON);
}

// +--------------------------------------------------------------------+
// Set up the planes of the frustum, in worldspace coordinates.
// +--------------------------------------------------------------------+

void
Projector::SetUpFrustum()
{
    double  angle, s, c;
    Vec3    normal;

    angle = XAngle();
    s = sin(angle);
    c = cos(angle);

    // Left clip plane
    normal.x = (float) s;
    normal.y = (float) 0;
    normal.z = (float) c;
    view_planes[0].normal   = normal;
    view_planes[0].distance = CLIP_PLANE_EPSILON;
    SetWorldspaceClipPlane(normal, world_planes[0]);

    // Right clip plane
    normal.x = (float) -s;
    view_planes[1].normal   = normal;
    view_planes[1].distance = CLIP_PLANE_EPSILON;
    SetWorldspaceClipPlane(normal, world_planes[1]);

    angle = YAngle();
    s = sin(angle);
    c = cos(angle);

    // Bottom clip plane
    normal.x = (float) 0;
    normal.y = (float) s;
    normal.z = (float) c;
    view_planes[2].normal   = normal;
    view_planes[2].distance = CLIP_PLANE_EPSILON;
    SetWorldspaceClipPlane(normal, world_planes[2]);

    // Top clip plane
    normal.y = (float) -s;
    view_planes[3].normal   = normal;
    view_planes[3].distance = CLIP_PLANE_EPSILON;
    SetWorldspaceClipPlane(normal, world_planes[3]);
}

// +--------------------------------------------------------------------+
// Clip the point against the frustum and return 1 if partially inside
// Return 2 if completely inside
// +--------------------------------------------------------------------+

int
Projector::IsVisible(const Vec3& v, float radius) const
{
    int visible = 1;
    int complete = 1;

    Plane* plane = (Plane*) frustum_planes;
    if (infinite) {
        complete = 0;

        for (int i = 0; visible && (i < NUM_FRUSTUM_PLANES); i++) {
            visible = ((v * plane->normal) >= CLIP_PLANE_EPSILON);
            plane++;
        }
    }
    else {
        for (int i = 0; visible && (i < NUM_FRUSTUM_PLANES); i++) {
            float dot = v * plane->normal;
            visible  = ((dot + radius) >= plane->distance);
            complete = complete && ((dot - radius) >= plane->distance);
            plane++;
        }
    }

    return visible + complete;
}

// +--------------------------------------------------------------------+
// Clip the bouding point against the frustum and return non zero
// if at least partially inside.  This version is not terribly
// efficient as it checks all eight box corners rather than just
// the minimum two.
// +--------------------------------------------------------------------+

int
Projector::IsBoxVisible(const Point* p) const
{
    int   i, j, outside = 0;

    // if all eight corners are outside of the same
    // frustrum plane, then the box is not visible
    Plane* plane = (Plane*) frustum_planes;

    if (infinite) {
        for (i = 0; !outside && (i < NUM_FRUSTUM_PLANES); i++) {
            for (j = 0; j < 8; j++)
            outside += (p[j] * plane->normal) < CLIP_PLANE_EPSILON;

            if (outside < 8)
            outside = 0;

            plane++;
        }
    }
    else {
        for (i = 0; !outside && (i < NUM_FRUSTUM_PLANES); i++) {
            for (j = 0; j < 8; j++)
            outside += (p[j] * plane->normal) < plane->distance;

            if (outside < 8)
            outside = 0;

            plane++;
        }
    }

    // if not outside, then the box is visible
    return !outside;
}

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

float
Projector::ApparentRadius(const Vec3& v, float radius) const
{
    Vec3 vloc = v;

    Transform(vloc);                  // transform in place
    return ProjectRadius(vloc, radius);
}


// +--------------------------------------------------------------------+
// Rotate a vector from viewspace to worldspace.
// +--------------------------------------------------------------------+

void
Projector::ViewToWorld(Point& pin, Point& pout)
{
    // Rotate into the world orientation
    pout.x = pin.x * camera->vrt().x + pin.y * camera->vup().x + pin.z * camera->vpn().x;
    pout.y = pin.x * camera->vrt().y + pin.y * camera->vup().y + pin.z * camera->vpn().y;
    pout.z = pin.x * camera->vrt().z + pin.y * camera->vup().z + pin.z * camera->vpn().z;
}

void
Projector::ViewToWorld(Vec3& vin, Vec3& vout)
{
    // Rotate into the world orientation
    vout.x = (float) (vin.x * camera->vrt().x + vin.y * camera->vup().x + vin.z * camera->vpn().x);
    vout.y = (float) (vin.x * camera->vrt().y + vin.y * camera->vup().y + vin.z * camera->vpn().y);
    vout.z = (float) (vin.x * camera->vrt().z + vin.y * camera->vup().z + vin.z * camera->vpn().z);
}