summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian Park <SebPark03@gmail.com>2024-05-09 22:02:12 -0400
committerSebastian Park <SebPark03@gmail.com>2024-05-09 22:02:12 -0400
commit481e582be02385271f87524bfe46d6c41654e23c (patch)
treeddb9a0b98de7e5b40aeea55464deb75cc753a15d /src
parentf1230ea24dc87a3bb5c9c709d4571e3298d7d07f (diff)
"raytraced" caustics.
Diffstat (limited to 'src')
-rw-r--r--src/arap.cpp63
-rw-r--r--src/arap.h6
-rwxr-xr-xsrc/glwidget.cpp61
-rwxr-xr-xsrc/glwidget.h7
4 files changed, 132 insertions, 5 deletions
diff --git a/src/arap.cpp b/src/arap.cpp
index e1ab1ca..ae7240d 100644
--- a/src/arap.cpp
+++ b/src/arap.cpp
@@ -51,6 +51,8 @@ void ARAP::init
minCorner = coeffMin;
maxCorner = coeffMax;
+
+ initCausticsShape(10);
}
void ARAP::update(double seconds)
@@ -98,3 +100,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 7c8c76a..2c768bb 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"
@@ -20,7 +21,7 @@ class ARAP
private:
Shape m_shape;
// OceanShape m_oceanShape;
-
+ void initCausticsShape(int res);
public:
ARAP();
@@ -38,6 +39,7 @@ public:
void draw(Shader *shader, GLenum mode)
{
+// m_causticsShape.draw(shader, mode);
m_shape.draw(shader, mode);
}
@@ -85,5 +87,7 @@ public:
double m_timestep = 0.1;
Eigen::Vector3f minCorner, maxCorner;
+
+ Shape m_causticsShape;
};
diff --git a/src/glwidget.cpp b/src/glwidget.cpp
index bac695c..62d8482 100755
--- a/src/glwidget.cpp
+++ b/src/glwidget.cpp
@@ -67,7 +67,7 @@ void GLWidget::initializeGL()
glClearColor(1, 0.98f, 0.85f, 1);
// Enable depth-testing and backface culling
- glEnable(GL_DEPTH_TEST);
+// glEnable(GL_DEPTH_TEST);
// glEnable(GL_CULL_FACE);
// glCullFace(GL_BACK);
// glShadeModel(GL_SMOOTH);
@@ -78,7 +78,7 @@ void GLWidget::initializeGL()
m_pointShader = new Shader(":resources/shaders/anchorPoint.vert", ":resources/shaders/anchorPoint.geom", ":resources/shaders/anchorPoint.frag");
// m_texture_shader = new Shader(":/resources/shaders/texture.vert", ":/resources/shaders/texture.frag");
m_colorShader = new Shader(":resources/shaders/color.vert", ":resources/shaders/color.frag");
-
+ m_causticsShader = new Shader(":resources/shaders/caustics.vert", ":resources/shaders/caustics.frag");
initCaustics();
// INITIALIZE TEXTURE STUFF
@@ -103,11 +103,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
@@ -171,7 +172,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
@@ -189,6 +190,7 @@ void GLWidget::paintCaustics() {
//// // Draw the VAO
// glDrawArrays(GL_TRIANGLES, 0, 3);
+
m_colorShader->bind();
//
m_colorShader->setUniform("proj", m_camera.getProjection());
@@ -205,6 +207,23 @@ 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 );
+
+ 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() {
@@ -307,7 +326,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()
@@ -356,7 +405,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 810829a..f77c4ee 100755
--- a/src/glwidget.h
+++ b/src/glwidget.h
@@ -41,6 +41,7 @@ private:
void keyReleaseEvent (QKeyEvent *event) override;
void makeFBO();
+ void makeFBO1();
void initCaustics();
void paintCaustics();
@@ -55,6 +56,7 @@ private:
Shader *m_defaultShader;
Shader *m_pointShader;
Shader *m_texture_shader;
+ Shader *m_causticsShader;
Shader *m_colorShader;
@@ -70,6 +72,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;