~ubuntu-branches/ubuntu/raring/glmark2/raring

« back to all changes in this revision

Viewing changes to src/scene-bump.cpp

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo
  • Date: 2012-08-21 15:38:09 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120821153809-bwux72bat8qp2n5v
Tags: 2012.08-0ubuntu1
* New upstream release 2012.08 (LP: #1039736)
  - Avoid crashing if gl used is not >= 2.0 (LP: #842279)
* Bumping dh compatibility level to v9
* debian/control:
  - Update Standards-Version to 3.9.3.
  - Add libjpeg-dev build dependency.
  - Use libegl1-x11-dev as an build-dep alternative instead of libegl1-dev.
  - Update description of glmark2-data binary package.
* debian/copyright:
  - Refresh copyright based on the current upstrem version
* debian/rules:
  - Clean compiled python code from unpacked waflib/ directory, as
    described in http://wiki.debian.org/UnpackWaf

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *
19
19
 * Authors:
20
20
 *  Alexandros Frantzis (glmark2)
 
21
 *  Jesse Barker (glmark2)
21
22
 */
22
23
#include "scene.h"
23
24
#include "log.h"
24
25
#include "mat.h"
25
26
#include "stack.h"
26
27
#include "shader-source.h"
 
28
#include "model.h"
 
29
#include "texture.h"
 
30
#include "util.h"
27
31
#include <cmath>
28
32
 
29
33
SceneBump::SceneBump(Canvas &pCanvas) :
30
34
    Scene(pCanvas, "bump"),
31
 
    mTexture(0), mRotation(0.0f), mRotationSpeed(0.0f)
 
35
    texture_(0), rotation_(0.0f), rotationSpeed_(0.0f)
32
36
{
33
 
    mOptions["bump-render"] = Scene::Option("bump-render", "off",
34
 
                                            "How to render bumps [off, normals, high-poly]");
 
37
    options_["bump-render"] = Scene::Option("bump-render", "off",
 
38
                                            "How to render bumps",
 
39
                                            "off,normals,normals-tangent,height,high-poly");
35
40
}
36
41
 
37
42
SceneBump::~SceneBump()
38
43
{
39
44
}
40
45
 
41
 
int SceneBump::load()
42
 
{
43
 
    mRotationSpeed = 36.0f;
44
 
 
45
 
    mRunning = false;
46
 
 
47
 
    return 1;
48
 
}
49
 
 
50
 
void SceneBump::unload()
51
 
{
 
46
bool
 
47
SceneBump::load()
 
48
{
 
49
    rotationSpeed_ = 36.0f;
 
50
 
 
51
    running_ = false;
 
52
 
 
53
    return true;
52
54
}
53
55
 
54
56
void
 
57
SceneBump::unload()
 
58
{
 
59
}
 
60
 
 
61
bool
55
62
SceneBump::setup_model_plain(const std::string &type)
56
63
{
57
64
    static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-poly.vert");
58
65
    static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-poly.frag");
59
 
    static const std::string low_poly_filename(GLMARK_DATA_PATH"/models/asteroid-low.3ds");
60
 
    static const std::string high_poly_filename(GLMARK_DATA_PATH"/models/asteroid-high.3ds");
 
66
    static const std::string low_poly_filename("asteroid-low");
 
67
    static const std::string high_poly_filename("asteroid-high");
61
68
    static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
62
69
    Model model;
63
70
 
70
77
    std::string poly_filename = type == "high-poly" ?
71
78
                                high_poly_filename : low_poly_filename;
72
79
 
73
 
    if(!model.load_3ds(poly_filename))
74
 
        return;
 
80
    if(!model.load(poly_filename))
 
81
        return false;
75
82
 
76
83
    model.calculate_normals();
77
84
 
80
87
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypePosition, 3));
81
88
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeNormal, 3));
82
89
 
83
 
    model.convert_to_mesh(mMesh, attribs);
 
90
    model.convert_to_mesh(mesh_, attribs);
84
91
 
85
92
    /* Load shaders */
86
93
    ShaderSource vtx_source(vtx_shader_filename);
90
97
    frg_source.add_const("LightSourcePosition", lightPosition);
91
98
    frg_source.add_const("LightSourceHalfVector", halfVector);
92
99
 
93
 
    if (!Scene::load_shaders_from_strings(mProgram, vtx_source.str(),
 
100
    if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
94
101
                                          frg_source.str()))
95
102
    {
96
 
        return;
 
103
        return false;
97
104
    }
98
105
 
99
106
    std::vector<GLint> attrib_locations;
100
 
    attrib_locations.push_back(mProgram["position"].location());
101
 
    attrib_locations.push_back(mProgram["normal"].location());
102
 
    mMesh.set_attrib_locations(attrib_locations);
 
107
    attrib_locations.push_back(program_["position"].location());
 
108
    attrib_locations.push_back(program_["normal"].location());
 
109
    mesh_.set_attrib_locations(attrib_locations);
 
110
 
 
111
    return true;
103
112
}
104
113
 
105
 
void
 
114
bool
106
115
SceneBump::setup_model_normals()
107
116
{
108
117
    static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals.vert");
110
119
    static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
111
120
    Model model;
112
121
 
113
 
    if(!model.load_3ds(GLMARK_DATA_PATH"/models/asteroid-low.3ds"))
114
 
        return;
 
122
    if(!model.load("asteroid-low"))
 
123
        return false;
115
124
 
116
125
    /* Calculate the half vector */
117
126
    LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z());
119
128
    halfVector += LibMatrix::vec3(0.0, 0.0, 1.0);
120
129
    halfVector.normalize();
121
130
 
122
 
    /* 
 
131
    /*
123
132
     * We don't care about the vertex normals. We are using a per-fragment
124
133
     * normal map (in object space coordinates).
125
134
     */
127
136
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypePosition, 3));
128
137
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeTexcoord, 2));
129
138
 
130
 
    model.convert_to_mesh(mMesh, attribs);
131
 
 
132
 
    /* Load shaders */
133
 
    ShaderSource vtx_source(vtx_shader_filename);
134
 
    ShaderSource frg_source(frg_shader_filename);
135
 
 
136
 
    /* Add constants to shaders */
137
 
    frg_source.add_const("LightSourcePosition", lightPosition);
138
 
    frg_source.add_const("LightSourceHalfVector", halfVector);
139
 
 
140
 
    if (!Scene::load_shaders_from_strings(mProgram, vtx_source.str(),
141
 
                                          frg_source.str()))
142
 
    {
143
 
        return;
144
 
    }
145
 
 
146
 
    std::vector<GLint> attrib_locations;
147
 
    attrib_locations.push_back(mProgram["position"].location());
148
 
    attrib_locations.push_back(mProgram["texcoord"].location());
149
 
    mMesh.set_attrib_locations(attrib_locations);
150
 
 
151
 
    Texture::load(GLMARK_DATA_PATH"/textures/asteroid-normal-map.png", &mTexture,
152
 
                  GL_NEAREST, GL_NEAREST, 0);
153
 
}
154
 
 
155
 
void SceneBump::setup()
156
 
{
157
 
    Scene::setup();
158
 
 
159
 
    const std::string &bump_render = mOptions["bump-render"].value;
 
139
    model.convert_to_mesh(mesh_, attribs);
 
140
 
 
141
    /* Load shaders */
 
142
    ShaderSource vtx_source(vtx_shader_filename);
 
143
    ShaderSource frg_source(frg_shader_filename);
 
144
 
 
145
    /* Add constants to shaders */
 
146
    frg_source.add_const("LightSourcePosition", lightPosition);
 
147
    frg_source.add_const("LightSourceHalfVector", halfVector);
 
148
 
 
149
    if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
 
150
                                          frg_source.str()))
 
151
    {
 
152
        return false;
 
153
    }
 
154
 
 
155
    std::vector<GLint> attrib_locations;
 
156
    attrib_locations.push_back(program_["position"].location());
 
157
    attrib_locations.push_back(program_["texcoord"].location());
 
158
    mesh_.set_attrib_locations(attrib_locations);
 
159
 
 
160
    if (!Texture::load("asteroid-normal-map", &texture_,
 
161
                       GL_NEAREST, GL_NEAREST, 0))
 
162
    {
 
163
        return false;
 
164
    }
 
165
 
 
166
    return true;
 
167
}
 
168
 
 
169
bool
 
170
SceneBump::setup_model_normals_tangent()
 
171
{
 
172
    static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals-tangent.vert");
 
173
    static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-normals-tangent.frag");
 
174
    static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
 
175
    Model model;
 
176
 
 
177
    if(!model.load("asteroid-low"))
 
178
        return false;
 
179
 
 
180
    model.calculate_normals();
 
181
 
 
182
    /* Calculate the half vector */
 
183
    LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z());
 
184
    halfVector.normalize();
 
185
    halfVector += LibMatrix::vec3(0.0, 0.0, 1.0);
 
186
    halfVector.normalize();
 
187
 
 
188
    std::vector<std::pair<Model::AttribType, int> > attribs;
 
189
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypePosition, 3));
 
190
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeNormal, 3));
 
191
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeTexcoord, 2));
 
192
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeTangent, 3));
 
193
 
 
194
    model.convert_to_mesh(mesh_, attribs);
 
195
 
 
196
    /* Load shaders */
 
197
    ShaderSource vtx_source(vtx_shader_filename);
 
198
    ShaderSource frg_source(frg_shader_filename);
 
199
 
 
200
    /* Add constants to shaders */
 
201
    frg_source.add_const("LightSourcePosition", lightPosition);
 
202
    frg_source.add_const("LightSourceHalfVector", halfVector);
 
203
 
 
204
    if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
 
205
                                          frg_source.str()))
 
206
    {
 
207
        return false;
 
208
    }
 
209
 
 
210
    std::vector<GLint> attrib_locations;
 
211
    attrib_locations.push_back(program_["position"].location());
 
212
    attrib_locations.push_back(program_["normal"].location());
 
213
    attrib_locations.push_back(program_["texcoord"].location());
 
214
    attrib_locations.push_back(program_["tangent"].location());
 
215
    mesh_.set_attrib_locations(attrib_locations);
 
216
 
 
217
    if (!Texture::load("asteroid-normal-map-tangent", &texture_,
 
218
                       GL_NEAREST, GL_NEAREST, 0))
 
219
    {
 
220
        return false;
 
221
    }
 
222
 
 
223
    return true;
 
224
}
 
225
 
 
226
bool
 
227
SceneBump::setup_model_height()
 
228
{
 
229
    static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/bump-height.vert");
 
230
    static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/bump-height.frag");
 
231
    static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
 
232
    Model model;
 
233
 
 
234
    if(!model.load("asteroid-low"))
 
235
        return false;
 
236
 
 
237
    model.calculate_normals();
 
238
 
 
239
    /* Calculate the half vector */
 
240
    LibMatrix::vec3 halfVector(lightPosition.x(), lightPosition.y(), lightPosition.z());
 
241
    halfVector.normalize();
 
242
    halfVector += LibMatrix::vec3(0.0, 0.0, 1.0);
 
243
    halfVector.normalize();
 
244
 
 
245
    std::vector<std::pair<Model::AttribType, int> > attribs;
 
246
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypePosition, 3));
 
247
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeNormal, 3));
 
248
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeTexcoord, 2));
 
249
    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeTangent, 3));
 
250
 
 
251
    model.convert_to_mesh(mesh_, attribs);
 
252
 
 
253
    /* Load shaders */
 
254
    ShaderSource vtx_source(vtx_shader_filename);
 
255
    ShaderSource frg_source(frg_shader_filename);
 
256
 
 
257
    /* Add constants to shaders */
 
258
    frg_source.add_const("LightSourcePosition", lightPosition);
 
259
    frg_source.add_const("LightSourceHalfVector", halfVector);
 
260
    frg_source.add_const("TextureStepX", 1.0 / 1024.0);
 
261
    frg_source.add_const("TextureStepY", 1.0 / 1024.0);
 
262
 
 
263
    if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
 
264
                                          frg_source.str()))
 
265
    {
 
266
        return false;
 
267
    }
 
268
 
 
269
    std::vector<GLint> attrib_locations;
 
270
    attrib_locations.push_back(program_["position"].location());
 
271
    attrib_locations.push_back(program_["normal"].location());
 
272
    attrib_locations.push_back(program_["texcoord"].location());
 
273
    attrib_locations.push_back(program_["tangent"].location());
 
274
    mesh_.set_attrib_locations(attrib_locations);
 
275
 
 
276
    if (!Texture::load("asteroid-height-map", &texture_,
 
277
                       GL_NEAREST, GL_NEAREST, 0))
 
278
    {
 
279
        return false;
 
280
    }
 
281
 
 
282
    return true;
 
283
}
 
284
 
 
285
bool
 
286
SceneBump::setup()
 
287
{
 
288
    if (!Scene::setup())
 
289
        return false;
 
290
 
 
291
    const std::string &bump_render = options_["bump-render"].value;
 
292
    Texture::find_textures();
 
293
    Model::find_models();
 
294
 
 
295
    bool setup_succeeded = false;
160
296
 
161
297
    if (bump_render == "normals")
162
 
        setup_model_normals();
 
298
        setup_succeeded = setup_model_normals();
 
299
    else if (bump_render == "normals-tangent")
 
300
        setup_succeeded = setup_model_normals_tangent();
 
301
    else if (bump_render == "height")
 
302
        setup_succeeded = setup_model_height();
163
303
    else if (bump_render == "off" || bump_render == "high-poly")
164
 
        setup_model_plain(bump_render);
165
 
 
166
 
 
167
 
    mMesh.build_vbo();
168
 
 
169
 
    mProgram.start();
 
304
        setup_succeeded = setup_model_plain(bump_render);
 
305
 
 
306
    if (!setup_succeeded)
 
307
        return false;
 
308
 
 
309
    mesh_.build_vbo();
 
310
 
 
311
    program_.start();
170
312
 
171
313
    // Load texture sampler value
172
 
    mProgram["NormalMap"] = 0;
173
 
 
174
 
    mCurrentFrame = 0;
175
 
    mRotation = 0.0;
176
 
    mRunning = true;
177
 
    mStartTime = Scene::get_timestamp_us() / 1000000.0;
178
 
    mLastUpdateTime = mStartTime;
 
314
    program_["NormalMap"] = 0;
 
315
    program_["HeightMap"] = 0;
 
316
 
 
317
    currentFrame_ = 0;
 
318
    rotation_ = 0.0;
 
319
    running_ = true;
 
320
    startTime_ = Util::get_timestamp_us() / 1000000.0;
 
321
    lastUpdateTime_ = startTime_;
 
322
 
 
323
    return true;
179
324
}
180
325
 
181
326
void
182
327
SceneBump::teardown()
183
328
{
184
 
    mMesh.reset();
185
 
 
186
 
    mProgram.stop();
187
 
    mProgram.release();
188
 
 
189
 
    glDeleteTextures(1, &mTexture);
190
 
    mTexture = 0;
 
329
    mesh_.reset();
 
330
 
 
331
    program_.stop();
 
332
    program_.release();
 
333
 
 
334
    glDeleteTextures(1, &texture_);
 
335
    texture_ = 0;
191
336
 
192
337
    Scene::teardown();
193
338
}
194
339
 
195
 
void SceneBump::update()
 
340
void
 
341
SceneBump::update()
196
342
{
197
 
    double current_time = Scene::get_timestamp_us() / 1000000.0;
198
 
    double dt = current_time - mLastUpdateTime;
199
 
    double elapsed_time = current_time - mStartTime;
200
 
 
201
 
    mLastUpdateTime = current_time;
202
 
 
203
 
    if (elapsed_time >= mDuration) {
204
 
        mAverageFPS = mCurrentFrame / elapsed_time;
205
 
        mRunning = false;
206
 
    }
207
 
 
208
 
    mRotation += mRotationSpeed * dt;
209
 
 
210
 
    mCurrentFrame++;
 
343
    Scene::update();
 
344
 
 
345
    double elapsed_time = lastUpdateTime_ - startTime_;
 
346
 
 
347
    rotation_ = rotationSpeed_ * elapsed_time;
211
348
}
212
349
 
213
 
void SceneBump::draw()
 
350
void
 
351
SceneBump::draw()
214
352
{
215
353
    LibMatrix::Stack4 model_view;
216
354
 
217
355
    // Load the ModelViewProjectionMatrix uniform in the shader
218
 
    LibMatrix::mat4 model_view_proj(mCanvas.projection());
 
356
    LibMatrix::mat4 model_view_proj(canvas_.projection());
219
357
 
220
358
    model_view.translate(0.0f, 0.0f, -3.5f);
221
 
    model_view.rotate(mRotation, 0.0f, 1.0f, 0.0f);
 
359
    model_view.rotate(rotation_, 0.0f, 1.0f, 0.0f);
222
360
    model_view_proj *= model_view.getCurrent();
223
361
 
224
 
    mProgram["ModelViewProjectionMatrix"] = model_view_proj;
 
362
    program_["ModelViewProjectionMatrix"] = model_view_proj;
225
363
 
226
364
    // Load the NormalMatrix uniform in the shader. The NormalMatrix is the
227
365
    // inverse transpose of the model view matrix.
228
366
    LibMatrix::mat4 normal_matrix(model_view.getCurrent());
229
367
    normal_matrix.inverse().transpose();
230
 
    mProgram["NormalMatrix"] = normal_matrix;
231
 
 
232
 
    mMesh.render_vbo();
 
368
    program_["NormalMatrix"] = normal_matrix;
 
369
 
 
370
    glActiveTexture(GL_TEXTURE0);
 
371
    glBindTexture(GL_TEXTURE_2D, texture_);
 
372
 
 
373
    mesh_.render_vbo();
233
374
}
234
375
 
235
376
Scene::ValidationResult
237
378
{
238
379
    static const double radius_3d(std::sqrt(3.0));
239
380
 
240
 
    if (mRotation != 0) 
 
381
    if (rotation_ != 0)
241
382
        return Scene::ValidationUnknown;
242
383
 
243
384
    Canvas::Pixel ref;
244
385
 
245
 
    Canvas::Pixel pixel = mCanvas.read_pixel(mCanvas.width() / 2,
246
 
                                             mCanvas.height() / 2);
 
386
    Canvas::Pixel pixel = canvas_.read_pixel(canvas_.width() / 2,
 
387
                                             canvas_.height() / 2);
247
388
 
248
 
    const std::string &bump_render = mOptions["bump-render"].value;
 
389
    const std::string &bump_render = options_["bump-render"].value;
249
390
 
250
391
    if (bump_render == "off")
251
392
        ref = Canvas::Pixel(0x81, 0x81, 0x81, 0xff);
253
394
        ref = Canvas::Pixel(0x9c, 0x9c, 0x9c, 0xff);
254
395
    else if (bump_render == "normals")
255
396
        ref = Canvas::Pixel(0xa4, 0xa4, 0xa4, 0xff);
 
397
    else if (bump_render == "normals-tangent")
 
398
        ref = Canvas::Pixel(0x99, 0x99, 0x99, 0xff);
 
399
    else if (bump_render == "height")
 
400
        ref = Canvas::Pixel(0x9d, 0x9d, 0x9d, 0xff);
256
401
    else
257
402
        return Scene::ValidationUnknown;
258
403
 
259
 
    double dist = pixel_value_distance(pixel, ref);
 
404
    double dist = pixel.distance_rgb(ref);
260
405
 
261
406
    if (dist < radius_3d + 0.01) {
262
407
        return Scene::ValidationSuccess;