Skip to content

Project 6 Submission #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
331 changes: 163 additions & 168 deletions README.md

Large diffs are not rendered by default.

84 changes: 76 additions & 8 deletions base/res/shaders/ambient.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_AO 6
#define DISPLAY_TOON 7
#define DISPLAY_SPEC 8


/////////////////////////////////////
Expand Down Expand Up @@ -67,7 +70,7 @@ vec3 samplePos(vec2 texcoords) {
return texture(u_Positiontex,texcoords).xyz;
}

//Helper function to automicatlly sample and unpack positions
//Helper function to automicatlly sample and unpack colors
vec3 sampleCol(vec2 texcoords) {
return texture(u_Colortex,texcoords).xyz;
}
Expand Down Expand Up @@ -95,21 +98,86 @@ float getRandomScalar(vec2 texcoords) {
const float occlusion_strength = 1.5f;
void main() {

// depth data
float exp_depth = texture(u_Depthtex, fs_Texcoord).r;
float lin_depth = linearizeDepth(exp_depth,u_Near,u_Far);

// fragment data
vec3 normal = sampleNrm(fs_Texcoord);
vec3 position = samplePos(fs_Texcoord);
vec3 color = sampleCol(fs_Texcoord);

// ambient light data
vec3 light = u_Light.xyz;
float strength = u_Light.w;
if (lin_depth > 0.99f) {
out_Color = vec4(vec3(0.0), 1.0);
} else {
float ambient = u_LightIl;
float diffuse = max(0.0, dot(normalize(light),normal));
out_Color = vec4(color*(strength*diffuse + ambient),1.0f);
}

// computer SSAO (screen space ambient occlusion)
// float occl = sampleBlurredDepth(fs_Texcoord);

vec2 sz = textureSize(u_Positiontex, 0);

float hup = 0.0; float tup = 0.0;
float hdown = 0.0; float tdown = 0.0;
float hleft = 0.0; float tleft = 0.0;
float hright = 0.0; float tright = 0.0;

for (float i = 1.0; i <= 6.0; i += 1.0) {

// neighbor positions
vec3 posUp = samplePos(vec2(fs_Texcoord.s, fs_Texcoord.t + (1.0 / sz.y)));
vec3 posDown = samplePos(vec2(fs_Texcoord.s, fs_Texcoord.t - (1.0 / sz.y)));
vec3 posRight = samplePos(vec2(fs_Texcoord.s + (1.0 / sz.y), fs_Texcoord.t));
vec3 posLeft = samplePos(vec2(fs_Texcoord.s - (1.0 / sz.y), fs_Texcoord.t));

// up occlusion
vec3 Hup = posUp - position;
vec3 Tup = cross(normal, vec3(0,1,0));
if (Hup.z > 0.01) {
hup += atan(Hup.z / length(Hup.xy)) / 6.0;
tup += atan(Tup.z / length(Tup.xy)) / 6.0;
}

// down occlusion
vec3 Hdown = posDown - position;
vec3 Tdown = cross(normal, vec3(0,-1,0));
if (Hdown.z > 0.01) {
hdown += atan(Hdown.z / length(Hdown.xy)) / 6.0;
tdown += atan(Tdown.z / length(Tdown.xy)) / 6.0;
}

// right occlusion
vec3 Hright = posRight - position;
vec3 Tright = cross(normal, vec3(1,0,0));
if (Hright.z > 0.01) {
hright += atan(Hright.z / length(Hright.xy)) / 6.0;
tright += atan(Tright.z / length(Tright.xy)) / 6.0;
}

// left occlusion
vec3 Hleft = posLeft - position;
vec3 Tleft = cross(normal, vec3(-1,0,0));
if (Hleft.z > 0.01) {
hleft += atan(Hleft.z / length(Hleft.xy)) / 6.0;
tleft += atan(Tleft.z / length(Tleft.xy)) / 6.0;
}
}

// ambient occlusion term
float occl = (sin(hup) - sin(tup)) / 4.0 + (sin(hdown) - sin(tdown)) / 4.0 + (sin(hright) - sin(tright)) / 4.0 + (sin(hleft) - sin(tleft)) / 4.0;

if (u_DisplayType == DISPLAY_AO) {
out_Color = vec4(1.0-occl);
}
else {
// compute final color
if (lin_depth > 0.99f) {
out_Color = vec4(vec3(0.0), 1.0);
} else {
float ambient = u_LightIl;
float diffuse = max(0.0, dot(normalize(light),normal));
out_Color = vec4(color*(strength*diffuse + ambient*(1.0-occl)),1.0f);
}
}
return;
}

3 changes: 3 additions & 0 deletions base/res/shaders/diagnostic.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_AO 6
#define DISPLAY_TOON 7
#define DISPLAY_SPEC 8


/////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions base/res/shaders/directional.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_AO 6
#define DISPLAY_TOON 7
#define DISPLAY_SPEC 8


/////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions base/res/shaders/pass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ in vec4 fs_Position;
out vec4 out_Normal;
out vec4 out_Position;
out vec4 out_Color;
out vec4 out_Glow;

void main(void)
{
out_Normal = vec4(normalize(fs_Normal),0.0f);
out_Position = vec4(fs_Position.xyz,1.0f); //Tuck position into 0 1 range
float lum = 0.2126*u_Color.r + 0.7152*u_Color.g + 0.0722*u_Color.b;
out_Glow = vec4(vec3(lum), 1.0);
out_Color = vec4(u_Color,1.0);
}
68 changes: 65 additions & 3 deletions base/res/shaders/point.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_AO 6
#define DISPLAY_TOON 7
#define DISPLAY_SPEC 8


/////////////////////////////////////
Expand All @@ -21,6 +24,7 @@ uniform sampler2D u_Depthtex;
uniform sampler2D u_Normaltex;
uniform sampler2D u_Positiontex;
uniform sampler2D u_Colortex;
uniform sampler2D u_Glowtex;
uniform sampler2D u_RandomNormaltex;
uniform sampler2D u_RandomScalartex;

Expand Down Expand Up @@ -71,6 +75,11 @@ vec3 sampleCol(vec2 texcoords) {
return texture(u_Colortex,texcoords).xyz;
}

//Helper function to automicatlly sample and unpack positions
vec3 sampleGlow(vec2 texcoords) {
return texture(u_Glowtex,texcoords).xyz;
}

//Get a random normal vector given a screen-space texture coordinate
//Actually accesses a texture of random vectors
vec3 getRandomNormal(vec2 texcoords) {
Expand Down Expand Up @@ -100,17 +109,70 @@ void main() {
vec3 normal = sampleNrm(fs_Texcoord);
vec3 position = samplePos(fs_Texcoord);
vec3 color = sampleCol(fs_Texcoord);
float glow = sampleGlow(fs_Texcoord).r;

vec3 light = u_Light.xyz;
float lightRadius = u_Light.w;
out_Color = vec4(0,0,0,1.0);
out_Color = vec4(0, 0, 0, 0);
if( u_DisplayType == DISPLAY_LIGHTS )
{
//Put some code here to visualize the fragment associated with this point light
out_Color += vec4(0, 0, 0.4, 0);
}
else if (u_DisplayType == DISPLAY_TOON )
{
ivec2 sz = textureSize(u_Normaltex,0);
vec3 normal_up = sampleNrm(vec2(fs_Texcoord.x+(1.0/sz.x), fs_Texcoord.y));
vec3 normal_down = sampleNrm(vec2(fs_Texcoord.x-(1.0/sz.x), fs_Texcoord.y));
vec3 normal_right = sampleNrm(vec2(fs_Texcoord.x, fs_Texcoord.y+(1.0/sz.y)));
vec3 normal_left = sampleNrm(vec2(fs_Texcoord.x, fs_Texcoord.y-(1.0/sz.y)));

if ( dot(normal, normal_up) <= 0.96 ||
dot(normal, normal_down) <= 0.96 ||
dot(normal, normal_right) <= 0.96 ||
dot(normal, normal_left) <= 0.96)
{
out_Color = vec4(vec3(0), 1.0f);
}

else
{
vec3 dirToLight = vec3(light.xyz - position.xyz);
float distFromLight = length(dirToLight);
if (distFromLight < lightRadius)
{
float I = max(0.0, dot(normal, normalize(dirToLight)))*u_Light.w / (distFromLight / (lightRadius - distFromLight));

// toon shading
if (I <= 0.05) { I = 0.1; }
else if (I <= 0.10) { I = 0.3; }
else if (I <= 0.15) { I = 0.5; }
else if (I <= 0.20) { I = 0.7; }
else { I = 0.9; }

out_Color += vec4(I);
out_Color *= vec4(color, 1.0);
}
}
}
else
{
//Put some code here to actually compute the light from the point light
vec3 dirToLight = vec3(light.xyz - position.xyz);
float distFromLight = length(dirToLight);
if (distFromLight < lightRadius) {
float I = max(0.0, dot(normal, normalize(dirToLight)))*u_Light.w / (distFromLight / (lightRadius - distFromLight));

float diffuse = max(0.0, dot(normalize(dirToLight),normal));

out_Color += diffuse*vec4(vec3(I), 1.0)*vec4(color, 1.0);

if (u_DisplayType == DISPLAY_SPEC) {
vec3 specRefl = 2.0 * dot(normal, dirToLight) * normal - dirToLight;
float specular = clamp(pow(dot(-position, specRefl), glow), 0.0, 1.0);
out_Color += glow*glow*specular*vec4(vec3(I), 1.0)*vec4(color,1.0);
}
}
}

return;
}

90 changes: 89 additions & 1 deletion base/res/shaders/post.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
#define DISPLAY_COLOR 3
#define DISPLAY_TOTAL 4
#define DISPLAY_LIGHTS 5
#define DISPLAY_AO 6
#define DISPLAY_TOON 7
#define DISPLAY_SPEC 8
#define DISPLAY_GLOW 9


/////////////////////////////////////
// Uniforms, Attributes, and Outputs
////////////////////////////////////
uniform sampler2D u_Posttex;
uniform sampler2D u_Glowtex;
uniform sampler2D u_RandomNormaltex;
uniform sampler2D u_RandomScalartex;

uniform int u_ScreenWidth;
uniform int u_ScreenHeight;

uniform int u_DisplayType;

in vec2 fs_Texcoord;

out vec4 out_Color;
Expand All @@ -43,6 +50,11 @@ vec3 sampleCol(vec2 texcoords) {
return texture(u_Posttex,texcoords).xyz;
}

//Helper function to automicatlly sample and unpack positions
vec3 sampleGlow(vec2 texcoords) {
return texture(u_Glowtex,texcoords).xyz;
}

//Get a random normal vector given a screen-space texture coordinate
//Actually accesses a texture of random vectors
vec3 getRandomNormal(vec2 texcoords) {
Expand All @@ -68,7 +80,83 @@ void main() {
vec3 color = sampleCol(fs_Texcoord);
float gray = dot(color, vec3(0.2125, 0.7154, 0.0721));
float vin = min(2*distance(vec2(0.5), fs_Texcoord), 1.0);
out_Color = vec4(mix(pow(color,vec3(1.0/1.8)),vec3(gray),vin), 1.0);
vec3 glow = sampleGlow(fs_Texcoord);

if (u_DisplayType == DISPLAY_GLOW)
{
vec2 glowWidth = textureSize(u_Glowtex, 0);

int sz = 49;
float gaussKernel[49] = float[](
0.010463550522761987,
0.01131615298268607,
0.012197501949023151,
0.013103742011676042,
0.014030466903471261,
0.014972739372363077,
0.01592512163091689,
0.016881716501857894,
0.01783621912965036,
0.018781978866145247,
0.019712070670514083,
0.020619375097847215,
0.021496665695212178,
0.022336702387036944,
0.02313232922160704,
0.023876574674941628,
0.024562752574221497,
0.02518456161604963,
0.02573618141953292,
0.026212363073296496,
0.02660851221015391,
0.026920762772496484,
0.027146039812917307,
0.02728210990372968,
0.02732761799978166,
0.02728210990372968,
0.027146039812917307,
0.026920762772496484,
0.02660851221015391,
0.026212363073296496,
0.02573618141953292,
0.02518456161604963,
0.024562752574221497,
0.023876574674941628,
0.02313232922160704,
0.022336702387036944,
0.021496665695212178,
0.020619375097847215,
0.019712070670514083,
0.018781978866145247,
0.01783621912965036,
0.016881716501857894,
0.01592512163091689,
0.014972739372363077,
0.014030466903471261,
0.013103742011676042,
0.012197501949023151,
0.01131615298268607,
0.010463550522761987
);

float accum = 0.0;
for (float i = -sz/2; i <= sz/2; i += 1.0) {
int index = int(i) + sz/2;
accum += gaussKernel[index]
* sampleGlow(vec2(fs_Texcoord.s + (i/glowWidth.x), fs_Texcoord.t))
* sampleCol(vec2(fs_Texcoord.s + (i/glowWidth.x), fs_Texcoord.t));
accum += gaussKernel[index]
* sampleGlow(vec2(fs_Texcoord.s, fs_Texcoord.t + (i/glowWidth.y)))
* sampleCol(vec2(fs_Texcoord.s, fs_Texcoord.t + (i/glowWidth.y)));
}

vec3 c = accum + color;
out_Color = vec4(c, 1.0);
}
else
{
out_Color = vec4(color,1.0);//vec4(mix(pow(color,vec3(1.0/1.8)),vec3(gray),vin), 1.0);
}
return;
}

Loading