Skip to content

Commit

Permalink
Finally got irradiance maps working, needs a big tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronoid committed Mar 31, 2018
1 parent 3418362 commit e900ff0
Show file tree
Hide file tree
Showing 17 changed files with 489 additions and 58 deletions.
7 changes: 5 additions & 2 deletions Criminowl.pro
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
INCLUDEPATH += \
/usr/local/include/glm/glm \
/usr/local/include/glm \
/usr/local/include/stb \
$$PWD/include \
$$PWD/ui \
$$PWD/shaders
Expand All @@ -43,7 +44,8 @@ HEADERS += \
include/MaterialWireframe.h \
include/MaterialFractal.h \
include/MaterialEnvMap.h \
include/MaterialBump.h
include/MaterialBump.h \
include/HDR_cube.h


SOURCES += \
Expand All @@ -63,7 +65,8 @@ SOURCES += \
src/MaterialWireframe.cpp \
src/MaterialFractal.cpp \
src/MaterialEnvMap.cpp \
src/MaterialBump.cpp
src/MaterialBump.cpp \
src/HDR_cube.cpp

OTHER_FILES += \
$$files(shaders/*, true) \
Expand Down
23 changes: 9 additions & 14 deletions include/DemoScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public slots:
/// calls loadMesh.
//-----------------------------------------------------------------------------------------------------
void generateNewGeometry();
//-----------------------------------------------------------------------------------------------------
/// @brief Used to link a Qt button to the scene, to cycle through the materials and apply them to
/// the current Mesh.
//-----------------------------------------------------------------------------------------------------
void nextMaterial();

private:
//-----------------------------------------------------------------------------------------------------
Expand All @@ -94,6 +89,9 @@ public slots:
//-----------------------------------------------------------------------------------------------------
virtual void renderScene() override;

void initSphereMap();
void initCubeMap();

private:
//-----------------------------------------------------------------------------------------------------
/// @brief Holds our test meshes.
Expand All @@ -117,21 +115,18 @@ public slots:
//-----------------------------------------------------------------------------------------------------
/// @brief The materials used in this scene.
//-----------------------------------------------------------------------------------------------------
std::vector<std::unique_ptr<Material>> m_materials;
//-----------------------------------------------------------------------------------------------------
/// @brief Holds the index of the currently drawn mesh in our array of meshes.
//-----------------------------------------------------------------------------------------------------
size_t m_meshIndex = 0;
//-----------------------------------------------------------------------------------------------------
/// @brief The current material.
//-----------------------------------------------------------------------------------------------------
size_t m_currentMaterial = 0;
std::unique_ptr<Material> m_material;
//-----------------------------------------------------------------------------------------------------
/// @brief Is the mesh rotating.
//-----------------------------------------------------------------------------------------------------
bool m_rotating = false;



unsigned int m_hdrTextureID;
unsigned int m_envTextureID;
unsigned int m_irradianceTextureID;

};

#endif // DEMOSCENE_H
28 changes: 28 additions & 0 deletions include/HDR_cube.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef MATERIALHDRCUBE_H
#define MATERIALHDRCUBE_H

#include "Material.h"
#include <QOpenGLTexture>
#include <QImage>

class HDR_cube
{
public:
HDR_cube() = default;
HDR_cube(const HDR_cube&) = default;
HDR_cube& operator=(const HDR_cube&) = default;
HDR_cube(HDR_cube&&) = default;
HDR_cube& operator=(HDR_cube&&) = default;
~HDR_cube() = default;

void init(const std::shared_ptr<ShaderLib> &io_shaderLib, QOpenGLContext* io_context);

unsigned int irradianceMapID() const noexcept { return m_irradianceMap; }

private:
unsigned int m_irradianceMap;
unsigned int initSphereMap(QOpenGLContext *io_context);

};

#endif // MATERIALHDRCUBE_H
4 changes: 4 additions & 0 deletions include/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Material
//-----------------------------------------------------------------------------------------------------
void setShaderName(const std::string &_name);
//-----------------------------------------------------------------------------------------------------
/// @brief Used to get the name of the shader that this material should be applied to.
//-----------------------------------------------------------------------------------------------------
std::string getShaderName() const noexcept;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to update shader values.
//-----------------------------------------------------------------------------------------------------
virtual void update() = 0;
Expand Down
2 changes: 0 additions & 2 deletions include/MaterialEnvMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <QOpenGLTexture>
#include <QImage>

class Camera;

class MaterialEnvMap : public Material
{
public:
Expand Down
2 changes: 0 additions & 2 deletions include/MaterialFractal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "Material.h"
#include <chrono>

class Camera;

class MaterialFractal : public Material
{
public:
Expand Down
9 changes: 9 additions & 0 deletions include/MaterialPBR.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Material.h"
#include "vec3.hpp"
#include "HDR_cube.h"

class MaterialPBR : public Material
{
Expand All @@ -11,6 +12,7 @@ class MaterialPBR : public Material
const std::shared_ptr<Camera> &io_camera,
const std::shared_ptr<ShaderLib> &io_shaderLib,
std::array<glm::mat4, 3>* io_matrices,
QOpenGLContext* io_context,
const glm::vec3 &_albedo,
const float _ao,
const float _exposure,
Expand All @@ -19,6 +21,7 @@ class MaterialPBR : public Material
) :
Material(io_camera, io_shaderLib, io_matrices),
m_albedo(_albedo),
m_context(io_context),
m_ao(_ao),
m_exposure(_exposure),
m_roughness(_roughness),
Expand All @@ -38,7 +41,13 @@ class MaterialPBR : public Material


private:
void initEnvMap();
std::unique_ptr<QOpenGLTexture> m_envMap;


glm::vec3 m_albedo;
// HDR_cube m_envMap;
QOpenGLContext* m_context;
float m_ao;
float m_exposure;
float m_roughness;
Expand Down
2 changes: 0 additions & 2 deletions include/MaterialPhong.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "Material.h"

class Camera;

class MaterialPhong : public Material
{
public:
Expand Down
2 changes: 0 additions & 2 deletions include/MaterialWireframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "Material.h"

class Camera;

class MaterialWireframe : public Material
{
public:
Expand Down
15 changes: 12 additions & 3 deletions shaders/owl_pbr_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ uniform float ao;
// camera parameters
uniform vec3 camPos;
uniform float exposure;
uniform samplerCube irradianceMap;
uniform sampler2D sphereMap;


// lights
Expand Down Expand Up @@ -80,15 +82,18 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0)
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
// ----------------------------------------------------------------------------

vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
{
return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(1.0 - cosTheta, 5.0);
}


void main()
{

vec3 N = normalize(Normal);

vec3 eyeAlbedo = vec3(EyeVal);
vec3 eyeAlbedo = albedo;//vec3(texture(sphereMap, TexCoords));//albedo;//vec3(EyeVal);

vec3 V = normalize(camPos - WorldPos);
vec3 R = reflect(-V, N);
Expand Down Expand Up @@ -140,7 +145,11 @@ void main()

// ambient lighting (note that the next IBL tutorial will replace
// this ambient lighting with environment lighting).
vec3 ambient = vec3(0.03) * eyeAlbedo * ao;
vec3 kS = fresnelSchlickRoughness(max(dot(N, V), 0.0), F0, roughness);
vec3 kD = 1.0 - kS;
vec3 irradiance = texture(irradianceMap, R).rgb;
vec3 diffuse = irradiance * albedo;
vec3 ambient = (kD * diffuse) * ao;

vec3 color = ambient + Lo;

Expand Down
2 changes: 1 addition & 1 deletion shaders/owl_pbr_tess_control.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main(void)
tc_uv[ID] = v_uv[ID];

float tessMask = mask(eyeMask(posA, eyeFuzz, 1.0), eyeMask(posB, eyeFuzz, 1.0), eyeFuzz, v_normal[ID].z);
int tessLevel = 1 + int(ceil(64 * smoothstep(0.0, 1.0, tessMask)));
int tessLevel = 1 + int(ceil(31 * smoothstep(0.0, 1.0, tessMask)));

gl_TessLevelInner[0] = tessLevel;
gl_TessLevelOuter[0] = tessLevel;
Expand Down
Loading

0 comments on commit e900ff0

Please sign in to comment.