Skip to content

Commit

Permalink
This patch showcases a screen space ambient occlusion
Browse files Browse the repository at this point in the history
implementation.

rework and add imgui
  • Loading branch information
Janos95 committed Aug 24, 2020
1 parent 9db2614 commit d177cb6
Show file tree
Hide file tree
Showing 20 changed files with 1,670 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ option(WITH_PICKING_EXAMPLE "Build Picking example" OFF)
option(WITH_PRIMITIVES_EXAMPLE "Build Primitives example" OFF)
option(WITH_RAYTRACING_EXAMPLE "Build Ray Tracing example" OFF)
option(WITH_SHADOWS_EXAMPLE "Build Shadow Mapping example" OFF)
option(WITH_SSAO_EXAMPLE "Build Screen Space Ambient Occlusion Example" OFF)
option(WITH_TEXT_EXAMPLE "Build Text example (requires some TTF font plugin)" OFF)
cmake_dependent_option(WITH_TEXTUREDTRIANGLE_EXAMPLE "Build TexturedTriangle example (requires some TGA importer plugin)" OFF "NOT MAGNUM_TARGET_GLES" OFF)
option(WITH_TRIANGLE_EXAMPLE "Build Triangle example" ON)
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ if(WITH_SHADOWS_EXAMPLE)
add_subdirectory(shadows)
endif()

if(WITH_SSAO_EXAMPLE)
add_subdirectory(ssao)
endif()

if(WITH_TEXT_EXAMPLE)
add_subdirectory(text)
endif()
Expand Down
2 changes: 2 additions & 0 deletions src/ssao/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/cmake-*
.idea/
Binary file added src/ssao/Armadillo.ply
Binary file not shown.
88 changes: 88 additions & 0 deletions src/ssao/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#
# This file is part of Magnum.
#
# Original authors — credit is appreciated but not required:
#
# 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 —
# Vladimír Vondruš <mosra@centrum.cz>
# 2020 — Janos Meny <janos.meny@gmail.com>
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute
# this software, either in source code form or as a compiled binary, for any
# purpose, commercial or non-commercial, and by any means.
#
# In jurisdictions that recognize copyright laws, the author or authors of
# this software dedicate any and all copyright interest in the software to
# the public domain. We make this dedication for the benefit of the public
# at large and to the detriment of our heirs and successors. We intend this
# dedication to be an overt act of relinquishment in perpetuity of all
# present and future rights to this software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

cmake_minimum_required(VERSION 3.4)

project(MagnumSSAOExample CXX)

# Add module path in case this is project root
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../../modules/" ${CMAKE_MODULE_PATH})
endif()

set(SSAO_EXAMPLE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SSAO_EXAMPLE_INSTALL_DIR ${MAGNUM_DATA_INSTALL_DIR}/examples/ssao)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/configure.h)

find_package(Corrade REQUIRED Main)
find_package(Magnum REQUIRED
GL
MeshTools
Shaders
SceneGraph
Trade
Sdl2Application
DebugTools)
find_package(MagnumIntegration REQUIRED ImGui)

set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON)

corrade_add_resource(SSAO_Rcs Shaders/resources.conf)

add_executable(magnum-ssao WIN32
SsaoExample.cpp
../arcball/ArcBall.cpp
Shaders/DeferredGeometryShader.cpp
Shaders/DeferredGeometryShader.h
Shaders/SsaoShader.cpp
Shaders/SsaoShader.h
Shaders/SsaoApplyShader.cpp
Shaders/SsaoApplyShader.h
${SSAO_Rcs})

target_link_libraries(magnum-ssao PRIVATE
Corrade::Main
Magnum::Application
Magnum::GL
Magnum::Magnum
Magnum::MeshTools
Magnum::Trade
Magnum::Shaders
Magnum::DebugTools
MagnumIntegration::ImGui)

target_include_directories(magnum-ssao PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

install(TARGETS magnum-ssao DESTINATION ${MAGNUM_BINARY_INSTALL_DIR})
install(FILES Armadillo.ply DESTINATION ${MAGNUM_DATA_INSTALL_DIR}/examples/ssao)

# Make the executable a default target to build & run in Visual Studio
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT magnum-ssao)
47 changes: 47 additions & 0 deletions src/ssao/Shaders/DeferredGeometry.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 —
Vladimír Vondruš <mosra@centrum.cz>
2020 — Janos Meny <janos.meny@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

in highp vec3 viewPosition;
in highp vec3 viewNormal;

layout(location = 0)
out highp vec4 color;
layout(location = 1)
out highp vec3 position;
layout(location = 2)
out highp vec3 normal;

uniform vec4 diffuseColor;

void main() {
color = diffuseColor;
position = viewPosition;
normal = viewNormal;
}
49 changes: 49 additions & 0 deletions src/ssao/Shaders/DeferredGeometry.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 —
Vladimír Vondruš <mosra@centrum.cz>
2020 — Janos Meny <janos.meny@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

in highp vec4 position;
layout(location = 5) //@TODO get rid of layout specifier
in highp vec3 normal;

uniform mat4 transformation;
uniform mat3 normalMatrix;
uniform mat4 projection;

out highp vec3 viewPosition;
out highp vec3 viewNormal;

void main(){
vec4 position4 = transformation*position;

viewPosition = position4.xyz;
viewNormal = normalMatrix*normal;

gl_Position = projection*position4;
}
96 changes: 96 additions & 0 deletions src/ssao/Shaders/DeferredGeometryShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 —
Vladimír Vondruš <mosra@centrum.cz>
2020 — Janos Meny <janos.meny@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "DeferredGeometryShader.h"

#include <Corrade/Utility/Resource.h>
#include <Corrade/Containers/Reference.h>

#include <Magnum/GL/Shader.h>
#include <Magnum/GL/Version.h>
#include <Magnum/GL/Context.h>
#include <Magnum/Math/Matrix4.h>
#include <Magnum/Math/Matrix3.h>
#include <Magnum/Math/Color.h>

namespace Magnum { namespace Examples {

DeferredGeometryShader::DeferredGeometryShader() {
MAGNUM_ASSERT_GL_VERSION_SUPPORTED(GL::Version::GL330);

const Utility::Resource rs{"ssao-data"};

GL::Shader vert{GL::Version::GL330, GL::Shader::Type::Vertex};
GL::Shader frag{GL::Version::GL330, GL::Shader::Type::Fragment};

vert.addSource(rs.get("DeferredGeometry.vert"));

frag.addSource(rs.get("DeferredGeometry.frag"));

CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag}));

attachShaders({vert, frag});

CORRADE_INTERNAL_ASSERT_OUTPUT(link());

bindAttributeLocation(Position::Location, "position");
bindAttributeLocation(Normal::Location, "normal");

bindFragmentDataLocation(AlbedoOutput, "color");
bindFragmentDataLocation(PositionsOutput, "position");
bindFragmentDataLocation(NormalsOutput, "normal");

_transformationUniform = uniformLocation("transformation");
_normalMatrixUniform = uniformLocation("normalMatrix");
_projectionUniform = uniformLocation("projection");
_diffuseColorUniform = uniformLocation("diffuseColor");
}

DeferredGeometryShader& DeferredGeometryShader::setTransformation(const Matrix4& transformation){
setUniform(_transformationUniform, transformation);
return *this;
}

DeferredGeometryShader& DeferredGeometryShader::setNormalMatrix(const Matrix3x3& normalMatrix){
setUniform(_normalMatrixUniform, normalMatrix);
return *this;
}

DeferredGeometryShader& DeferredGeometryShader::setProjection(const Matrix4& projection) {
setUniform(_projectionUniform, projection);
return *this;
}

DeferredGeometryShader& DeferredGeometryShader::setDiffuseColor(const Color4 &color) {
setUniform(_diffuseColorUniform, color);
return *this;
}

}}
75 changes: 75 additions & 0 deletions src/ssao/Shaders/DeferredGeometryShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef Magnum_Examples_Ssao_DeferredGeometryShader_h
#define Magnum_Examples_Ssao_DeferredGeometryShader_h

/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 —
Vladimír Vondruš <mosra@centrum.cz>
2020 — Janos Meny <janos.meny@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <Magnum/GL/AbstractShaderProgram.h>
#include <Magnum/Shaders/Generic.h>
#include <Magnum/GL/GL.h>

namespace Magnum { namespace Examples {

class DeferredGeometryShader : public Magnum::GL::AbstractShaderProgram {
public:

using Position = Magnum::Shaders::Generic3D::Position;
using Normal = Magnum::Shaders::Generic3D::Normal;
using TextureCoordinates = Magnum::Shaders::Generic3D::TextureCoordinates;

enum {
AlbedoOutput = 0,
PositionsOutput = 1,
NormalsOutput = 2,
};

explicit DeferredGeometryShader(Magnum::NoCreateT) : Magnum::GL::AbstractShaderProgram{Magnum::NoCreate} {};

explicit DeferredGeometryShader();

DeferredGeometryShader& setTransformation(const Matrix4& transformation);

DeferredGeometryShader& setNormalMatrix(const Matrix3x3& normalMatrix);

DeferredGeometryShader& setProjection(const Matrix4& projection);

DeferredGeometryShader& setDiffuseColor(const Color4& color);

private:

Int _transformationUniform,
_normalMatrixUniform,
_projectionUniform,
_diffuseColorUniform;
};

}}

#endif
Loading

0 comments on commit d177cb6

Please sign in to comment.