summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Scene.cpp
blob: 4f977f4c1d7758099fb44b1162186c31bf4e5e44 (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
/*  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
    ========
    A 3D Scene
*/

#include "Scene.h"
#include "Graphic.h"
#include "Light.h"
#include "Utils.h"

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

Scene::Scene()
{ }

Scene::~Scene()
{
    background.destroy();
    foreground.destroy();
    graphics.destroy();
    sprites.destroy();

    lights.destroy();
}

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

void
Scene::AddBackground(Graphic* g)
{
    if (g) {
        if (!background.contains(g))
        background.append(g);

        g->SetScene(this);
    }
}

void
Scene::DelBackground(Graphic* g)
{
    if (g) {
        background.remove(g);
        g->SetScene(0);
    }
}

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

void
Scene::AddForeground(Graphic* g)
{
    if (g) {
        if (!foreground.contains(g))
        foreground.append(g);

        g->SetScene(this);
    }
}

void
Scene::DelForeground(Graphic* g)
{
    if (g) {
        foreground.remove(g);
        g->SetScene(0);
    }
}

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

void
Scene::AddGraphic(Graphic* g)
{
    if (g) {
        if (!graphics.contains(g))
        graphics.append(g);

        g->SetScene(this);
    }
}

void
Scene::DelGraphic(Graphic* g)
{
    if (g) {
        graphics.remove(g)   || // it's gotta be in here somewhere!
        foreground.remove(g) || // use the logical-or operator to early
        sprites.remove(g)    || // out when we find it...
        background.remove(g);

        g->SetScene(0);
    }
}

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

void
Scene::AddSprite(Graphic* g)
{
    if (g) {
        if (!sprites.contains(g))
        sprites.append(g);

        g->SetScene(this);
    }
}

void
Scene::DelSprite(Graphic* g)
{
    if (g) {
        sprites.remove(g);
        g->SetScene(0);
    }
}

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

void
Scene::AddLight(Light* l)
{
    if (l) {
        if (!lights.contains(l))
        lights.append(l);
        l->SetScene(this);
    }
}

void
Scene::DelLight(Light* l)
{
    if (l) {
        lights.remove(l);
        l->SetScene(0);
    }
}

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

void
Scene::Collect()
{
    ListIter<Graphic> iter = graphics;

    while (++iter) {
        Graphic* g = iter.value();
        if (g->Life() == 0) {
            delete iter.removeItem();
        }
    }

    iter.attach(sprites);

    while (++iter) {
        Graphic* g = iter.value();
        if (g->Life() == 0) {
            delete iter.removeItem();
        }
    }

    ListIter<Light> iter1 = lights;

    while (++iter1) {
        Light* l = iter1.value();
        if (l->Life() == 0) {
            delete iter1.removeItem();
        }
    }
}


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

bool
Scene::IsLightObscured(const Point& obj_pos, const Point& light_pos, double obj_radius, Point* impact_point) const
{
    Point  dir = light_pos - obj_pos;
    double len = dir.Normalize();

    Scene*      pThis    = (Scene*) this; // cast-away const
    Graphic*    g        = 0;
    bool        obscured = false;

    ListIter<Graphic> g_iter = pThis->graphics;
    while (++g_iter && !obscured) {
        g = g_iter.value();

        if (g->CastsShadow() && !g->Hidden() && !g->IsInfinite()) {
            double gdist = (g->Location() - obj_pos).length();
            if (gdist > 0.1 &&                     // different than object being obscured
                    g->Radius() > obj_radius &&        // larger than object being obscured
                    (g->Radius()*400)/gdist > 10) {    // projects to a resonable size

                Point  delta = (g->Location() - light_pos);

                if (delta.length() > g->Radius() / 100) { // not the object that is emitting the light
                    Point  impact;
                    obscured = g->CheckRayIntersection(obj_pos, dir, len, impact, false) ? true : false;

                    if (impact_point)
                    *impact_point = impact;
                }
            }

            else if (obj_radius < 0 && gdist < 0.1) { // special case for camera (needed for cockpits)
                Point  delta = (g->Location() - light_pos);

                if (delta.length() > g->Radius() / 100) { // not the object that is emitting the light
                    Point  impact;
                    obscured = g->CheckRayIntersection(obj_pos, dir, len, impact, false) ? true : false;
                }
            }
        }
    }

    g_iter.attach(pThis->foreground);
    while (++g_iter && !obscured) {
        g = g_iter.value();

        if (g->CastsShadow() && !g->Hidden()) {
            double gdist = (g->Location() - obj_pos).length();
            if (gdist > 0.1 &&                     // different than object being obscured
                    g->Radius() > obj_radius &&        // larger than object being obscured
                    (g->Radius()*400)/gdist > 10) {    // projects to a resonable size

                Point  delta = (g->Location() - light_pos);

                if (delta.length() > g->Radius() / 100) { // not the object that is emitting the light
                    Point  impact;
                    obscured = g->CheckRayIntersection(obj_pos, dir, len, impact, false) ? true : false;

                    if (impact_point)
                    *impact_point = impact;
                }
            }

            else if (obj_radius < 0 && gdist < 0.1) { // special case for camera (needed for cockpits)
                Point  delta = (g->Location() - light_pos);

                if (delta.length() > g->Radius() / 100) { // not the object that is emitting the light
                    Point  impact;
                    obscured = g->CheckRayIntersection(obj_pos, dir, len, impact, false) ? true : false;
                }
            }
        }
    }

    return obscured;
}