@@ -157,22 +157,29 @@ void OpenGLWidget::init() {
157
157
glUniformBlockBinding (earth_prog, index , 1 );
158
158
159
159
// load textures
160
+ loadTextures (" earth_day" , uniform_texture_location[0 ], " textures/earth_day.jpg" , texture_id[0 ]);
161
+ loadTextures (" earth_water" , uniform_texture_location[1 ], " textures/earth_water.jpg" , texture_id[1 ]);
162
+ }
163
+
164
+ // ------------------------------------------------------------------------------------------------
165
+
166
+ void OpenGLWidget::loadTextures (const char * uniform_name, GLint& tex_location, const char * file, GLuint& id) {
160
167
int w, h;
161
168
int channels;
162
169
unsigned char * image;
163
- const char * filename = " textures/earth_day.jpg" ;
164
170
stbi_set_flip_vertically_on_load (1 );
165
- image = stbi_load (filename , &w, &h, &channels, STBI_rgb);
171
+ image = stbi_load (file , &w, &h, &channels, STBI_rgb);
166
172
167
173
if (image == nullptr ) {
168
- printf (" Failed to load image %s\n " , filename );
174
+ printf (" Failed to load image %s\n " , file );
169
175
assert (false );
170
176
exit (EXIT_FAILURE);
171
177
}
172
178
173
- glGenTextures (1 , &texture_id);
174
- glBindTexture (GL_TEXTURE_2D, texture_id);
175
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
179
+ glGenTextures (1 , &id);
180
+ glBindTexture (GL_TEXTURE_2D, id);
181
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
182
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
176
183
glTexImage2D (GL_TEXTURE_2D, 0 ,
177
184
GL_RGB, // internalformat
178
185
w, h, 0 ,
@@ -181,9 +188,8 @@ void OpenGLWidget::init() {
181
188
stbi_image_free (image);
182
189
183
190
// bind texture uniform
184
- const char * uniform_name = " texture" ; // name of uniform in shader
185
- uniform_texture_location = glGetUniformLocation (earth_prog, uniform_name);
186
- if (uniform_texture_location == -1 ) {
191
+ tex_location = glGetUniformLocation (earth_prog, uniform_name);
192
+ if (tex_location == -1 ) {
187
193
std::cout << " Could not bind uniform " << uniform_name << std::endl;
188
194
}
189
195
}
@@ -193,7 +199,7 @@ void OpenGLWidget::init() {
193
199
void OpenGLWidget::show (const PhysicalInstance& instance, const float t0) {
194
200
visualizeInstance (instance);
195
201
sim_time = t0;
196
- glm::vec3 clear_color = glm::vec3 (0 .15f , 0 . 15f , 0 . 15f );
202
+ glm::vec3 clear_color = glm::vec3 (0 .03f );
197
203
198
204
// Main loop
199
205
while (!glfwWindowShouldClose (window)) {
@@ -221,7 +227,7 @@ void OpenGLWidget::show(const PhysicalInstance& instance, const float t0) {
221
227
222
228
void OpenGLWidget::show (const PhysicalInstance& instance, const Solution& solution) {
223
229
visualizeSolution (instance, solution);
224
- glm::vec3 clear_color = glm::vec3 (0 .15f , 0 . 15f , 0 . 15f );
230
+ glm::vec3 clear_color = glm::vec3 (0 .03f );
225
231
226
232
// Main loop
227
233
while (!glfwWindowShouldClose (window)) {
@@ -250,10 +256,14 @@ void OpenGLWidget::show(const PhysicalInstance& instance, const Solution& soluti
250
256
void OpenGLWidget::renderScene () {
251
257
recalculate ();
252
258
253
- // bind texture
259
+ // bind textures
254
260
glActiveTexture (GL_TEXTURE0);
255
- glUniform1i (uniform_texture_location, /* GL_TEXTURE*/ 0 );
256
- glBindTexture (GL_TEXTURE_2D, texture_id);
261
+ glUniform1i (uniform_texture_location[0 ], /* GL_TEXTURE*/ 0 );
262
+ glBindTexture (GL_TEXTURE_2D, texture_id[0 ]);
263
+
264
+ glActiveTexture (GL_TEXTURE1);
265
+ glUniform1i (uniform_texture_location[1 ], /* GL_TEXTURE*/ 1 );
266
+ glBindTexture (GL_TEXTURE_2D, texture_id[1 ]);
257
267
258
268
// Draw the scene:
259
269
for (const Subscene& s : scene) {
@@ -273,6 +283,10 @@ void OpenGLWidget::recalculate() {
273
283
if (!paused)
274
284
sim_time += diff * sim_speed;
275
285
286
+ // sun rotation
287
+ float sun_angle = sim_time * 0 .000290f ; // 6h -> one turn around the earth
288
+ glm::mat4 sun_rotation = glm::rotate (glm::mat4 (1 .f ), sun_angle, glm::vec3 (0 .f , 1 .f , 0 .f ));
289
+
276
290
/* Camera circumnavigating around the central mass.
277
291
* Instead of performing two rotations for the camera, the rotation around the y-axis is done by rotation the entire
278
292
* world itself. */
@@ -294,15 +308,17 @@ void OpenGLWidget::recalculate() {
294
308
glGetIntegerv (GL_VIEWPORT, viewport);
295
309
projection = glm::perspective (45 .0f , 1 .0f * viewport[2 ] / viewport[3 ], 0 .1f , 10 .0f );
296
310
glm::mat4 scale = glm::scale (glm::vec3 (1 .0f ) * zoom);
297
- glm::mat4 modelview = view * world_rotation * scale ;
298
- glm::mat4 normal_proj = glm::transpose (glm::inverse (modelview));
311
+ glm::mat4 model = world_rotation;
312
+ // glm::mat4 normal_proj = glm::transpose(glm::inverse(modelview));
299
313
300
314
// push mvp to VBO
301
315
glBindBuffer (GL_UNIFORM_BUFFER, vbo_uniforms);
302
- glBufferData (GL_UNIFORM_BUFFER, 3 * sizeof (glm::mat4), 0 , GL_STREAM_DRAW);
303
- glBufferSubData (GL_UNIFORM_BUFFER, 0 , sizeof (glm::mat4), glm::value_ptr (modelview));
304
- glBufferSubData (GL_UNIFORM_BUFFER, sizeof (glm::mat4), sizeof (glm::mat4), glm::value_ptr (projection));
305
- glBufferSubData (GL_UNIFORM_BUFFER, 2 * sizeof (glm::mat4), sizeof (glm::mat4), glm::value_ptr (normal_proj));
316
+ glBufferData (GL_UNIFORM_BUFFER, 5 * sizeof (glm::mat4), 0 , GL_STREAM_DRAW);
317
+ glBufferSubData (GL_UNIFORM_BUFFER, 0 , sizeof (glm::mat4), glm::value_ptr (model));
318
+ glBufferSubData (GL_UNIFORM_BUFFER, sizeof (glm::mat4), sizeof (glm::mat4), glm::value_ptr (view));
319
+ glBufferSubData (GL_UNIFORM_BUFFER, 2 * sizeof (glm::mat4), sizeof (glm::mat4), glm::value_ptr (projection));
320
+ glBufferSubData (GL_UNIFORM_BUFFER, 3 * sizeof (glm::mat4), sizeof (glm::mat4), glm::value_ptr (scale));
321
+ glBufferSubData (GL_UNIFORM_BUFFER, 4 * sizeof (glm::mat4), sizeof (glm::mat4), glm::value_ptr (sun_rotation));
306
322
glBindBuffer (GL_UNIFORM_BUFFER, 0 );
307
323
308
324
// dynamic part of scene
@@ -762,7 +778,9 @@ void OpenGLWidget::destroy() {
762
778
deleteInstance ();
763
779
glDeleteProgram (basic_program);
764
780
glDeleteProgram (satellite_prog);
765
- glDeleteTextures (1 , &texture_id);
781
+ glDeleteTextures (1 , &texture_id[0 ]);
782
+ glDeleteTextures (1 , &texture_id[1 ]);
783
+ glDeleteTextures (1 , &texture_id[2 ]);
766
784
767
785
glfwDestroyWindow (window);
768
786
glfwTerminate ();
0 commit comments