aboutsummaryrefslogtreecommitdiff
path: root/src/scene-ideas.cpp
blob: af716bd10b92d5bcdeed3a45e9efb7a3e7fe3102 (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
/*
 * (c) Copyright 1993, Silicon Graphics, Inc.
 * Copyright © 2012 Linaro Limited
 *
 * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
 *
 * glmark2 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * glmark2.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *  Jesse Barker
 */
#include "scene.h"
#include "stack.h"
#include "splines.h"
#include "table.h"
#include "logo.h"
#include "lamp.h"
#include "util.h"
#include "log.h"
#include <sys/time.h>

using LibMatrix::Stack4;
using LibMatrix::mat4;
using LibMatrix::vec3;
using LibMatrix::vec4;
using LibMatrix::uvec3;
using std::string;
using std::map;

class SceneIdeasPrivate
{
public:
    SceneIdeasPrivate() :
        valid_(false),
        currentSpeed_(1.0), // Real time.
        currentTime_(START_TIME_),
        timeOffset_(START_TIME_)
    {
        startTime_.tv_sec = 0;
        startTime_.tv_usec = 0; 
    }
    ~SceneIdeasPrivate()
    {
    }
    void initialize(map<string, Scene::Option>& options);
    void reset_time();
    void update_time();
    void update_projection(const mat4& proj);
    void draw();
    bool valid() { return valid_; }

private:
    void postIdle();
    void initLights();
    bool valid_;
    Stack4 projection_;
    Stack4 modelview_;
    float currentSpeed_;
    float currentTime_;
    float timeOffset_;
    struct timeval startTime_;
    static const float CYCLE_TIME_;
    static const float TIME_;
    static const float START_TIME_;
    // Table
    Table table_;
    // Logo
    SGILogo logo_;
    // Lamp
    Lamp lamp_;
    // Light constants
    static const vec4 light0_position_;
    static const vec4 light1_position_;
    static const vec4 light2_position_;
    // Object constants
    ViewFromSpline viewFromSpline_;
    ViewToSpline viewToSpline_;
    LightPositionSpline lightPosSpline_;
    LogoPositionSpline logoPosSpline_;
    LogoRotationSpline logoRotSpline_;
    vec3 viewFrom_;
    vec3 viewTo_;
    vec3 lightPos_;
    vec3 logoPos_;
    vec3 logoRot_;
    vec4 lightPositions_[3];
};

const float SceneIdeasPrivate::TIME_(15.0);
const float SceneIdeasPrivate::CYCLE_TIME_(TIME_ * 1.0 - 3.0);
const float SceneIdeasPrivate::START_TIME_(0.6);
const vec4 SceneIdeasPrivate::light0_position_(0.0, 1.0, 0.0, 0.0);
const vec4 SceneIdeasPrivate::light1_position_(-1.0, 0.0, 0.0, 0.0);
const vec4 SceneIdeasPrivate::light2_position_(0.0, -1.0, 0.0, 0.0);

void
SceneIdeasPrivate::initLights()
{
    const mat4& curMV(modelview_.getCurrent());
    lightPositions_[0] = curMV * light0_position_;
    lightPositions_[1] = curMV * light1_position_;
    lightPositions_[2] = curMV * light2_position_;
}

void
SceneIdeasPrivate::initialize(map<string, Scene::Option>& options)
{
    // Initialize the positions for the lights we'll use.
    initLights();

    // Tell the objects in the scene to initialize themselves.
    table_.init();
    if (!table_.valid())
    {
        Log::debug("SceneIdeas: table object not properly initialized!\n");
        return;
    }
    logo_.init();
    if (!logo_.valid())
    {
        Log::debug("SceneIdeas: logo object not properly initialized!\n");
        return;
    }
    lamp_.init();
    if (!lamp_.valid())
    {
        Log::debug("SceneIdeas: lamp object not properly initialized!\n");
        return;
    }

    reset_time();

    // If the option string tells us the user wants the speed to be a function
    // of the scene duration, do it.  Otherwise, take the value explicitly.
    static const string durationLabel("duration");
    static const string speedLabel("speed");
    if (options[speedLabel].value == durationLabel)
    {
        float duration = Util::fromString<float>(options[durationLabel].value);
        currentSpeed_ = (CYCLE_TIME_ - START_TIME_) / duration;
    }
    else
    {
        currentSpeed_ = Util::fromString<float>(options[speedLabel].value);
    }

    // If we're here, we're okay to run.
    valid_ = true;
}

void
SceneIdeasPrivate::reset_time()
{
    timeOffset_ = START_TIME_;
    gettimeofday(&startTime_, NULL);
}

void
SceneIdeasPrivate::update_time()
{
    // Compute new time
    struct timeval current = {0, 0};
    gettimeofday(&current, NULL);
    float timediff = (current.tv_sec - startTime_.tv_sec) + 
        static_cast<double>(current.tv_usec - startTime_.tv_usec) / 1000000.0;
    float sceneTime = timediff * currentSpeed_ + timeOffset_;

    // Keep the current time in [START_TIME_..CYCLE_TIME_)
    // Every other cycle starting with 0 start at the beginning and goes
    // forward in time.  Other cycles start at the end and go backwards.
    currentTime_ = std::fmod(sceneTime, CYCLE_TIME_);
    unsigned int cycle = sceneTime/CYCLE_TIME_;
    if (cycle % 2)
    {
        currentTime_ = CYCLE_TIME_ - currentTime_;
    }
}

void
SceneIdeasPrivate::update_projection(const mat4& proj)
{
    // Projection hasn't changed since last frame.
    if (projection_.getCurrent() == proj)
    {
        return;
    }

    projection_.loadIdentity();
    projection_ *= proj;
}

SceneIdeas::SceneIdeas(Canvas& canvas) :
    Scene(canvas, "ideas"), priv_(0)
{
    options_["speed"] = Scene::Option("speed", "duration",
                                      "Time coefficient (1.0 is \"wall clock\" speed, <1.0 is slower, >1.0 is faster).  A special value of \"duration\" computes this as a function of the \"duration\" option");
}

SceneIdeas::~SceneIdeas()
{
    delete priv_;
}

bool
SceneIdeas::load()
{
    running_ = false;
    return true;
}

void
SceneIdeas::unload()
{
}

bool
SceneIdeas::setup()
{
    if (!Scene::setup())
        return false;

    priv_ = new SceneIdeasPrivate();
    priv_->initialize(options_);
    if (!priv_->valid())
        return false;

    priv_->update_projection(canvas_.projection());

    // Core Scene state
    currentFrame_ = 0;
    running_ = true;
    startTime_ = Util::get_timestamp_us() / 1000000.0;
    lastUpdateTime_ = startTime_;

    return true;
}

void
SceneIdeas::update()
{
    Scene::update();
    priv_->update_time();
    priv_->update_projection(canvas_.projection());
}

void
SceneIdeasPrivate::draw()
{
    viewFromSpline_.getCurrentVec(currentTime_, viewFrom_);
    viewToSpline_.getCurrentVec(currentTime_, viewTo_);
    lightPosSpline_.getCurrentVec(currentTime_, lightPos_);
    logoPosSpline_.getCurrentVec(currentTime_, logoPos_);
    logoRotSpline_.getCurrentVec(currentTime_, logoRot_);

    // Tell the logo its new position
    logo_.setPosition(logoPos_);

    vec4 lp4(lightPos_.x(), lightPos_.y(), lightPos_.z(), 0.0);

    //
    // SHADOW
    //
    modelview_.loadIdentity();
    modelview_.lookAt(viewFrom_.x(), viewFrom_.y(), viewFrom_.z(), 
                      viewTo_.x(), viewTo_.y(), viewTo_.z(),
                      0.0, 1.0, 0.0);

    float pca(0.0);
    if (viewFrom_.y() > 0.0)
    {
        table_.draw(modelview_, projection_, lightPos_, logoPos_, currentTime_, pca);
    }

    glEnable(GL_CULL_FACE); 
    glDisable(GL_DEPTH_TEST); 

    if (logoPos_.y() < 0.0)
    {
        // Set the color assuming we're still under the table.
        uvec3 flatColor(128 / 2,  102 / 2,  179 / 2);
        if (logoPos_.y() > -0.33)
        {
            // We're emerging from the table
            float c(1.0 - logoPos_.y() / -0.33);
            pca /= 4.0;
            flatColor.x(static_cast<unsigned int>(128.0 * (1.0 - c) * 0.5 + 255.0 * pca * c));
            flatColor.y(static_cast<unsigned int>(102.0 * (1.0 - c) * 0.5 + 255.0 * pca * c));
            flatColor.z(static_cast<unsigned int>(179.0 * (1.0 - c) * 0.5 + 200.0 * pca * c));
        }

        modelview_.push();
        modelview_.scale(0.04, 0.0, 0.04);
        modelview_.rotate(-90.0, 1.0, 0.0, 0.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.z()), 0.0, 0.0, 1.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.y()), 0.0, 1.0, 0.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.x()), 1.0, 0.0, 0.0);
        modelview_.rotate(0.1 * 353, 1.0, 0.0, 0.0);
        modelview_.rotate(0.1 * 450, 0.0, 1.0, 0.0);

        logo_.draw(modelview_, projection_, lightPositions_[0], SGILogo::LOGO_FLAT, flatColor);

        modelview_.pop();
    }
    
    if (logoPos_.y() > 0.0)
    {
        modelview_.push();
        modelview_.translate(lightPos_.x(), lightPos_.y(), lightPos_.z());
        mat4 tv;
        tv[3][1] = -1.0;
        tv[3][3] = 0.0;
        tv[0][0] = tv[1][1] = tv[2][2] = lightPos_.y();
        modelview_ *= tv;
        modelview_.translate(-lightPos_.x() + logoPos_.x(),
                             -lightPos_.y() + logoPos_.y(),
                             -lightPos_.z() + logoPos_.z());
        modelview_.scale(0.04, 0.04, 0.04);
        modelview_.rotate(-90.0, 1.0, 0.0, 0.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.z()), 0.0, 0.0, 1.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.y()), 0.0, 1.0, 0.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.x()), 1.0, 0.0, 0.0);
        modelview_.rotate(35.3, 1.0, 0.0, 0.0);
        modelview_.rotate(45.0, 0.0, 1.0, 0.0);

        logo_.draw(modelview_, projection_, lightPositions_[0], SGILogo::LOGO_SHADOW);

        modelview_.pop();
    }
    //
    // DONE SHADOW 
    //

    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    modelview_.loadIdentity();
    modelview_.lookAt(viewFrom_.x(), viewFrom_.y(), viewFrom_.z(),
                      viewTo_.x(), viewTo_.y(), viewTo_.z(), 
                      0.0, 1.0, 0.0);
    modelview_.push();
    modelview_.translate(lightPos_.x(), lightPos_.y(), lightPos_.z());
    modelview_.scale(0.1, 0.1, 0.1);
    float x(lightPos_.x() - logoPos_.x());
    float y(lightPos_.y() - logoPos_.y());
    float z(lightPos_.z() - logoPos_.z());
    double a3(0.0);
    if (x != 0.0)
    {
        a3 = -atan2(z, x) * 10.0 * 180.0 / M_PI;
    }
    double a4(-atan2(sqrt(x * x + z * z), y) * 10.0 * 180.0 / M_PI);
    modelview_.rotate(0.1 * static_cast<int>(a3), 0.0, 1.0, 0.0);
    modelview_.rotate(0.1 * static_cast<int>(a4), 0.0, 0.0, 1.0);
    modelview_.rotate(-90.0, 1.0, 0.0, 0.0);

    lamp_.draw(modelview_, projection_, lightPositions_);

    modelview_.pop();
    
    lightPositions_[0] = modelview_.getCurrent() * lp4;
    
    if (logoPos_.y() > -0.33)
    {
        modelview_.push();
        modelview_.translate(logoPos_.x(), logoPos_.y(), logoPos_.z());
        modelview_.scale(0.04, 0.04, 0.04);
        modelview_.rotate(-90.0, 1.0, 0.0, 0.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.z()), 0.0, 0.0, 1.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.y()), 0.0, 1.0, 0.0);
        modelview_.rotate(0.1 * static_cast<int>(10.0 * logoRot_.x()), 1.0, 0.0, 0.0);
        modelview_.rotate(35.3, 1.0, 0.0, 0.0);
        modelview_.rotate(45.0, 0.0, 1.0, 0.0);

        logo_.draw(modelview_, projection_, lightPositions_[0], SGILogo::LOGO_NORMAL);

        modelview_.pop();
    }
    
    if (viewFrom_.y() < 0.0)
    {
        table_.drawUnder(modelview_, projection_);
    }
}

void
SceneIdeas::draw()
{
    priv_->draw();
}

Scene::ValidationResult
SceneIdeas::validate()
{
    return Scene::ValidationUnknown;
}

void
SceneIdeas::teardown()
{
    delete priv_;
    priv_ = 0;
    Scene::teardown();
}