Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Submission: Ziwei Zong #5

Open
wants to merge 17 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
451 changes: 48 additions & 403 deletions README.md

Large diffs are not rendered by default.

Binary file added glsl/.DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions glsl/copy.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ varying vec3 v_position;
varying vec3 v_normal;
varying vec2 v_uv;

vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
normap = normap * 2.0 - 1.0;
vec3 up = normalize(vec3(0.001, 1, 0.001));
vec3 surftan = normalize(cross(geomnor, up));
vec3 surfbinor = cross(geomnor, surftan);
return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;
}

void main() {
// TODO: copy values into gl_FragData[0], [1], etc.

vec3 geomnor = v_normal;
vec3 normap = texture2D(u_normap,v_uv).rgb;
vec3 nor = normalize(applyNormalMap(geomnor,normap));

gl_FragData[0] = vec4(v_position,nor.x);

vec3 col = texture2D(u_colmap,v_uv).rgb*texture2D(u_colmap,v_uv).a;
gl_FragData[1] = vec4(col,nor.z);
//gl_FragData[2] = vec4(col,nor.z);
//gl_FragData[3] = vec4(texture2D(u_normap,v_uv));
}
17 changes: 10 additions & 7 deletions glsl/deferred/ambient.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

uniform sampler2D u_gbufs[NUM_GBUFFERS];
uniform sampler2D u_depth;

varying vec2 v_uv;

void main() {
vec4 gb0 = texture2D(u_gbufs[0], v_uv);
vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[2], v_uv);
vec4 gb3 = texture2D(u_gbufs[3], v_uv);
//vec4 gb0 = texture2D(u_gbufs[0], v_uv);
//vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[1], v_uv);
//vec4 gb3 = texture2D(u_gbufs[3], v_uv);
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables

vec3 colmap = gb2.xyz;

if (depth == 1.0) {
gl_FragColor = vec4(0, 0, 0, 0); // set alpha to 0
return;
}

gl_FragColor = vec4(0.1, 0.1, 0.1, 1); // TODO: replace this
// TODO: replace this
colmap = clamp(colmap,0.0,1.0);
gl_FragColor = vec4(0.2*colmap,1.0);//vec4(0.1, 0.1, 0.1, 1);
}
59 changes: 43 additions & 16 deletions glsl/deferred/blinnphong-pointlight.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

uniform bool u_toon;
uniform vec3 u_camPos;
uniform vec3 u_lightCol;
uniform vec3 u_lightPos;
uniform float u_lightRad;
Expand All @@ -12,28 +14,53 @@ uniform sampler2D u_depth;

varying vec2 v_uv;

vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
normap = normap * 2.0 - 1.0;
vec3 up = normalize(vec3(0.001, 1, 0.001));
vec3 surftan = normalize(cross(geomnor, up));
vec3 surfbinor = cross(geomnor, surftan);
return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;
}

void main() {
vec4 gb0 = texture2D(u_gbufs[0], v_uv);
vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[2], v_uv);
vec4 gb3 = texture2D(u_gbufs[3], v_uv);
vec4 gb0 = texture2D(u_gbufs[0], v_uv); //pos
//vec4 gb1 = texture2D(u_gbufs[1], v_uv); //nor
vec4 gb2 = texture2D(u_gbufs[1], v_uv); //colmap
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables

vec3 pos = gb0.xyz;
vec3 diff = gb2.xyz;
vec3 nor = vec3(gb0.a,0.0,gb2.a);

//nor.x = sqrt(1.0 - nor.y*nor.y - nor.z*nor.z);
nor.y = sqrt(1.0 - nor.x*nor.x - nor.z*nor.z);
//nor.z = sqrt(1.0-nor.x*nor.x-nor.y*nor.y);
vec3 viewDir = normalize(u_camPos - pos);

if(dot(-viewDir,nor)<=0.0) nor.y = -nor.y;

vec3 lightDir = normalize(u_lightPos - pos);
vec3 reflDir = reflect(-lightDir,nor);


// If nothing was rendered to this pixel, set alpha to 0 so that the
// postprocessing step can render the sky color.
if (depth == 1.0) {
gl_FragColor = vec4(0, 0, 0, 0);
return;
}

gl_FragColor = vec4(0, 0, 1, 1); // TODO: perform lighting calculations
//http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/code/WebGLShaderLightMat/ShaderLightMat.html
// TO_DO: perform lighting calculations

float attenuation = max(0.0, u_lightRad - length(pos-u_lightPos));
attenuation/=u_lightRad;
//attenuation*=attenuation;
float lamb = max(dot(lightDir,nor),0.0);
float spec = 0.0;
if(lamb>0.0)
{
float specAngle = max(dot(reflDir,viewDir),0.0);
spec = pow(specAngle,4.0);
}
if(u_toon)//toon
{
//lamb = lamb>0.5?1.0:0.0;
if(lamb<0.1) lamb = 0.0;
else if(lamb<0.6) lamb = 0.6;
else lamb = 1.0;
spec = spec>0.5?1.0:0.0;
}
gl_FragColor = vec4(attenuation*(lamb+spec)*diff*u_lightCol, 1.0);
}
26 changes: 11 additions & 15 deletions glsl/deferred/debug.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

uniform int u_debug;
uniform sampler2D u_gbufs[NUM_GBUFFERS];
Expand All @@ -22,30 +22,26 @@ vec3 applyNormalMap(vec3 geomnor, vec3 normap) {

void main() {
vec4 gb0 = texture2D(u_gbufs[0], v_uv);
vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[2], v_uv);
vec4 gb3 = texture2D(u_gbufs[3], v_uv);
//vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[1], v_uv);
//vec4 gb3 = texture2D(u_gbufs[3], v_uv);
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables
// TO_DO: Extract needed properties from the g-buffers into local variables
// These definitions are suggested for starting out, but you will probably want to change them.
vec3 pos; // World-space position
vec3 geomnor; // Normals of the geometry as defined, without normal mapping
vec3 colmap; // The color map - unlit "albedo" (surface color)
vec3 normap; // The raw normal map (normals relative to the surface they're on)
vec3 nor; // The true normals as we want to light them - with the normal map applied to the geometry normals (applyNormalMap above)
vec3 pos = gb0.xyz; // World-space position
vec3 colmap = gb2.xyz; // Normals of the geometry as defined, without normal mapping

vec3 nor = vec3(gb0.a,0.0,gb2.a);
nor.y = sqrt(1.0 - nor.x*nor.x - nor.z*nor.z);

if (u_debug == 0) {
gl_FragColor = vec4(vec3(depth), 1.0);
} else if (u_debug == 1) {
gl_FragColor = vec4(abs(pos) * 0.1, 1.0);
} else if (u_debug == 2) {
gl_FragColor = vec4(abs(geomnor), 1.0);
gl_FragColor = vec4(abs(nor), 1.0);
} else if (u_debug == 3) {
gl_FragColor = vec4(colmap, 1.0);
} else if (u_debug == 4) {
gl_FragColor = vec4(normap, 1.0);
} else if (u_debug == 5) {
gl_FragColor = vec4(abs(nor), 1.0);
} else {
gl_FragColor = vec4(1, 0, 1, 1);
}
Expand Down
Empty file added glsl/deferred/untitled 2.txt
Empty file.
Empty file added glsl/deferred/untitled.txt
Empty file.
33 changes: 33 additions & 0 deletions glsl/post/bloomX.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform vec2 u_texSize;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

void main() {

vec2 onePixel = vec2(1.0,1.0)/u_texSize;
vec4 gauss = vec4 (10.0,8.0,5.0,2.0) ;
gauss*=(1.0/25.0);
vec4 color = vec4 (0.0,0.0,0.0,0.0);//texture2D(u_color, v_uv);

color += texture2D(u_color, v_uv + onePixel * vec2(0, 0))*gauss[0];

color += texture2D(u_color, v_uv + onePixel * vec2(-1, 0))*gauss[1];
color += texture2D(u_color, v_uv + onePixel * vec2(-2, 0))*gauss[2];
color += texture2D(u_color, v_uv + onePixel * vec2(-3, 0))*gauss[3];

color += texture2D(u_color, v_uv + onePixel * vec2(1, 0))*gauss[1];
color += texture2D(u_color, v_uv + onePixel * vec2(2, 0))*gauss[2];
color += texture2D(u_color, v_uv + onePixel * vec2(3, 0))*gauss[3];


gl_FragColor = color;

//gl_FragColor = texture2D(u_color, v_uv);
}
41 changes: 41 additions & 0 deletions glsl/post/bloomY.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform sampler2D u_origCol;

uniform int u_debug;
uniform vec2 u_texSize;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

void main() {

vec2 onePixel = vec2(1.0,1.0)/u_texSize;
vec4 gauss = vec4 (10.0,8.0,5.0,2.0) ;
gauss*=(1.0/25.0);
vec4 color = vec4 (0.0,0.0,0.0,0.0);//texture2D(u_color, v_uv);

color += texture2D(u_color, v_uv + onePixel * vec2(0, 0))*gauss[0];

color += texture2D(u_color, v_uv + onePixel * vec2(0, 1))*gauss[1];
color += texture2D(u_color, v_uv + onePixel * vec2(0, 2))*gauss[2];
color += texture2D(u_color, v_uv + onePixel * vec2(0, 3))*gauss[3];

color += texture2D(u_color, v_uv + onePixel * vec2(0, -1))*gauss[1];
color += texture2D(u_color, v_uv + onePixel * vec2(0, -2))*gauss[2];
color += texture2D(u_color, v_uv + onePixel * vec2(0, -3))*gauss[3];


//gl_FragColor = color;
vec4 origCol = texture2D(u_origCol, v_uv);
if (origCol.a == 0.0) {
origCol = SKY_COLOR;
}
if(u_debug==5) origCol = vec4(0.0);
gl_FragColor = color+origCol;

}
59 changes: 59 additions & 0 deletions glsl/post/motion.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform sampler2D u_pos;
//uniform sampler2D u_depthTex;

uniform mat4 u_cameraMat;
uniform mat4 u_prevMat;


uniform vec3 u_crntEye;
uniform vec3 u_prevEye;

uniform vec2 u_texSize;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void main() {

vec2 oneP = vec2(1.0,1.0)/u_texSize;

vec4 origCol = texture2D(u_color, v_uv);

vec4 testM = vec4(0.5,0.5,0.5,1.0);
if(u_crntEye==u_prevEye) testM.r = 0.7;
//if(u_cameraMat==u_prevMat) testM.r = 0.7;
else testM.g = 0.7;

vec3 vel = u_prevEye-u_crntEye;
vec3 pos = texture2D(u_pos, v_uv).xyz;
vec3 newPos = pos+vel;

vec2 sc_pos = (vec4(pos,1.0)*u_prevMat).xy;
vec2 sc_newPos = (vec4(newPos,1.0)*u_cameraMat).xy;

vec2 uv_vel = (sc_newPos-sc_pos).xy*0.5;

origCol = vec4(0.0,0.0,0.0,0.0);//origCol*(1.0/11.0);
//v_uv += vel.xy*oneP;

for(int i = 0; i < 10; ++i)
{
vec4 tempCol = texture2D(u_color, v_uv + float(i)*oneP*uv_vel);
origCol += tempCol/10.0;
}


gl_FragColor = origCol;//*testM;

//gl_FragColor = texture2D(u_color, v_uv);
}
2 changes: 1 addition & 1 deletion glsl/post/one.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ precision highp float;
precision highp int;

uniform sampler2D u_color;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

void main() {

vec4 color = texture2D(u_color, v_uv);

if (color.a == 0.0) {
Expand Down
27 changes: 27 additions & 0 deletions glsl/post/srcmask.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform float u_thresh;
varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

void main() {

vec4 org_color = texture2D(u_color, v_uv);
vec4 color = org_color;//clamp(org_color,0.0,1.0);
float colMag = color.a*length(color.rgb);


//gl_FragColor = color;
//return;

if (colMag>=u_thresh)//TODO: later,uniform
{
gl_FragColor = org_color;
}
else
gl_FragColor = vec4(0,0,0,0);
}
Loading