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));
130
model.convert_to_mesh(mMesh, attribs);
133
ShaderSource vtx_source(vtx_shader_filename);
134
ShaderSource frg_source(frg_shader_filename);
136
/* Add constants to shaders */
137
frg_source.add_const("LightSourcePosition", lightPosition);
138
frg_source.add_const("LightSourceHalfVector", halfVector);
140
if (!Scene::load_shaders_from_strings(mProgram, vtx_source.str(),
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);
151
Texture::load(GLMARK_DATA_PATH"/textures/asteroid-normal-map.png", &mTexture,
152
GL_NEAREST, GL_NEAREST, 0);
155
void SceneBump::setup()
159
const std::string &bump_render = mOptions["bump-render"].value;
139
model.convert_to_mesh(mesh_, attribs);
142
ShaderSource vtx_source(vtx_shader_filename);
143
ShaderSource frg_source(frg_shader_filename);
145
/* Add constants to shaders */
146
frg_source.add_const("LightSourcePosition", lightPosition);
147
frg_source.add_const("LightSourceHalfVector", halfVector);
149
if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
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);
160
if (!Texture::load("asteroid-normal-map", &texture_,
161
GL_NEAREST, GL_NEAREST, 0))
170
SceneBump::setup_model_normals_tangent()
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);
177
if(!model.load("asteroid-low"))
180
model.calculate_normals();
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();
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));
194
model.convert_to_mesh(mesh_, attribs);
197
ShaderSource vtx_source(vtx_shader_filename);
198
ShaderSource frg_source(frg_shader_filename);
200
/* Add constants to shaders */
201
frg_source.add_const("LightSourcePosition", lightPosition);
202
frg_source.add_const("LightSourceHalfVector", halfVector);
204
if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
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);
217
if (!Texture::load("asteroid-normal-map-tangent", &texture_,
218
GL_NEAREST, GL_NEAREST, 0))
227
SceneBump::setup_model_height()
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);
234
if(!model.load("asteroid-low"))
237
model.calculate_normals();
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();
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));
251
model.convert_to_mesh(mesh_, attribs);
254
ShaderSource vtx_source(vtx_shader_filename);
255
ShaderSource frg_source(frg_shader_filename);
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);
263
if (!Scene::load_shaders_from_strings(program_, vtx_source.str(),
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);
276
if (!Texture::load("asteroid-height-map", &texture_,
277
GL_NEAREST, GL_NEAREST, 0))
291
const std::string &bump_render = options_["bump-render"].value;
292
Texture::find_textures();
293
Model::find_models();
295
bool setup_succeeded = false;
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);
304
setup_succeeded = setup_model_plain(bump_render);
306
if (!setup_succeeded)
171
313
// Load texture sampler value
172
mProgram["NormalMap"] = 0;
177
mStartTime = Scene::get_timestamp_us() / 1000000.0;
178
mLastUpdateTime = mStartTime;
314
program_["NormalMap"] = 0;
315
program_["HeightMap"] = 0;
320
startTime_ = Util::get_timestamp_us() / 1000000.0;
321
lastUpdateTime_ = startTime_;
182
327
SceneBump::teardown()
189
glDeleteTextures(1, &mTexture);
334
glDeleteTextures(1, &texture_);
192
337
Scene::teardown();
195
void SceneBump::update()
197
double current_time = Scene::get_timestamp_us() / 1000000.0;
198
double dt = current_time - mLastUpdateTime;
199
double elapsed_time = current_time - mStartTime;
201
mLastUpdateTime = current_time;
203
if (elapsed_time >= mDuration) {
204
mAverageFPS = mCurrentFrame / elapsed_time;
208
mRotation += mRotationSpeed * dt;
345
double elapsed_time = lastUpdateTime_ - startTime_;
347
rotation_ = rotationSpeed_ * elapsed_time;
213
void SceneBump::draw()
215
353
LibMatrix::Stack4 model_view;
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());
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();
224
mProgram["ModelViewProjectionMatrix"] = model_view_proj;
362
program_["ModelViewProjectionMatrix"] = model_view_proj;
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;
368
program_["NormalMatrix"] = normal_matrix;
370
glActiveTexture(GL_TEXTURE0);
371
glBindTexture(GL_TEXTURE_2D, texture_);
235
376
Scene::ValidationResult