Skip to content

Commit

Permalink
Merge pull request #24 from mortennobel/1.0.1
Browse files Browse the repository at this point in the history
1.0.1
  • Loading branch information
mortennobel authored Jan 31, 2018
2 parents 2915ba2 + 173ca36 commit 09dfeb2
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ sre provides:
* GUI rendering (using Dear ImGui)
* Emscripten support (allows cross compiling to HTML 5 + WebGL)
* VR support
* Bump mapping

To keep sre as simple and flexible as possible the following features are not a part of sre:
* Scenegraphs
* Deferred rendering
* Bump mapping
* Shadowmap support
* Dynamic particle systems

Expand Down
15 changes: 8 additions & 7 deletions em-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fi

source ${EMSDK}/emsdk_env.sh

for FILENAME in benchmark64k custom-mesh-layout gui hello-engine hello-engine-raw obj-viewer picking render-to-texture spheres spinning-cube spinning-primitives-tex sprite-example
for FILENAME in multiple-lights imgui-color-test pbr-test custom-mesh-layout-default-values imgui_demo multi-cameras particle-sprite particle-test polygon-offset-example spinning-sphere-cubemap sprite-test static_vertex_attribute texture-test
do
echo $FILENAME
emcc -Iinclude src/imgui/imgui.cpp \
Expand All @@ -33,11 +33,12 @@ emcc -Iinclude src/imgui/imgui.cpp \
src/sre/SpriteAtlas.cpp \
src/sre/Inspector.cpp \
src/sre/Log.cpp \
examples/$FILENAME.cpp \
-O2 -std=c++14 -s FORCE_FILESYSTEM=1 -s TOTAL_MEMORY=33554432 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' --preload-file examples_data -s USE_SDL=2 -o html/$FILENAME.html
test/$FILENAME.cpp \
-O2 -std=c++14 -s FORCE_FILESYSTEM=1 -s TOTAL_MEMORY=67108864 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' --preload-file test_data -s USE_SDL=2 -o html/$FILENAME.html
done

for FILENAME in imgui-color-test pbr-test custom-mesh-layout-default-values imgui_demo multi-cameras multiple-lights particle-sprite particle-test polygon-offset-example spinning-sphere-cubemap sprite-test static_vertex_attribute texture-test

for FILENAME in benchmark64k custom-mesh-layout gui hello-engine hello-engine-raw obj-viewer picking render-to-texture spheres spinning-cube spinning-primitives-tex sprite-example
do
echo $FILENAME
emcc -Iinclude src/imgui/imgui.cpp \
Expand All @@ -62,6 +63,6 @@ emcc -Iinclude src/imgui/imgui.cpp \
src/sre/SpriteAtlas.cpp \
src/sre/Inspector.cpp \
src/sre/Log.cpp \
test/$FILENAME.cpp \
-O2 -std=c++14 -s FORCE_FILESYSTEM=1 -s TOTAL_MEMORY=67108864 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' --preload-file test_data -s USE_SDL=2 -o html/$FILENAME.html
done
examples/$FILENAME.cpp \
-O2 -std=c++14 -s FORCE_FILESYSTEM=1 -s TOTAL_MEMORY=33554432 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' --preload-file examples_data -s USE_SDL=2 -o html/$FILENAME.html
done
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 = 0;
static constexpr int sre_version_point = 1;

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

Expand Down
91 changes: 60 additions & 31 deletions include/sre/impl/ShaderSource.inl
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,23 @@ void main(void) {
vUV = uv;
})"),
std::make_pair<std::string,std::string>("light_phong_incl.glsl",R"(uniform vec3 g_ambientLight;
uniform vec4 g_lightPosType[SI_LIGHTS];
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 specularity;
vec3 computeLight(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);
for (int i=0;i<SI_LIGHTS;i++){
bool isDirectional = g_lightPosType[i].w == 0.0;
bool isPoint = g_lightPosType[i].w == 1.0;
vec3 lightDirection;
float att = 1.0;
if (isDirectional){
lightDirection = g_lightPosType[i].xyz;
} else if (isPoint) {
vec3 lightVector = g_lightPosType[i].xyz - wsPos;
float lightVectorLength = length(lightVector);
float lightRange = g_lightColorRange[i].w;
lightDirection = lightVector / lightVectorLength; // compute normalized lightDirection (using length)
if (lightRange <= 0.0){
att = 1.0;
} else if (lightVectorLength >= lightRange){
att = 0.0;
} else {
att = pow(1.0 - lightVectorLength / lightRange,1.5); // non physical range based attenuation
}
} else {
float att = vLightDir[i].w;
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){
Expand All @@ -122,11 +109,11 @@ vec3 computeLight(vec3 wsPos, vec3 wsCameraPos, vec3 normal, out vec3 specularit
// specular light
if (specularity.a > 0.0){
vec3 H = normalize(lightDirection - normalize(wsPos - wsCameraPos));
vec3 H = normalize(lightDirection + normalize(wsCameraPos - wsPos));
float nDotHV = dot(normal, H);
if (nDotHV > 0.0){
float pf = pow(nDotHV, specularity.a);
specularityOut += specularity.rgb*pf; // white specular highlights
specularityOut += specularity.rgb * pf * att; // white specular highlights
}
}
}
Expand Down Expand Up @@ -243,10 +230,14 @@ in vec3 vNormal;
#endif
in vec2 vUV;
in vec3 vWsPos;
in vec3 vLightDir[SI_LIGHTS];
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 Down Expand Up @@ -381,7 +372,12 @@ 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++) {
vec3 l = normalize(vLightDir[i]); // Vector from surface point to light
float attenuation = vLightDir[i].w;
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 @@ -414,7 +410,7 @@ void main(void)
// Calculation of analytical lighting contribution
vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
color += NdotL * g_lightColorRange[i].xyz * (diffuseContrib + specContrib);
color += attenuation * NdotL * g_lightColorRange[i].xyz * (diffuseContrib + specContrib);
}
// Apply optional PBR terms for additional (optional) shading
Expand Down Expand Up @@ -444,14 +440,15 @@ in vec4 color;
out vec4 vColor;
#endif
out vec2 vUV;
out vec3 vLightDir[SI_LIGHTS];
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 @@ -469,12 +466,21 @@ void main(void) {
for (int i=0;i<SI_LIGHTS;i++){
bool isDirectional = g_lightPosType[i].w == 0.0;
bool isPoint = g_lightPosType[i].w == 1.0;
vec3 lightDirection;
float att = 1.0;
if (isDirectional){
vLightDir[i] = g_lightPosType[i].xyz;
vLightDir[i] = vec4(g_lightPosType[i].xyz, 1.0);
} else if (isPoint) {
vLightDir[i] = normalize(g_lightPosType[i].xyz - vWsPos);
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
Expand Down Expand Up @@ -535,11 +541,14 @@ 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 @@ -552,7 +561,27 @@ void main(void) {
vNormal = normalize(g_model_it * normal);
#endif
vUV = uv.xy;
vWsPos = vWsPos.xyz;
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
Expand Down
33 changes: 10 additions & 23 deletions src/embedded_deps/light_phong_incl.glsl
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
uniform vec3 g_ambientLight;
uniform vec4 g_lightPosType[SI_LIGHTS];
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 specularity;

vec3 computeLight(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);
for (int i=0;i<SI_LIGHTS;i++){
bool isDirectional = g_lightPosType[i].w == 0.0;
bool isPoint = g_lightPosType[i].w == 1.0;
vec3 lightDirection;
float att = 1.0;
if (isDirectional){
lightDirection = g_lightPosType[i].xyz;
} else if (isPoint) {
vec3 lightVector = g_lightPosType[i].xyz - wsPos;
float lightVectorLength = length(lightVector);
float lightRange = g_lightColorRange[i].w;
lightDirection = lightVector / lightVectorLength; // compute normalized lightDirection (using length)
if (lightRange <= 0.0){
att = 1.0;
} else if (lightVectorLength >= lightRange){
att = 0.0;
} else {
att = pow(1.0 - lightVectorLength / lightRange,1.5); // non physical range based attenuation
}
} else {
float att = vLightDir[i].w;
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){
Expand All @@ -37,11 +24,11 @@ vec3 computeLight(vec3 wsPos, vec3 wsCameraPos, vec3 normal, out vec3 specularit

// specular light
if (specularity.a > 0.0){
vec3 H = normalize(lightDirection - normalize(wsPos - wsCameraPos));
vec3 H = normalize(lightDirection + normalize(wsCameraPos - wsPos));
float nDotHV = dot(normal, H);
if (nDotHV > 0.0){
float pf = pow(nDotHV, specularity.a);
specularityOut += specularity.rgb*pf; // white specular highlights
specularityOut += specularity.rgb * pf * att; // white specular highlights
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/embedded_deps/standard_blinn_phong_vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ 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 @@ -31,7 +34,27 @@ void main(void) {
vNormal = normalize(g_model_it * normal);
#endif
vUV = uv.xy;
vWsPos = vWsPos.xyz;
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
Expand Down
15 changes: 12 additions & 3 deletions src/embedded_deps/standard_pbr_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ in vec3 vNormal;
#endif
in vec2 vUV;
in vec3 vWsPos;
in vec3 vLightDir[SI_LIGHTS];
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 Down Expand Up @@ -147,7 +151,12 @@ 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++) {
vec3 l = normalize(vLightDir[i]); // Vector from surface point to light
float attenuation = vLightDir[i].w;
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 @@ -180,7 +189,7 @@ void main(void)
// Calculation of analytical lighting contribution
vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
color += NdotL * g_lightColorRange[i].xyz * (diffuseContrib + specContrib);
color += attenuation * NdotL * g_lightColorRange[i].xyz * (diffuseContrib + specContrib);
}

// Apply optional PBR terms for additional (optional) shading
Expand Down
Loading

0 comments on commit 09dfeb2

Please sign in to comment.