Skip to content

Commit

Permalink
Demo scene now has shader swapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronoid committed Feb 8, 2018
1 parent a7e169a commit 7b45666
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 108 deletions.
6 changes: 4 additions & 2 deletions Project.pro
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ HEADERS += \
include/Mesh.h \
include/Scene.h \
include/DemoScene.h \
include/OpenglPlatform.h
include/OpenglPlatform.h \
include/MaterialPhong.h

SOURCES += \
src/main.cpp \
Expand All @@ -54,7 +55,8 @@ SOURCES += \
src/Material.cpp \
src/MaterialPBR.cpp \
src/Scene.cpp \
src/DemoScene.cpp
src/DemoScene.cpp \
src/MaterialPhong.cpp

OTHER_FILES += \
$$files(shaders/*, true) \
Expand Down
15 changes: 11 additions & 4 deletions include/DemoScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Scene.h"
#include "MaterialPBR.h"
#include "MaterialPhong.h"


class DemoScene : public Scene
Expand All @@ -16,8 +17,7 @@ class DemoScene : public Scene
/// @param [io] io_parent the parent window to create the GL context in.
//----------------------------------------------------------------------------------------------------------------------
DemoScene(Camera* io_camera, QWidget *_parent) :
Scene(io_camera, _parent),
m_material(new MaterialPBR(io_camera))
Scene(io_camera, _parent)
{}
//-----------------------------------------------------------------------------------------------------
/// @brief Default copy constructor.
Expand Down Expand Up @@ -56,6 +56,8 @@ public slots:
//-----------------------------------------------------------------------------------------------------
void generateNewGeometry();

void nextMaterial();

private:
//-----------------------------------------------------------------------------------------------------
/// @brief Used to load mesh data into our buffer.
Expand All @@ -78,11 +80,16 @@ public slots:
//----------------------------------------------------------------------------------------------------------------------
/// @brief Holds our shader program.
//----------------------------------------------------------------------------------------------------------------------
ShaderProgram m_shaderProgram;
std::array<ShaderProgram, 2> m_shaderPrograms;
//----------------------------------------------------------------------------------------------------------------------
/// @brief Holds our material settings for the shader.
//----------------------------------------------------------------------------------------------------------------------
std::unique_ptr<Material> m_material;
using matPtr = std::unique_ptr<Material>;
std::array<matPtr, 2> m_materials = {{
matPtr{new MaterialPBR(m_camera)},
matPtr{new MaterialPhong}
}};
size_t m_currentMaterial = 0;
//----------------------------------------------------------------------------------------------------------------------
/// @brief Is the mesh rotating.
//----------------------------------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions include/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Material
//-----------------------------------------------------------------------------------------------------
virtual void update() = 0;

virtual const char* vertexName() const = 0;

virtual const char* fragName() const = 0;

protected:
//-----------------------------------------------------------------------------------------------------
/// @brief A pointer to the shader program that this material affects.
Expand Down
5 changes: 4 additions & 1 deletion include/MaterialPBR.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Camera;
class MaterialPBR : public Material
{
public:
MaterialPBR() = default;
MaterialPBR(Camera* _cam) :
Material(),
m_cam(_cam)
Expand All @@ -23,6 +22,10 @@ class MaterialPBR : public Material

virtual void update() override;

virtual const char* vertexName() const override;

virtual const char* fragName() const override;

private:
Camera* m_cam = nullptr;

Expand Down
25 changes: 25 additions & 0 deletions include/MaterialPhong.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MATERIALPHONG_H
#define MATERIALPHONG_H

#include "Material.h"

class MaterialPhong : public Material
{
public:
MaterialPhong() = default;
MaterialPhong(const MaterialPhong&) = default;
MaterialPhong& operator=(const MaterialPhong&) = default;
MaterialPhong(MaterialPhong&&) = default;
MaterialPhong& operator=(MaterialPhong&&) = default;
~MaterialPhong() override = default;

virtual void init(ShaderProgram* io_shader, std::array<glm::mat4, 3>* io_matrices) override;

virtual void update() override;

virtual const char* vertexName() const override;

virtual const char* fragName() const override;
};

#endif // MATERIALPHONG_H
1 change: 0 additions & 1 deletion include/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class Scene : public QOpenGLWidget
/// @brief Array of matrices, that stores the model view, projection, and normal matrices.
//----------------------------------------------------------------------------------------------------------------------
std::array<glm::mat4, 3> m_matrices;
private:
//----------------------------------------------------------------------------------------------------------------------
/// @brief a reference pointer to the camera that is used to view this scene.
//----------------------------------------------------------------------------------------------------------------------
Expand Down
11 changes: 9 additions & 2 deletions include/ShaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "OpenglPlatform.h"
#include <string>
#include <unordered_map>
#include <fstream>
#include <glm.hpp>

Expand All @@ -14,7 +15,7 @@ class ShaderProgram
ShaderProgram& operator=(const ShaderProgram&) = default;
ShaderProgram(ShaderProgram&&) = default;
ShaderProgram& operator=(ShaderProgram&&) = default;
~ShaderProgram() = default;
~ShaderProgram();

void init(const std::string &_name, const std::string &_vertex, const std::string &_fragment);
std::string getName();
Expand All @@ -36,12 +37,18 @@ class ShaderProgram
void setUniform(const char*_name, const glm::mat3 _v);
void setUniform(const char*_name, const glm::mat4 _v);

void setUniform(const char*_name, const bool _v);


void loadShader(const std::string &_filename, const GLenum _shaderType);
void use();
void clearShader(const GLenum _shaderType);
void clearAllShaders();

private:
std::string m_name;
GLuint m_shaderProgram;
void loadShader(const std::string &_filename, const GLenum _shaderType);
std::unordered_map<GLenum, GLuint> m_attachedShaders;
std::string loadShaderFile(std::string _filename);

};
Expand Down
158 changes: 78 additions & 80 deletions shaders/phong_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,143 +20,141 @@ layout ( location = 0 ) out vec4 FragColor;
/* light copied from vert */
struct LightInfo
{
vec4 Position; // Light position in eye coords.
vec3 La; // Ambient light intensity
vec3 Ld; // Diffuse light intensity
vec3 Ls; // Specular light intensity
vec4 Position; // Light position in eye coords.
vec3 La; // Ambient light intensity
vec3 Ld; // Diffuse light intensity
vec3 Ls; // Specular light intensity
};

// We'll have a single light in the scene with some default values
uniform LightInfo Light = LightInfo
(
vec4( 10, 25.0, 30.0, 1.0 ), // position
vec3( 2.5, 2.5, 2.5 ), // La
vec3( 1.0, 1.0, 1.0 ), // Ld
vec3( 1.0, 1.0, 1.0 ) // Ls
);
uniform LightInfo Light = LightInfo(
vec4( 10, 25.0, 30.0, 1.0 ), // position
vec3( 2.5, 2.5, 2.5 ), // La
vec3( 1.0, 1.0, 1.0 ), // Ld
vec3( 1.0, 1.0, 1.0 ) // Ls
);


/* material copied from vert */
// The material properties of our object
struct MaterialInfo
{
vec3 Ka; // Ambient reflectivity
vec3 Kd; // Diffuse reflectivity
vec3 Ks; // Specular reflectivity
float Shininess; // Specular shininess factor
vec3 Ka; // Ambient reflectivity
vec3 Kd; // Diffuse reflectivity
vec3 Ks; // Specular reflectivity
float Shininess; // Specular shininess factor
};

// The object has a material
uniform MaterialInfo Material = MaterialInfo
(
vec3(0.1, 0.1, 0.1), // Ka
vec3(1.0, 1.0, 1.0), // Kd
vec3(1, 1, 1), // Ks
4.0 // Shininess
);
uniform MaterialInfo Material = MaterialInfo(
vec3(0.1, 0.1, 0.1), // Ka
vec3(1.0, 1.0, 1.0), // Kd
vec3(1, 1, 1), // Ks
4.0 // Shininess
);

/** From http://www.neilmendoza.com/glsl-rotation-about-an-arbitrary-axis/ */
mat4 rotationMatrix( vec3 axis, float angle )
{
//axis = normalize(axis);
float s = sin( angle );
float c = cos( angle );
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
//axis = normalize(axis);
float s = sin( angle );
float c = cos( angle );
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0 );
}

vec3 rotateVector( vec3 src, vec3 tgt, vec3 vec )
{
float angle = acos( dot( src, tgt ) );

if ( angle == 0 )
{
return vec;
}
vec3 axis = normalize( cross( src, tgt ) );
mat4 R = rotationMatrix( axis, angle );
vec4 norm = R * vec4( vec, 1.0f );
return norm.xyz / norm.w;
float angle = acos( dot( src, tgt ) );

if ( angle == 0 )
{
return vec;
}
vec3 axis = normalize( cross( src, tgt ) );
mat4 R = rotationMatrix( axis, angle );
vec4 norm = R * vec4( vec, 1.0f );
return norm.xyz / norm.w;
}

/** https://www.shadertoy.com/view/XtV3z3 **/
float texture_lum(sampler2D _texture, vec2 _uv)
{
vec3 rgb = texture( _texture, _uv ).rgb;
return 0.2126 * rgb.r + 0.7152 * rgb.g + 0.0722 * rgb.b;
vec3 rgb = texture( _texture, _uv ).rgb;
return 0.2126 * rgb.r + 0.7152 * rgb.g + 0.0722 * rgb.b;
}

/** https://www.shadertoy.com/view/XtV3z3 **/
vec3 toNormal()
{
float r = 0.00078125f; // 1 / width of the texture
float r = 0.00078125f; // 1 / width of the texture

float x0 = texture_lum( ColourTexture, vec2( texCoord.x + r, texCoord.y ) );
float x1 = texture_lum( ColourTexture, vec2( texCoord.x - r, texCoord.y ) );
float y0 = texture_lum( ColourTexture, vec2( texCoord.x, texCoord.y + r ) );
float y1 = texture_lum( ColourTexture, vec2( texCoord.x, texCoord.y - r ) );
float x0 = texture_lum( ColourTexture, vec2( texCoord.x + r, texCoord.y ) );
float x1 = texture_lum( ColourTexture, vec2( texCoord.x - r, texCoord.y ) );
float y0 = texture_lum( ColourTexture, vec2( texCoord.x, texCoord.y + r ) );
float y1 = texture_lum( ColourTexture, vec2( texCoord.x, texCoord.y - r ) );

vec3 n = normalize( vec3( x1 - x0, y1 - y0, 1.0f ) );
vec3 n = normalize( vec3( x1 - x0, y1 - y0, 1.0f ) );

return n * 0.5 + 0.5;
return n * 0.5 + 0.5;
}

vec4 useColor(vec3 _LightIntensity)
{
if ( texture( ColourTexture, texCoord ).r < 0.4f )
return vec4( _LightIntensity * dotsColor, 1.0 );
else
return vec4( _LightIntensity * baseColor , 1.0 );
if ( texture( ColourTexture, texCoord ).r < 0.4f )
return vec4( _LightIntensity * dotsColor, 1.0 );
else
return vec4( _LightIntensity * baseColor , 1.0 );
}


vec3 PhongReflection(vec3 _s, vec3 _n, vec3 _v)
{
vec3 r = reflect( -_s, _n );
vec3 r = reflect( -_s, _n );

// Compute the light from the ambient, diffuse and specular components
return vec3(
Light.La * Material.Ka +
Light.Ld * Material.Kd * max( dot( _s, _n ), 0.0 ) +
Light.Ls * Material.Ks * pow( max( dot( r, _v ), 0.0 ), Material.Shininess ) );
// Compute the light from the ambient, diffuse and specular components
return vec3(
Light.La * Material.Ka +
Light.Ld * Material.Kd * max( dot( _s, _n ), 0.0 ) +
Light.Ls * Material.Ks * pow( max( dot( r, _v ), 0.0 ), Material.Shininess ) );
}

vec3 FresnelReflection(vec3 _s, vec3 _n, vec3 _v)
{

float base = dot( _v, _s );
float exponential = pow( base, 5.0 );
float fresnel;
vec3 r = reflect( -_s, _n );
fresnel = exponential + .028f * ( 1.0 - exponential );
float base = dot( _v, _s );
float exponential = pow( base, 5.0 );
float fresnel;
vec3 r = reflect( -_s, _n );
fresnel = exponential + .028f * ( 1.0 - exponential );

return vec3(
Light.La * Material.Ka +
Light.Ld * Material.Kd * max( dot( _s, _n ), 0.0 ) +
Light.Ls * Material.Ks * pow( max( dot( r, _v ), 0.0 ), Material.Shininess ) * fresnel );
return vec3(
Light.La * Material.Ka +
Light.Ld * Material.Kd * max( dot( _s, _n ), 0.0 ) +
Light.Ls * Material.Ks * pow( max( dot( r, _v ), 0.0 ), Material.Shininess ) * fresnel );
}

void main()
{
vec3 n = normalize( FragmentNormal ); // normal
vec3 n = normalize( FragmentNormal ); // normal

vec3 s = normalize( vec3( LightPosition.xyz ) - FragmentPosition ); // Calculate the light vector
vec3 s = normalize( vec3( LightPosition.xyz ) - FragmentPosition ); // Calculate the light vector

vec3 v = normalize( -vec3( FragmentPosition ) ); // eye
vec3 v = normalize( -vec3( FragmentPosition ) ); // eye

vec3 tgt = normalize( toNormal() * 2.0 - 1.0 );
vec3 tgt = normalize( toNormal() * 2.0 - 1.0 );

vec3 src = vec3( 0.0, 0.0, 1.0 );
n = rotateVector( src, tgt, n);
vec3 src = vec3( 0.0, 0.0, 1.0 );
n = rotateVector( src, tgt, n);

// vec3 LightIntensity = FresnelReflection(s, n, v);
vec3 LightIntensity = PhongReflection(s, n, v);
// vec3 LightIntensity = FresnelReflection(s, n, v);
vec3 LightIntensity = PhongReflection(s, n, v);

if ( addingColor )
FragColor = useColor(LightIntensity);
else
FragColor = vec4( texture( ColourTexture, texCoord ).rgb, 1);
if ( addingColor )
FragColor = useColor(LightIntensity);
else
FragColor = vec4( texture( ColourTexture, texCoord ).rgb, 1);
}
Loading

0 comments on commit 7b45666

Please sign in to comment.