Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Build-147: Add Normal Mapping and Parallax Mapping support.
Browse files Browse the repository at this point in the history
Updated License Years in shaders. New Options in options menu.
  • Loading branch information
Guerra24 committed Jan 12, 2016
1 parent 4e00e8c commit 49af74e
Show file tree
Hide file tree
Showing 51 changed files with 618 additions and 151 deletions.
6 changes: 4 additions & 2 deletions assets/game/settings.conf
@@ -1,13 +1,15 @@
#Voxel Settings
#Mon Jan 11 11:22:46 CST 2016
#Tue Jan 12 15:58:55 CST 2016
UPS=30
VSYNC=false
useMotionBlur=false
useFXAA=false
DrawDistance=2
SettingsVersion=3
useParallax=false
useVolumetricLight=false
TESTXMOD=Hello Voxel
useDOF=false
FPS=30
useDOF=false
useReflections=false
useShadows=false
2 changes: 1 addition & 1 deletion assets/shaders/FragmentComposite0.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
89 changes: 44 additions & 45 deletions assets/shaders/FragmentComposite1.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -59,8 +59,8 @@ uniform samplerCube skyboxNight;
uniform int useFXAA;
uniform int useDOF;
uniform int useMotionBlur;
uniform int useBloom;
uniform int useVolumetricLight;
uniform int useReflections;

/*--------------------------------------------------------*/
/*------------------COMPOSITE 1 CONFIG--------------------*/
Expand All @@ -84,50 +84,49 @@ void main(void){
float depth = texture(gDepth, vec3(texcoord.xy, 0.0), 16);
if(data.b != 1) {
if(data.r == 1.0){
vec3 worldStartingPos = position.xyz;
vec3 normal = normal.xyz;
vec3 cameraToWorld = worldStartingPos.xyz - cameraPosition.xyz;
float cameraToWorldDist = length(cameraToWorld);
vec3 cameraToWorldNorm = normalize(cameraToWorld);
vec3 refl = normalize(reflect(cameraToWorldNorm, normal));
float cosAngle = abs(dot(normal, cameraToWorldNorm));
float fact = 1 - cosAngle;
fact = min(1, 1.38 - fact*fact);
vec3 newPos;
vec4 newScreen;
float i = 0;
vec3 rayTrace = worldStartingPos;
float currentWorldDist, rayDist;
float incr = 0.4;
do {
i += 0.05;
rayTrace += refl*incr;
incr *= 1.4;
newScreen = projectionMatrix * viewMatrix * vec4(rayTrace, 1);
newScreen /= newScreen.w;
newPos = texture(gPosition, newScreen.xy/2.0+0.5).xyz;
currentWorldDist = length(newPos.xyz - cameraPosition.xyz);
rayDist = length(rayTrace.xyz - cameraPosition.xyz);
if (newScreen.x > 1 || newScreen.x < -1 || newScreen.y > 1 || newScreen.y < -1 || dot(refl, cameraToWorldNorm) < 0)
break;
} while(rayDist < currentWorldDist);
vec4 newColor = texture(composite0, newScreen.xy/2.0 + 0.5);
if (dot(refl, cameraToWorldNorm) < 0)
fact = 1.0;
else if (newScreen.x > 1 || newScreen.x < -1 || newScreen.y > 1 || newScreen.y < -1)
fact = 1.0;
else if (cameraToWorldDist > currentWorldDist)
fact = 1.0;
if(fact != 1.0){
image = image*fact + newColor;
} else {
vec3 I = normalize(position.xyz - cameraPosition);
vec3 R = reflect(I, normalize(normal.xyz));
vec4 imageDay = texture(skyboxDay, R);
vec4 imageNight = texture(skyboxNight, R);
image += mix(imageDay, imageNight, skyboxBlendFactor);
if(useReflections == 1){
vec3 worldStartingPos = position.xyz;
vec3 normal = normal.xyz;
vec3 cameraToWorld = worldStartingPos.xyz - cameraPosition.xyz;
float cameraToWorldDist = length(cameraToWorld);
vec3 cameraToWorldNorm = normalize(cameraToWorld);
vec3 refl = normalize(reflect(cameraToWorldNorm, normal));
float cosAngle = abs(dot(normal, cameraToWorldNorm));
float fact = 1 - cosAngle;
fact = min(1, 1.38 - fact*fact);
vec3 newPos;
vec4 newScreen;
float i = 0;
vec3 rayTrace = worldStartingPos;
float currentWorldDist, rayDist;
float incr = 0.4;
do {
i += 0.05;
rayTrace += refl*incr;
incr *= 1.4;
newScreen = projectionMatrix * viewMatrix * vec4(rayTrace, 1);
newScreen /= newScreen.w;
newPos = texture(gPosition, newScreen.xy/2.0+0.5).xyz;
currentWorldDist = length(newPos.xyz - cameraPosition.xyz);
rayDist = length(rayTrace.xyz - cameraPosition.xyz);
if (newScreen.x > 1 || newScreen.x < -1 || newScreen.y > 1 || newScreen.y < -1 || dot(refl, cameraToWorldNorm) < 0 || cameraToWorldDist > currentWorldDist)
break;
} while(rayDist < currentWorldDist);
vec4 newColor = texture(composite0, newScreen.xy/2.0 + 0.5);
if (dot(refl, cameraToWorldNorm) < 0)
fact = 1.0;
else if (newScreen.x > 1 || newScreen.x < -1 || newScreen.y > 1 || newScreen.y < -1)
fact = 1.0;
else if (cameraToWorldDist > currentWorldDist)
fact = 1.0;
//vec3 I = normalize(position.xyz - cameraPosition);
//vec3 R = reflect(I, normalize(normal.xyz));
//vec4 imageDay = texture(skyboxDay, R);
//vec4 imageNight = texture(skyboxNight, R);
//vec4 fakeRelf = mix(imageDay, imageNight, skyboxBlendFactor);
image = image*fact + newColor *(1-fact);
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentComposite3.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentComposite4.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 1 addition & 2 deletions assets/shaders/FragmentComposite5.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -58,7 +58,6 @@ uniform sampler2DShadow gDepth;
uniform int useFXAA;
uniform int useDOF;
uniform int useMotionBlur;
uniform int useBloom;
uniform int useVolumetricLight;

/*--------------------------------------------------------*/
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentComposite6.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentCompositeFinal0.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentEntity.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentEntityBasic.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentFont.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentGui.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentParticle.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/FragmentSkybox.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
66 changes: 59 additions & 7 deletions assets/shaders/FragmentTessellator.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,17 +25,22 @@
#version 330 core

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec4 pass_position;
in mat3 TBN;
in vec3 pass_normal;
in vec3 pass_position;
in vec4 pass_Data;
in vec4 ShadowCoord;
in vec3 posInTangent;

out vec4 [5] out_Color;

uniform sampler2D texture0;
uniform sampler2DShadow depth;
uniform sampler2D normalMap;
uniform sampler2D heightMap;

uniform int useShadows;
uniform int useParallax;

vec2 poissonDisk[16] = vec2[](
vec2( -0.94201624, -0.39906216 ),
Expand All @@ -56,10 +61,53 @@ vec2 poissonDisk[16] = vec2[](
vec2( 0.14383161, -0.14100790 )
);

const float heightScale = 0.01f;

vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir) {

const float minLayers = 8;
const float maxLayers = 32;
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
// calculate the size of each layer
float layerDepth = 1.0 / numLayers;
// depth of current layer
float currentLayerDepth = 0.0;
vec2 P = viewDir.xy * heightScale;
float deltaTexCoords = P / numLayers;

vec2 currentTexCoords = texCoords;
float currentDepthMapValue = texture(heightMap, currentTexCoords).r;

while(currentLayerDepth < currentDepthMapValue) {
// shift texture coordinates along direction of P
currentTexCoords -= deltaTexCoords;
// get depthmap value at current texture coordinates
currentDepthMapValue = texture(heightMap, currentTexCoords).r;
// get depth of next layer
currentLayerDepth += layerDepth;
}
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;

// get depth after and before collision for linear interpolation
float afterDepth = currentDepthMapValue - currentLayerDepth;
float beforeDepth = texture(heightMap, prevTexCoords).r - currentLayerDepth + layerDepth;

// interpolation of texture coordinates
float weight = afterDepth / (afterDepth - beforeDepth);
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);

return finalTexCoords;
}

void main(void) {
vec4 data = pass_Data;
float bright = data.y;
int id = int(data.x + 0.5f);

vec3 eyeDir = normalize(posInTangent);
vec2 texcoords = pass_textureCoords;
if(useParallax == 1)
texcoords = ParallaxMapping(texcoords, eyeDir);

float shadow = 0;
if(useShadows == 1){
Expand All @@ -74,15 +122,19 @@ void main(void) {
}
}

vec4 textureColour = texture(texture0, pass_textureCoords);
vec4 textureColour = texture(texture0, texcoords);
if(textureColour.a < 0.5)
discard;

vec3 normal = texture(normalMap, texcoords).rgb;
normal = normalize(normal * 2.0 - 1.0);
normal = normalize(TBN * normal);

out_Color[0] = textureColour;
out_Color[1] = vec4(pass_position.xyz,0);
out_Color[2] = vec4(surfaceNormal.xyz,0);
out_Color[2] = vec4(normal,0.0);
out_Color[3] = vec4(0.0,1.0,0.0,shadow);
out_Color[4] = vec4(0.0, 0.0, 0.0, bright);
if(id == 8 || id == 13)
if(id == 8 || id == 13)
out_Color[3].r = 1.0;

}
2 changes: 1 addition & 1 deletion assets/shaders/FragmentTessellatorBasic.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 4 additions & 3 deletions assets/shaders/FragmentWater.glsl
@@ -1,7 +1,7 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Guerra24
// Copyright (c) 2015-2016 Guerra24
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,7 +24,8 @@

#version 330 core

in vec4 pass_position;
in vec3 pass_position;
in vec3 normal;
in vec4 ShadowCoord;

out vec4 [5] out_Color;
Expand Down Expand Up @@ -70,7 +71,7 @@ void main(void) {

out_Color[0] = vec4(0.0, 0.266, 0.635, 0.0);
out_Color[1] = vec4(pass_position.xyz, 0.0);
out_Color[2] = vec4(sin(pass_position.x + moveFactor) * 0.05, 1.0, cos(pass_position.z - moveFactor) * 0.02, 0.0);
out_Color[2] = vec4(normal, 0.0);
out_Color[3] = vec4(1.0,1.0,0.0,shadow);
out_Color[4] = vec4(0.0,0.0,0.0,0.0);

Expand Down

0 comments on commit 49af74e

Please sign in to comment.