diff --git a/glsl/forward.frag.glsl b/glsl/forward.frag.glsl index 41e337f..2108367 100644 --- a/glsl/forward.frag.glsl +++ b/glsl/forward.frag.glsl @@ -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 } \ No newline at end of file diff --git a/glsl/forward.vert.glsl b/glsl/forward.vert.glsl index 0e0ad2d..8417456 100644 --- a/glsl/forward.vert.glsl +++ b/glsl/forward.vert.glsl @@ -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 } \ No newline at end of file diff --git a/glsl/lightAccumulation.frag.glsl b/glsl/lightAccumulation.frag.glsl index dd013a1..a7ee39c 100644 --- a/glsl/lightAccumulation.frag.glsl +++ b/glsl/lightAccumulation.frag.glsl @@ -21,14 +21,15 @@ 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)); @@ -36,70 +37,7 @@ vec3 applyNormalMap(vec3 geomnor, vec3 normap) { 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 } \ No newline at end of file diff --git a/glsl/lightAccumulation.vert.glsl b/glsl/lightAccumulation.vert.glsl index e804946..8417456 100644 --- a/glsl/lightAccumulation.vert.glsl +++ b/glsl/lightAccumulation.vert.glsl @@ -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 } \ No newline at end of file diff --git a/glsl/lightCulling.frag.glsl b/glsl/lightCulling.frag.glsl index 8931751..459fb89 100644 --- a/glsl/lightCulling.frag.glsl +++ b/glsl/lightCulling.frag.glsl @@ -1,6 +1,5 @@ #version 100 -// precision highp vec4; precision highp float; precision highp int; @@ -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 } \ No newline at end of file diff --git a/glsl/lightDebug.frag.glsl b/glsl/lightDebug.frag.glsl index e24d3b8..7589042 100644 --- a/glsl/lightDebug.frag.glsl +++ b/glsl/lightDebug.frag.glsl @@ -5,5 +5,5 @@ precision highp int; void main() { - gl_FragColor = vec4(1.0); + // TODO } \ No newline at end of file diff --git a/glsl/lightDebug.vert.glsl b/glsl/lightDebug.vert.glsl index ac188e8..77d2bd8 100644 --- a/glsl/lightDebug.vert.glsl +++ b/glsl/lightDebug.vert.glsl @@ -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; } \ No newline at end of file diff --git a/js/forwardPlusRenderer/pass/lightDebug.js b/js/forwardPlusRenderer/pass/lightDebug.js index 8b4f02b..f0553ef 100644 --- a/js/forwardPlusRenderer/pass/lightDebug.js +++ b/js/forwardPlusRenderer/pass/lightDebug.js @@ -23,10 +23,11 @@ var ForwardPlusRenderer = ForwardPlusRenderer || {}; // Retrieve the uniform and attribute locations - p.u_viewMatrix = gl.getUniformLocation(prog, 'u_viewMatrix'); - p.u_projectionMatrix = gl.getUniformLocation(prog, 'u_projectionMatrix'); - - p.a_position = gl.getAttribLocation(prog, 'a_position'); + // get uniform and attribute location from shader + // TODO: uncomment + // p.u_viewMatrix = gl.getUniformLocation(prog, 'u_viewMatrix'); + // p.u_projectionMatrix = gl.getUniformLocation(prog, 'u_projectionMatrix'); + // p.a_position = gl.getAttribLocation(prog, 'a_position'); console.log("Shader Loaded: lightDebug"); }; @@ -35,24 +36,34 @@ var ForwardPlusRenderer = ForwardPlusRenderer || {}; FPR.pass.lightDebug.render = function () { var gl = FPR.gl; - gl.useProgram(this.program); - // gl.disable(gl.DEPTH_TEST); - - gl.uniformMatrix4fv(this.u_viewMatrix, false, FPR.camera.matrixWorldInverse.elements); - gl.uniformMatrix4fv(this.u_projectionMatrix, false, FPR.camera.projectionMatrix.elements); + // use light debug program + // TODO: uncomment + // gl.useProgram(this.program); - gl.bindBuffer(gl.ARRAY_BUFFER, lightPositionBuffer); + // assign uniform matrix value + // TODO: uncomment + // gl.uniformMatrix4fv(this.u_viewMatrix, false, FPR.camera.matrixWorldInverse.elements); + // gl.uniformMatrix4fv(this.u_projectionMatrix, false, FPR.camera.projectionMatrix.elements); - gl.bufferData(gl.ARRAY_BUFFER, FPR.light.position, gl.DYNAMIC_DRAW); + // bind light position buffer as array buffer to draw + // also copy light position data to the buffer, since it gets updated each frame + // TODO: uncomment + // gl.bindBuffer(gl.ARRAY_BUFFER, lightPositionBuffer); + // gl.bufferData(gl.ARRAY_BUFFER, FPR.light.position, gl.DYNAMIC_DRAW); - gl.enableVertexAttribArray(this.a_position); - gl.vertexAttribPointer(this.a_position, 3, gl.FLOAT, false, 0, 0); + // set vertex attribute pointer + // TODO: uncomment + // gl.enableVertexAttribArray(this.a_position); + // gl.vertexAttribPointer(this.a_position, 3, gl.FLOAT, false, 0, 0); - gl.drawArrays(gl.POINTS, 0, FPR.NUM_LIGHTS); + // draw lights as POINTS + // TODO: uncomment + // gl.drawArrays(gl.POINTS, 0, FPR.NUM_LIGHTS); - gl.bindBuffer(gl.ARRAY_BUFFER, null); + // unbind buffer + // TODO: uncomment + // gl.bindBuffer(gl.ARRAY_BUFFER, null); - // gl.enable(gl.DEPTH_TEST); }; })(); \ No newline at end of file diff --git a/js/ui.js b/js/ui.js index 7270702..31bc05c 100644 --- a/js/ui.js +++ b/js/ui.js @@ -7,7 +7,8 @@ var Cfg = function() { // TODO: Define config fields and defaults here - this.curPipeline = 'forwardPlusPipeline'; + // this.curPipeline = 'forwardPlusPipeline'; + this.curPipeline = 'depthDebugPipeline'; this.lightPositionDebug = true; this.enableEffect0 = false; };