Skip to content

Commit 1871295

Browse files
committed
REFLECTIONS WORK OH BABY
1 parent fbe738e commit 1871295

16 files changed

+145
-54
lines changed

Fbo.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,34 @@ void FBO::bind(GLuint framebuffer){
102102
glBindTexture(GL_TEXTURE_2D, 0);
103103
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
104104
glViewport(0, 0, Window::width, Window::height);
105+
106+
GLenum drawBuffs = GL_COLOR_ATTACHMENT0;
107+
glDrawBuffers(1, &drawBuffs);
105108
}
106109

107110
void FBO::unbind(){
108111
glBindFramebuffer(GL_FRAMEBUFFER, 0);
109112
glViewport(0, 0, Window::width, Window::height);
110113
}
114+
GLuint FBO::getReflectionFBO(){
115+
return fboRefl;
116+
}
117+
GLuint FBO::getRefractionFBO(){
118+
return fboRefr;
119+
}
120+
121+
GLuint FBO::getReflTex(){
122+
return reflTex;
123+
}
124+
125+
GLuint FBO::getRefrTex(){
126+
return refrTex;
127+
}
128+
129+
GLuint FBO::getRefrDepth(){
130+
return refrDepthTexture;
131+
}
132+
133+
GLuint FBO::getReflDepth(){
134+
return reflDepthBuffer;
135+
}

Fbo.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ class FBO{
2424
GLuint createDepth();
2525
GLuint createDepthBuff();
2626
void checkErrors(GLuint fbo);
27-
27+
GLuint getReflectionFBO();
28+
GLuint getRefractionFBO();
29+
GLuint getReflTex();
30+
GLuint getRefrTex();
31+
GLuint getReflDepth();
32+
GLuint getRefrDepth();
2833

2934
private:
3035
GLuint fboRefl;

SkyBox.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ void SkyBox::draw(GLuint shaderProgram)
7575
// Now draw the SkyBox. We simply need to bind the VAO associated with it.
7676
glBindVertexArray(VAO);
7777

78-
glActiveTexture(GL_TEXTURE0);
79-
glUniform1i(glGetUniformLocation(shaderProgram, "skybox"), 0);
80-
// glBindTexture(GL_TEXTURE_SkyBox_MAP, SkyBoxMapTexture);
78+
// glBindTexture(GL_TEXTURE_SkyBox_MAP, SkyBoxMapTexture);
8179

8280
// Tell OpenGL to draw with triangles, using 36 indices, the type of the indices, and the offset to start from
8381
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);

SkyBox.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class SkyBox
4747
// This just looks nicer since it's easy to tell what coordinates/indices belong where.
4848
const GLfloat vertices[8][3] = {
4949
// "Front" vertices
50-
{-300.0, -300.0, 300.0}, {300.0, -300.0, 300.0}, {300.0, 300.0, 300.0}, {-300.0, 300.0, 300.0},
50+
{-450.0f, -450.0f, 450.0f}, {450.0f, -450.0f, 450.0f}, {450.0f, 450.0f, 450.0f}, {-450.0f, 450.0f, 450.0f},
5151
// "Back" vertices
52-
{-300.0, -300.0, -300.0}, {300.0, -300.0, -300.0}, {300.0, 300.0, -300.0}, {-300.0, 300.0, -300.0}
52+
{-450.0f, -450.0f, -450.0f}, {450.0f, -450.0f, -450.0f}, {450.0f, 450.0f, -450.0f}, {-450.0f, 450.0f, -450.0f}
5353
};
5454

5555
// Note that GL_QUADS is deprecated in modern OpenGL (and removed from OSX systems).

Terrain.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Terrain::Terrain(GLuint shader, int w, int h, int scl){
2020

2121
glGenVertexArrays(1, &VAO);
2222
glGenBuffers(1, &VBO);
23-
glGenBuffers(1, &EBO);
2423

2524
// Bind VAO
2625
glBindVertexArray(VAO);
@@ -45,7 +44,6 @@ Terrain::Terrain(GLuint shader, const char* heightmap, int scl){
4544

4645
glGenVertexArrays(1, &VAO);
4746
glGenBuffers(1, &VBO);
48-
glGenBuffers(1, &EBO);
4947

5048
glBindVertexArray(VAO);
5149

Water.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ void Water::draw(glm::mat4 C){
6060
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
6161

6262
glBindVertexArray(0);
63-
64-
6563
}
6664

6765
void Water::createFrameBuffer(){
@@ -148,3 +146,7 @@ void Water::connectTex(){
148146
}
149147

150148
void Water::update(){}
149+
150+
GLuint Water::getVAO(){
151+
return VAO;
152+
}

Water.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Water : public Geode{
2929
void unbindFrameBuffer();
3030
void getLocations();
3131
void connectTex();
32+
GLuint getVAO();
3233

3334
protected:
3435
GLfloat vertices[6][3] = {

Window.cpp

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const char* window_title = "CSE 167 Final Project";
77
int tr_counter = 0;
88

99
// 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
1111
glm::vec3 cam_look_at(0.0f, 0.0f, 0.0f); // d | This is where the camera looks at
1212
glm::vec3 cam_up(0.0f, 1.0f, 0.0f); // up | What orientation "up" is
1313

@@ -69,6 +69,10 @@ glm::mat4 water_m(1.0f);
6969

7070
// FBOS
7171
FBO * waterFBO;
72+
//location of textures in shader
73+
GLuint loc_reflection, loc_refraction;
74+
75+
Water* water2;
7276

7377
void Window::initialize_objects()
7478
{
@@ -95,12 +99,19 @@ void Window::initialize_objects()
9599
tr = new Terrain(shaderProgram, 2000, 1600, 10);
96100
tr->update();
97101

102+
//Create the water FBO
98103
waterFBO = new FBO();
99-
104+
glUseProgram(waterProgram);
100105
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+
104115

105116
//For terrain
106117
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)
194205
}
195206
}
196207

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+
197220
void Window::drawSkybox(){
198221
// Skybox
199222
glUseProgram(skyShaderProgram);
223+
// V = glm::mat4(glm::mat3(Window::V));
200224
glEnable(GL_CULL_FACE);
201-
glCullFace(GL_BACK);
225+
// glCullFace(GL_BACK);
202226
glFrontFace(GL_CW);
203227
glDepthMask(GL_FALSE);
204228
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
205229
glBindVertexArray(skybox->getVAO());
230+
glActiveTexture(GL_TEXTURE0);
231+
glUniform1i(glGetUniformLocation(skyShaderProgram, "skybox"), 0);
206232
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapTexture);
207233
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);
215242
}
216243

217244
void Window::drawObjects(){
245+
drawSkybox();
246+
247+
glUseProgram(shaderProgram);
218248
tr->draw(trn);
219249
cube->draw(glm::mat4(1.0f));
220250
}
221251
void Window::drawReflection(){
222252
//Render everything above water (reflection)
223253
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);
226256
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);
228263
//Draw things here <<<<<<<<
229264
drawObjects();
230-
265+
231266
//Move camera back
267+
invertPitch();
232268
cam_pos.y += distance;
269+
270+
waterFBO->unbind();
271+
233272
}
234273
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);
237278
//Draw things here <<<<<<<<
238279
drawObjects();
280+
239281
}
240282

241283
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);
243285
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();
247288

248289
//Draw things here <<<<<<<<
249290
drawObjects();
291+
250292
}
251293

252294
void Window::drawWater(){
253295
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+
254310
glDisable(GL_CULL_FACE);
255311
water->draw(water_m);
256312
}
257313

258314
void Window::display_callback(GLFWwindow * window)
259315
{
260316
tr_counter += 1;
261-
317+
262318
// Clear the color and depth buffers
263319
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
264320
// Set the matrix mode to GL_MODELVIEW
@@ -270,7 +326,7 @@ void Window::display_callback(GLFWwindow * window)
270326
tr->update();
271327
//Draw reflecions
272328
drawReflection();
273-
329+
274330
//Render everything below water (refraction)
275331
drawRefraction();
276332

@@ -279,7 +335,7 @@ void Window::display_callback(GLFWwindow * window)
279335

280336
//Draw the water
281337
drawWater();
282-
338+
283339
// Gets events, including input such as keyboard and mouse or window resizinh
284340
glfwPollEvents();
285341
// Swap buffers
@@ -306,7 +362,7 @@ void Window::scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
306362

307363
} else {
308364
// 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);
310366
V = glm::lookAt(cam_pos, cam_look_at, cam_up);
311367
}
312368
}

Window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Window
7979
static void drawRefraction();
8080
static void drawAll();
8181
static void drawWater();
82+
static void invertPitch();
8283
};
8384

8485

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void setup_opengl_settings()
7575
// Set clear color
7676
// glClearColor(0.05f, 0.8f, 0.85f, 1.0f);
7777
// glClearColor(1.0f, 0.8f, 0.85f, 1.0f);
78-
glClearColor(1.0, 0.3, 0.5, 1);
78+
glClearColor(1.0, 1.0, 0.5, 1);
7979
}
8080

8181
void print_versions()

0 commit comments

Comments
 (0)