Skip to content

Commit

Permalink
1.0.2 Add Phong shader. Improve light (PBR and Blinn-Phong).
Browse files Browse the repository at this point in the history
  • Loading branch information
mortennobel committed Feb 1, 2018
2 parents 09dfeb2 + b44feab commit 0391f98
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 200 deletions.
2 changes: 1 addition & 1 deletion include/sre/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace sre {
~Renderer();
static constexpr int sre_version_major = 1;
static constexpr int sre_version_minor = 0;
static constexpr int sre_version_point = 1;
static constexpr int sre_version_point = 2;

glm::ivec2 getWindowSize(); // Return the current size of the window

Expand Down
2 changes: 2 additions & 0 deletions include/sre/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ namespace sre {
// S_VERTEX_COLOR
// Adds VertexAttribute "color" vec4 defined in linear space.

static std::shared_ptr<Shader> getStandardPhong(); // Similar to Blinn-Phong, but with more accurate specular highlights

static std::shared_ptr<Shader> getUnlit(); // Unlit model.
// Uniforms
// "color" vec4 (default (1,1,1,1))
Expand Down
230 changes: 159 additions & 71 deletions include/sre/impl/ShaderSource.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// autogenerated by
// files_to_cpp shader src/embedded_deps/sre_utils_incl.glsl sre_utils_incl.glsl src/embedded_deps/debug_normal_frag.glsl debug_normal_frag.glsl src/embedded_deps/debug_normal_vert.glsl debug_normal_vert.glsl src/embedded_deps/debug_uv_frag.glsl debug_uv_frag.glsl src/embedded_deps/debug_uv_vert.glsl debug_uv_vert.glsl src/embedded_deps/light_phong_incl.glsl light_phong_incl.glsl src/embedded_deps/particles_frag.glsl particles_frag.glsl src/embedded_deps/particles_vert.glsl particles_vert.glsl src/embedded_deps/sprite_frag.glsl sprite_frag.glsl src/embedded_deps/sprite_vert.glsl sprite_vert.glsl src/embedded_deps/standard_pbr_frag.glsl standard_pbr_frag.glsl src/embedded_deps/standard_pbr_vert.glsl standard_pbr_vert.glsl src/embedded_deps/standard_blinn_phong_frag.glsl standard_blinn_phong_frag.glsl src/embedded_deps/standard_blinn_phong_vert.glsl standard_blinn_phong_vert.glsl src/embedded_deps/unlit_frag.glsl unlit_frag.glsl src/embedded_deps/unlit_vert.glsl unlit_vert.glsl src/embedded_deps/debug_tangent_frag.glsl debug_tangent_frag.glsl src/embedded_deps/debug_tangent_vert.glsl debug_tangent_vert.glsl src/embedded_deps/normalmap_incl.glsl normalmap_incl.glsl include/sre/impl/ShaderSource.inl
// files_to_cpp shader src/embedded_deps/sre_utils_incl.glsl sre_utils_incl.glsl src/embedded_deps/debug_normal_frag.glsl debug_normal_frag.glsl src/embedded_deps/debug_normal_vert.glsl debug_normal_vert.glsl src/embedded_deps/debug_uv_frag.glsl debug_uv_frag.glsl src/embedded_deps/debug_uv_vert.glsl debug_uv_vert.glsl src/embedded_deps/light_incl.glsl light_incl.glsl src/embedded_deps/particles_frag.glsl particles_frag.glsl src/embedded_deps/particles_vert.glsl particles_vert.glsl src/embedded_deps/sprite_frag.glsl sprite_frag.glsl src/embedded_deps/sprite_vert.glsl sprite_vert.glsl src/embedded_deps/standard_pbr_frag.glsl standard_pbr_frag.glsl src/embedded_deps/standard_pbr_vert.glsl standard_pbr_vert.glsl src/embedded_deps/standard_blinn_phong_frag.glsl standard_blinn_phong_frag.glsl src/embedded_deps/standard_blinn_phong_vert.glsl standard_blinn_phong_vert.glsl src/embedded_deps/standard_phong_frag.glsl standard_phong_frag.glsl src/embedded_deps/standard_phong_vert.glsl standard_phong_vert.glsl src/embedded_deps/unlit_frag.glsl unlit_frag.glsl src/embedded_deps/unlit_vert.glsl unlit_vert.glsl src/embedded_deps/debug_tangent_frag.glsl debug_tangent_frag.glsl src/embedded_deps/debug_tangent_vert.glsl debug_tangent_vert.glsl src/embedded_deps/normalmap_incl.glsl normalmap_incl.glsl include/sre/impl/ShaderSource.inl
#include <map>
#include <utility>
#include <string>
Expand Down Expand Up @@ -83,33 +83,61 @@ void main(void) {
gl_Position = g_projection * g_view * g_model * vec4(position,1.0);
vUV = uv;
})"),
std::make_pair<std::string,std::string>("light_phong_incl.glsl",R"(uniform vec3 g_ambientLight;
std::make_pair<std::string,std::string>("light_incl.glsl",R"(uniform vec3 g_ambientLight;
in vec4 vLightDir[SI_LIGHTS];
#ifdef GL_ES
uniform highp vec4 g_lightColorRange[SI_LIGHTS];
#else
uniform vec4 g_lightColorRange[SI_LIGHTS];
#endif
uniform vec4 g_lightPosType[SI_LIGHTS];
uniform vec4 specularity;
vec3 computeLight(vec3 wsPos, vec3 wsCameraPos, vec3 normal, out vec3 specularityOut){
void lightDirectionAndAttenuation(vec4 lightPosType, float lightRange, vec3 pos, out vec3 lightDirection, out float attenuation){
bool isDirectional = lightPosType.w == 0.0;
bool isPoint = lightPosType.w == 1.0;
if (isDirectional){
lightDirection = lightPosType.xyz;
attenuation = 1.0;
} else if (isPoint) {
vec3 lightVector = lightPosType.xyz - pos;
float lightVectorLength = length(lightVector);
lightDirection = lightVector / lightVectorLength; // normalize
if (lightRange <= 0.0){ // attenuation disabled
attenuation = 1.0;
} else if (lightVectorLength >= lightRange){
attenuation = 0.0;
return;
} else {
attenuation = pow(1.0 - lightVectorLength / lightRange, 1.5); // non physical range based attenuation
}
} else {
attenuation = 0.0;
lightDirection = vec3(0.0, 0.0, 0.0);
}
}
vec3 computeLightBlinnPhong(vec3 wsPos, vec3 wsCameraPos, vec3 normal, out vec3 specularityOut){
specularityOut = vec3(0.0, 0.0, 0.0);
vec3 lightColor = vec3(0.0,0.0,0.0);
vec3 cam = normalize(wsCameraPos - wsPos);
for (int i=0;i<SI_LIGHTS;i++){
float att = vLightDir[i].w;
vec3 lightDirection = vec3(0.0,0.0,0.0);
float att = 0.0;
lightDirectionAndAttenuation(g_lightPosType[i], g_lightColorRange[i].w, wsPos, lightDirection, att);
if (att <= 0.0){
continue;
}
vec3 lightDirection = normalize(vLightDir[i].xyz);
// diffuse light
float thisDiffuse = max(0.0,dot(lightDirection, normal));
if (thisDiffuse > 0.0){
lightColor += (att * thisDiffuse) * g_lightColorRange[i].xyz;
float diffuse = dot(lightDirection, normal);
if (diffuse > 0.0){
lightColor += (att * diffuse) * g_lightColorRange[i].xyz;
}
// specular light
if (specularity.a > 0.0){
vec3 H = normalize(lightDirection + normalize(wsCameraPos - wsPos));
vec3 H = normalize(lightDirection + cam);
float nDotHV = dot(normal, H);
if (nDotHV > 0.0){
float pf = pow(nDotHV, specularity.a);
Expand All @@ -119,6 +147,40 @@ vec3 computeLight(vec3 wsPos, vec3 wsCameraPos, vec3 normal, out vec3 specularit
}
lightColor = max(g_ambientLight.xyz, lightColor);
return lightColor;
}
vec3 computeLightPhong(vec3 wsPos, vec3 wsCameraPos, vec3 normal, out vec3 specularityOut){
specularityOut = vec3(0.0, 0.0, 0.0);
vec3 lightColor = vec3(0.0,0.0,0.0);
vec3 cam = normalize(wsCameraPos - wsPos);
for (int i=0;i<SI_LIGHTS;i++){
vec3 lightDirection = vec3(0.0,0.0,0.0);
float att = 0.0;
lightDirectionAndAttenuation(g_lightPosType[i], g_lightColorRange[i].w, wsPos, lightDirection, att);
if (att <= 0.0){
continue;
}
// diffuse light
float diffuse = dot(lightDirection, normal);
if (diffuse > 0.0){
lightColor += (att * diffuse) * g_lightColorRange[i].xyz;
}
// specular light
if (specularity.a > 0.0){
vec3 R = reflect(-lightDirection, normal);
float nDotRV = dot(cam, R);
if (nDotRV > 0.0){
float pf = pow(nDotRV, specularity.a);
specularityOut += specularity.rgb * (pf * att); // white specular highlights
}
}
}
lightColor = max(g_ambientLight.xyz, lightColor);
return lightColor;
})"),
std::make_pair<std::string,std::string>("particles_frag.glsl",R"(#version 140
Expand Down Expand Up @@ -230,14 +292,7 @@ in vec3 vNormal;
#endif
in vec2 vUV;
in vec3 vWsPos;
in vec4 vLightDir[SI_LIGHTS];
uniform vec3 g_ambientLight;
#ifdef GL_ES
uniform highp vec4 g_lightColorRange[SI_LIGHTS];
#else
uniform vec4 g_lightColorRange[SI_LIGHTS];
#endif
uniform vec4 color;
uniform vec4 metallicRoughness;
uniform vec4 g_cameraPos;
Expand All @@ -262,6 +317,7 @@ in vec4 vColor;
#endif
#pragma include "normalmap_incl.glsl"
#pragma include "light_incl.glsl"
#pragma include "sre_utils_incl.glsl"
Expand Down Expand Up @@ -372,12 +428,13 @@ void main(void)
vec3 n = getNormal(); // Normal at surface point
vec3 v = normalize(g_cameraPos.xyz - vWsPos.xyz); // Vector from surface point to camera
for (int i=0;i<SI_LIGHTS;i++) {
float attenuation = vLightDir[i].w;
float attenuation = 0.0;
vec3 l = vec3(0.0,0.0,0.0);
lightDirectionAndAttenuation(g_lightPosType[i], g_lightColorRange[i].w, vWsPos, l, attenuation);
if (attenuation <= 0.0){
continue;
}
vec3 l = normalize(vLightDir[i].xyz); // Vector from surface point to light
vec3 h = normalize(l+v); // Half vector between both l and v
vec3 reflection = -normalize(reflect(v, n));
Expand Down Expand Up @@ -440,15 +497,12 @@ in vec4 color;
out vec4 vColor;
#endif
out vec2 vUV;
out vec4 vLightDir[SI_LIGHTS];
out vec3 vWsPos;
uniform mat4 g_model;
uniform mat4 g_view;
uniform mat4 g_projection;
uniform mat3 g_model_it;
uniform vec4 g_lightPosType[SI_LIGHTS];
uniform vec4 g_lightColorRange[SI_LIGHTS];
#pragma include "normalmap_incl.glsl"
Expand All @@ -462,27 +516,6 @@ void main(void) {
vNormal = normalize(g_model_it * normal);
#endif
vUV = uv.xy;
for (int i=0;i<SI_LIGHTS;i++){
bool isDirectional = g_lightPosType[i].w == 0.0;
bool isPoint = g_lightPosType[i].w == 1.0;
float att = 1.0;
if (isDirectional){
vLightDir[i] = vec4(g_lightPosType[i].xyz, 1.0);
} else if (isPoint) {
vec3 dir = g_lightPosType[i].xyz - vWsPos;
float dirLength = length(dir);
float att = 0.0;
if (g_lightColorRange[i].w == 0.0){
att = 1.0;
} else if (dirLength < g_lightColorRange[i].w) {
att = pow(1.0 - (dirLength / g_lightColorRange[i].w), 1.5); // non physical range based attenuation
}
vLightDir[i] = vec4(dir / dirLength, att);
} else {
vLightDir[i] = vec4(0.0, 0.0, 0.0, 0.0);
}
}
#ifdef S_VERTEX_COLOR
vColor = color;
#endif
Expand All @@ -508,7 +541,7 @@ in vec4 vColor;
uniform vec4 color;
uniform sampler2D tex;
#pragma include "light_phong_incl.glsl"
#pragma include "light_incl.glsl"
#pragma include "normalmap_incl.glsl"
#pragma include "sre_utils_incl.glsl"
Expand All @@ -520,7 +553,7 @@ void main()
#endif
vec3 normal = getNormal();
vec3 specularLight = vec3(0.0,0.0,0.0);
vec3 l = computeLight(vWsPos, g_cameraPos.xyz, normal, specularLight);
vec3 l = computeLightBlinnPhong(vWsPos, g_cameraPos.xyz, normal, specularLight);
fragColor = c * vec4(l, 1.0) + vec4(specularLight,0);
fragColor = toOutput(fragColor);
Expand All @@ -541,14 +574,11 @@ out vec3 vWsPos;
in vec4 color;
out vec4 vColor;
#endif
out vec4 vLightDir[SI_LIGHTS];
uniform mat4 g_model;
uniform mat4 g_view;
uniform mat4 g_projection;
uniform mat3 g_model_it;
uniform vec4 g_lightPosType[SI_LIGHTS];
uniform vec4 g_lightColorRange[SI_LIGHTS];
#pragma include "normalmap_incl.glsl"
Expand All @@ -562,26 +592,84 @@ void main(void) {
#endif
vUV = uv.xy;
vWsPos = wsPos.xyz;
for (int i=0;i<SI_LIGHTS;i++){
bool isDirectional = g_lightPosType[i].w == 0.0;
bool isPoint = g_lightPosType[i].w == 1.0;
float att = 1.0;
if (isDirectional){
vLightDir[i] = vec4(g_lightPosType[i].xyz, 1.0);
} else if (isPoint) {
vec3 dir = g_lightPosType[i].xyz - vWsPos;
float dirLength = length(dir);
float att = 0.0;
if (g_lightColorRange[i].w == 0.0){
att = 1.0;
} else if (dirLength < g_lightColorRange[i].w) {
att = pow(1.0 - (dirLength / g_lightColorRange[i].w), 1.5); // non physical range based attenuation
}
vLightDir[i] = vec4(dir / dirLength, att);
} else {
vLightDir[i] = vec4(0.0, 0.0, 0.0, 0.0);
}
}
#ifdef S_VERTEX_COLOR
vColor = color;
#endif
})"),
std::make_pair<std::string,std::string>("standard_phong_frag.glsl",R"(#version 140
out vec4 fragColor;
#if defined(S_TANGENTS) && defined(S_NORMALMAP)
in mat3 vTBN;
#else
in vec3 vNormal;
#endif
in vec2 vUV;
in vec3 vWsPos;
uniform vec4 g_cameraPos;
#ifdef S_NORMALMAP
uniform sampler2D normalTex;
uniform float normalScale;
#endif
#ifdef S_VERTEX_COLOR
in vec4 vColor;
#endif
uniform vec4 color;
uniform sampler2D tex;
#pragma include "light_incl.glsl"
#pragma include "normalmap_incl.glsl"
#pragma include "sre_utils_incl.glsl"
void main()
{
vec4 c = color * toLinear(texture(tex, vUV));
#ifdef S_VERTEX_COLOR
c = c * vColor;
#endif
vec3 normal = getNormal();
vec3 specularLight = vec3(0.0,0.0,0.0);
vec3 l = computeLightPhong(vWsPos, g_cameraPos.xyz, normal, specularLight);
fragColor = c * vec4(l, 1.0) + vec4(specularLight,0);
fragColor = toOutput(fragColor);
})"),
std::make_pair<std::string,std::string>("standard_phong_vert.glsl",R"(#version 140
in vec3 position;
in vec3 normal;
in vec4 uv;
out vec2 vUV;
#if defined(S_TANGENTS) && defined(S_NORMALMAP)
in vec4 tangent;
out mat3 vTBN;
#else
out vec3 vNormal;
#endif
out vec3 vWsPos;
#ifdef S_VERTEX_COLOR
in vec4 color;
out vec4 vColor;
#endif
uniform mat4 g_model;
uniform mat4 g_view;
uniform mat4 g_projection;
uniform mat3 g_model_it;
#pragma include "normalmap_incl.glsl"
void main(void) {
vec4 wsPos = g_model * vec4(position,1.0);
gl_Position = g_projection * g_view * wsPos;
#if defined(S_TANGENTS) && defined(S_NORMALMAP)
vTBN = computeTBN(g_model_it, normal, tangent);
#else
vNormal = normalize(g_model_it * normal);
#endif
vUV = uv.xy;
vWsPos = wsPos.xyz;
#ifdef S_VERTEX_COLOR
vColor = color;
#endif
Expand Down
Loading

0 comments on commit 0391f98

Please sign in to comment.