Skip to content

Commit

Permalink
delete part for assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
shrekshao committed Oct 29, 2016
1 parent 7c0f281 commit ff0c1d2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 302 deletions.
40 changes: 3 additions & 37 deletions glsl/forward.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,7 @@ vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;
}

void main() {

vec3 color = vec3(0.0, 0.0, 0.0);

vec3 diffuseColor = texture2D(u_diffuse, v_uv).rgb;
vec3 ambientColor = diffuseColor * 0.2;
vec3 diffuseLight = vec3(0.0);

vec3 normal = applyNormalMap (v_normal, texture2D(u_normalMap,v_uv).rgb);

for (int i = 0; i < MAX_LIGHT_NUM; i++)
{
if (i >= u_numLights) break;

vec2 lightUV = vec2( (float(i) + 0.5 ) / float(u_numLights) , 0.5);
vec4 lightPos = vec4(texture2D(u_lightPositionTexture, lightUV).xyz, 1.0);
// float lightRadius = texture2D(u_lightColorRadiusTexture, lightUV).w;
vec4 lightColorRadius = texture2D(u_lightColorRadiusTexture, lightUV);

// shading
lightPos = u_viewMatrix * lightPos;
vec3 l = lightPos.xyz - v_eyePosition;
float dist = length(l);
// if (dist > lightColorRadius.w) continue;
l /= dist;
float attenuation = max(0.0, 1.0 - dist / lightColorRadius.w);
diffuseLight += attenuation * lightColorRadius.rgb * max(0.0, dot(normal, l));
}

color += ambientColor;

diffuseColor *= diffuseLight;
color += diffuseColor;


gl_FragColor = vec4(color, 1.0);
// gl_FragColor = vec4 (v_normal, 1.0);
void main()
{
// TODO
}
11 changes: 3 additions & 8 deletions glsl/forward.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ varying vec2 v_uv;

varying vec3 v_eyePosition;

void main() {
v_normal = normalize(u_inverseTransposeModelViewMatrix * a_normal);
v_uv = a_uv;
vec4 tmp = u_modelViewMatrix * vec4(a_position, 1.0);
v_eyePosition = tmp.xyz / tmp.w;

gl_Position = u_projectionMatrix * tmp;
// gl_Position = u_projectionMatrix * u_modelViewMatrix * vec4(a_position, 1.0);
void main()
{
// TODO
}
74 changes: 6 additions & 68 deletions glsl/lightAccumulation.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,85 +21,23 @@ uniform int u_textureHeight;
uniform sampler2D u_lightPositionTexture; //xyz
uniform sampler2D u_lightColorRadiusTexture; //rgba

uniform sampler2D u_tileLightsTexture; //
uniform sampler2D u_tileLightsTexture;


uniform sampler2D u_diffuse;
uniform sampler2D u_normalMap;


vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
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() {

ivec2 pixelIdx = ivec2(gl_FragCoord.xy); //floored
ivec2 tileIdx = pixelIdx / TILE_SIZE;
ivec2 tilePixel0Idx = tileIdx * TILE_SIZE; // first pixel idx of this tile

int lightIdx = 0;

vec3 color = vec3(0.0, 0.0, 0.0);

vec3 diffuseColor = texture2D(u_diffuse, v_uv).rgb;
vec3 ambientColor = diffuseColor * 0.2;
vec3 diffuseLight = vec3(0.0);

vec3 normal = applyNormalMap (v_normal, texture2D(u_normalMap,v_uv).rgb);


for (int y = 0; y < TILE_SIZE; y++)
{
// if (lightIdx >= u_numLights) break;

for (int x = 0; x < TILE_SIZE; x++)
{
if (lightIdx >= u_numLights) break;

ivec2 pid = tilePixel0Idx + ivec2(x, y);
vec2 uv = (vec2(pid) + vec2(0.5, 0.5)) / vec2(u_textureWidth, u_textureHeight);
bool visible = (texture2D(u_tileLightsTexture, uv).r) > 0.5;

if (visible)
{

vec2 lightUV = vec2( (float(lightIdx) + 0.5 ) / float(u_numLights) , 0.5);
vec4 lightPos = vec4(texture2D(u_lightPositionTexture, lightUV).xyz, 1.0);
// float lightRadius = texture2D(u_lightColorRadiusTexture, lightUV).w;
vec4 lightColorRadius = texture2D(u_lightColorRadiusTexture, lightUV);

// shading
lightPos = u_viewMatrix * lightPos;
vec3 l = lightPos.xyz - v_eyePosition;
float dist = length(l);
if (dist > lightColorRadius.w)
{
lightIdx++;
continue;
}
l /= dist;
float attenuation = max(0.0, 1.0 - dist / lightColorRadius.w);
diffuseLight += attenuation * lightColorRadius.rgb * max(0.0, dot(normal, l));
}

lightIdx++;
}
}

color += ambientColor;

diffuseColor *= diffuseLight;
color += diffuseColor;

gl_FragColor = vec4(color, 1.0);
// gl_FragColor = vec4(vec3(lightColorRadius.w), 1.0);
// gl_FragColor = vec4(v_normal, 1.0);
// gl_FragColor = vec4(v_uv, 0.0, 1.0);
// gl_FragColor = vec4(texture2D(u_diffuse, v_uv).rgb, 1.0);
// gl_FragColor = vec4(test, 1.0);
void main()
{
// TODO
}
10 changes: 3 additions & 7 deletions glsl/lightAccumulation.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ varying vec2 v_uv;

varying vec3 v_eyePosition;

void main() {
v_normal = normalize(u_inverseTransposeModelViewMatrix * a_normal);
v_uv = a_uv;
vec4 tmp = u_modelViewMatrix * vec4(a_position, 1.0);
v_eyePosition = tmp.xyz / tmp.w;

gl_Position = u_projectionMatrix * tmp;
void main()
{
// TODO
}
165 changes: 3 additions & 162 deletions glsl/lightCulling.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#version 100

// precision highp vec4;
precision highp float;
precision highp int;

Expand Down Expand Up @@ -29,165 +28,7 @@ uniform sampler2D u_depthTexture;



void main() {

ivec2 pixelIdx = ivec2(gl_FragCoord.xy); // floored
ivec2 tileIdx = pixelIdx / TILE_SIZE;
ivec2 tilePixel0Idx = tileIdx * TILE_SIZE; // bottom-left pixelId of this tile

ivec2 deltaIdx = pixelIdx - tilePixel0Idx;
int lightIdx = deltaIdx.y * TILE_SIZE + deltaIdx.x;

// TODO: unwrap the rgba (one pixel handle 4 lights)


#if USE_TILE_MIN_MAX_DEPTH_CULLING
// get min and max depth
float farDepth = 999999.0;
float nearDepth = -999999.0;
for (int y = 0; y < TILE_SIZE; y++)
{
for (int x = 0; x < TILE_SIZE; x++)
{
ivec2 pid = tilePixel0Idx + ivec2(x, y);
vec2 uv = (vec2(pid) + vec2(0.5, 0.5)) / vec2(u_textureWidth, u_textureHeight);

float d = texture2D(u_depthTexture, uv).r;
// transform depth value to view space

d = 2.0 * d - 1.0; //(0, 1) => (-1, 1)
d = - u_projectionMatrix[3][2] / (d + u_projectionMatrix[2][2]);

farDepth = min(d, farDepth);
nearDepth = max(d, nearDepth);
}
}
#endif



if (lightIdx < u_numLights)
{
vec2 lightUV = vec2( (float(lightIdx) + 0.5 ) / float(u_numLights) , 0.5);

vec4 lightPos = vec4(texture2D(u_lightPositionTexture, lightUV).xyz, 1.0);
float lightRadius = texture2D(u_lightColorRadiusTexture, lightUV).w;

// Test if light overlap with this tile (lightCulling)

// calculate the frustum box in view space

mat4 M = u_projectionMatrix;

vec2 fullScreenSize = vec2(u_textureWidth, u_textureHeight);

// tile position in NDC space
vec2 floorCoord = 2.0 * vec2(tilePixel0Idx) / fullScreenSize - vec2(1.0); // -1, 1
vec2 ceilCoord = 2.0 * vec2(tilePixel0Idx + ivec2(TILE_SIZE)) / fullScreenSize - vec2(1.0); // -1, 1

float viewNear = - M[3][2] / ( -1.0 + M[2][2]);
float viewFar = - M[3][2] / (1.0 + M[2][2]);
// float viewNear = -1.0;
// float viewFar = -1000.0;
vec2 viewFloorCoord = vec2( (- viewNear * floorCoord.x - M[2][0] * viewNear) / M[0][0] , (- viewNear * floorCoord.y - M[2][1] * viewNear) / M[1][1] );
vec2 viewCeilCoord = vec2( (- viewNear * ceilCoord.x - M[2][0] * viewNear) / M[0][0] , (- viewNear * ceilCoord.y - M[2][1] * viewNear) / M[1][1] );



// calculate frustumPlanes for each tile in view space

#if USE_TILE_MIN_MAX_DEPTH_CULLING
vec4 frustumPlanes[6];
#else
vec4 frustumPlanes[4];
#endif

frustumPlanes[0] = vec4(1.0, 0.0, - viewFloorCoord.x / viewNear, 0.0); // left
frustumPlanes[1] = vec4(-1.0, 0.0, viewCeilCoord.x / viewNear, 0.0); // right
frustumPlanes[2] = vec4(0.0, 1.0, - viewFloorCoord.y / viewNear, 0.0); // bottom
frustumPlanes[3] = vec4(0.0, -1.0, viewCeilCoord.y / viewNear, 0.0); // top

#if USE_TILE_MIN_MAX_DEPTH_CULLING
frustumPlanes[4] = vec4(0.0, 0.0, -1.0, nearDepth); // near
frustumPlanes[5] = vec4(0.0, 0.0, 1.0, -farDepth); // far
#endif

// transform lightPos to view space
lightPos = u_viewMatrix * lightPos;
lightPos /= lightPos.w;

vec4 boxMin = lightPos - vec4( vec3(lightRadius), 0.0);
vec4 boxMax = lightPos + vec4( vec3(lightRadius), 0.0);


float dp = 0.0; //dot product

#if USE_TILE_MIN_MAX_DEPTH_CULLING
for (int i = 0; i < 6; i++)
#else
dp += lightPos.z > viewNear + lightRadius ? -1.0 : 0.0;
dp += lightPos.z < viewFar - lightRadius ? -1.0 : 0.0;

for (int i = 0; i < 4; i++)
#endif
{
dp += min(0.0, dot(
vec4(
frustumPlanes[i].x > 0.0 ? boxMax.x : boxMin.x,
frustumPlanes[i].y > 0.0 ? boxMax.y : boxMin.y,
frustumPlanes[i].z > 0.0 ? boxMax.z : boxMin.z,
1.0),
frustumPlanes[i]));
}




if (dp < 0.0)
{
// exists some of the plane fails the test
// no overlapping

gl_FragColor = vec4(0.0, 0.0, 0.5, 1.0);
// gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
else
{
// overlapping
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}



// ------------ debug output ------------------
// gl_FragColor = vec4(frustumPlanes[0].x / 10.0, 0.0, 0.0, 1.0);
// gl_FragColor = vec4(viewFloorCoord*2.0 - 0.1, 0.0, 1.0);
// gl_FragColor = vec4(vec3(-viewNear * 0.5), 1.0);
// gl_FragColor = vec4(vec3(-viewFar / 2000.0), 1.0);
// gl_FragColor = vec4(vec3(-nearDepth)/20.0, 1.0);
// gl_FragColor = vec4( 0.5 * (lightPos.xy + 1.0), 0.0, 1.0);
// gl_FragColor = vec4(vec2(tilePixel0Idx) / fullScreenSize, 0.0 , 1.0);
// gl_FragColor = vec4(vec3(1.0 - lightRadius), 1.0);
// gl_FragColor = vec4(vec3(lightRadius), 1.0);
// gl_FragColor = vec4(vec3(radiusHorizontalNDC), 1.0);

// gl_FragColor = vec4(vec3(1.0 - 0.0), 1.0);
// gl_FragColor = vec4(floorCoord.xy, 0.0, 1.0);
// gl_FragColor = vec4(ceilCoord.xy, 0.0, 1.0);
// gl_FragColor = vec4(radiusHorizontalNDC, radiusVerticalNDC, 0.0, 1.0);



// uv that we are going to write 1/0 for u_tileLightsTexture
// vec2 uv = (vec2(pixelIdx) + vec2(0.5, 0.5)) / vec2(u_textureWidth, u_textureHeight);


// // Debug output: lightPos
// gl_FragColor = vec4(0.0, lightPos.y / 18.0, 0.0, 1.0);
}
else
{
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}

void main()
{
// TODO
}
2 changes: 1 addition & 1 deletion glsl/lightDebug.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ precision highp int;

void main()
{
gl_FragColor = vec4(1.0);
// TODO
}
4 changes: 2 additions & 2 deletions glsl/lightDebug.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ uniform mat4 u_projectionMatrix;
attribute vec3 a_position;

void main() {
gl_Position = u_projectionMatrix * u_viewMatrix * vec4(a_position, 1.0);
gl_PointSize = 5.0;
// gl_Position = TODO
// gl_PointSize = 5.0;
}
Loading

0 comments on commit ff0c1d2

Please sign in to comment.