Skip to content

Commit

Permalink
Ready to publish bonu3 & bonus5
Browse files Browse the repository at this point in the history
  • Loading branch information
syby119 committed Oct 22, 2022
1 parent e1c7f7a commit f151b74
Show file tree
Hide file tree
Showing 28 changed files with 625 additions and 413 deletions.
11 changes: 10 additions & 1 deletion projects/bonus3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

project(bonus3)

set(THIRD_PARTY_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/external)
set(SHADER_TARGET_PATH ${CMAKE_BINARY_DIR}/media/shader/bonus3)

include_directories(${THIRD_PARTY_LIBRARY_PATH}/glm)

Expand Down Expand Up @@ -48,6 +49,14 @@ add_executable(bonus3 ${PROJECT_SRC} ${PROJECT_HDR} ${BASE_SRC} ${BASE_HDR} ${PR

source_group("Shader Files" FILES ${PROJECT_SHADERS})

add_custom_command(
TARGET bonus3
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
make_directory ${SHADER_TARGET_PATH}
COMMENT "make directory ${SHADER_TARGET_PATH}"
)

set(first True)
foreach(shader ${PROJECT_SHADERS})
get_filename_component(name ${shader} NAME)
Expand Down
3 changes: 1 addition & 2 deletions projects/bonus3/blend_bloom_map.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ in vec2 screenTexCoord;
uniform sampler2D scene;
uniform sampler2D bloomBlur;

void main()
{
void main() {
vec3 sceneColor = texture(scene, screenTexCoord).rgb;
vec3 bloomColor = texture(bloomBlur, screenTexCoord).rgb;
FragColor = vec4(sceneColor + bloomColor, 1.0);
Expand Down
2 changes: 1 addition & 1 deletion projects/bonus3/extract_bright_color.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ uniform sampler2D sceneMap;
in vec2 screenTexCoord;

void main() {
// TODO
// TODO: extract the bright color with color components greater than 1.0
brightColorMap = vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
10 changes: 5 additions & 5 deletions projects/bonus3/gaussian_blur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ in vec2 screenTexCoord;
uniform sampler2D image;

uniform bool horizontal;
uniform float weight[5] = float[] (0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
uniform float weight[5] = float[] (
0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);

void main()
{
// TODO
FragColor = vec4(texture(image, screenTexCoord).rgb, 1.0);
void main() {
// TODO: perform gaussian blur
FragColor = vec4(texture(image, screenTexCoord).rgb, 1.0);
}
2 changes: 1 addition & 1 deletion projects/bonus3/geometry.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ in vec2 texCoord;

void main() {
gPosition = position;
gNormal = normal;
gNormal = gl_FrontFacing ? normal : -normal;
gAlbedo = vec3(0.95f);
}
2 changes: 1 addition & 1 deletion projects/bonus3/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
#include <cstdlib>

#include "postprocessing.h"
#include "post_processing.h"

Options getOptions(int argc, char* argv[]) {
Options options;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <random>

#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>

#include "postprocessing.h"
#include "post_processing.h"

const std::string ballRelPath = "obj/sphere.obj";
const std::string bunnyRelPath = "obj/bunny.obj";
const std::string cubeRelPath = "obj/cube.obj";
const std::string sphereRelPath = "obj/sphere.obj";

const std::string geometryVsRelPath = "shader/bonus3/geometry.vert";
const std::string geometryFsRelPath = "shader/bonus3/geometry.frag";
Expand Down Expand Up @@ -42,9 +45,11 @@ PostProcessing::PostProcessing(const Options& options) : Application(options) {
_pointLight->kl = 0.0f;
_pointLight->kq = 0.05f;

_ball.reset(new Model(getAssetFullPath(ballRelPath)));

createGround();
_sphere.reset(new Model(getAssetFullPath(sphereRelPath)));

_cube.reset(new Model(getAssetFullPath(cubeRelPath)));
_cube->transform.position = glm::vec3(22.5659f, 25.1945f, 0.0f);
_cube->transform.scale = glm::vec3(50.0f);

_screenQuad.reset(new FullscreenQuad);

Expand All @@ -71,6 +76,10 @@ PostProcessing::~PostProcessing() {
}

void PostProcessing::handleInput() {
if (_input.keyboard.keyStates[GLFW_KEY_ESCAPE] != GLFW_RELEASE) {
glfwSetWindowShouldClose(_window, true);
return;
}
}

void PostProcessing::renderFrame() {
Expand Down Expand Up @@ -146,10 +155,13 @@ void PostProcessing::initSSAOPassResources() {
_ssaoBlurFBO->drawBuffer(GL_COLOR_ATTACHMENT0);
_ssaoBlurFBO->unbind();

std::default_random_engine e;
std::uniform_real_distribution<float> u(0.0f, 1.0f);

for (unsigned int i = 0; i < 64; ++i) {
glm::vec3 sample(randomFloat() * 2.0f - 1.0f, randomFloat() * 2.0f - 1.0f, randomFloat());
glm::vec3 sample(u(e) * 2.0f - 1.0f, u(e) * 2.0f - 1.0f, u(e));
sample = glm::normalize(sample);
sample *= randomFloat();
sample *= u(e);
float scale = float(i) / 64.0f;

// scale samples s.t. they're more aligned to center of kernel
Expand All @@ -161,7 +173,7 @@ void PostProcessing::initSSAOPassResources() {
std::vector<glm::vec3> ssaoNoises;
for (unsigned int i = 0; i < 16; i++) {
// rotate around z-axis (in tangent space)
glm::vec3 noise(randomFloat() * 2.0f - 1.0f, randomFloat() * 2.0f - 1.0f, 0.0f);
glm::vec3 noise(u(e) * 2.0f - 1.0f, u(e) * 2.0f - 1.0f, 0.0f);
ssaoNoises.push_back(noise);
}

Expand All @@ -173,6 +185,7 @@ void PostProcessing::initSSAOPassResources() {
_ssaoNoise->setParamterInt(GL_TEXTURE_WRAP_T, GL_REPEAT);
_ssaoNoise->unbind();

// TODO: modify ssao.frag
_ssaoShader.reset(new GLSLProgram);
_ssaoShader->attachVertexShaderFromFile(getAssetFullPath(quadVsRelPath));
_ssaoShader->attachFragmentShaderFromFile(getAssetFullPath(ssaoFsRelPath));
Expand All @@ -183,6 +196,7 @@ void PostProcessing::initSSAOPassResources() {
_ssaoBlurShader->attachFragmentShaderFromFile(getAssetFullPath(ssaoBlurFsRelPath));
_ssaoBlurShader->link();

// TODO: modify ssao_lighting.frag
_ssaoLightingShader.reset(new GLSLProgram);
_ssaoLightingShader->attachVertexShaderFromFile(getAssetFullPath(quadVsRelPath));
_ssaoLightingShader->attachFragmentShaderFromFile(getAssetFullPath(ssaoLightingFsRelPath));
Expand Down Expand Up @@ -228,11 +242,13 @@ void PostProcessing::initBloomPassResources() {
_lightShader->attachFragmentShaderFromFile(getAssetFullPath(lightFsRelPath));
_lightShader->link();

// TODO: modify extract_bright_color.frag
_brightColorShader.reset(new GLSLProgram);
_brightColorShader->attachVertexShaderFromFile(getAssetFullPath(quadVsRelPath));
_brightColorShader->attachFragmentShaderFromFile(getAssetFullPath(brightColorFsRelPath));
_brightColorShader->link();

// TODO: modify gaussian_blur.frag
_blurShader.reset(new GLSLProgram);
_blurShader->attachVertexShaderFromFile(getAssetFullPath(quadVsRelPath));
_blurShader->attachFragmentShaderFromFile(getAssetFullPath(gaussianBlurFsRelPath));
Expand All @@ -251,25 +267,6 @@ void PostProcessing::initShaders() {
_drawScreenShader->link();
}

void PostProcessing::createGround() {
constexpr float groundWidth = 50.0f;
std::vector<Vertex> vertices(4);
for (size_t i = 0; i < vertices.size(); ++i) {
vertices[i].position.x = (i % 2) ? groundWidth : -groundWidth;
vertices[i].position.y = 0.0f;
vertices[i].position.z = (i > 1) ? groundWidth : -groundWidth;
vertices[i].normal = glm::vec3(0.0f, 1.0f, 0.0f);
vertices[i].texCoord.x = (i % 2) ? 1.0f : 0.0f;
vertices[i].texCoord.y = (i > 1) ? 1.0f : 0.0f;
};

std::vector<uint32_t> indices = {
0, 1, 2, 1, 2, 3
};

_ground.reset(new Model(vertices, indices));
}

void PostProcessing::renderScene() {
glClearColor(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a);
// deferred rendering: geometry pass
Expand All @@ -284,12 +281,9 @@ void PostProcessing::renderScene() {
_gBufferShader->setUniformMat4("model", _bunny->transform.getLocalMatrix());
_bunny->draw();

_gBufferShader->setUniformMat4("model", glm::mat4(1.0f));
_ground->draw();
_gBufferShader->setUniformMat4("model", _cube->transform.getLocalMatrix());
_cube->draw();

_gBufferShader->setUniformMat4("model", glm::translate(glm::mat4(1.0f), glm::vec3(-2.5f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
_ground->draw();
_gBufferFBO->unbind();

// deferred rendering: lighting passes
Expand Down Expand Up @@ -338,6 +332,7 @@ void PostProcessing::renderScene() {
_ssaoResult[0]->unbind();
}

// + bloom pass
_bloomFBO->bind();
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
Expand Down Expand Up @@ -371,7 +366,7 @@ void PostProcessing::renderScene() {
_lightShader->setUniformVec3("lightColor", _pointLight->color);
_lightShader->setUniformFloat("lightIntensity", _pointLight->intensity);

_ball->draw();
_sphere->draw();

_bloomFBO->unbind();

Expand Down Expand Up @@ -400,7 +395,7 @@ void PostProcessing::renderUI() {
if (!ImGui::Begin("Control Panel", nullptr, flags)) {
ImGui::End();
} else {
ImGui::Text("render method");
ImGui::Text("post processing technics");
ImGui::Separator();
ImGui::Checkbox("bloom", &_enableBloom);
ImGui::Checkbox("ssao", &_enableSSAO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "../base/light.h"
#include "../base/framebuffer.h"
#include "../base/fullscreen_quad.h"
#include "random.h"

class PostProcessing : public Application {
public:
Expand All @@ -18,15 +17,15 @@ class PostProcessing : public Application {

private:
std::unique_ptr<Model> _bunny;
std::unique_ptr<Model> _ground;
std::unique_ptr<Model> _cube;

std::unique_ptr<GLSLProgram> _drawScreenShader;
std::unique_ptr<FullscreenQuad> _screenQuad;

std::unique_ptr<Camera> _camera;

std::unique_ptr<PointLight> _pointLight;
std::unique_ptr<Model> _ball;
std::unique_ptr<Model> _sphere;

// deferred rendering: geometry pass resources
std::unique_ptr<Framebuffer> _gBufferFBO;
Expand Down Expand Up @@ -79,8 +78,6 @@ class PostProcessing : public Application {

void initShaders();

void createGround();

void renderScene();

void renderUI();
Expand Down
25 changes: 0 additions & 25 deletions projects/bonus3/random.h

This file was deleted.

2 changes: 1 addition & 1 deletion projects/bonus3/ssao.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ uniform mat4 projection;
in vec2 screenTexCoord;

void main() {
// TODO
// TODO: perform SSAO
ssaoResult = 1.0f;
}
10 changes: 0 additions & 10 deletions projects/bonus3/ssaoDrawLight.vert

This file was deleted.

7 changes: 3 additions & 4 deletions projects/bonus3/ssao_blur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ in vec2 screenTexCoord;
void main() {
vec2 texelSize = 1.0 / vec2(textureSize(ssaoResult, 0));
float result = 0.0;
for (int x = -2; x <= 2; ++x)
{
for (int y = -2; y <= 2; ++y)
{
for (int x = -2; x <= 2; ++x) {
for (int y = -2; y <= 2; ++y) {
vec2 offset = vec2(float(x), float(y)) * texelSize;
result += texture(ssaoResult, screenTexCoord + offset).r;
}
}

blurResult = result / (5.0 * 5.0);
}
9 changes: 0 additions & 9 deletions projects/bonus3/ssao_draw_light.frag

This file was deleted.

3 changes: 1 addition & 2 deletions projects/bonus3/ssao_lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ void main() {
vec3 albedo = texture(gAlbedo, screenTexCoord).rgb;
float occlusion = texture(ssaoResult, screenTexCoord).x;

//TODO

//TODO: perform lambert shading
fragColor = vec4(normal, 1.0f);
}
18 changes: 18 additions & 0 deletions projects/bonus5/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ struct AABB {
);
}

bool intersect(const Ray& ray, const glm::vec3& invDir, const int negDir[3]) const {
const auto& box = *this;
const auto& o = ray.o;
const auto& dir = ray.dir;

float tMinX = (box[negDir[0]].x - o.x) * invDir.x;
float tMaxX = (box[1 - negDir[0]].x - o.x) * invDir.x;
float tMinY = (box[negDir[1]].y - o.y) * invDir.y;
float tMaxY = (box[1 - negDir[1]].y - o.y) * invDir.y;
float tMinZ = (box[negDir[2]].z - o.z) * invDir.z;
float tMaxZ = (box[1 - negDir[2]].z - o.z) * invDir.z;

float tMin = std::max(tMinX, std::max(tMinY, tMinZ));
float tMax = std::min(tMaxX, std::min(tMaxY, tMaxZ));

return tMin < tMax && tMin < ray.tMax && tMax > 0;
}

float surfaceArea() const {
glm::vec3 diagonal = pMax - pMin;
return 2 * (diagonal.x * diagonal.y + diagonal.x * diagonal.z + diagonal.y * diagonal.z);
Expand Down
Loading

0 comments on commit f151b74

Please sign in to comment.