diff options
Diffstat (limited to 'src/glwidget.cpp')
-rwxr-xr-x | src/glwidget.cpp | 156 |
1 files changed, 153 insertions, 3 deletions
diff --git a/src/glwidget.cpp b/src/glwidget.cpp index a8d8dee..f5fc941 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -3,6 +3,9 @@ #include <QApplication> #include <QKeyEvent> #include <iostream> +#define STB_IMAGE_IMPLEMENTATION +#include "stb/stb_image.h" + #define SPEED 1.5 #define ROTATE_SPEED 0.0025 @@ -77,6 +80,9 @@ void GLWidget::initializeGL() m_defaultShader = new Shader(":resources/shaders/shader.vert", ":resources/shaders/shader.frag"); m_pointShader = new Shader(":resources/shaders/anchorPoint.vert", ":resources/shaders/anchorPoint.geom", ":resources/shaders/anchorPoint.frag"); + // initialize texture + m_texture0 = loadTextureFromFile("/Users/jesswan/Desktop/cs2240/ocean-simulation/resources/images/hello.png").textureID; + // Initialize ARAP, and get parameters needed to decide the camera position, etc Vector3f coeffMin, coeffMax; m_arap.init(coeffMin, coeffMax); @@ -109,7 +115,18 @@ void GLWidget::initializeGL() m_deltaTimeProvider.start(); m_intervalTimer.start(1000 / 60); - m_arap.initGroundPlane(":resources/images/kitty.png", 2, m_defaultShader); + //m_arap.initGroundPlane(":resources/images/kitty.png", 2, m_defaultShader); + + //INIT FBO + m_devicePixelRatio = this->devicePixelRatio(); + + m_defaultFBO = 2; + m_screen_width = size().width() * m_devicePixelRatio; + m_screen_height = size().height() * m_devicePixelRatio; + m_fbo_width = m_screen_width; + m_fbo_height = m_screen_height; + + initFullScreenQuad(); } void GLWidget::paintGL() @@ -125,13 +142,31 @@ void GLWidget::paintGL() m_defaultShader->setUniform("inverseView", inverseView); m_defaultShader->setUniform("widthBounds", m_arap.minCorner[0], m_arap.maxCorner[0]); m_defaultShader->setUniform("lengthBounds", m_arap.minCorner[2], m_arap.maxCorner[2]); + + // bind texture + m_defaultShader->setUniform("texture_img", 0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_texture0); + + // m_defaultShader->setUniform(""); m_arap.draw(m_defaultShader, GL_TRIANGLES); m_defaultShader->unbind(); - glBindTexture(GL_TEXTURE_2D, 0); + //glBindTexture(GL_TEXTURE_2D, 0); + +// glClear(GL_DEPTH_BUFFER_BIT); + + +// glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); +// // Task 25: Bind the default framebuffer +// glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO); +// glViewport(0, 0, m_screen_width, m_screen_height); - glClear(GL_DEPTH_BUFFER_BIT); + // Task 26: Clear the color and depth buffers + + // Task 27: Call paintTexture to draw our FBO color attachment texture | Task 31: Set bool parameter to true + paintTexture(m_texture0, true); // m_pointShader->bind(); // m_pointShader->setUniform("proj", m_camera.getProjection()); @@ -143,6 +178,121 @@ void GLWidget::paintGL() // m_pointShader->unbind(); } +void GLWidget::paintTexture(GLuint texture, bool postProcessOn){ + m_defaultShader->bind(); + + // Task 32: Set your bool uniform on whether or not to filter the texture drawn + glBindVertexArray(m_fullscreen_vao); + // Task 10: Bind "texture" to slot 0 + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture); + + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindTexture(GL_TEXTURE_2D, 0); + glBindVertexArray(0); + m_defaultShader->unbind(); +} + +void GLWidget::initFullScreenQuad(){ + // Generate and bind a VBO and a VAO for a fullscreen quad + glGenBuffers(1, &m_fullscreen_vbo); + glBindBuffer(GL_ARRAY_BUFFER, m_fullscreen_vbo); + glBufferData(GL_ARRAY_BUFFER, fullscreen_quad_data.size()*sizeof(GLfloat), fullscreen_quad_data.data(), GL_STATIC_DRAW); + glGenVertexArrays(1, &m_fullscreen_vao); + glBindVertexArray(m_fullscreen_vao); + + // Task 14: modify the code below to add a second attribute to the vertex attribute array + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast<void *>(0*sizeof(GLfloat))); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast<void *>(3*sizeof(GLfloat))); + + // Unbind the fullscreen quad's VBO and VAO + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + makeFBO(); +} + +void GLWidget::makeFBO(){ + // Task 19: Generate and bind an empty texture, set its min/mag filter interpolation, then unbind + glGenTextures(1, &m_fbo_texture); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fbo_texture); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_fbo_width, m_fbo_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + + // 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); + + glBindTexture(GL_TEXTURE_2D, 0); + + // Task 20: Generate and bind a renderbuffer of the right size, set its format, then unbind + glGenRenderbuffers(1, &m_fbo_renderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, m_fbo_renderbuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, m_fbo_width, m_fbo_height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + // Task 18: Generate and bind an FBO + glGenFramebuffers(1, &m_fbo); + glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); + + // Task 21: Add our texture as a color attachment, and our renderbuffer as a depth+stencil attachment, to our FBO + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fbo_texture, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_fbo_renderbuffer); + + // Task 22: Unbind the FBO + glBindFramebuffer(GL_FRAMEBUFFER, m_defaultFBO); + +} + +TextureData GLWidget::loadTextureFromFile(const char *path) +{ + std::string filename = std::string(path); + + GLuint textureID; + glGenTextures(1, &textureID); + + int width, height, nrComponents; + stbi_set_flip_vertically_on_load(true); + unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0); + stbi_set_flip_vertically_on_load(false); + if (data) + { + GLenum format; + if (nrComponents == 1) + format = GL_RED; + else if (nrComponents == 3) + format = GL_RGB; + else if (nrComponents == 4) + format = GL_RGBA; + + glBindTexture(GL_TEXTURE_2D, textureID); + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + stbi_image_free(data); + } + else + { + std::cout << "Texture failed to load at path: " << path << std::endl; + stbi_image_free(data); + } + + TextureData newtex; + newtex.textureID = textureID; + newtex.height = height; + newtex.width = width; + return newtex; +} + void GLWidget::resizeGL(int w, int h) { glViewport(0, 0, w, h); |