Skip to content

Commit

Permalink
added cool DOF
Browse files Browse the repository at this point in the history
  • Loading branch information
russellxie7 committed Dec 11, 2017
1 parent bb83a85 commit 07cbc6c
Show file tree
Hide file tree
Showing 18 changed files with 3,053 additions and 96 deletions.
8 changes: 7 additions & 1 deletion CSE167StarterCodeFinal-master/CameraTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ CameraTexture::CameraTexture(int priority) {
GL_TEXTURE_2D, depthBuf);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, texture, 0);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}


void CameraTexture::resize() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Window::width, Window::height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}

void CameraTexture::takeSnapShot(glm::vec3 camPos, glm::vec3 camLookAt,
glm::vec3 camUp, glm::vec4 clipPlane) {
// store window parameter
Expand Down
1 change: 1 addition & 0 deletions CSE167StarterCodeFinal-master/CameraTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct CameraTexture {
CameraTexture(int);

void takeSnapShot(glm::vec3, glm::vec3, glm::vec3, glm::vec4);
void resize();
};

#endif
Expand Down
228 changes: 228 additions & 0 deletions CSE167StarterCodeFinal-master/DofEffect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// This is the final version
/**
Usage: include "DofEffect.h" at the top of your Window.cpp, then, make sure you have shader.h:
1. Declaration: copy this:
#define SCENE_VERTEX_SHADER_PATH "../Shaders/bokeh.vert"
#define SCENE_FRAGMENT_SHADER_PATH "../Shaders/bokeh.frag"
GLint screenShaderProgram;
bool isDof = true;
DofEffect* dof_effect;
2. in your initialization: copy this:
screenShaderProgram = LoadShaders(SCENE_VERTEX_SHADER_PATH, SCENE_FRAGMENT_SHADER_PATH);
dof_effect = new DofEffect(screenShaderProgram);
3. in your display_callback or draw function,
3.1. in the beginning of where you want to post process, most likely in the first line copy:
if (isDof) {
dof_effect->bindFrameBuffer();
}
3.2. in the end, most likely right before swapbuffers copy:
// func(bokeh_shader, aperture, focus, maxBlur)
if (isDof) {
dof_effect->dof_post_processing(screenShaderProgram);
}
4. at your key callback:
if (key == GLFW_KEY_P) {
isDof = !isDof;
}
if (key == GLFW_KEY_U) {
dof_effect->increase_focus();
}
if (key == GLFW_KEY_I) {
dof_effect->decrease_focus();
}
*/

#include "DofEffect.h"
#include "Window.h"

#include <iostream>


using namespace std;
using namespace glm;

GLuint frameBuffer;
GLuint colormap;
GLuint depthmap;
GLuint dofVAO, dofVBO;

// Quad vertices
GLfloat quadVertices[] = {
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,

1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};

// need to be called after the framebuffer has been created
void createColorDepthMap() {
glGenTextures(1, &depthmap);
glGenTextures(1, &colormap);

// DEPTH
glBindTexture(GL_TEXTURE_2D, depthmap);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, Window::width, Window::height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthmap, 0
);

// COLOR
glBindTexture(GL_TEXTURE_2D, colormap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, Window::width, Window::height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL
);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colormap, 0
);


}


void createFrameBuffer() {
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
}

DofEffect::DofEffect(GLuint screenShaderProgram){

aperture = 2.0f;
focus = 1.0f;
max_blur = 1.0f;

// create the frame buffer and depth buffer color buffer
glGenVertexArrays(1, &dofVAO);
glGenBuffers(1, &dofVBO);

glBindVertexArray(dofVAO);
glBindBuffer(GL_ARRAY_BUFFER, dofVBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);

GLint posAttrib = glGetAttribLocation(screenShaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);

GLint texAttrib = glGetAttribLocation(screenShaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

// create them
createFrameBuffer();
createColorDepthMap();

// check if it is completed
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (err == GL_FRAMEBUFFER_COMPLETE) {
cout << "frame buffer is completed" << endl;
}

}

DofEffect::~DofEffect(){}

void DOF_Postprocessing(GLuint screenShaderProgram, float aperture, float focus, float maxBlur) {

glUseProgram(screenShaderProgram);
glBindVertexArray(dofVAO);

glUniform1f(glGetUniformLocation(screenShaderProgram, "maxBlur"), maxBlur);
glUniform1f(glGetUniformLocation(screenShaderProgram, "aperture"), aperture);
glUniform1f(glGetUniformLocation(screenShaderProgram, "focus"), focus);
glUniform1f(glGetUniformLocation(screenShaderProgram, "aspect"), Window::width / (float)Window::height);

//cout << "the sizes are " << Window::width << " and " << Window::height << " and " << Window::width / (float)Window::height << endl;

// bindColormap
glUniform1i(glGetUniformLocation(screenShaderProgram, "texture"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colormap);

// bind ddepthmap
glUniform1i(glGetUniformLocation(screenShaderProgram, "tDepth"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthmap);

glDrawArrays(GL_TRIANGLES, 0, 6);

glBindVertexArray(0);

}

void DofEffect::bindFrameBuffer() {
// use the customized framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
}

void DofEffect::dof_post_processing(GLuint screenShaderProgram) {
// unbind the framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// DOF post processing
DOF_Postprocessing(screenShaderProgram, aperture, focus, max_blur);
}

// controls
void DofEffect::increase_focus() {
// cap to 1
float temp = focus + 0.001f;

focus = temp;
cout << "focus is " << focus << endl;
}

void DofEffect::decrease_focus() {
// cap to 1
float temp = focus - 0.001f;
focus = temp;

cout << "focus is " << focus << endl;
}

void DofEffect::reset_focus() {
focus = 1.0f;
}

void DofEffect::increase_aperture() {
aperture += 0.1f;

cout << "aperture is " << aperture << endl;
}

void DofEffect::decrease_aperture() {
aperture -= 0.1f;
cout << "aperture is " << aperture << endl;
}



53 changes: 53 additions & 0 deletions CSE167StarterCodeFinal-master/DofEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef _DOF_EFFECT_
#define _DOF_EFFECT_

#ifdef __APPLE__
#define GLFW_INCLUDE_GLCOREARB
#else
#include <GL/glew.h>
#endif

#include <GLFW/glfw3.h>
#include "glm/ext.hpp"

#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/quaternion.hpp>

#include <math.h>
#include <cstdint>
#include <algorithm>
#include <iomanip>
#include <random>


class DofEffect{

public:
DofEffect(GLuint);
~DofEffect();

/// need to be called at the front of the thing that you want to post processing
void bindFrameBuffer();
/// better be called before swapping buffer
void dof_post_processing(GLuint screenShaderProgram);

// controls
void increase_aperture();
void decrease_aperture();
void reset_aperture();

void increase_focus();
void decrease_focus();
void reset_focus();

void increase_blur();
void decrease_blur();
void reset_blur();


float aperture, focus, max_blur;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,40 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Cube.h" />
<ClInclude Include="..\Curve.h" />
<ClInclude Include="..\Group.h" />
<ClInclude Include="..\Light.h" />
<ClInclude Include="..\CameraTexture.h" />
<ClInclude Include="..\DofEffect.h" />
<ClInclude Include="..\LowPolyOBJ.h" />
<ClInclude Include="..\LowPolyPt.h" />
<ClInclude Include="..\LowPolyWater.h" />
<ClInclude Include="..\main.h" />
<ClInclude Include="..\Material.h" />
<ClInclude Include="..\Node.h" />
<ClInclude Include="..\OBJObject.h" />
<ClInclude Include="..\shader.h" />
<ClInclude Include="..\SkyboxColorGen.h" />
<ClInclude Include="..\SphereGen.h" />
<ClInclude Include="..\stb_image.h" />
<ClInclude Include="..\Transform.h" />
<ClInclude Include="..\TerrainColorGen.h" />
<ClInclude Include="..\TerrainGen.h" />
<ClInclude Include="..\Window.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Cube.cpp" />
<ClCompile Include="..\Curve.cpp" />
<ClCompile Include="..\Group.cpp" />
<ClCompile Include="..\Light.cpp" />
<ClCompile Include="..\CameraTexture.cpp" />
<ClCompile Include="..\DofEffect.cpp" />
<ClCompile Include="..\LowPolyOBJ.cpp" />
<ClCompile Include="..\LowPolyPt.cpp" />
<ClCompile Include="..\LowPolyWater.cpp" />
<ClCompile Include="..\main.cpp" />
<ClCompile Include="..\Material.cpp" />
<ClCompile Include="..\OBJObject.cpp" />
<ClCompile Include="..\shader.cpp" />
<ClCompile Include="..\Transform.cpp" />
<ClCompile Include="..\SkyboxColorGen.cpp" />
<ClCompile Include="..\SphereGen.cpp" />
<ClCompile Include="..\Terrain.cpp" />
<ClCompile Include="..\TerrainColorGen.cpp" />
<ClCompile Include="..\TerrainGen.cpp" />
<ClCompile Include="..\Window.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="..\Shaders\anchorShader.frag" />
<None Include="..\Shaders\anchorShader.vert" />
<None Include="..\Shaders\controlShader.frag" />
<None Include="..\Shaders\controlShader.vert" />
<None Include="..\Shaders\curveShader.frag" />
<None Include="..\Shaders\curveShader.vert" />
<None Include="..\Shaders\reflectionShader.frag" />
<None Include="..\Shaders\reflectionShader.vert" />
<None Include="..\Shaders\selectionShader.frag" />
<None Include="..\Shaders\selectionShader.vert" />
<None Include="..\Shaders\skyboxShader.frag" />
<None Include="..\Shaders\skyboxShader.vert" />
<None Include="..\Shaders\tangentShader.frag" />
<None Include="..\Shaders\tangentShader.vert" />
<None Include="..\LowPoly.frag" />
<None Include="..\LowPoly.vert" />
<None Include="..\Water.frag" />
<None Include="..\Water.vert" />
<None Include="packages.config" />
</ItemGroup>
<PropertyGroup Label="Globals">
Expand Down
Loading

0 comments on commit 07cbc6c

Please sign in to comment.