@@ -7,7 +7,7 @@ const char* window_title = "CSE 167 Final Project";
7
7
int tr_counter = 0 ;
8
8
9
9
// Default camera parameters
10
- glm::vec3 cam_pos (0 .0f , 100 .0f , -70 .0f ); // e | Position of camera
10
+ glm::vec3 cam_pos (0 .0f , 0 .0f , -70 .0f ); // e | Position of camera
11
11
glm::vec3 cam_look_at (0 .0f , 0 .0f , 0 .0f ); // d | This is where the camera looks at
12
12
glm::vec3 cam_up (0 .0f , 1 .0f , 0 .0f ); // up | What orientation "up" is
13
13
@@ -69,6 +69,10 @@ glm::mat4 water_m(1.0f);
69
69
70
70
// FBOS
71
71
FBO * waterFBO;
72
+ // location of textures in shader
73
+ GLuint loc_reflection, loc_refraction;
74
+
75
+ Water* water2;
72
76
73
77
void Window::initialize_objects ()
74
78
{
@@ -95,12 +99,19 @@ void Window::initialize_objects()
95
99
tr = new Terrain (shaderProgram, 2000 , 1600 , 10 );
96
100
tr->update ();
97
101
102
+ // Create the water FBO
98
103
waterFBO = new FBO ();
99
-
104
+ glUseProgram (waterProgram);
100
105
water = new Water (waterProgram);
101
- water->createFrameBuffer ();
102
- water->getLocations ();
103
- water->connectTex ();
106
+ water2 = new Water (waterProgram);
107
+ // water->createFrameBuffer();
108
+ // water->getLocations();
109
+ // water->connectTex();
110
+ // Get the locations
111
+ loc_reflection = glGetUniformLocation (waterProgram, " reflectionTex" );
112
+ loc_refraction = glGetUniformLocation (waterProgram, " refractionTex" );
113
+
114
+
104
115
105
116
// For terrain
106
117
trn *= glm::rotate (glm::mat4 (1 .0f ), glm::pi<float >()/180 .0f * 90 , glm::vec3 (1.0 , 0 , 0 ));
@@ -194,71 +205,116 @@ void Window::resize_callback(GLFWwindow * window, int width, int height)
194
205
}
195
206
}
196
207
208
+ void Window::invertPitch (){
209
+ float pitch = glm::asin (cam_look_at.y );
210
+ float yaw = glm::acos (cam_look_at.x /glm::cos (pitch));
211
+ // std::cout << pitch << " : P\n";
212
+ // std::cout << yaw << " : Y\n";
213
+ float invertPitch = -pitch;
214
+
215
+ cam_look_at.x = glm::cos (invertPitch)*glm::cos (yaw);
216
+ cam_look_at.y = glm::sin (invertPitch);
217
+ cam_look_at.z = glm::cos (invertPitch)*glm::sin (yaw);
218
+ }
219
+
197
220
void Window::drawSkybox (){
198
221
// Skybox
199
222
glUseProgram (skyShaderProgram);
223
+ // V = glm::mat4(glm::mat3(Window::V));
200
224
glEnable (GL_CULL_FACE);
201
- glCullFace (GL_BACK);
225
+ // glCullFace(GL_BACK);
202
226
glFrontFace (GL_CW);
203
227
glDepthMask (GL_FALSE);
204
228
glEnable (GL_TEXTURE_CUBE_MAP_SEAMLESS);
205
229
glBindVertexArray (skybox->getVAO ());
230
+ glActiveTexture (GL_TEXTURE0);
231
+ glUniform1i (glGetUniformLocation (skyShaderProgram, " skybox" ), 0 );
206
232
glBindTexture (GL_TEXTURE_CUBE_MAP, cubeMapTexture);
207
233
skybox->draw (skyShaderProgram);
208
- glBindVertexArray (0 );
209
- glBindTexture (GL_TEXTURE_CUBE_MAP, 0 );
210
- glDepthMask (GL_TRUE);
211
- // Set things back to normal
212
- glFrontFace (GL_CCW);
213
- glCullFace (GL_BACK);
214
- glDepthMask (GL_TRUE);
234
+ // glBindVertexArray(0);
235
+ // glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
236
+ // glDepthMask(GL_TRUE);
237
+ // Set things back to normal
238
+ // glFrontFace(GL_CCW);
239
+ // glCullFace(GL_BACK);
240
+ // glDepthMask(GL_TRUE);
241
+ glDisable (GL_CULL_FACE);
215
242
}
216
243
217
244
void Window::drawObjects (){
245
+ drawSkybox ();
246
+
247
+ glUseProgram (shaderProgram);
218
248
tr->draw (trn);
219
249
cube->draw (glm::mat4 (1 .0f ));
220
250
}
221
251
void Window::drawReflection (){
222
252
// Render everything above water (reflection)
223
253
glUniform4f (clipPlaneN, 0 .0f ,1 .0f ,0 .0f ,-140 .0f );
224
- // gotta move camera down 2*distance_to_water 70 - (-140) = 210 *2 = 420 ayyy
225
- float distance = 2 *(cam_pos. y + 140 );
254
+ // gotta move camera down 2*distance_to_water - 70 - (-140) = 70 *2 = 210 ayyy
255
+ float distance = 2 *(70 );
226
256
cam_pos.y -= distance;
227
- water->bindFrameBuffer (water->getReflectionFBO (), Window::width, Window::height);
257
+ invertPitch ();
258
+
259
+ waterFBO->bind (waterFBO->getReflectionFBO ());
260
+
261
+ // Clear colors
262
+ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228
263
// Draw things here <<<<<<<<
229
264
drawObjects ();
230
-
265
+
231
266
// Move camera back
267
+ invertPitch ();
232
268
cam_pos.y += distance;
269
+
270
+ waterFBO->unbind ();
271
+
233
272
}
234
273
void Window::drawRefraction (){
235
- glUniform4f (clipPlaneW, 0 .0f , -1 .0f , 0 .0f , 140 .0f );
236
- water->bindFrameBuffer (water->getRefractionFBO (), width, height);
274
+ glUniform4f (clipPlaneW, 0 .0f , -1 .0f , 0 .0f , -140 .0f );
275
+ waterFBO->bind (waterFBO->getRefractionFBO ());
276
+ // Clear colors
277
+ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
237
278
// Draw things here <<<<<<<<
238
279
drawObjects ();
280
+
239
281
}
240
282
241
283
void Window::drawAll (){
242
- glUniform4f (clipPlaneN, 0 .0f , 0 .0f ,0 .0f ,- 140 .0f );
284
+ glUniform4f (clipPlaneN, 0 .0f , 0 .0f ,0 .0f , 0 .0f );
243
285
glUseProgram (waterProgram);
244
- glUniform4f (clipPlaneW, 0 .0f , 0 .0f , 0 .0f , 140 .0f );
245
- water->unbindFrameBuffer ();
246
- glUseProgram (shaderProgram);
286
+ glUniform4f (clipPlaneW, 0 .0f , 0 .0f , 0 .0f , 0 .0f );
287
+ waterFBO->unbind ();
247
288
248
289
// Draw things here <<<<<<<<
249
290
drawObjects ();
291
+
250
292
}
251
293
252
294
void Window::drawWater (){
253
295
glUseProgram (waterProgram);
296
+
297
+ // Connect the texture units
298
+ glUniform1i (loc_reflection, 4 ); // texunit 4 REFLECTION
299
+ glUniform1i (loc_refraction, 5 ); // texunit 5 REFRADTION
300
+
301
+ // Bind vertex array for the water
302
+ glBindVertexArray (water->getVAO ());
303
+ glEnableVertexAttribArray (0 );
304
+ glEnable (GL_TEXTURE_2D);
305
+ glActiveTexture (GL_TEXTURE4);
306
+ glBindTexture (GL_TEXTURE_2D, waterFBO->getReflTex ());
307
+ glActiveTexture (GL_TEXTURE5);
308
+ glBindTexture (GL_TEXTURE_2D, waterFBO->getRefrTex ());
309
+
254
310
glDisable (GL_CULL_FACE);
255
311
water->draw (water_m);
256
312
}
257
313
258
314
void Window::display_callback (GLFWwindow * window)
259
315
{
260
316
tr_counter += 1 ;
261
-
317
+
262
318
// Clear the color and depth buffers
263
319
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
264
320
// Set the matrix mode to GL_MODELVIEW
@@ -270,7 +326,7 @@ void Window::display_callback(GLFWwindow * window)
270
326
tr->update ();
271
327
// Draw reflecions
272
328
drawReflection ();
273
-
329
+
274
330
// Render everything below water (refraction)
275
331
drawRefraction ();
276
332
@@ -279,7 +335,7 @@ void Window::display_callback(GLFWwindow * window)
279
335
280
336
// Draw the water
281
337
drawWater ();
282
-
338
+
283
339
// Gets events, including input such as keyboard and mouse or window resizinh
284
340
glfwPollEvents ();
285
341
// Swap buffers
@@ -306,7 +362,7 @@ void Window::scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
306
362
307
363
} else {
308
364
// pos y offset is to zoom in
309
- cam_pos = cam_pos - glm::vec3 (0 ,0 ,0.5 );
365
+ cam_pos = cam_pos - glm::vec3 (0 ,0 ,- 0.5 );
310
366
V = glm::lookAt (cam_pos, cam_look_at, cam_up);
311
367
}
312
368
}
0 commit comments