diff --git a/tutorial/src/main/assets/shaders/csm/10.debug_cascade.frag b/tutorial/src/main/assets/shaders/csm/debug_cascade.frag similarity index 100% rename from tutorial/src/main/assets/shaders/csm/10.debug_cascade.frag rename to tutorial/src/main/assets/shaders/csm/debug_cascade.frag diff --git a/tutorial/src/main/assets/shaders/csm/10.debug_cascade.vert b/tutorial/src/main/assets/shaders/csm/debug_cascade.vert similarity index 100% rename from tutorial/src/main/assets/shaders/csm/10.debug_cascade.vert rename to tutorial/src/main/assets/shaders/csm/debug_cascade.vert diff --git a/tutorial/src/main/assets/shaders/csm/10.debug_quad.vert b/tutorial/src/main/assets/shaders/csm/debug_quad.vert similarity index 100% rename from tutorial/src/main/assets/shaders/csm/10.debug_quad.vert rename to tutorial/src/main/assets/shaders/csm/debug_quad.vert diff --git a/tutorial/src/main/assets/shaders/csm/10.debug_quad_depth.frag b/tutorial/src/main/assets/shaders/csm/debug_quad_depth.frag similarity index 92% rename from tutorial/src/main/assets/shaders/csm/10.debug_quad_depth.frag rename to tutorial/src/main/assets/shaders/csm/debug_quad_depth.frag index 3a71910c..ead97666 100644 --- a/tutorial/src/main/assets/shaders/csm/10.debug_quad_depth.frag +++ b/tutorial/src/main/assets/shaders/csm/debug_quad_depth.frag @@ -1,5 +1,8 @@ #version 320 es precision mediump float; +precision mediump int; +precision mediump sampler2DArray; + out vec4 FragColor; in vec2 TexCoords; diff --git a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping.frag b/tutorial/src/main/assets/shaders/csm/shadow_mapping.frag similarity index 91% rename from tutorial/src/main/assets/shaders/csm/10.shadow_mapping.frag rename to tutorial/src/main/assets/shaders/csm/shadow_mapping.frag index 36e47894..756477d5 100644 --- a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping.frag +++ b/tutorial/src/main/assets/shaders/csm/shadow_mapping.frag @@ -1,5 +1,8 @@ #version 320 es precision mediump float; +precision mediump int; +precision mediump sampler2DArray; + out vec4 FragColor; in VS_OUT { @@ -61,19 +64,19 @@ float ShadowCalculation(vec3 fragPosWorldSpace) // calculate bias (based on depth map resolution and slope) vec3 normal = normalize(fs_in.Normal); float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); - const float biasModifier = 0.5f; + const float biasModifier = 0.5; if (layer == cascadeCount) { - bias *= 1 / (farPlane * biasModifier); + bias *= 1.0 / (farPlane * biasModifier); } else { - bias *= 1 / (cascadePlaneDistances[layer] * biasModifier); + bias *= 1.0 / (cascadePlaneDistances[layer] * biasModifier); } // PCF float shadow = 0.0; - vec2 texelSize = 1.0 / vec2(textureSize(shadowMap, 0)); + vec2 texelSize = 1.0 / vec2(textureSize(shadowMap, 0).xy); for(int x = -1; x <= 1; ++x) { for(int y = -1; y <= 1; ++y) diff --git a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping.vert b/tutorial/src/main/assets/shaders/csm/shadow_mapping.vert similarity index 94% rename from tutorial/src/main/assets/shaders/csm/10.shadow_mapping.vert rename to tutorial/src/main/assets/shaders/csm/shadow_mapping.vert index cb189702..d141c396 100644 --- a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping.vert +++ b/tutorial/src/main/assets/shaders/csm/shadow_mapping.vert @@ -12,7 +12,7 @@ out VS_OUT { } vs_out; uniform mat4 projection; -uniform mat4 view; +uniform mediump mat4 view; uniform mat4 model; void main() diff --git a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.frag b/tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.frag similarity index 100% rename from tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.frag rename to tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.frag diff --git a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.geom b/tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.geom similarity index 89% rename from tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.geom rename to tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.geom index b9da3ebc..f30f69ab 100644 --- a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.geom +++ b/tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.geom @@ -1,4 +1,6 @@ #version 320 es +precision mediump float; +precision mediump int; layout(triangles, invocations = 5) in; layout(triangle_strip, max_vertices = 3) out; diff --git a/tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.vert b/tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.vert similarity index 100% rename from tutorial/src/main/assets/shaders/csm/10.shadow_mapping_depth.vert rename to tutorial/src/main/assets/shaders/csm/shadow_mapping_depth.vert diff --git a/tutorial/src/main/assets/shaders/normal_mapping/4.normal_mapping.frag b/tutorial/src/main/assets/shaders/normal_mapping/normal_mapping.frag similarity index 100% rename from tutorial/src/main/assets/shaders/normal_mapping/4.normal_mapping.frag rename to tutorial/src/main/assets/shaders/normal_mapping/normal_mapping.frag diff --git a/tutorial/src/main/assets/shaders/normal_mapping/4.normal_mapping.vert b/tutorial/src/main/assets/shaders/normal_mapping/normal_mapping.vert similarity index 100% rename from tutorial/src/main/assets/shaders/normal_mapping/4.normal_mapping.vert rename to tutorial/src/main/assets/shaders/normal_mapping/normal_mapping.vert diff --git a/tutorial/src/main/assets/shaders/parallax_mapping/5.1.parallax_mapping.frag b/tutorial/src/main/assets/shaders/parallax_mapping/parallax_mapping.frag similarity index 100% rename from tutorial/src/main/assets/shaders/parallax_mapping/5.1.parallax_mapping.frag rename to tutorial/src/main/assets/shaders/parallax_mapping/parallax_mapping.frag diff --git a/tutorial/src/main/assets/shaders/parallax_mapping/5.1.parallax_mapping.vert b/tutorial/src/main/assets/shaders/parallax_mapping/parallax_mapping.vert similarity index 100% rename from tutorial/src/main/assets/shaders/parallax_mapping/5.1.parallax_mapping.vert rename to tutorial/src/main/assets/shaders/parallax_mapping/parallax_mapping.vert diff --git a/tutorial/src/main/assets/shaders/pbr/1.2.pbr.frag b/tutorial/src/main/assets/shaders/pbr/pbr.frag similarity index 100% rename from tutorial/src/main/assets/shaders/pbr/1.2.pbr.frag rename to tutorial/src/main/assets/shaders/pbr/pbr.frag diff --git a/tutorial/src/main/assets/shaders/pbr/1.2.pbr.vert b/tutorial/src/main/assets/shaders/pbr/pbr.vert similarity index 100% rename from tutorial/src/main/assets/shaders/pbr/1.2.pbr.vert rename to tutorial/src/main/assets/shaders/pbr/pbr.vert diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_downsample.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_downsample.frag deleted file mode 100644 index f1cf4db7..00000000 --- a/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_downsample.frag +++ /dev/null @@ -1,115 +0,0 @@ -#version 320 es -precision mediump float; - -// This shader performs downsampling on a texture, -// as taken from Call Of Duty method, presented at ACM Siggraph 2014. -// This particular method was customly designed to eliminate -// "pulsating artifacts and temporal stability issues". - -// Remember to add bilinear minification filter for this texture! -// Remember to use a floating-point texture format (for HDR)! -// Remember to use edge clamping for this texture! -uniform sampler2D srcTexture; -uniform vec2 srcResolution; - -// which mip we are writing to, used for Karis average -uniform int mipLevel = 1; - -in vec2 texCoord; -layout (location = 0) out vec3 downsample; - -vec3 PowVec3(vec3 v, float p) -{ - return vec3(pow(v.x, p), pow(v.y, p), pow(v.z, p)); -} - -const float invGamma = 1.0 / 2.2; -vec3 ToSRGB(vec3 v) { return PowVec3(v, invGamma); } - -float sRGBToLuma(vec3 col) -{ - //return dot(col, vec3(0.2126f, 0.7152f, 0.0722f)); - return dot(col, vec3(0.299f, 0.587f, 0.114f)); -} - -float KarisAverage(vec3 col) -{ - // Formula is 1 / (1 + luma) - float luma = sRGBToLuma(ToSRGB(col)) * 0.25f; - return 1.0f / (1.0f + luma); -} - -// NOTE: This is the readable version of this shader. It will be optimized! -void main() -{ - vec2 srcTexelSize = 1.0 / srcResolution; - float x = srcTexelSize.x; - float y = srcTexelSize.y; - - // Take 13 samples around current texel: - // a - b - c - // - j - k - - // d - e - f - // - l - m - - // g - h - i - // === ('e' is the current texel) === - vec3 a = texture(srcTexture, vec2(texCoord.x - 2*x, texCoord.y + 2*y)).rgb; - vec3 b = texture(srcTexture, vec2(texCoord.x, texCoord.y + 2*y)).rgb; - vec3 c = texture(srcTexture, vec2(texCoord.x + 2*x, texCoord.y + 2*y)).rgb; - - vec3 d = texture(srcTexture, vec2(texCoord.x - 2*x, texCoord.y)).rgb; - vec3 e = texture(srcTexture, vec2(texCoord.x, texCoord.y)).rgb; - vec3 f = texture(srcTexture, vec2(texCoord.x + 2*x, texCoord.y)).rgb; - - vec3 g = texture(srcTexture, vec2(texCoord.x - 2*x, texCoord.y - 2*y)).rgb; - vec3 h = texture(srcTexture, vec2(texCoord.x, texCoord.y - 2*y)).rgb; - vec3 i = texture(srcTexture, vec2(texCoord.x + 2*x, texCoord.y - 2*y)).rgb; - - vec3 j = texture(srcTexture, vec2(texCoord.x - x, texCoord.y + y)).rgb; - vec3 k = texture(srcTexture, vec2(texCoord.x + x, texCoord.y + y)).rgb; - vec3 l = texture(srcTexture, vec2(texCoord.x - x, texCoord.y - y)).rgb; - vec3 m = texture(srcTexture, vec2(texCoord.x + x, texCoord.y - y)).rgb; - - // Apply weighted distribution: - // 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1 - // a,b,d,e * 0.125 - // b,c,e,f * 0.125 - // d,e,g,h * 0.125 - // e,f,h,i * 0.125 - // j,k,l,m * 0.5 - // This shows 5 square areas that are being sampled. But some of them overlap, - // so to have an energy preserving downsample we need to make some adjustments. - // The weights are the distributed, so that the sum of j,k,l,m (e.g.) - // contribute 0.5 to the final color output. The code below is written - // to effectively yield this sum. We get: - // 0.125*5 + 0.03125*4 + 0.0625*4 = 1 - - // Check if we need to perform Karis average on each block of 4 samples - vec3 groups[5]; - switch (mipLevel) - { - case 0: - // We are writing to mip 0, so we need to apply Karis average to each block - // of 4 samples to prevent fireflies (very bright subpixels, leads to pulsating - // artifacts). - groups[0] = (a+b+d+e) * (0.125f/4.0f); - groups[1] = (b+c+e+f) * (0.125f/4.0f); - groups[2] = (d+e+g+h) * (0.125f/4.0f); - groups[3] = (e+f+h+i) * (0.125f/4.0f); - groups[4] = (j+k+l+m) * (0.5f/4.0f); - groups[0] *= KarisAverage(groups[0]); - groups[1] *= KarisAverage(groups[1]); - groups[2] *= KarisAverage(groups[2]); - groups[3] *= KarisAverage(groups[3]); - groups[4] *= KarisAverage(groups[4]); - downsample = groups[0]+groups[1]+groups[2]+groups[3]+groups[4]; - downsample = max(downsample, 0.0001f); - break; - default: - downsample = e*0.125; // ok - downsample += (a+c+g+i)*0.03125; // ok - downsample += (b+d+f+h)*0.0625; // ok - downsample += (j+k+l+m)*0.125; // ok - break; - } -} diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_upsample.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_upsample.frag deleted file mode 100644 index acb27536..00000000 --- a/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_upsample.frag +++ /dev/null @@ -1,48 +0,0 @@ -#version 320 es -precision mediump float; - -// This shader performs upsampling on a texture, -// as taken from Call Of Duty method, presented at ACM Siggraph 2014. - -// Remember to add bilinear minification filter for this texture! -// Remember to use a floating-point texture format (for HDR)! -// Remember to use edge clamping for this texture! -uniform sampler2D srcTexture; -uniform float filterRadius; - -in vec2 texCoord; -layout (location = 0) out vec3 upsample; - -void main() -{ - // The filter kernel is applied with a radius, specified in texture - // coordinates, so that the radius will vary across mip resolutions. - float x = filterRadius; - float y = filterRadius; - - // Take 9 samples around current texel: - // a - b - c - // d - e - f - // g - h - i - // === ('e' is the current texel) === - vec3 a = texture(srcTexture, vec2(texCoord.x - x, texCoord.y + y)).rgb; - vec3 b = texture(srcTexture, vec2(texCoord.x, texCoord.y + y)).rgb; - vec3 c = texture(srcTexture, vec2(texCoord.x + x, texCoord.y + y)).rgb; - - vec3 d = texture(srcTexture, vec2(texCoord.x - x, texCoord.y)).rgb; - vec3 e = texture(srcTexture, vec2(texCoord.x, texCoord.y)).rgb; - vec3 f = texture(srcTexture, vec2(texCoord.x + x, texCoord.y)).rgb; - - vec3 g = texture(srcTexture, vec2(texCoord.x - x, texCoord.y - y)).rgb; - vec3 h = texture(srcTexture, vec2(texCoord.x, texCoord.y - y)).rgb; - vec3 i = texture(srcTexture, vec2(texCoord.x + x, texCoord.y - y)).rgb; - - // Apply weighted distribution, by using a 3x3 tent filter: - // 1 | 1 2 1 | - // -- * | 2 4 2 | - // 16 | 1 2 1 | - upsample = e*4.0; - upsample += (b+d+f+h)*2.0; - upsample += (a+c+g+i); - upsample *= 1.0 / 16.0; -} diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/bloom.frag similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom.frag rename to tutorial/src/main/assets/shaders/physically_based_bloom/bloom.frag diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom.vert b/tutorial/src/main/assets/shaders/physically_based_bloom/bloom.vert similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom.vert rename to tutorial/src/main/assets/shaders/physically_based_bloom/bloom.vert diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom_final.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/bloom_final.frag similarity index 96% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom_final.frag rename to tutorial/src/main/assets/shaders/physically_based_bloom/bloom_final.frag index 1c7df045..8fcec0e2 100644 --- a/tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom_final.frag +++ b/tutorial/src/main/assets/shaders/physically_based_bloom/bloom_final.frag @@ -7,7 +7,7 @@ in vec2 TexCoords; uniform sampler2D scene; uniform sampler2D bloomBlur; uniform float exposure; -uniform float bloomStrength = 0.04f; +uniform float bloomStrength; uniform int programChoice; vec3 bloom_none() diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom_final.vert b/tutorial/src/main/assets/shaders/physically_based_bloom/bloom_final.vert similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.bloom_final.vert rename to tutorial/src/main/assets/shaders/physically_based_bloom/bloom_final.vert diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.light_box.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/light_box.frag similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.light_box.frag rename to tutorial/src/main/assets/shaders/physically_based_bloom/light_box.frag diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/new_downsample.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/new_downsample.frag new file mode 100644 index 00000000..ad69aa3d --- /dev/null +++ b/tutorial/src/main/assets/shaders/physically_based_bloom/new_downsample.frag @@ -0,0 +1,115 @@ +#version 320 es +precision mediump float; + +// This shader performs downsampling on a texture, +// as taken from Call Of Duty method, presented at ACM Siggraph 2014. +// This particular method was customly designed to eliminate +// "pulsating artifacts and temporal stability issues". + +// Remember to add bilinear minification filter for this texture! +// Remember to use a floating-point texture format (for HDR)! +// Remember to use edge clamping for this texture! +uniform sampler2D srcTexture; +uniform vec2 srcResolution; + +// which mip we are writing to, used for Karis average +uniform int mipLevel; + +in vec2 texCoord; +layout (location = 0) out vec3 downsample; + +vec3 PowVec3(vec3 v, float p) +{ + return vec3(pow(v.x, p), pow(v.y, p), pow(v.z, p)); +} + +const float invGamma = 1.0 / 2.2; +vec3 ToSRGB(vec3 v) { return PowVec3(v, invGamma); } + +float sRGBToLuma(vec3 col) +{ + //return dot(col, vec3(0.2126f, 0.7152f, 0.0722f)); + return dot(col, vec3(0.299f, 0.587f, 0.114f)); +} + +float KarisAverage(vec3 col) +{ + // Formula is 1 / (1 + luma) + float luma = sRGBToLuma(ToSRGB(col)) * 0.25f; + return 1.0f / (1.0f + luma); +} + +// NOTE: This is the readable version of this shader. It will be optimized! +void main() +{ + vec2 srcTexelSize = 1.0 / srcResolution; + float x = srcTexelSize.x; + float y = srcTexelSize.y; + + // Take 13 samples around current texel: + // a - b - c + // - j - k - + // d - e - f + // - l - m - + // g - h - i + // === ('e' is the current texel) === + vec3 a = texture(srcTexture, vec2(texCoord.x - 2. * x, texCoord.y + 2. * y)).rgb; + vec3 b = texture(srcTexture, vec2(texCoord.x, texCoord.y + 2. * y)).rgb; + vec3 c = texture(srcTexture, vec2(texCoord.x + 2. * x, texCoord.y + 2. * y)).rgb; + + vec3 d = texture(srcTexture, vec2(texCoord.x - 2. * x, texCoord.y)).rgb; + vec3 e = texture(srcTexture, vec2(texCoord.x, texCoord.y)).rgb; + vec3 f = texture(srcTexture, vec2(texCoord.x + 2. * x, texCoord.y)).rgb; + + vec3 g = texture(srcTexture, vec2(texCoord.x - 2. * x, texCoord.y - 2. * y)).rgb; + vec3 h = texture(srcTexture, vec2(texCoord.x, texCoord.y - 2. * y)).rgb; + vec3 i = texture(srcTexture, vec2(texCoord.x + 2. * x, texCoord.y - 2. * y)).rgb; + + vec3 j = texture(srcTexture, vec2(texCoord.x - x, texCoord.y + y)).rgb; + vec3 k = texture(srcTexture, vec2(texCoord.x + x, texCoord.y + y)).rgb; + vec3 l = texture(srcTexture, vec2(texCoord.x - x, texCoord.y - y)).rgb; + vec3 m = texture(srcTexture, vec2(texCoord.x + x, texCoord.y - y)).rgb; + + // Apply weighted distribution: + // 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1 + // a,b,d,e * 0.125 + // b,c,e,f * 0.125 + // d,e,g,h * 0.125 + // e,f,h,i * 0.125 + // j,k,l,m * 0.5 + // This shows 5 square areas that are being sampled. But some of them overlap, + // so to have an energy preserving downsample we need to make some adjustments. + // The weights are the distributed, so that the sum of j,k,l,m (e.g.) + // contribute 0.5 to the final color output. The code below is written + // to effectively yield this sum. We get: + // 0.125*5 + 0.03125*4 + 0.0625*4 = 1 + + // Check if we need to perform Karis average on each block of 4 samples + vec3 groups[5]; + switch (mipLevel) + { + case 0: + // We are writing to mip 0, so we need to apply Karis average to each block + // of 4 samples to prevent fireflies (very bright subpixels, leads to pulsating + // artifacts). + groups[0] = (a + b + d + e) * (0.125f / 4.0f); + groups[1] = (b + c + e + f) * (0.125f / 4.0f); + groups[2] = (d + e + g + h) * (0.125f / 4.0f); + groups[3] = (e + f + h + i) * (0.125f / 4.0f); + groups[4] = (j + k + l + m) * (0.5f / 4.0f); + groups[0] *= KarisAverage(groups[0]); + groups[1] *= KarisAverage(groups[1]); + groups[2] *= KarisAverage(groups[2]); + groups[3] *= KarisAverage(groups[3]); + groups[4] *= KarisAverage(groups[4]); + downsample = groups[0] + groups[1] + groups[2] + groups[3] + groups[4]; + downsample = max(downsample, 0.0001f); + break; + default: + downsample = e * 0.125; // ok + downsample += (a + c + g + i) * 0.03125; // ok + downsample += (b + d + f + h) * 0.0625; // ok + downsample += (j + k + l + m) * 0.125; // ok + break; + } +} diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_downsample.vert b/tutorial/src/main/assets/shaders/physically_based_bloom/new_downsample.vert similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.new_downsample.vert rename to tutorial/src/main/assets/shaders/physically_based_bloom/new_downsample.vert diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/new_upsample.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/new_upsample.frag new file mode 100644 index 00000000..519eb872 --- /dev/null +++ b/tutorial/src/main/assets/shaders/physically_based_bloom/new_upsample.frag @@ -0,0 +1,48 @@ +#version 320 es +precision mediump float; + +// This shader performs upsampling on a texture, +// as taken from Call Of Duty method, presented at ACM Siggraph 2014. + +// Remember to add bilinear minification filter for this texture! +// Remember to use a floating-point texture format (for HDR)! +// Remember to use edge clamping for this texture! +uniform sampler2D srcTexture; +uniform float filterRadius; + +in vec2 texCoord; +layout (location = 0) out vec3 upsample; + +void main() +{ + // The filter kernel is applied with a radius, specified in texture + // coordinates, so that the radius will vary across mip resolutions. + float x = filterRadius; + float y = filterRadius; + + // Take 9 samples around current texel: + // a - b - c + // d - e - f + // g - h - i + // === ('e' is the current texel) === + vec3 a = texture(srcTexture, vec2(texCoord.x - x, texCoord.y + y)).rgb; + vec3 b = texture(srcTexture, vec2(texCoord.x, texCoord.y + y)).rgb; + vec3 c = texture(srcTexture, vec2(texCoord.x + x, texCoord.y + y)).rgb; + + vec3 d = texture(srcTexture, vec2(texCoord.x - x, texCoord.y)).rgb; + vec3 e = texture(srcTexture, vec2(texCoord.x, texCoord.y)).rgb; + vec3 f = texture(srcTexture, vec2(texCoord.x + x, texCoord.y)).rgb; + + vec3 g = texture(srcTexture, vec2(texCoord.x - x, texCoord.y - y)).rgb; + vec3 h = texture(srcTexture, vec2(texCoord.x, texCoord.y - y)).rgb; + vec3 i = texture(srcTexture, vec2(texCoord.x + x, texCoord.y - y)).rgb; + + // Apply weighted distribution, by using a 3x3 tent filter: + // 1 | 1 2 1 | + // -- * | 2 4 2 | + // 16 | 1 2 1 | + upsample = e * 4.0; + upsample += (b + d + f + h) * 2.0; + upsample += (a + c + g + i); + upsample *= 1.0 / 16.0; +} diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.new_upsample.vert b/tutorial/src/main/assets/shaders/physically_based_bloom/new_upsample.vert similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.new_upsample.vert rename to tutorial/src/main/assets/shaders/physically_based_bloom/new_upsample.vert diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.old_blur.frag b/tutorial/src/main/assets/shaders/physically_based_bloom/old_blur.frag similarity index 68% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.old_blur.frag rename to tutorial/src/main/assets/shaders/physically_based_bloom/old_blur.frag index 25eb48a9..09d08b37 100644 --- a/tutorial/src/main/assets/shaders/physically_based_bloom/6.old_blur.frag +++ b/tutorial/src/main/assets/shaders/physically_based_bloom/old_blur.frag @@ -7,26 +7,26 @@ in vec2 TexCoords; uniform sampler2D image; uniform bool horizontal; -uniform float weight[5] = float[] (0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162); +uniform float weight[5]; void main() { - vec2 tex_offset = 1.0 / textureSize(image, 0); // gets size of single texel + vec2 tex_offset = 1.0 / vec2(textureSize(image, 0)); // gets size of single texel vec3 result = texture(image, TexCoords).rgb * weight[0]; if(horizontal) { for(int i = 1; i < 5; ++i) { - result += texture(image, TexCoords + vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; - result += texture(image, TexCoords - vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; + result += texture(image, TexCoords + vec2(tex_offset.x * float(i), 0.0)).rgb * weight[i]; + result += texture(image, TexCoords - vec2(tex_offset.x * float(i), 0.0)).rgb * weight[i]; } } else { for(int i = 1; i < 5; ++i) { - result += texture(image, TexCoords + vec2(0.0, tex_offset.y * i)).rgb * weight[i]; - result += texture(image, TexCoords - vec2(0.0, tex_offset.y * i)).rgb * weight[i]; + result += texture(image, TexCoords + vec2(0.0, tex_offset.y * float(i))).rgb * weight[i]; + result += texture(image, TexCoords - vec2(0.0, tex_offset.y * float(i))).rgb * weight[i]; } } FragColor = vec4(result, 1.0); diff --git a/tutorial/src/main/assets/shaders/physically_based_bloom/6.old_blur.vert b/tutorial/src/main/assets/shaders/physically_based_bloom/old_blur.vert similarity index 100% rename from tutorial/src/main/assets/shaders/physically_based_bloom/6.old_blur.vert rename to tutorial/src/main/assets/shaders/physically_based_bloom/old_blur.vert diff --git a/tutorial/src/main/cpp/Shader.cpp b/tutorial/src/main/cpp/Shader.cpp index 273c486e..b9d4a84f 100644 --- a/tutorial/src/main/cpp/Shader.cpp +++ b/tutorial/src/main/cpp/Shader.cpp @@ -44,7 +44,7 @@ Shader::Shader(const char *vertexPath, const char *fragmentPath, const char *geo unsigned int geometry; if (geometryPath != nullptr) { geometry = glCreateShader(GL_GEOMETRY_SHADER); - glShaderSource(geometry, 1, &gShaderCode, NULL); + glShaderSource(geometry, 1, &gShaderCode, &gShaderSize); glCompileShader(geometry); checkCompileErrors(geometry, "GEOMETRY"); } diff --git a/tutorial/src/main/cpp/advanced/CsmScene.cpp b/tutorial/src/main/cpp/advanced/CsmScene.cpp index 78695608..4795f1c5 100644 --- a/tutorial/src/main/cpp/advanced/CsmScene.cpp +++ b/tutorial/src/main/cpp/advanced/CsmScene.cpp @@ -23,10 +23,11 @@ void CsmScene::init() { // build and compile shaders // ------------------------- - shader = new Shader("shaders/csm/10.shadow_mapping.vert", "shaders/csm/10.shadow_mapping.frag"); - simpleDepthShader = new Shader("shaders/csm/10.shadow_mapping_depth.vert", "shaders/csm/10.shadow_mapping_depth.frag", "10.shadow_mapping_depth.geom"); - debugDepthQuad = new Shader("shaders/csm/10.debug_quad.vert", "shaders/csm/10.debug_quad_depth.frag"); - debugCascadeShader = new Shader("shaders/csm/10.debug_cascade.vert", "shaders/csm/10.debug_cascade.frag"); + shader = new Shader("shaders/csm/shadow_mapping.vert", "shaders/csm/shadow_mapping.frag"); + simpleDepthShader = new Shader("shaders/csm/shadow_mapping_depth.vert", "shaders/csm/shadow_mapping_depth.frag", + "shaders/csm/shadow_mapping_depth.geom"); + debugDepthQuad = new Shader("shaders/csm/debug_quad.vert", "shaders/csm/debug_quad_depth.frag"); + debugCascadeShader = new Shader("shaders/csm/debug_cascade.vert", "shaders/csm/debug_cascade.frag"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ diff --git a/tutorial/src/main/cpp/lighting/PhysicallyBasedBloomScene.cpp b/tutorial/src/main/cpp/lighting/PhysicallyBasedBloomScene.cpp index 432d0269..85d92659 100644 --- a/tutorial/src/main/cpp/lighting/PhysicallyBasedBloomScene.cpp +++ b/tutorial/src/main/cpp/lighting/PhysicallyBasedBloomScene.cpp @@ -10,10 +10,10 @@ static void _renderQuad(unsigned int& quadVAO, unsigned int& quadVBO) { float quadVertices[] = { // positions // texture Coords - -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, - 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-left + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-right + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right }; glGenVertexArrays(1, &quadVAO); glGenBuffers(1, &quadVBO); @@ -30,20 +30,22 @@ static void _renderQuad(unsigned int& quadVAO, unsigned int& quadVBO) glBindVertexArray(0); } -PhysicallyBasedBloomScene::PhysicallyBasedBloomScene() { +PhysicallyBasedBloomScene::PhysicallyBasedBloomScene() + : bloom(true), exposure(1.0f), programChoice(3), bloomFilterRadius(0.005f), m_width(0), m_height(0) { } void PhysicallyBasedBloomScene::init() { m_camera = new TargetCamera; + glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); - shader = new Shader("shaders/physically_based_bloom/6.bloom.vert", - "shaders/physically_based_bloom/6.bloom.frag"); - shaderLight = new Shader("shaders/physically_based_bloom/6.bloom.vert", - "shaders/physically_based_bloom/6.light_box.frag"); - shaderBlur = new Shader("shaders/physically_based_bloom/6.old_blur.vert", - "shaders/physically_based_bloom/6.old_blur.frag"); - shaderBloomFinal = new Shader("shaders/physically_based_bloom/6.bloom_final.vert", - "shaders/physically_based_bloom/6.bloom_final.frag"); + shader = new Shader("shaders/physically_based_bloom/bloom.vert", + "shaders/physically_based_bloom/bloom.frag"); + shaderLight = new Shader("shaders/physically_based_bloom/bloom.vert", + "shaders/physically_based_bloom/light_box.frag"); + shaderBlur = new Shader("shaders/physically_based_bloom/old_blur.vert", + "shaders/physically_based_bloom/old_blur.frag"); + shaderBloomFinal = new Shader("shaders/physically_based_bloom/bloom_final.vert", + "shaders/physically_based_bloom/bloom_final.frag"); woodTexture = loadTexture("textures/wood.png", true); containerTexture = loadTexture("textures/container2.png", true); @@ -108,6 +110,24 @@ void PhysicallyBasedBloomScene::resize(int width, int height) { m_width = width; m_height = height; glViewport(0, 0, width, height); + + // update textures + for (unsigned int i = 0; i < 2; i++) + { + glBindTexture(GL_TEXTURE_2D, colorBuffers[i]); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, NULL); + } + glBindRenderbuffer(GL_RENDERBUFFER, rboDepth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width, m_height); + + for (unsigned int i = 0; i < 2; i++) + { + glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[i]); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, NULL); + } + + bloomRenderer.Destroy(); + bloomRenderer.Init(m_width, m_height); } void PhysicallyBasedBloomScene::draw() { @@ -286,52 +306,66 @@ void PhysicallyBasedBloomScene::renderCube() { if (cubeVAO == 0) { float vertices[] = { - -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, - -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, - -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, - -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, - -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, - 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, - 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, - -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, - 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, - 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, - 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, - -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, - -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, - -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, - 1.0f, 1.0f , 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, - 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, - -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, - -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f + // back face + -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left + 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right + 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, // bottom-right + 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, // top-right + -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, // bottom-left + -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, // top-left + // front face + -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left + 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // bottom-right + 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right + 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top-right + -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, // top-left + -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom-left + // left face + -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right + -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-left + -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left + -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-left + -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-right + -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-right + // right face + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left + 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right + 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top-right + 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom-right + 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top-left + 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // bottom-left + // bottom face + -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right + 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, // top-left + 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left + 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom-left + -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom-right + -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, // top-right + // top face + -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left + 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right + 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top-right + 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom-right + -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top-left + -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f // bottom-left }; - glGenVertexArrays(1, &cubeVAO); glGenBuffers(1, &cubeVBO); - glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glGenVertexArrays(1, &cubeVAO); + glGenBuffers(1, &cubeVBO); glBindVertexArray(cubeVAO); - glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); - glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } - glBindVertexArray(cubeVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); + glBindVertexArray(cubeVAO); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); } void PhysicallyBasedBloomScene::renderQuad() { @@ -416,8 +450,10 @@ bool BloomRenderer::Init(unsigned int windowWidth, unsigned int windowHeight) { const unsigned int num_bloom_mips = 6; if (!mFBO.Init(windowWidth, windowHeight, num_bloom_mips)) return false; - mDownsampleShader = new Shader("6.new_downsample.vert", "6.new_downsample.frag"); - mUpsampleShader = new Shader("6.new_upsample.vert", "6.new_upsample.frag"); + mDownsampleShader = new Shader("shaders/physically_based_bloom/new_downsample.vert", + "shaders/physically_based_bloom/new_downsample.frag"); + mUpsampleShader = new Shader("shaders/physically_based_bloom/new_upsample.vert", + "shaders/physically_based_bloom/new_upsample.frag"); mDownsampleShader->use(); mDownsampleShader->setInt("srcTexture", 0); mUpsampleShader->use(); @@ -432,6 +468,7 @@ void BloomRenderer::Destroy() { delete mUpsampleShader; glDeleteBuffers(1, &quadVBO); glDeleteVertexArrays(1, &quadVAO); + mInit = false; } void BloomRenderer::RenderBloomTexture(unsigned int srcTexture, float filterRadius) { diff --git a/tutorial/src/main/cpp/pbr/NormalMappingScene.cpp b/tutorial/src/main/cpp/pbr/NormalMappingScene.cpp index 6c4eacdb..7699bbe1 100644 --- a/tutorial/src/main/cpp/pbr/NormalMappingScene.cpp +++ b/tutorial/src/main/cpp/pbr/NormalMappingScene.cpp @@ -6,18 +6,23 @@ #include "TimeUtil.h" NormalMappingScene::NormalMappingScene() { + m_lightPos = glm::vec3(0.5f, 1.0f, 0.3f); + m_quadVAO = 0; + m_quadVBO = 0; } void NormalMappingScene::init() { m_camera = new TargetCamera; + dynamic_cast(m_camera)->setRadius(3.0f); // configure global opengl state // ----------------------------- + glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); // build and compile shaders // ------------------------- - m_pShader = new Shader("shaders/normal_mapping/4.normal_mapping.vert", - "shaders/normal_mapping/4.normal_mapping.frag"); + m_pShader = new Shader("shaders/normal_mapping/normal_mapping.vert", + "shaders/normal_mapping/normal_mapping.frag"); // load textures // ------------- @@ -53,8 +58,8 @@ void NormalMappingScene::draw() { m_pShader->setMat4("view", view); // render normal-mapped quad glm::mat4 model = glm::mat4(1.0f); - model = glm::rotate(model, glm::radians(GetEscapeSecs() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show normal mapping from multiple directions - m_pShader->setMat4("model", model); +// model = glm::rotate(model, glm::radians(GetEscapeSecs() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show normal mapping from multiple directions + m_pShader->setMat4("model", model) ; m_pShader->setVec3("viewPos", m_camera->getPosition()); m_pShader->setVec3("lightPos", m_lightPos); glActiveTexture(GL_TEXTURE0); @@ -64,17 +69,19 @@ void NormalMappingScene::draw() { renderQuad(); // render light source (simply re-renders a smaller plane at the light's position for debugging/visualization) - model = glm::mat4(1.0f); - model = glm::translate(model, m_lightPos); - model = glm::scale(model, glm::vec3(0.1f)); - m_pShader->setMat4("model", model); - renderQuad(); +// model = glm::mat4(1.0f); +// model = glm::translate(model, m_lightPos); +// model = glm::scale(model, glm::vec3(0.1f)); +// m_pShader->setMat4("model", model); +// renderQuad(); } } void NormalMappingScene::destroy() { glDeleteVertexArrays(1, &m_quadVAO); glDeleteBuffers(1, &m_quadVBO); + glDeleteTextures(1, &m_diffuseMap); + glDeleteTextures(1, &m_normalMap); if (m_pShader) { delete m_pShader; @@ -130,23 +137,21 @@ void NormalMappingScene::renderQuad() { if (m_quadVAO == 0) { // positions - glm::vec3 pos1(-1.0f, 1.0f, 0.0f); - glm::vec3 pos2(-1.0f, -1.0f, 0.0f); - glm::vec3 pos3( 1.0f, -1.0f, 0.0f); - glm::vec3 pos4( 1.0f, 1.0f, 0.0f); + glm::vec3 pos1(-1.0f, -1.0f, 0.0f); // Bottom-Left + glm::vec3 pos2( 1.0f, -1.0f, 0.0f); // Bottom-Right + glm::vec3 pos3( 1.0f, 1.0f, 0.0f); // Top-Right + glm::vec3 pos4(-1.0f, 1.0f, 0.0f); // Top-Left // texture coordinates - glm::vec2 uv1(0.0f, 1.0f); - glm::vec2 uv2(0.0f, 0.0f); - glm::vec2 uv3(1.0f, 0.0f); - glm::vec2 uv4(1.0f, 1.0f); + glm::vec2 uv1(0.0f, 0.0f); + glm::vec2 uv2(1.0f, 0.0f); + glm::vec2 uv3(1.0f, 1.0f); + glm::vec2 uv4(0.0f, 1.0f); // normal vector glm::vec3 nm(0.0f, 0.0f, 1.0f); - // calculate tangent/bitangent vectors of both triangles - glm::vec3 tangent1, bitangent1; - glm::vec3 tangent2, bitangent2; + // calculate tangent/bitangent vectors + glm::vec3 tangent, bitangent; // triangle 1 - // ---------- glm::vec3 edge1 = pos2 - pos1; glm::vec3 edge2 = pos3 - pos1; glm::vec2 deltaUV1 = uv2 - uv1; @@ -154,42 +159,23 @@ void NormalMappingScene::renderQuad() { float f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y); - tangent1.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x); - tangent1.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y); - tangent1.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z); - - bitangent1.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x); - bitangent1.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y); - bitangent1.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z); - - // triangle 2 - // ---------- - edge1 = pos3 - pos1; - edge2 = pos4 - pos1; - deltaUV1 = uv3 - uv1; - deltaUV2 = uv4 - uv1; - - f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y); - - tangent2.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x); - tangent2.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y); - tangent2.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z); - - - bitangent2.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x); - bitangent2.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y); - bitangent2.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z); + tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x); + tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y); + tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z); + bitangent.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x); + bitangent.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y); + bitangent.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z); float quadVertices[] = { // positions // normal // texcoords // tangent // bitangent - pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z, - pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z, - pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent1.x, tangent1.y, tangent1.z, bitangent1.x, bitangent1.y, bitangent1.z, + pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z, + pos2.x, pos2.y, pos2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z, + pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z, - pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z, - pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z, - pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent2.x, tangent2.y, tangent2.z, bitangent2.x, bitangent2.y, bitangent2.z + pos1.x, pos1.y, pos1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z, + pos3.x, pos3.y, pos3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z, + pos4.x, pos4.y, pos4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, tangent.x, tangent.y, tangent.z, bitangent.x, bitangent.y, bitangent.z }; // configure plane VAO glGenVertexArrays(1, &m_quadVAO); diff --git a/tutorial/src/main/cpp/pbr/ParallaxMappingScene.cpp b/tutorial/src/main/cpp/pbr/ParallaxMappingScene.cpp index b66894f6..5f0ab763 100644 --- a/tutorial/src/main/cpp/pbr/ParallaxMappingScene.cpp +++ b/tutorial/src/main/cpp/pbr/ParallaxMappingScene.cpp @@ -17,8 +17,8 @@ void ParallaxMappingScene::init() { // build and compile shaders // ------------------------- - m_pShader = new Shader("shaders/parallax_mapping/5.1.parallax_mapping.vert", - "shaders/parallax_mapping/5.1.parallax_mapping.frag"); + m_pShader = new Shader("shaders/parallax_mapping/parallax_mapping.vert", + "shaders/parallax_mapping/parallax_mapping.frag"); // load textures // ------------- @@ -56,7 +56,7 @@ void ParallaxMappingScene::draw() { m_pShader->setMat4("view", view); // render parallax-mapped quad glm::mat4 model = glm::mat4(1.0f); - model = glm::rotate(model, glm::radians(GetEscapeSecs() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show parallax mapping from multiple directions + m_pShader->setMat4("model", model); m_pShader->setVec3("viewPos", m_camera->getPosition()); m_pShader->setVec3("lightPos", m_lightPos); @@ -70,11 +70,11 @@ void ParallaxMappingScene::draw() { renderQuad(); // render light source (simply re-renders a smaller plane at the light's position for debugging/visualization) - model = glm::mat4(1.0f); - model = glm::translate(model, m_lightPos); - model = glm::scale(model, glm::vec3(0.1f)); - m_pShader->setMat4("model", model); - renderQuad(); +// model = glm::mat4(1.0f); +// model = glm::translate(model, m_lightPos); +// model = glm::scale(model, glm::vec3(0.1f)); +// m_pShader->setMat4("model", model); +// renderQuad(); } } @@ -133,5 +133,67 @@ void ParallaxMappingScene::parseTargetCameraEvent(std::mapuse(); m_pShader->setInt("albedoMap", 0); @@ -30,11 +37,11 @@ void PbrScene::init() { // load PBR material textures // -------------------------- - albedo = loadTexture("textures/pbr/rusted_iron/albedo.png"); - normal = loadTexture("textures/pbr/rusted_iron/normal.png"); - metallic = loadTexture("textures/pbr/rusted_iron/metallic.png"); + albedo = loadTexture("textures/pbr/rusted_iron/albedo.png"); + normal = loadTexture("textures/pbr/rusted_iron/normal.png"); + metallic = loadTexture("textures/pbr/rusted_iron/metallic.png"); roughness = loadTexture("textures/pbr/rusted_iron/roughness.png"); - ao = loadTexture("textures/pbr/rusted_iron/ao.png"); + ao = loadTexture("textures/pbr/rusted_iron/ao.png"); } void PbrScene::resize(int width, int height) { @@ -51,8 +58,7 @@ void PbrScene::draw() { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - if(m_pShader) - { + if (m_pShader) { m_pShader->use(); glm::mat4 projection = m_camera->getProjectionMatrix(); glm::mat4 view = m_camera->getViewMatrix(); @@ -73,14 +79,12 @@ void PbrScene::draw() { // render rows*column number of spheres with material properties defined by textures (they all have the same material properties) glm::mat4 model = glm::mat4(1.0f); - for (int row = 0; row < nrRows; ++row) - { - for (int col = 0; col < nrColumns; ++col) - { + for (int row = 0; row < nrRows; ++row) { + for (int col = 0; col < nrColumns; ++col) { model = glm::mat4(1.0f); model = glm::translate(model, glm::vec3( - (float)(col - (nrColumns / 2)) * spacing, - (float)(row - (nrRows / 2)) * spacing, + (float) (col - (nrColumns / 2)) * spacing, + (float) (row - (nrRows / 2)) * spacing, 0.0f )); m_pShader->setMat4("model", model); @@ -92,31 +96,28 @@ void PbrScene::draw() { // render light source (simply re-render sphere at light positions) // this looks a bit off as we use the same shader, but it'll make their positions obvious and // keeps the codeprint small. - for (unsigned int i = 0; i < sizeof(lightPositions) / sizeof(lightPositions[0]); ++i) - { - glm::vec3 newPos = lightPositions[i] + glm::vec3(sin(GetEscapeSecs() * 5.0) * 5.0, 0.0, 0.0); - newPos = lightPositions[i]; - m_pShader->setVec3("lightPositions[" + std::to_string(i) + "]", newPos); - m_pShader->setVec3("lightColors[" + std::to_string(i) + "]", lightColors[i]); - - model = glm::mat4(1.0f); - model = glm::translate(model, newPos); - model = glm::scale(model, glm::vec3(0.5f)); - m_pShader->setMat4("model", model); - m_pShader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model)))); - renderSphere(); - } +// for (unsigned int i = 0; i < 4; ++i) { +// glm::vec3 newPos = lightPositions[i]; +// m_pShader->setVec3("lightPositions[" + std::to_string(i) + "]", newPos); +// m_pShader->setVec3("lightC +// olors[" + std::to_string(i) + "]", lightColors[i]); +// +// model = glm::mat4(1.0f); +// model = glm::translate(model, newPos); +// model = glm::scale(model, glm::vec3(0.5f)); +// m_pShader->setMat4("model", model); +// m_pShader->setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model)))); +// renderSphere(); +// } } } void PbrScene::destroy() { - if (m_pShader) - { + if (m_pShader) { delete m_pShader; m_pShader = nullptr; } - if (sphereVAO != 0) - { + if (sphereVAO != 0) { glDeleteVertexArrays(1, &sphereVAO); sphereVAO = 0; } @@ -139,12 +140,12 @@ std::map PbrScene::propertyEvent(std::map &event) { - auto* targetCamera = dynamic_cast(m_camera); + auto *targetCamera = dynamic_cast(m_camera); if (!targetCamera) return; if (auto it = event.find("single_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto& val = std::any_cast&>(it->second); + const auto &val = std::any_cast &>(it->second); if (val.size() >= 4) { targetCamera->onSingleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3])); } @@ -153,10 +154,10 @@ void PbrScene::parseTargetCameraEvent(std::map &event) { if (auto it = event.find("double_touching"); it != event.end()) { if (it->second.type() == typeid(std::vector)) { - const auto& val = std::any_cast&>(it->second); + const auto &val = std::any_cast &>(it->second); if (val.size() >= 8) { targetCamera->onDoubleTouching(glm::vec2(val[0], val[1]), glm::vec2(val[2], val[3]), - glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); + glm::vec2(val[4], val[5]), glm::vec2(val[6], val[7])); } } } @@ -167,8 +168,7 @@ void PbrScene::parseTargetCameraEvent(std::map &event) { } void PbrScene::renderSphere() { - if (sphereVAO == 0) - { + if (sphereVAO == 0) { glGenVertexArrays(1, &sphereVAO); unsigned int vbo, ebo; @@ -183,12 +183,10 @@ void PbrScene::renderSphere() { const unsigned int X_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64; const float PI = 3.14159265359f; - for (unsigned int x = 0; x <= X_SEGMENTS; ++x) - { - for (unsigned int y = 0; y <= Y_SEGMENTS; ++y) - { - float xSegment = (float)x / (float)X_SEGMENTS; - float ySegment = (float)y / (float)Y_SEGMENTS; + for (unsigned int x = 0; x <= X_SEGMENTS; ++x) { + for (unsigned int y = 0; y <= Y_SEGMENTS; ++y) { + float xSegment = (float) x / (float) X_SEGMENTS; + float ySegment = (float) y / (float) Y_SEGMENTS; float xPos = std::cos(xSegment * 2.0f * PI) * std::sin(ySegment * PI); float yPos = std::cos(ySegment * PI); float zPos = std::sin(xSegment * 2.0f * PI) * std::sin(ySegment * PI); @@ -200,20 +198,15 @@ void PbrScene::renderSphere() { } bool oddRow = false; - for (unsigned int y = 0; y < Y_SEGMENTS; ++y) - { + for (unsigned int y = 0; y < Y_SEGMENTS; ++y) { if (!oddRow) // even rows: y == 0, y == 2; and so on { - for (unsigned int x = 0; x <= X_SEGMENTS; ++x) - { + for (unsigned int x = 0; x <= X_SEGMENTS; ++x) { indices.push_back(y * (X_SEGMENTS + 1) + x); indices.push_back((y + 1) * (X_SEGMENTS + 1) + x); } - } - else - { - for (int x = X_SEGMENTS; x >= 0; --x) - { + } else { + for (int x = X_SEGMENTS; x >= 0; --x) { indices.push_back((y + 1) * (X_SEGMENTS + 1) + x); indices.push_back(y * (X_SEGMENTS + 1) + x); } @@ -223,19 +216,16 @@ void PbrScene::renderSphere() { indexCount = static_cast(indices.size()); std::vector data; - for (unsigned int i = 0; i < positions.size(); ++i) - { + for (unsigned int i = 0; i < positions.size(); ++i) { data.push_back(positions[i].x); data.push_back(positions[i].y); data.push_back(positions[i].z); - if (normals.size() > 0) - { + if (normals.size() > 0) { data.push_back(normals[i].x); data.push_back(normals[i].y); data.push_back(normals[i].z); } - if (uv.size() > 0) - { + if (uv.size() > 0) { data.push_back(uv[i].x); data.push_back(uv[i].y); } @@ -247,11 +237,11 @@ void PbrScene::renderSphere() { glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); unsigned int stride = (3 + 2 + 3) * sizeof(float); glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void *) 0); glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void *) (3 * sizeof(float))); glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void *) (6 * sizeof(float))); } glBindVertexArray(sphereVAO); diff --git a/tutorial/src/main/cpp/pbr/PbrScene.h b/tutorial/src/main/cpp/pbr/PbrScene.h index 7e67dedd..10d2ccef 100644 --- a/tutorial/src/main/cpp/pbr/PbrScene.h +++ b/tutorial/src/main/cpp/pbr/PbrScene.h @@ -32,11 +32,11 @@ public : unsigned int albedo = 0u, normal = 0u, metallic = 0u, roughness = 0u, ao = 0u; - glm::vec3 lightPositions[1]; - glm::vec3 lightColors[1]; - int nrRows = 0; - int nrColumns = 0; - float spacing = 0.f; + glm::vec3 lightPositions[4]; + glm::vec3 lightColors[4]; + int nrRows = 7; + int nrColumns = 7; + float spacing = 2.5f; int m_width = 0; int m_height = 0; diff --git a/tutorial/src/main/java/com/minininja/learngles/SampleData.kt b/tutorial/src/main/java/com/minininja/learngles/SampleData.kt index f6e62551..2d914abc 100644 --- a/tutorial/src/main/java/com/minininja/learngles/SampleData.kt +++ b/tutorial/src/main/java/com/minininja/learngles/SampleData.kt @@ -55,7 +55,7 @@ object SampleData { ) val advancedItems = listOf( TutorialItem("oit", R.string.item_oit, "images/oit.png", OitActivity::class), - TutorialItem("scene", R.string.item_scene, "images/scene.png", SceneActivity::class), +// TutorialItem("scene", R.string.item_scene, "images/scene.png", SceneActivity::class), TutorialItem("csm", R.string.item_csm, "images/csm.png", CsmActivity::class), // TutorialItem("dsa", R.string.item_dsa, "images/dsa.png", DsaActivity::class) )