summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian Park <SebPark03@gmail.com>2024-05-09 23:18:46 -0400
committerSebastian Park <SebPark03@gmail.com>2024-05-09 23:18:46 -0400
commitf9819ae7f8b11b298c16eb14d929a9c09038a877 (patch)
treecef7f0c104d49d41a1e2c76bec2343f875ca8dff /src
parentac3f1e423e696392ea0a50c2652b829e5c5734b6 (diff)
parent99c588d4da2ca044df8276b7efbd9d27c19c8352 (diff)
Merge branch 'shaders'
Diffstat (limited to 'src')
-rw-r--r--src/arap.cpp123
-rw-r--r--src/arap.h8
-rwxr-xr-xsrc/glwidget.cpp60
-rwxr-xr-xsrc/glwidget.h7
4 files changed, 146 insertions, 52 deletions
diff --git a/src/arap.cpp b/src/arap.cpp
index 6239f83..b6f2f8c 100644
--- a/src/arap.cpp
+++ b/src/arap.cpp
@@ -66,41 +66,7 @@ void ARAP::init
// minCorner = Vector3f(-1.0f, -1.0f, -1.0f);
// maxCorner = Vector3f(1.0f, 1.0f, 1.0f);
-
-// m_shape.initGroundPlane("cornell_box_full_lighting.png")
-// QImage ocean_floor_image;
-// GLuint ocean_floor_texture;
-// // Prepare filepath
-// QString ocean_floor_filepath = QString(":/resources/images/kitten.png");
-
-// // TASK 1: Obtain image from filepath
-// ocean_floor_image = QImage(ocean_floor_filepath);
-
-// // TASK 2: Format image to fit OpenGL
-// ocean_floor_image = ocean_floor_image.convertToFormat(QImage::Format_RGBA8888).mirrored();
-
-// // TASK 3: Generate kitten texture
-// glGenTextures(1, &ocean_floor_texture);
-
-// // TASK 9: Set the active texture slot to texture slot 0
-// glActiveTexture(GL_TEXTURE0);
-
-// // TASK 4: Bind kitten texture
-// glBindTexture(GL_TEXTURE_2D, ocean_floor_texture);
-
-// // TASK 5: Load image into kitten texture
-// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ocean_floor_image.width(), ocean_floor_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, ocean_floor_image.bits());
-
-// // TASK 6: Set min and mag filters' interpolation mode to linear
-// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
-// // TASK 7: Unbind kitten texture
-// glBindTexture(GL_TEXTURE_2D, 0);
-
-// // TASK 10: set the texture.frag uniform for our texture
-// glUseProgram(m_texture_shader);
-// glUniform1i(glGetUniformLocation(m_texture_shader, "sampler"), 0);
+ initCausticsShape(10);
}
void ARAP::update(double seconds)
@@ -117,27 +83,27 @@ void ARAP::update(double seconds)
// Note that the "seconds" parameter represents the amount of time that has passed since
// the last update
m_ocean.fft_prime(m_time);
- m_ocean.update_ocean();
- m_shape.setVertices_and_Normals(m_ocean.get_vertices(), m_ocean.getNormals());
- // m_shape.setVertices(m_ocean.get_vertices());
+ m_ocean.update_ocean();
+// m_shape.setVertices_and_Normals(m_ocean.get_vertices(), m_ocean.getNormals());
+ m_shape.setVertices(m_ocean.get_vertices());
- auto tmp = m_ocean.get_vertices();
+// auto tmp = m_ocean.get_vertices();
// print the min and max of the vertices
- Vector3f min = Vector3f::Ones() * 1000000;
- Vector3f max = Vector3f::Ones() * -1000000;
- for (int i = 0; i < tmp.size(); i++) {
- min = min.cwiseMin(tmp[i]);
- max = max.cwiseMax(tmp[i]);
- }
- std::cout << "min: " << min << std::endl;
-std::cout << "max: " << max << std::endl;
+// Vector3f min = Vector3f::Ones() * 1000000;
+// Vector3f max = Vector3f::Ones() * -1000000;
+// for (int i = 0; i < tmp.size(); i++) {
+// min = min.cwiseMin(tmp[i]);
+// max = max.cwiseMax(tmp[i]);
+// }
+// std::cout << "min: " << min << std::endl;
+//std::cout << "max: " << max << std::endl;
FoamConstants foam = m_ocean.getFoamConstants();
m_foam_shape.setFoamInputs(m_shape.getVertices(), foam.wavelengths, foam.k_vectors, foam.texCoords);
m_time += m_timestep;
- // std::cout << m_time << std::endl;
+// std::cout << m_time << std::endl;
}
// Move an anchored vertex, defined by its index, to targetPosition
@@ -162,3 +128,64 @@ void ARAP::move
// - Minus and equal keys (click repeatedly) to change the size of the vertices
}
+void ARAP::initCausticsShape(int res) {
+// std::vector<Eigen::Vector3f> gridPoints;
+// float step = 2.f / ((float) res);
+
+// for (int i = 0; i < res; ++i) {
+// for (int j = 0; j < res; ++j) {
+// float x = -1.f + i * step; // calculate x coordinate
+// float y = -1.f + j * step; // calculate y coordinate
+// gridPoints.push_back(Eigen::Vector3f(x, y, 0.f)); // add point to grid
+// }
+// }
+// std::vector<Eigen::Vector3f> verts;
+// float step = 2.f / ((float) res);
+
+// for (int i = 0; i < res; ++i) {
+// for (int j = 0; j < res; ++j) {
+// float x = -1.f + i * step; // calculate x coordinate
+// float y = -1.f + j * step; // calculate y coordinate
+// Eigen::Vector3f bottomLeft = Eigen::Vector3f(x, y, 0.f);
+// Eigen::Vector3f bottomRight = Eigen::Vector3f(x + step, y, 0.f);
+// Eigen::Vector3f topRight = Eigen::Vector3f(x + step, y + step, 0.f);
+// Eigen::Vector3f topLeft = Eigen::Vector3f(x, y + step, 0.f);
+// verts.push_back(topLeft);
+// verts.push_back(bottomLeft);
+// verts.push_back(bottomRight);
+// verts.push_back(topLeft);
+// verts.push_back(bottomRight);
+// verts.push_back(topRight);
+// }
+// }
+// m_causticsShape.setVertices(verts);
+ std::vector<Eigen::Vector3f> verts;
+ std::vector<Eigen::Vector3i> faces;
+ float size = 2.f;
+ float step = size / ((float) res);
+
+ for (int i = 0; i <= res; ++i) {
+ for (int j = 0; j <= res; ++j) {
+ float x = -(size / 2.f) + i * step; // calculate x coordinate
+ float y = -(size / 2.f) + j * step; // calculate y coordinate
+ Eigen::Vector3f bottomLeft = Eigen::Vector3f(x, y, 0.f);
+ verts.push_back(bottomLeft);
+ }
+ }
+
+ for (int i = 0; i < res; ++i) {
+ for (int j = 0; j < res; ++j) {
+ int bottomLeft = i * (res + 1) + j;
+ int bottomRight = i * (res + 1) + j + 1;
+ int topRight = (i + 1) * (res + 1) + j + 1;
+ int topLeft = (i + 1) * (res + 1) + j;
+ faces.push_back(Eigen::Vector3i(topLeft, bottomLeft, bottomRight));
+ faces.push_back(Eigen::Vector3i(topLeft, bottomRight, topRight));
+ faces.push_back(Eigen::Vector3i(bottomRight, bottomLeft, topLeft));
+ faces.push_back(Eigen::Vector3i(topRight, bottomRight, topLeft));
+ }
+ }
+ m_causticsShape.init(verts, faces);
+ m_causticsShape.setColor(0.27f, .803f, .96f);
+}
+
diff --git a/src/arap.h b/src/arap.h
index e5529d9..39b2076 100644
--- a/src/arap.h
+++ b/src/arap.h
@@ -1,6 +1,7 @@
#pragma once
#include "graphics/shape.h"
+//#include "graphics/simpleshape.h"
//#include "graphics/oceanshape.h"
#include "Eigen/StdList"
#include "Eigen/StdVector"
@@ -22,6 +23,8 @@ private:
Shape m_foam_shape;
+// OceanShape m_oceanShape;
+ void initCausticsShape(int res);
public:
ARAP();
@@ -39,8 +42,7 @@ public:
void draw(Shader *shader, GLenum mode)
{
-
-
+// m_causticsShape.draw(shader, mode);
m_shape.draw(shader, mode);
}
@@ -99,5 +101,7 @@ public:
double m_timestep = 0.3;
Eigen::Vector3f minCorner, maxCorner;
+
+ Shape m_causticsShape;
};
diff --git a/src/glwidget.cpp b/src/glwidget.cpp
index 91d38bc..47df4f5 100755
--- a/src/glwidget.cpp
+++ b/src/glwidget.cpp
@@ -110,6 +110,7 @@ void GLWidget::initializeGL()
m_foam_tex = loadTextureFromFile(":resources/images/foam3.png").textureID;
+ m_causticsShader = new Shader(":resources/shaders/caustics.vert", ":resources/shaders/caustics.frag");
initCaustics();
@@ -138,11 +139,12 @@ void GLWidget::initializeGL()
m_devicePixelRatio = this->devicePixelRatio();
- m_defaultFBO = 2;
+ m_defaultFBO = 3;
m_fbo_width = size().width() * m_devicePixelRatio;
m_fbo_height = size().height() * m_devicePixelRatio;
makeFBO();
+ makeFBO1();
// FBO STUFF END
@@ -209,7 +211,7 @@ void GLWidget::paintCaustics() {
glClearColor(0.68f, 0.58f, 0.38f, 1);
// glClearColor(0., 0., 0., 1);
- glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
+ glBindFramebuffer(GL_FRAMEBUFFER, m_fbo1);
// glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO);
// Clear Screen
@@ -227,6 +229,7 @@ void GLWidget::paintCaustics() {
//// // Draw the VAO
// glDrawArrays(GL_TRIANGLES, 0, 3);
+
m_colorShader->bind();
//
m_colorShader->setUniform("proj", m_camera.getProjection());
@@ -243,6 +246,24 @@ void GLWidget::paintCaustics() {
// Unbind the shader
glUseProgram(0);
+
+ m_causticsShader->bind();
+
+// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glBindFramebuffer(GL_FRAMEBUFFER, m_fbo_texture);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable( GL_BLEND );
+ glDisable( GL_DEPTH_TEST );
+
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, m_fbo_texture1);
+ glUniform1i(glGetUniformLocation(m_causticsShader ->id(), "normSamp"), 2);
+
+ m_arap.m_causticsShape.draw(m_causticsShader, GL_TRIANGLES);
+ glBindVertexArray(0);
+ glUseProgram(0);
}
void GLWidget::initCaustics() {
@@ -345,7 +366,37 @@ void GLWidget::makeFBO() {
// Task 22
glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO);
+}
+void GLWidget::makeFBO1() {
+ // Task 19
+ glActiveTexture(GL_TEXTURE2);
+ glGenTextures(1, &m_fbo_texture1);
+ glBindTexture(GL_TEXTURE_2D, m_fbo_texture1);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_fbo_width, m_fbo_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ // Task 20
+ glGenRenderbuffers(1, &m_fbo_renderbuffer1);
+ glBindRenderbuffer(GL_RENDERBUFFER, m_fbo_renderbuffer1);
+ glRenderbufferStorage(GL_RENDERBUFFER,
+ GL_DEPTH24_STENCIL8,
+ m_fbo_width,
+ m_fbo_height);
+ glBindRenderbuffer(GL_RENDERBUFFER, 0);
+
+ // Task 18
+ glGenFramebuffers(1, &m_fbo1);
+ glBindFramebuffer(GL_FRAMEBUFFER, m_fbo1);
+
+ // Task 21
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fbo_texture1, 0);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_fbo_renderbuffer1);
+
+ // Task 22
+ glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO);
}
void GLWidget::paintGL()
@@ -358,6 +409,7 @@ void GLWidget::paintGL()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
+ glEnable(GL_DEPTH_TEST);
m_defaultShader->bind();
m_defaultShader->setUniform("proj", m_camera.getProjection());
@@ -517,7 +569,11 @@ void GLWidget::resizeGL(int w, int h)
glDeleteTextures(1, &m_fbo_texture);
glDeleteRenderbuffers(1, &m_fbo_renderbuffer);
glDeleteFramebuffers(1, &m_fbo);
+ glDeleteTextures(1, &m_fbo_texture1);
+ glDeleteRenderbuffers(1, &m_fbo_renderbuffer1);
+ glDeleteFramebuffers(1, &m_fbo1);
makeFBO();
+ makeFBO1();
}
// ================== Event Listeners
diff --git a/src/glwidget.h b/src/glwidget.h
index 006f027..d992ab6 100755
--- a/src/glwidget.h
+++ b/src/glwidget.h
@@ -47,6 +47,7 @@ private:
void keyReleaseEvent (QKeyEvent *event) override;
void makeFBO();
+ void makeFBO1();
void initCaustics();
void paintCaustics();
@@ -65,6 +66,7 @@ private:
Shader *m_defaultShader;
Shader *m_pointShader;
Shader *m_texture_shader;
+ Shader *m_causticsShader;
Shader *m_colorShader;
Shader *m_foamShader;
@@ -84,6 +86,11 @@ private:
GLuint m_fbo;
GLuint m_fbo_texture;
GLuint m_fbo_renderbuffer;
+
+ GLuint m_fbo1;
+ GLuint m_fbo_texture1;
+ GLuint m_fbo_renderbuffer1;
+
GLuint m_defaultFBO;
GLuint m_floor_vbo;