From e10ec320ba61850d216da6601bb3b946226bc28a Mon Sep 17 00:00:00 2001 From: samthakur587 Date: Thu, 8 Aug 2024 12:18:36 +0530 Subject: [PATCH 1/3] fix: updated the samples with new structure --- samples/Adv complexShader.cgl | 71 ++++++++----- samples/Advanced Post-Processing Effects.cgl | 24 +++-- samples/Advanced Shader.cgl | 59 ++++++----- samples/Advanced calculateLighting.cgl | 72 ++++++------- samples/Anaglyph 3D.cgl | 35 ++++--- samples/Atmospheric Scattering.cgl | 21 ++-- samples/Basic Fog Shader.cgl | 16 ++- samples/Basic Reflection Shader.cgl | 23 ++++- samples/Basic Texture Shade.cgl | 12 ++- samples/Blinn-Phong Shading.cgl | 72 ++++++------- samples/Bloom Effect.cgl | 49 ++++++--- samples/Bump Mapping.cgl | 62 +++++++---- samples/Calculate Lighting.cgl | 75 +++++--------- samples/Cartoon Outline.cgl | 57 +++++++---- samples/Cel Shading.cgl | 41 +++++--- samples/Chromakey Shader.cgl | 45 ++++---- samples/Chromatic Aberration.cgl | 44 ++++---- samples/Color Inversion.cgl | 32 +++--- samples/Complex Shader.cgl | 102 +++++++++++++++---- samples/Depth Fog.cgl | 49 +++++---- samples/Depth of Field.cgl | 65 ++++++++---- samples/Displacement Mapping.cgl | 53 +++++++--- samples/Dynamic Day-Night Cycle.cgl | 21 ++-- samples/Dynamic Water Shader.cgl | 26 +++-- samples/Edge Detection.cgl | 56 ++++++---- samples/Emboss Effect.cgl | 72 +++++++------ samples/Flat Shading.cgl | 31 +++--- samples/Fluid Dynamics Simulation.cgl | 23 +++-- samples/Fog Effect.cgl | 37 ++++--- samples/Fresnel Effect.cgl | 49 +++++---- samples/Glitch Effect.cgl | 56 +++++++--- samples/Gouraud Shading.cgl | 46 +++++---- samples/Gradient Background.cgl | 35 ++++--- samples/Halftone Effect.cgl | 70 +++++++++---- samples/Hatching shader.cgl | 82 ++++++++++----- samples/Heat Haze.cgl | 55 +++++++--- samples/Kaleidoscope Effect.cgl | 69 +++++++++---- samples/Lambertian Shading.cgl | 49 +++++---- samples/Light Shaft Effect.cgl | 70 +++++++++---- samples/Metallic Shader.cgl | 55 +++++----- samples/Motion Blur.cgl | 42 +++++--- samples/Nested Loop Shader.cgl | 63 +++++++++--- samples/Normal Mapping.cgl | 66 +++++++----- 43 files changed, 1358 insertions(+), 794 deletions(-) diff --git a/samples/Adv complexShader.cgl b/samples/Adv complexShader.cgl index dafe8bb..b3f03a9 100644 --- a/samples/Adv complexShader.cgl +++ b/samples/Adv complexShader.cgl @@ -2,46 +2,63 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; + input vec3 normal; + output vec4 color; + + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); + } void main() { - fragTexCoord = texCoord; - fragPosition = position; // Directly pass position to fragment shader - gl_Position = vec4(position, 1.0); + // Pass texture coordinates and normals to the fragment shader + color.xy = texCoord; + color.zw = normal; + + // Simulate normal map calculation in the vertex shader + vec3 mapNormal = normalize(vec3(texCoord, 1.0) * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(normal, mapNormal); + + // Pass perturbed normal to the fragment shader + color.rgb = vec3(perturbedNormal); + color.a = 1.0; // Alpha channel } } fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; + input vec4 color; + input vec2 texCoord; output vec4 fragColor; - vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { - float ambientStrength = 0.1; - vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Default light color - - float diff = max(dot(normal, lightDir), 0.0); - vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Default light color - - float specularStrength = 0.5; + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) { vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Default light color - - return ambient + diffuse + specular; + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0); } void main() { - vec4 texColor = texture2D(textureSampler, fragTexCoord); // Assumed default sampler - vec3 normal = normalize(cross(dFdx(fragPosition), dFdy(fragPosition))); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Default light direction - vec3 viewDir = normalize(-fragPosition); // Simple view direction + vec3 perturbedNormal = normalize(color.rgb); + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position + vec3 lightDir = normalize(lightPos - vec3(color)); + vec3 viewDir = normalize(viewPos - vec3(color)); + + // Ambient component + vec3 ambient = vec3(0.1, 0.1, 0.1); // Example ambient color + + // Diffuse component + float diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * vec3(0.8, 0.8, 0.8); // Example diffuse color + + // Specular component + float shininess = 32.0; // Example shininess + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir, shininess); + + // Combine lighting components + vec3 lighting = ambient + diffuse + specular; - vec3 lighting = calculateLighting(normal, lightDir, viewDir); - vec4 color = vec4(lighting, 1.0) * texColor; + vec4 finalColor = vec4(lighting, 1.0); - fragColor = color; + fragColor = finalColor; } } -} \ No newline at end of file +} diff --git a/samples/Advanced Post-Processing Effects.cgl b/samples/Advanced Post-Processing Effects.cgl index 7ad3a1d..ffc5528 100644 --- a/samples/Advanced Post-Processing Effects.cgl +++ b/samples/Advanced Post-Processing Effects.cgl @@ -1,17 +1,29 @@ shader main { vertex { - // Vertex shader logic goes here. + input vec2 texCoord; + output vec4 color; + + void main() { + // Pass through texture coordinates as color + color = vec4(texCoord, 0.0, 1.0); + } } fragment { - input vec2 texCoord; + input vec4 color; output vec4 fragColor; + vec4 textureSample(vec2 texCoord) { + // Example function to simulate texture sampling + return vec4(texCoord.x, texCoord.y, 0.5, 1.0); + } + void main() { - float brightness = 0.0; // Placeholder for brightness calculation + float brightness = textureSample(color.xy); float bloom = max(0.0, brightness - 0.5); - vec3 color = vec3(0.0); // Placeholder for texture color - fragColor = vec4(color + vec3(bloom), 1.0); + vec3 texColor = textureSample(color.xy); + vec3 colorWithBloom = texColor + vec3(bloom); + fragColor = vec4(colorWithBloom, 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Advanced Shader.cgl b/samples/Advanced Shader.cgl index ab68ef3..f911fbd 100644 --- a/samples/Advanced Shader.cgl +++ b/samples/Advanced Shader.cgl @@ -2,48 +2,57 @@ shader advancedShader { vertex { input vec3 position; input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; + output vec4 color; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - gl_Position = vec4(position, 1.0); - } - } - - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - output vec4 fragColor; - - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) { vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); return spec * vec3(1.0, 1.0, 1.0); // White specular highlight } void main() { - vec4 texColor = texture2D(textureSampler, fragTexCoord); // Default sampler + // Pass texture coordinates to the fragment shader + color.xy = texCoord; + + // Simulate ambient, light position, view position, and shininess + vec3 ambientColor = vec3(0.1, 0.1, 0.1); + vec3 lightPos = vec3(10.0, 10.0, 10.0); + vec3 viewPos = vec3(0.0, 0.0, 10.0); + float shininess = 32.0; - vec3 norm = normalize(cross(dFdx(fragPosition), dFdy(fragPosition))); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position - vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position + // Calculate normal in the vertex shader + vec3 norm = normalize(cross(dFdx(position), dFdy(position))); + vec3 lightDir = normalize(lightPos - position); + vec3 viewDir = normalize(viewPos - position); // Ambient component - vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color + vec3 ambient = ambientColor; // Diffuse component float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // White diffuse light // Specular component - vec3 specular = calculateSpecular(norm, lightDir, viewDir); + vec3 specular = calculateSpecular(norm, lightDir, viewDir, shininess); vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * texColor; + color.rgb = lighting; + color.a = 1.0; // Alpha channel + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + vec4 textureSample(vec2 texCoord) { + // Example function to simulate texture sampling + return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Simulated texture color + } - fragColor = color; + void main() { + vec4 texColor = textureSample(color.xy); + fragColor = vec4(color.rgb * texColor.rgb, 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Advanced calculateLighting.cgl b/samples/Advanced calculateLighting.cgl index ad1a9f4..a469a29 100644 --- a/samples/Advanced calculateLighting.cgl +++ b/samples/Advanced calculateLighting.cgl @@ -2,60 +2,50 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - input vec3 normal; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; + output vec4 color; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; - gl_Position = vec4(position, 1.0); - } - } + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Example ambient color - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + float diff = max(dot(normal, lightDir), 0.0); + vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Example diffuse color - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + float specularStrength = 0.5; vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess - return spec * vec3(1.0, 1.0, 1.0) * 1.0; // Default specular intensity - } + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Example specular color - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); + return ambient + diffuse + specular; } void main() { - vec4 diffuseColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - vec4 specularColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - vec4 normalMapColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - - vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); + // Pass texture coordinates to the fragment shader + color.xy = texCoord; - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position - vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position + // Calculate normal in the vertex shader + vec3 normal = normalize(cross(dFdx(position), dFdy(position))); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Example light direction + vec3 viewDir = normalize(-position); // Example view direction - // Ambient component - vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color - - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * diffuseColor.rgb; + vec3 lighting = calculateLighting(normal, lightDir, viewDir); + color.rgb = lighting; + color.a = 1.0; // Alpha channel + } + } - // Specular component - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + fragment { + input vec4 color; + output vec4 fragColor; - vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * diffuseColor; + vec4 textureSample(vec2 texCoord) { + // Example function to simulate texture sampling + return vec4(texCoord.x, texCoord.y, 1.0 - texCoord.x, 1.0); + } - fragColor = color; + void main() { + vec4 texColor = textureSample(color.xy); // Simulated texture sampling + fragColor = vec4(color.rgb * texColor.rgb, 1.0); } } } diff --git a/samples/Anaglyph 3D.cgl b/samples/Anaglyph 3D.cgl index 99b196f..8bc6c96 100644 --- a/samples/Anaglyph 3D.cgl +++ b/samples/Anaglyph 3D.cgl @@ -1,21 +1,28 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec4 color; - void main() { - fragTexCoord = texCoord; + void main() { + // Pass through texture coordinates as color + color = vec4(texCoord, 0.0, 1.0); + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec4 color; + output vec4 fragColor; + + // Define a simulated texture sampling function + vec4 textureSample(vec2 texCoord) { + // Simulated texture sampling: using simple color mapping + return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Sample color with brightness in the red channel + } - void main() { - vec4 leftColor = texture(texture, fragTexCoord - eyeOffset); - vec4 rightColor = texture(texture, fragTexCoord + eyeOffset); - fragColor = vec4(leftColor, rightColor, rightColor, 1.0); + void main() { + vec4 leftColor = textureSample(color.xy - vec2(0.1)); // Simulate left texture sample + vec4 rightColor = textureSample(color.xy + vec2(0.1)); // Simulate right texture sample + fragColor = vec4(leftColor.r, rightColor.g, rightColor.b, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Atmospheric Scattering.cgl b/samples/Atmospheric Scattering.cgl index da6a9ad..79fefcc 100644 --- a/samples/Atmospheric Scattering.cgl +++ b/samples/Atmospheric Scattering.cgl @@ -1,11 +1,7 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; - output vec4 fragColor; + output vec4 color; vec3 rayleighScattering(float theta) { return vec3(0.5, 0.7, 1.0) * pow(max(0.0, 1.0 - theta * theta), 3.0); @@ -13,8 +9,17 @@ shader main { void main() { float theta = position.y; - vec3 color = rayleighScattering(theta); - fragColor = vec4(color, 1.0); + vec3 colorComputed = rayleighScattering(theta); + color = vec4(colorComputed, 1.0); + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Basic Fog Shader.cgl b/samples/Basic Fog Shader.cgl index cecec25..3e2a33d 100644 --- a/samples/Basic Fog Shader.cgl +++ b/samples/Basic Fog Shader.cgl @@ -17,11 +17,23 @@ shader main { input vec3 fragPosition; output vec4 fragColor; + // Simulate texture sampling function + vec4 textureSample(vec2 texCoord) { + // Simulated texture color based on texCoord + return vec4(texCoord, 0.5, 1.0); // Example color with depth information in the red channel + } + + void main() { - + + vec3 baseColor = textureSample(fragTexCoord); + + // Calculate depth based on position (simple distance from origin) float depth = length(fragPosition); float fogFactor = exp(-fogDensity * depth); - vec3 finalColor = mix(fogColor, baseColor, fogFactor); + + // Compute final color with fog effect + vec3 finalColor = mix(fogColor, baseColor.rgb, fogFactor); fragColor = vec4(finalColor, 1.0); } } diff --git a/samples/Basic Reflection Shader.cgl b/samples/Basic Reflection Shader.cgl index 494ed7e..ec75a33 100644 --- a/samples/Basic Reflection Shader.cgl +++ b/samples/Basic Reflection Shader.cgl @@ -17,12 +17,31 @@ shader main { input vec3 fragNormal; output vec4 fragColor; + // Simulate texture sampling function + vec4 textureSample(vec2 texCoord) { + // Example simulated texture color based on texCoord + return vec4(texCoord, 0.5, 1.0); // Simulated texture color + } + + // Simulate environment map sampling function + vec4 cubeTextureSample(vec3 direction) { + // Example simulated cube map color based on direction + return vec4(abs(direction), 1.0); // Simulated environment map color + } + + + void main() { + // Simulate view position + vec3 viewPos = vec3(0.0, 0.0, 5.0); // Example view position vec3 norm = normalize(fragNormal); vec3 viewDir = normalize(viewPos - fragPosition); vec3 reflectDir = reflect(-viewDir, norm); - fragColor = vec4(mix(objectColor, environmentColor, 0.5), 1.0); + vec3 environmentColor = cubeTextureSample(reflectDir); + vec3 objectColor = textureSample(fragPosition.xy); + + fragColor = vec4(mix(objectColor.rgb, environmentColor.rgb, 0.5), 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Basic Texture Shade.cgl b/samples/Basic Texture Shade.cgl index b6ec320..f0b4a5b 100644 --- a/samples/Basic Texture Shade.cgl +++ b/samples/Basic Texture Shade.cgl @@ -5,7 +5,10 @@ shader main { output vec2 fragTexCoord; void main() { + // Pass texture coordinates to the fragment shader fragTexCoord = texCoord; + + // Set the position of the vertex gl_Position = vec4(position, 1.0); } } @@ -14,8 +17,15 @@ shader main { input vec2 fragTexCoord; output vec4 fragColor; + // Simulate texture sampling function + vec4 textureSample(vec2 texCoord) { + // Example simulated texture color based on texCoord + return vec4(texCoord, 0.5, 1.0); // Simulated texture color + } + void main() { - fragColor = textureColor; + // Sample the texture using the simulated texture sampling function + fragColor = textureSample(fragTexCoord); } } } diff --git a/samples/Blinn-Phong Shading.cgl b/samples/Blinn-Phong Shading.cgl index c6e8efd..ca1bb34 100644 --- a/samples/Blinn-Phong Shading.cgl +++ b/samples/Blinn-Phong Shading.cgl @@ -1,41 +1,41 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; - - void main() { - fragPosition = position; - fragNormal = normal; + vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; + + // Inputs for vertex shader + void main() { + // Pass position and normal to fragment shader + fragPosition = position; + fragNormal = normal; + + // Set the position of the vertex + gl_Position = vec4(position, 1.0); + } } -} - -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - - void main() { - vec3 norm = normalize(fragNormal); - - // Hardcoded light and view positions - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Light position - vec3 viewPos = vec3(0.0, 0.0, 1.0); // View position - - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - - // Calculate the halfway vector - vec3 viewDir = normalize(viewPos - fragPosition); - vec3 halfwayDir = normalize(lightDir + viewDir); - - // Calculate specular lighting - float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); - // Combine diffuse and specular components - vec3 color = vec3(0.5, 0.5, 0.5) * diff + vec3(1.0, 1.0, 1.0) * spec; - fragColor = vec4(color, 1.0); + fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; + + + void main() { + // Calculate lighting + // Define constant light and view positions for this shader + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position + vec3 norm = normalize(fragNormal); + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 halfwayDir = normalize(lightDir + viewPos - fragPosition); + float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; + + // Output the final color + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Bloom Effect.cgl b/samples/Bloom Effect.cgl index acc85e3..f47262d 100644 --- a/samples/Bloom Effect.cgl +++ b/samples/Bloom Effect.cgl @@ -1,22 +1,41 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + // Inputs for vertex shader + void main() { + // Pass position and normal to fragment shader + fragPosition = position; + fragNormal = normal; + + // Set the position of the vertex + gl_Position = vec4(position, 1.0); + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; + + + void main() { + // Calculate lighting + // Define constant light and view positions for this shader + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position + vec3 norm = normalize(fragNormal); + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 wayDir = normalize(lightDir + viewPos - fragPosition); + float spec = pow(max(dot(norm, wayDir), 0.0), 32.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; - void main() { - vec4 color = texture(texture, fragCoord); - vec4 bloom = max(color - threshold, 0.0); - fragColor = color + bloom; + // Output the final color + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Bump Mapping.cgl b/samples/Bump Mapping.cgl index d5bd4cf..8af45a1 100644 --- a/samples/Bump Mapping.cgl +++ b/samples/Bump Mapping.cgl @@ -1,28 +1,48 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec2 fragTexCoord; + vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec3 fragPosition; + output vec3 fragNormal; + output vec2 fragTexCoord; - void main() { - fragPosition = position; - fragTexCoord = texCoord; + void main() { + fragPosition = position; + fragNormal = normal; + fragTexCoord = texCoord; + + // Set the position of the vertex + gl_Position = vec4(position, 1.0); + } } -} -fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec3 fragPosition; + input vec3 fragNormal; + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - vec3 norm = normalize(texture(normalMap, fragTexCoord) * 2.0 - 1.0); - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = texture(texture, fragTexCoord) * diff; - fragColor = vec4(color, 1.0); + + void main() { + // Hardcoded texture and normal map values (you may need to adapt this to your setup) + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 textureColor = vec3(1.0, 1.0, 1.0); // Example texture color + vec3 normalMapColor = vec3(0.5, 0.5, 1.0); // Example normal map color + // Fetch and normalize the normal from the normal map + vec3 norm = normalize(normalMapColor * 2.0 - 1.0); + + // Calculate the direction from the fragment to the light + vec3 lightDir = normalize(lightPos - fragPosition); + + // Compute the diffuse lighting term + float diff = max(dot(norm, lightDir), 0.0); + + // Fetch the color from the texture and apply the diffuse lighting + vec3 color = textureColor * diff; + + // Output the final color with full opacity + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Calculate Lighting.cgl b/samples/Calculate Lighting.cgl index 27aa059..a34dd79 100644 --- a/samples/Calculate Lighting.cgl +++ b/samples/Calculate Lighting.cgl @@ -2,64 +2,41 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - input vec3 normal; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; + output vec4 color; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; - gl_Position = vec4(position, 1.0); - } - } - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - - - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), defaultShininess); - return spec * vec3(1.0, 1.0, 1.0) * defaultSpecularIntensity; + float calculateLighting(vec3 normal, vec3 lightDir) { + float diff = max(dot(normal, lightDir), 0.0); + return diff; } - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); + vec4 textureColor(vec2 coord) { + // Hardcoded texture sampling (replace with actual sampling if needed) + return vec4(coord, 0.5, 1.0); // Simulated texture color } void main() { + // Hardcoded values for lighting and texture operations + vec3 hardcodedNormal = vec3(0.0, 0.0, 1.0); + vec3 hardcodedLightDir = vec3(1.0, 1.0, 1.0); + vec3 normal = hardcodedNormal; + vec3 lightDir = hardcodedLightDir; + float lightIntensity = calculateLighting(normal, lightDir); + + vec4 texColor = textureColor(texCoord); + color = texColor * lightIntensity; // Apply lighting to color + + // Pass texture coordinates to fragment shader + color.xy = texCoord; + } + } - vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); - - vec3 lightDir = normalize(defaultLightPos - fragPosition); - vec3 viewDir = normalize(defaultViewPos - fragPosition); - - // Ambient component - vec3 ambient = defaultAmbientColor; - - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * diffuseColor.rgb; - - // Specular component - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); - - // Loop to modify lighting based on iterations - vec3 lighting = ambient + diffuse + specular; - for (int i = 0; i < iterations; i++) { - lighting *= 0.9; - } - - // Ternary operator to conditionally modify color - vec4 color = (diff > 0.5) ? vec4(lighting, 1.0) * diffuseColor : vec4(lighting * 0.5, 1.0) * diffuseColor; + fragment { + input vec4 color; + output vec4 fragColor; + void main() { fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Cartoon Outline.cgl b/samples/Cartoon Outline.cgl index e2a6eaf..627bb9a 100644 --- a/samples/Cartoon Outline.cgl +++ b/samples/Cartoon Outline.cgl @@ -1,31 +1,44 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec4 fragColor; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + + + void main() { + // Pass texture coordinates to fragment shader + float outlineWidth = 0.01; // Example value for outline width + fragColor.xy = texCoord; + fragColor.zw = vec2(outlineWidth, 0.0); // Store outline width in the z component for later use + gl_Position = vec4(texCoord, 0.0, 1.0); // Example position + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 fragTexCoord; + input vec2 fragOutlineWidth; + output vec4 fragColor; + + // Simulated texture sampling + vec4 textureColor(vec2 coord) { + // Simulate texture sampling (replace with actual sampling if needed) + return vec4(coord, 0.5, 1.0); // Simulated texture color + } - void main() { - vec4 color = texture(texture, fragTexCoord); - vec4 north = texture(texture, fragTexCoord + vec2(0.0, outlineWidth)); - vec4 south = texture(texture, fragTexCoord - vec2(0.0, outlineWidth)); - vec4 east = texture(texture, fragTexCoord + vec2(outlineWidth, 0.0)); - vec4 west = texture(texture, fragTexCoord - vec2(outlineWidth, 0.0)); + void main() { + float outlineWidth = fragOutlineWidth.x; // Extract outline width + vec4 color = textureColor(fragTexCoord); + vec4 north = textureColor(fragTexCoord + vec2(0.0, outlineWidth)); + vec4 south = textureColor(fragTexCoord - vec2(0.0, outlineWidth)); + vec4 east = textureColor(fragTexCoord + vec2(outlineWidth, 0.0)); + vec4 west = textureColor(fragTexCoord - vec2(outlineWidth, 0.0)); - float edge = length((north + south + east + west) / 4.0 - color); - if (edge > 0.1) { - fragColor = vec4(0.0, 0.0, 0.0, 1.0); - } else { - fragColor = color; + float edge = length((north + south + east + west) / 4.0 - color); + if (edge > 0.1) { + fragColor = vec4(0.0, 0.0, 0.0, 1.0); + } else { + fragColor = color; + } } } } -} \ No newline at end of file diff --git a/samples/Cel Shading.cgl b/samples/Cel Shading.cgl index 6de128e..c7a3783 100644 --- a/samples/Cel Shading.cgl +++ b/samples/Cel Shading.cgl @@ -1,23 +1,32 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vertex { + input vec3 position; + input vec3 normal; + output vec4 color; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} + vec3 celShading(vec3 normal, vec3 lightDir) { + float inensity = dot(normal, lightDir); + if (inensity > 0.95) {return vec3(1.0, 1.0, 1.0);} + if (inensity > 0.5) {return vec3(0.8, 0.8, 0.8);} + if (inensity > 0.25) {return vec3(0.6, 0.6, 0.6);} + else {return vec3(0.4, 0.4, 0.4);} + } + + void main() { + vec3 norm = normalize(normal); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); + vec3 shadingColor = celShading(norm, lightDir); + + color = vec4(shadingColor, 1.0); // Alpha channel + } + } -fragment { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + fragment { + input vec4 color; + output vec4 fragColor; void main() { - vec3 norm = normalize(normal); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); // Example light position - - fragColor = vec4(color, 1.0); + fragColor = color; + } } } -} \ No newline at end of file diff --git a/samples/Chromakey Shader.cgl b/samples/Chromakey Shader.cgl index aa49ada..acbddad 100644 --- a/samples/Chromakey Shader.cgl +++ b/samples/Chromakey Shader.cgl @@ -1,28 +1,35 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - fragTexCoord = texCoord; + void main() { + fragTexCoord = texCoord; + gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // This should be set properly for your use case + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 fragTexCoord; + output vec4 fragColor; + + vec4 textureColor(vec2 coord) { + // Example function to simulate texture sampling + return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color + } - void main() { - vec4 color = texture(texture, fragTexCoord); - vec3 keyColor = vec3(1.0, 0.0, 0.0); // Example key color - float threshold = 0.1; // Example threshold + void main() { + vec3 keyColor = vec3(0.0, 1.0, 0.0); // Example key color + float threshold = 0.1; // Example threshold - float diff = length(color - keyColor); - if (diff < threshold) { - discard; - } else { - fragColor = color; + vec4 color = textureColor(fragTexCoord); + float diff = length(color.rgb - keyColor); + if (diff < threshold) { + // Discard fragment + fragColor = vec4(0.0, 0.0, 0.0, 0.0); + } else { + fragColor = color; + } } } } -} \ No newline at end of file diff --git a/samples/Chromatic Aberration.cgl b/samples/Chromatic Aberration.cgl index 4ceb3b8..e4b2f67 100644 --- a/samples/Chromatic Aberration.cgl +++ b/samples/Chromatic Aberration.cgl @@ -1,24 +1,34 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec4 color; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + // Pass through the texture coordinate + void main() { + color = vec4(texCoord, 0.0, 1.0); + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the texture coordinate + output vec4 fragColor; + + vec4 textureColor(vec2 coord) { + // Example function to simulate texture sampling + return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color + } + + void main() { + vec2 texCoord = color.xy; // Extract the texture coordinate - void main() { - vec2 offset = vec2(aberration / 512.0); - vec4 red = texture(texture, fragCoord + offset); - vec4 green = texture(texture, fragCoord); - vec4 blue = texture(texture, fragCoord - offset); - fragColor = vec4(red.r, green.g, blue.b, 1.0); + float aberration = 0.005; // Example aberration value + vec2 offset = vec2(aberration / 512.0); + + vec4 red = textureColor(texCoord + offset); + vec4 green = textureColor(texCoord); + vec4 blue = textureColor(texCoord - offset); + + fragColor = vec4(red.r, green.g, blue.b, 1.0); + } } } -} diff --git a/samples/Color Inversion.cgl b/samples/Color Inversion.cgl index 9cd7eb0..0ff4d0b 100644 --- a/samples/Color Inversion.cgl +++ b/samples/Color Inversion.cgl @@ -1,21 +1,25 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + output vec4 fragColor; + + vec4 textureColor(vec2 coord) { + // Example function to simulate texture sampling + return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color + } - void main() { - vec4 color = texture(texture, fragTexCoord); - fragColor = vec4(vec3(1.0) - color, 1.0); + void main() { + vec4 color = textureColor(passTexCoord); + fragColor = vec4(vec3(1.0) - color.rgb, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Complex Shader.cgl b/samples/Complex Shader.cgl index d8b82c0..b001e9a 100644 --- a/samples/Complex Shader.cgl +++ b/samples/Complex Shader.cgl @@ -1,31 +1,99 @@ shader complexShader { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; input vec2 texCoord; - output vec4 fragColor; + input vec3 normal; + input vec3 diffuseTexture; + input vec3 specularTexture; + input vec3 normalMap; + input vec3 lightPos; + input vec3 viewPos; + input vec3 ambientColor; + input float shininess; + input float specularIntensity; + input float shininess; + output vec4 color; + output vec2 passTexCoord; + + + + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; + } + + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); + } + + void main() { + passTexCoord = texCoord; + if (normalMap >= vec3(0.0, 0.0, 0.0)) { + test += 1.0; + test -= 1.0; + test *= 1.0; + test /= 1.0; + float test = 1.0; + float test = 1.0; + // No normal map + color.rgb = normal; + color.a = 1.0; // Alpha channel + } + // Calculate normal map in the vertex shader + vec4 normalMapColor = texture(normalMap, texCoord); + vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(normal, mapNormal); - float calculateLighting(vec3 normal, vec3 lightDir) { - float diff = max(dot(normal, lightDir), 0.0); - return diff; + // Pass perturbed normal to the fragment shader + color.rgb = vec3(perturbedNormal); + color.a = 1.0; // Alpha channel } + } - vec4 textureColor(vec2 coord) { - return texture2D(textureSampler, coord); + fragment { + input vec4 color; // Perturbed normal + input vec2 passTexCoord; + + + // Simulated textures and lighting parameters + input vec3 diffuseTexture; + input vec3 specularTexture; + input vec3 lightPos; + input vec3 viewPos; + input vec3 ambientColor; + input float shininess; + input float specularIntensity; + output vec4 fragColor; + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; } void main() { - vec3 normal = normalize(vec3(0.0, 0.0, 1.0)); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); - float lightIntensity = calculateLighting(normal, lightDir); - - vec4 color = textureColor(texCoord); - color *= lightIntensity; // Corrected error: multiplying RGB components + vec4 diffuseColor = texture(diffuseTexture, passTexCoord); + vec4 specularColor = texture(specularTexture, passTexCoord); + + vec3 perturbedNormal = normalize(color.rgb); + vec3 lightDir = normalize(lightPos - position); + vec3 viewDir = normalize(viewPos - position); + + // Ambient component + vec3 ambient = ambientColor; + + // Diffuse component + float diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * diffuseColor.rgb; + + // Specular component + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + + vec3 lighting = ambient + diffuse + specular; + vec4 color = vec4(lighting, 1.0) * diffuseColor; fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Depth Fog.cgl b/samples/Depth Fog.cgl index f4d2635..92facb9 100644 --- a/samples/Depth Fog.cgl +++ b/samples/Depth Fog.cgl @@ -1,30 +1,37 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec2 fragTexCoord; + vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec2 passTexCoord; + output float passDepth; - void main() { - fragPosition = position; - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + passDepth = length(position); + } } -} -fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + input float passDepth; + // Simulated input values + input float texture; + input float fogDensity; + input vec3 fogColor; + output vec4 fragColor; + // Simulated texture function + vec4 get_texture(float tex, vec2 coord) { + return vec4(coord.x, coord.y, 0.5, 1.0); + } - void main() { - vec3 baseColor = texture(texture, fragTexCoord); - float depth = length(fragPosition); - float fogFactor = exp(-fogDensity * depth); - vec3 finalColor = mix(fogColor, baseColor, fogFactor); + void main() { + vec3 baseColor = get_texture(texture, passTexCoord); + float fogFactor = exp(-fogDensity * passDepth); + vec3 finalColor = mix(fogColor, baseColor.rgb, fogFactor); - fragColor = vec4(finalColor, 1.0); + fragColor = vec4(finalColor, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Depth of Field.cgl b/samples/Depth of Field.cgl index eb7d641..4badaaf 100644 --- a/samples/Depth of Field.cgl +++ b/samples/Depth of Field.cgl @@ -1,31 +1,54 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + + + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input vec4 depthTextureData; // Example for a 512x512 depth texture + input int textureWidth; + input int textureHeight; + input float focusDepth; + input float focusRange; + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - void main() { - float depth = texture(depthTexture, fragCoord); - float blur = clamp(abs(depth - focusDepth) / focusRange, 0.0, 1.0); + void main() { + vec2 texCoord = passTexCoord; // Extract the texture coordinate + float depth = texture(depthTextureData, texCoord, textureWidth, textureHeight); + float blur = clamp(abs(depth.r - focusDepth) / focusRange, 0.0, 1.0); - vec4 color = vec4(0.0); - for (int i = -4; i < 4; i++) { - for (int j = -4; j < 4; j++) { - vec2 offset = vec2(i, j) * blur / 512.0; - color += texture(texture, fragCoord + offset); + vec4 colorAccum = vec4(0.0); + int blurRadius = 4; + for (int i = -blurRadius; i <= blurRadius; i=i+1) { + for (int j = -blurRadius; j <= blurRadius; j=j+1) { + vec2 offset = vec2(float(i), float(j)) * blur / float(textureWidth); + colorAccum += texture(textureData, texCoord + offset, textureWidth, textureHeight); + } } + colorAccum /= float((2 * blurRadius + 1) * (2 * blurRadius + 1)); + fragColor = colorAccum; } - color /= 81.0; - fragColor = color; } } -} \ No newline at end of file diff --git a/samples/Displacement Mapping.cgl b/samples/Displacement Mapping.cgl index 251afe6..673bf1a 100644 --- a/samples/Displacement Mapping.cgl +++ b/samples/Displacement Mapping.cgl @@ -1,22 +1,45 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + + + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input vec4 displacementMapData; // Example for a 512x512 displacement map + input int textureWidth; + input int textureHeight; + input float displacementFactor; + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - void main() { - vec2 displacement = texture(displacementMap, fragTexCoord) * 2.0 - 1.0; - vec2 uv = fragTexCoord + displacement * displacementFactor; - vec4 color = texture(texture, uv); - fragColor = vec4(color, 1.0); + void main() { + vec2 texCoord = passTexCoord; + vec2 displacement = texture(displacementMapData, texCoord, textureWidth, textureHeight); + displacement = displacement.rg * 2.0 - 1.0; + vec2 uv = texCoord + displacement * displacementFactor; + vec4 color = texture(textureData, uv, textureWidth, textureHeight); + fragColor = vec4(color.rgb, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Dynamic Day-Night Cycle.cgl b/samples/Dynamic Day-Night Cycle.cgl index 37aa01f..df71e9f 100644 --- a/samples/Dynamic Day-Night Cycle.cgl +++ b/samples/Dynamic Day-Night Cycle.cgl @@ -1,20 +1,25 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; - output vec4 fragColor; + output vec4 color; vec3 dayNightColor(float timeOfDay) { return mix(vec3(0.0, 0.5, 1.0), vec3(0.0, 0.0, 0.2), timeOfDay); } void main() { - float timeOfDay = 0.0; // Placeholder for time of day calculation - vec3 color = dayNightColor(timeOfDay); - fragColor = vec4(color, 1.0); + float timeOfDay = mod(iTime / 24.0, 1.0); + vec3 colorComputed = dayNightColor(timeOfDay); + color = vec4(colorComputed, 1.0); + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } } diff --git a/samples/Dynamic Water Shader.cgl b/samples/Dynamic Water Shader.cgl index b3cefc8..d57297b 100644 --- a/samples/Dynamic Water Shader.cgl +++ b/samples/Dynamic Water Shader.cgl @@ -1,22 +1,26 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec2 position; - output vec4 fragColor; + output vec4 color; float wave(vec2 p, float time) { - return sin(p.x * 10.0) * 0.1 + sin(p.y * 10.0) * 0.1; + return sin(p.x * 10.0 + time * 2.0) * 0.1 + sin(p.y * 10.0 + time * 2.0) * 0.1; } - + void main() { vec2 uv = position; - float waterHeight = wave(uv, 0.0); // Placeholder for time value - vec3 color = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); + float waterHeight = wave(uv, iTime); + vec3 colorComputed = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); + color = vec4(colorComputed, 1.0); + } + } - fragColor = vec4(color, 1.0); + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Edge Detection.cgl b/samples/Edge Detection.cgl index 1700e2a..84fda7a 100644 --- a/samples/Edge Detection.cgl +++ b/samples/Edge Detection.cgl @@ -1,27 +1,45 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - // Pass through the texCoord as fragCoord for simplicity - void main() { - fragCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input int textureWidth; + input int textureHeight; + output vec4 fragColor; - void main() { - vec2 texCoord = fragCoord; - vec2 offset = vec2(1.0 / 512.0, 1.0 / 512.0); - vec3 color = texture(texture, texCoord); - vec3 colorRight = texture(texture, texCoord + vec2(offset.x, 0.0)); - vec3 colorUp = texture(texture, texCoord + vec2(0.0, offset.y)); + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - vec3 edge = abs(color - colorRight) + abs(color - colorUp); - fragColor = vec4(edge, 1.0); + void main() { + vec2 texCoord = passTexCoord; // Extract the texture coordinate + vec2 offset = vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight)); + vec3 color = texture(textureData, texCoord, textureWidth, textureHeight); + vec3 colorRight = texture(textureData, texCoord + vec2(offset.x, 0.0), textureWidth, textureHeight); + vec3 colorUp = texture(textureData, texCoord + vec2(0.0, offset.y), textureWidth, textureHeight); + color = color.rgb; + colorRight = colorRight.rgb; + colorUp = colorUp.rgb; + vec3 edge = abs(color - colorRight) + abs(color - colorUp); + fragColor = vec4(edge, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Emboss Effect.cgl b/samples/Emboss Effect.cgl index 08f620c..84fda7a 100644 --- a/samples/Emboss Effect.cgl +++ b/samples/Emboss Effect.cgl @@ -1,39 +1,45 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} - -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - vec2 texOffset = vec2(1.0 / 512.0); - vec3 center = texture(texture, fragTexCoord); - vec3 north = texture(texture, fragTexCoord + vec2(0.0, texOffset)); - vec3 south = texture(texture, fragTexCoord - vec2(0.0, texOffset)); - vec3 east = texture(texture, fragTexCoord + vec2(texOffset, 0.0)); - vec3 west = texture(texture, fragTexCoord - vec2(texOffset, 0.0)); - vec3 northwest = texture(texture, fragTexCoord + vec2(-texOffset, texOffset)); - vec3 northeast = texture(texture, fragTexCoord + vec2(texOffset, texOffset)); - vec3 southwest = texture(texture, fragTexCoord + vec2(-texOffset, -texOffset)); - vec3 southeast = texture(texture, fragTexCoord + vec2(texOffset, -texOffset)); + fragment { + input vec2 passTexCoord; + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input int textureWidth; + input int textureHeight; + output vec4 fragColor; - vec3 color = center * -1.0 + - north * -1.0 + - south * -1.0 + - east * -1.0 + - west * -1.0 + - northwest * 1.0 + - northeast * 1.0 + - southwest * 1.0 + - southeast * 1.0; + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - fragColor = vec4(color + vec3(0.5), 1.0); - } \ No newline at end of file + void main() { + vec2 texCoord = passTexCoord; // Extract the texture coordinate + vec2 offset = vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight)); + vec3 color = texture(textureData, texCoord, textureWidth, textureHeight); + vec3 colorRight = texture(textureData, texCoord + vec2(offset.x, 0.0), textureWidth, textureHeight); + vec3 colorUp = texture(textureData, texCoord + vec2(0.0, offset.y), textureWidth, textureHeight); + color = color.rgb; + colorRight = colorRight.rgb; + colorUp = colorUp.rgb; + vec3 edge = abs(color - colorRight) + abs(color - colorUp); + fragColor = vec4(edge, 1.0); + } + } +} diff --git a/samples/Flat Shading.cgl b/samples/Flat Shading.cgl index 69508a0..629079e 100644 --- a/samples/Flat Shading.cgl +++ b/samples/Flat Shading.cgl @@ -1,24 +1,21 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragNormal; + vertex { + input vec3 position; + input vec3 normal; + output vec3 passNormal; - void main() { - fragNormal = normal; + void main() { + passNormal = normal; + } } -} -fragment { - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec3 passNormal; + output vec4 fragColor; - void main() { - // Base color - vec3 baseColor = vec3(0.5, 0.5, 0.5); - // Adjust color based on normal - vec3 color = baseColor + 0.5 * fragNormal; - fragColor = vec4(color, 1.0); + void main() { + vec3 color = vec3(0.5, 0.5, 0.5) + 0.5 * passNormal; + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Fluid Dynamics Simulation.cgl b/samples/Fluid Dynamics Simulation.cgl index 0917ce1..acfb3c7 100644 --- a/samples/Fluid Dynamics Simulation.cgl +++ b/samples/Fluid Dynamics Simulation.cgl @@ -1,20 +1,25 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; - output vec4 fragColor; + output vec4 color; float fluidMotion(vec3 p) { - return sin(p.x * 10.0) * 0.5 + 0.5; + return sin(p.x * 10.0 + iTime) * 0.5 + 0.5; } void main() { float motion = fluidMotion(position); - vec3 color = vec3(0.0, 0.0, 1.0) * motion; - fragColor = vec4(color, 1.0); + vec3 colorComputed = vec3(0.0, 0.0, 1.0) * motion; + color = vec4(colorComputed, 1.0); + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Fog Effect.cgl b/samples/Fog Effect.cgl index 37f0c9e..c413250 100644 --- a/samples/Fog Effect.cgl +++ b/samples/Fog Effect.cgl @@ -1,23 +1,28 @@ shader main { -vertex { - input vec3 position; - output vec3 fragPosition; + vertex { + input vec3 position; + output vec3 passPosition; - // Pass through the position to the fragment shader - void main() { - fragPosition = position; + // Pass through the position + void main() { + passPosition = position; + } } -} -fragment { - input vec3 fragPosition; - output vec4 fragColor; + fragment { + input vec3 passPosition; // This contains the position + + input vec3 cameraPos; // Simulated uniform + input vec3 fogColor; // Simulated uniform + input float fogDensity; // Simulated uniform + output vec4 fragColor; - void main() { - float distance = length(fragPosition - cameraPos); - float fogFactor = exp(-pow(distance * fogDensity, 2.0)); - vec3 color = mix(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); - fragColor = vec4(color, 1.0); + void main() { + vec3 position = passPosition; // Extract the position + float distance = length(position - cameraPos); + float fogFactor = exp(-pow(distance * fogDensity, 2.0)); + vec3 finalColor = mix(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); + fragColor = vec4(finalColor, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Fresnel Effect.cgl b/samples/Fresnel Effect.cgl index a9dc6e7..25c6300 100644 --- a/samples/Fresnel Effect.cgl +++ b/samples/Fresnel Effect.cgl @@ -1,28 +1,39 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vertex { + input vec3 position; + input vec3 normal; + input vec3 viewDir; // Simulated uniform - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} + output vec3 passNormal; + output vec3 passViewDir; -fragment { - input vec3 normal; - output vec4 fragColor; + + float fresnelEffect(vec3 normal, vec3 viewDir) { + return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + } - float fresnelEffect(vec3 normal, vec3 viewDir) { - return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + void main() { + passNormal = normal; + passViewDir = viewDir; + } } - void main() { - vec3 norm = normalize(normal); - vec3 viewDir = vec3(0.0, 0.0, 1.0); // Example view direction - float fresnel = fresnelEffect(norm, normalize(viewDir)); - vec3 color = vec3(0.1, 0.5, 1.0) * fresnel; + fragment { + input vec3 passNormal; + input vec3 passViewDir; + output vec4 fragColor; + + float fresnelEffect(vec3 normal, vec3 viewDir) { + return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + } + + void main() { + vec3 norm = normalize(passNormal); + vec3 viewDir = normalize(passViewDir); + float fresnel = fresnelEffect(norm, viewDir); + vec3 color = vec3(0.1, 0.5, 1.0) * fresnel; - fragColor = vec4(color, 1.0); + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Glitch Effect.cgl b/samples/Glitch Effect.cgl index 86fb1b5..f878c64 100644 --- a/samples/Glitch Effect.cgl +++ b/samples/Glitch Effect.cgl @@ -1,25 +1,49 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + // Pass through the texture coordinate + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + + + // Simulated inputs + input vec4 textureData; // Example for a 512x512 texture + input int textureWidth; + input int textureHeight; + input float time; // Simulated uniform + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } + + void main() { + vec2 uv = passTexCoord; - void main() { - vec2 uv = fragTexCoord; + // Apply the time-based distortion + if (fract(time * 10.0) < 0.5) { + uv.x += sin(passTexCoord.y * 10.0) * 0.1; + } - if (fract(time * 10.0) < 0.5) { - uv += sin(fragTexCoord * 10.0) * 0.1; + // Sample the texture using the simulated texture function + vec4 color = texture(textureData, uv, textureWidth, textureHeight); + fragColor = color; } - vec4 color = texture(texture, uv); - fragColor = color; } } -} \ No newline at end of file diff --git a/samples/Gouraud Shading.cgl b/samples/Gouraud Shading.cgl index 48c41fd..962dd81 100644 --- a/samples/Gouraud Shading.cgl +++ b/samples/Gouraud Shading.cgl @@ -1,28 +1,32 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + vertex { + input vec3 position; + input vec3 normal; + // Simulated light position + input vec3 lightPos; + output vec3 passPosition; + output vec3 passNormal; + output vec3 passLightPos; - void main() { - fragPosition = position; - fragNormal = normal; + void main() { + passPosition = position; + passNormal = normal; + passLightPos = lightPos; + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec3 passPosition; + input vec3 passNormal; + input vec3 passLightPos; + output vec4 fragColor; - void main() { - vec3 norm = normalize(fragNormal); - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Hardcoded light position - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = vec3(0.5, 0.5, 0.5) * diff; // Diffuse color - fragColor = vec4(color, 1.0); + void main() { + vec3 norm = normalize(passNormal); + vec3 lightDir = normalize(passLightPos - passPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff; + fragColor = vec4(color, 1.0); + } } } -} diff --git a/samples/Gradient Background.cgl b/samples/Gradient Background.cgl index 9d06de1..0ab6bc7 100644 --- a/samples/Gradient Background.cgl +++ b/samples/Gradient Background.cgl @@ -1,20 +1,27 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated topColor and bottomColor + input vec3 topColor; + input vec3 bottomColor; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - vec3 color = mix(bottomColor, topColor, fragTexCoord.y); - fragColor = vec4(color, 1.0); + fragment { + input vec2 passTexCoord; + // Simulated topColor and bottomColor + input vec3 topColor; + input vec3 bottomColor; + output vec4 fragColor; + + void main() { + vec3 color = mix(bottomColor, topColor, passTexCoord.y); + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Halftone Effect.cgl b/samples/Halftone Effect.cgl index 49096e4..95fadfe 100644 --- a/samples/Halftone Effect.cgl +++ b/samples/Halftone Effect.cgl @@ -1,24 +1,58 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated inputs + input vec2 textureSize; // Size of the texture (width, height) + input float dotSize; + input vec4 textureData; // Example for a 512x512 texture + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - float angle = mod(uv + uv, 2.0) * 3.14159265; - float radius = length(fract(uv) - vec2(0.5)); - vec4 color = texture(texture, fragTexCoord); - float itensity = dot(color, vec3(0.299, 0.587, 0.114)); - float dot = smoothstep(0.4, 0.5, sin(angle) * 0.5 + radius); - fragColor = vec4(vec3(dot * itensity), 1.0); + fragment { + input vec2 passTexCoord; + // Simulated inputs + input vec2 textureSize; + input float dotSize; + input vec4 textureData; // Example for a 512x512 texture + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } + + void main() { + // Scale texture coordinates by dotSize + vec2 uv = passTexCoord * dotSize; + + // Calculate angle and radius for the dot pattern + float angle = mod(uv.x + uv.y, 2.0) * 3.14159265; + float radius = length(fract(uv) - vec2(0.5)); + + // Sample color from texture using the simulated texture function + vec4 color = texture(textureData, passTexCoord, textureSize); + + // Calculate intensity based on luminance + float intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); + + // Compute dot pattern using smoothstep for edge softness + float dot = smoothstep(0.4, 0.5, sin(angle) * 0.5 + radius); + + // Set fragment color with dot pattern applied + fragColor = vec4(vec3(dot * intensity), 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Hatching shader.cgl b/samples/Hatching shader.cgl index 0aff9c6..680c000 100644 --- a/samples/Hatching shader.cgl +++ b/samples/Hatching shader.cgl @@ -1,36 +1,66 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated thresholds + input float threshold1; + input float threshold2; + input float threshold3; + - void main() { - fragTexCoord = texCoord; + + // Simulated texture data + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec2 passTexCoord; + + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + - void main() { - vec4 color = texture(texture, fragTexCoord); - float itensity = dot(color, vec3(0.299, 0.587, 0.114)); + // Simulated thresholds + input float threshold1; + input float threshold2; + input float threshold3; - vec3 hatching = vec3(1.0); - if (itensity < threshold1) { - hatching = vec3(0.8); - } - if (itensity < threshold2) { - hatching = vec3(0.6); - } - if (itensity < threshold3) { - hatching = vec3(0.4); - } - else { - hatching = vec3(0.2); + // Simulated texture data + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; } - fragColor = vec4(hatching, 1.0); + void main() { + // Sample color from texture using the simulated texture function + vec4 color = texture(textureData, passTexCoord, textureSize); + + // Calculate intensity based on luminance + float intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); + + // Determine hatching color based on thresholds + vec3 hatching = vec3(1.0); + if (intensity < threshold1) {hatching = vec3(0.8);} + if (intensity < threshold2) {hatching = vec3(0.6);} + if (intensity < threshold3) {hatching = vec3(0.4);} + else {hatching = vec3(0.2);} + + // Set the fragment color + fragColor = vec4(hatching, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Heat Haze.cgl b/samples/Heat Haze.cgl index 7b65273..f470b95 100644 --- a/samples/Heat Haze.cgl +++ b/samples/Heat Haze.cgl @@ -1,22 +1,47 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated input for time and texture data + input float time; + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec2 texCoordOut; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + + void main() { + texCoordOut = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 texCoordOut; + + // Simulated input for time and texture data + input float time; + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } + + void main() { + vec2 uv = texCoordOut + sin(texCoordOut.y * 10.0 + time) * 0.01; + + // Sample color from texture using the simulated texture function + vec4 color = texture(textureData, uv, textureSize); - void main() { - vec2 uv = fragTexCoord + sin(fragTexCoord.y * 10.0 + time) * 0.01; - vec4 color = texture(texture, uv); - fragColor = vec4(color.rgb, 1.0); + fragColor = vec4(color.rgb, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Kaleidoscope Effect.cgl b/samples/Kaleidoscope Effect.cgl index fd7aede..41006b5 100644 --- a/samples/Kaleidoscope Effect.cgl +++ b/samples/Kaleidoscope Effect.cgl @@ -1,24 +1,59 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + + // Simulated input for texture data and segments + input vec4 textureData; // Example for a 512x512 texture + input float segments; + input vec2 textureSize; // Size of the texture (width, height) + output vec2 texCoordOut; - void main() { - fragTexCoord = texCoord; + + void main() { + texCoordOut = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 texCoordOut; + + // Simulated input for texture data and segments + input vec4 textureData; // Example for a 512x512 texture + input float segments; + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } - void main() { - float angle = atan(fragTexCoord.y - 0.5, fragTexCoord.x - 0.5); - float radius = length(fragTexCoord - vec2(0.5)); - float segment = floor(segments * angle / (2.0 * 3.14159265)); - float newAngle = segment * 2.0 * 3.14159265 / segments; - vec2 newTexCoord = vec2(0.5) + radius * vec2(cos(newAngle), sin(newAngle)); - fragColor = texture(texture, newTexCoord); + void main() { + // Compute the angle of the current texCoord relative to the center + float angle = atan(texCoordOut.y - 0.5, texCoordOut.x - 0.5); + + // Compute the radius from the center of the texture + float radius = length(texCoordOut - vec2(0.5)); + + // Determine the segment index based on the angle and number of segments + float segment = floor(segments * angle / (2.0 * 3.14159265)); + + // Compute the new angle corresponding to the segment + float newAngle = segment * 2.0 * 3.14159265 / segments; + + // Compute the new texture coordinates based on the new angle + vec2 newTexCoord = vec2(0.5) + radius * vec2(cos(newAngle), sin(newAngle)); + + // Sample the texture at the new texture coordinates + fragColor = texture(textureData, newTexCoord, textureSize); + } } } -} \ No newline at end of file diff --git a/samples/Lambertian Shading.cgl b/samples/Lambertian Shading.cgl index 5cf9a10..4690a5c 100644 --- a/samples/Lambertian Shading.cgl +++ b/samples/Lambertian Shading.cgl @@ -1,28 +1,35 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + vertex { + input vec3 position; + input vec3 normal; + // Simulated input for light position + input vec3 lightPos; + output vec4 color; - void main() { - fragPosition = position; - fragNormal = normal; + + void main() { + // Pass the position and normal to the fragment shader + color.xyz = position; + color.w = 1.0; // Alpha channel + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the position + input vec3 normal; // Pass the normal directly + // Simulated input for light position + input vec3 lightPos; + output vec4 fragColor; + + - void main() { - vec3 norm = normalize(fragNormal); - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Hardcoded light position - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = vec3(0.5, 0.5, 0.5) * diff; - fragColor = vec4(color, 1.0); + void main() { + vec3 pos = color.xyz; // Extract position + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - pos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) * diff; + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Light Shaft Effect.cgl b/samples/Light Shaft Effect.cgl index 827a472..e148501 100644 --- a/samples/Light Shaft Effect.cgl +++ b/samples/Light Shaft Effect.cgl @@ -1,27 +1,55 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; - - // Pass through position and normal to the fragment shader - void main() { - fragPosition = position; - fragNormal = normal; + vertex { + input vec2 texCoord; + + // Simulated inputs for texture data and light position + input vec4 textureData; // Example for a 512x512 texture + input vec2 lightPos; // Simulated light position + input vec2 textureSize; // Size of the texture (width, height) + output vec2 texCoordOut; + + void main() { + texCoordOut = texCoord; + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec2 texCoordOut; + + // Simulated inputs for texture data and light position + input vec4 textureData; // Example for a 512x512 texture + input vec2 lightPos; // Simulated light position + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } - void main() { - vec3 viewDir = normalize(viewPos - fragPosition); - float rim = pow(1.0 - max(dot(fragNormal, viewDir), 0.0), rimPower); - vec3 color = vec3(0.5, 0.5, 0.5) + rim * rimColor; - fragColor = vec4(color, 1.0); + void main() { + vec2 uv = texCoordOut; + vec4 color = texture(textureData, uv, textureSize); + vec2 dir = uv - lightPos; + float dist = length(dir); + + // Compute offset based on distance + vec2 offset = dir * (1.0 - dist) / textureSize; // Normalize by textureSize + vec2 uvOffset = uv - offset; + + // Sample the texture at the offset position + vec4 shaftColor = texture(textureData, uvOffset, textureSize); + + // Mix the original color with the shaft color + fragColor = mix(color, shaftColor, 0.5); + } } } -} diff --git a/samples/Metallic Shader.cgl b/samples/Metallic Shader.cgl index 7c35fef..1e7732b 100644 --- a/samples/Metallic Shader.cgl +++ b/samples/Metallic Shader.cgl @@ -1,34 +1,37 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vertex { + input vec3 position; + input vec3 normal; + // Simulated inputs + input vec3 lightPos; + input vec3 viewPos; + input float metallic; + output vec4 color; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} - -fragment { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambient = 0.1; + float diff = max(dot(normal, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + return ambient + diff + metallic * spec; + } - vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { - float ambient = 0.1; - float diff = max(dot(normal, lightDir), 0.0); - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - return ambient + diff + 0.5 * spec; // Assuming metallic is set to 0.5 + void main() { + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - position); + vec3 viewDir = normalize(viewPos - position); + vec3 lighting = calculateLighting(norm, lightDir, viewDir); + color.rgb = vec3(1.0, 0.8, 0.6) * lighting; + color.a = 1.0; // Alpha channel + } } - void main() { - vec3 norm = normalize(normal); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); // Example light position - vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - position); // Example view position - vec3 lighting = calculateLighting(norm, lightDir, viewDir); - vec3 color = vec3(1.0, 0.8, 0.6) * lighting; + fragment { + input vec4 color; + output vec4 fragColor; - fragColor = vec4(color, 1.0); + void main() { + fragColor = color; + } } } -} \ No newline at end of file diff --git a/samples/Motion Blur.cgl b/samples/Motion Blur.cgl index 4f0c143..61e32f2 100644 --- a/samples/Motion Blur.cgl +++ b/samples/Motion Blur.cgl @@ -1,24 +1,36 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec4 color; - void main() { - fragCoord = texCoord; + // Pass through the texture coordinate + void main() { + color = vec4(texCoord, 0.0, 1.0); + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the texture coordinate + // Simulated inputs + input vec2 motionDirection; + input float blurAmount; + input float texture; // Simulated texture + output vec4 fragColor; + + vec4 sampleTexture(vec2 coord) { + // Simulate texture sampling (for demonstration purposes) + return vec4(texture, coord); + } void main() { - - for (int i = -4; i < 4; i++) { - color += texture(texture, fragCoord * motionDirection * blurAmount / 10.0); + vec2 texCoord = color.xy; // Extract the texture coordinate + vec4 colorAccum = vec4(0.0); + for (int i = -4; i <= 4; i=i+1) { + vec2 offset = float(i) * motionDirection * blurAmount / 10.0; + colorAccum += sampleTexture(texCoord + offset); + } + colorAccum /= 9.0; + fragColor = colorAccum; } - color /= 9.0; - fragColor = color; } } -} \ No newline at end of file diff --git a/samples/Nested Loop Shader.cgl b/samples/Nested Loop Shader.cgl index 00cc5c9..ca3e7cf 100644 --- a/samples/Nested Loop Shader.cgl +++ b/samples/Nested Loop Shader.cgl @@ -3,29 +3,62 @@ shader nestedLoopsShader { input vec3 position; input vec2 texCoord; input vec3 normal; - output vec3 fragPosition; - output vec2 fragTexCoord; - output vec3 fragNormal; + output vec4 color; + // Pass the texture coordinates and normal to the fragment shader void main() { - fragPosition = position; - fragTexCoord = texCoord; - fragNormal = normal; + color.xy = texCoord; + color.zw = normal; + color.a = 1.0; // Alpha channel } } fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - input vec3 fragNormal; + input vec4 color; // Contains the texture coordinates and normal + input vec2 texCoord; // Texture coordinates from vertex shader + + + // Simulated inputs + input vec2 texture; // Simulated texture + input vec3 lightPos; + input vec3 viewPos; + input vec3 ambientColor; + input float shininess; + input float specularIntensity; + input int outerIterations; + input int innerIterations; output vec4 fragColor; + vec4 sampleTexture(vec2 coord) { + // Simulate texture sampling (for demonstration purposes) + return vec4(texture, coord); + } + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; + } + void main() { - - for (int i = 0; i < 0; i++) { - for (int j = 0; j < 0; j++) { - vec3 reflectDir = reflect(-lightDir, fragNormal); - } + vec4 texColor = sampleTexture(texCoord); + vec3 lightDir = normalize(lightPos - color.xyz); + vec3 viewDir = normalize(viewPos - color.xyz); + + vec3 ambient = ambientColor; + vec3 diffuse = texColor.rgb; + vec3 specular = vec3(0.0); + + // Outer loop + for (int i = 0; i < outerIterations; i=i+1) { + // Inner loop + for (int j = 0; j < innerIterations; j=j+1) { + // Compute lighting + vec3 tempSpecular = calculateSpecular(color.zw, lightDir, viewDir); + specular += tempSpecular; + } + + // Reduce diffuse intensity progressively diffuse *= 0.9; } @@ -33,4 +66,4 @@ shader nestedLoopsShader { fragColor = vec4(lighting, 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Normal Mapping.cgl b/samples/Normal Mapping.cgl index 553d259..840ecbd 100644 --- a/samples/Normal Mapping.cgl +++ b/samples/Normal Mapping.cgl @@ -1,34 +1,44 @@ -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; +shader main { + vertex { + input vec3 position; + input vec3 normal; + output vec4 color; // To pass the normal and position - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; + + void main() { + // Define constants for light and view positions + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 5.0); // Example view position + float shininess = 32.0; + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - position); // Direction of light + vec3 viewDir = normalize(viewPos - position); // Direction of view + + // Calculate lighting + float ambient = 0.1; + float diff = max(dot(norm, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, norm); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + + // Combine lighting components + color.rgb = ambient + diff + spec; + color.a = 1.0; // Alpha channel + } } -} -fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the lighting information + input vec2 texCoord; + output vec4 fragColor; + + - - void main() { - vec3 norm = normalize(texture(normalMap, fragTexCoord) * 2.0 - 1.0); - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 reflectDir = reflect(-lightDir, norm); - vec3 viewDir = normalize(viewPos - fragPosition); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - vec3 color = texture(texture, fragTexCoord); - fragColor = vec4((0.1 + diff + spec) * color, 1.0); + void main() { + // Sample texture (assuming `texture` is a built-in function in this environment) + vec4 texColor = texture(texture, texCoord); // Sample texture using coordinates + vec3 ambient = vec3(0.1, 0.1, 0.1); // Hardcoded ambient color + vec3 finalColor = ambient + color.rgb * texColor.rgb; // Combine ambient and texture color + fragColor = vec4(finalColor, 1.0); // Final output color + } } } -} \ No newline at end of file From 8e38194e5c871f937fb965d33501fc6d166a1e7b Mon Sep 17 00:00:00 2001 From: samthakur587 Date: Thu, 8 Aug 2024 12:38:06 +0530 Subject: [PATCH 2/3] fix: updated samples --- samples/Adv complexShader.cgl | 71 +++++-------- samples/Advanced Post-Processing Effects.cgl | 24 ++--- samples/Advanced Shader.cgl | 59 +++++------ samples/Advanced calculateLighting.cgl | 72 +++++++------ samples/Anaglyph 3D.cgl | 35 +++---- samples/Atmospheric Scattering.cgl | 21 ++-- samples/Basic Fog Shader.cgl | 16 +-- samples/Basic Reflection Shader.cgl | 23 +---- samples/Basic Texture Shade.cgl | 12 +-- samples/Blinn-Phong Shading.cgl | 72 ++++++------- samples/Bloom Effect.cgl | 49 +++------ samples/Bump Mapping.cgl | 62 ++++------- samples/Calculate Lighting.cgl | 75 +++++++++----- samples/Cartoon Outline.cgl | 57 ++++------- samples/Cel Shading.cgl | 41 +++----- samples/Chromakey Shader.cgl | 45 ++++---- samples/Chromatic Aberration.cgl | 44 ++++---- samples/Color Inversion.cgl | 32 +++--- samples/Complex Shader.cgl | 102 ++++--------------- samples/Depth Fog.cgl | 49 ++++----- samples/Depth of Field.cgl | 65 ++++-------- samples/Displacement Mapping.cgl | 53 +++------- samples/Dynamic Day-Night Cycle.cgl | 21 ++-- samples/Dynamic Water Shader.cgl | 26 ++--- samples/Edge Detection.cgl | 56 ++++------ samples/Emboss Effect.cgl | 72 ++++++------- samples/Flat Shading.cgl | 31 +++--- samples/Fluid Dynamics Simulation.cgl | 23 ++--- samples/Fog Effect.cgl | 37 +++---- samples/Fresnel Effect.cgl | 49 ++++----- samples/Glitch Effect.cgl | 56 +++------- samples/Gouraud Shading.cgl | 46 ++++----- samples/Gradient Background.cgl | 35 +++---- samples/Halftone Effect.cgl | 70 ++++--------- samples/Hatching shader.cgl | 82 +++++---------- samples/Heat Haze.cgl | 55 +++------- samples/Kaleidoscope Effect.cgl | 69 ++++--------- samples/Lambertian Shading.cgl | 49 ++++----- samples/Light Shaft Effect.cgl | 70 ++++--------- samples/Metallic Shader.cgl | 55 +++++----- samples/Motion Blur.cgl | 42 +++----- samples/Nested Loop Shader.cgl | 63 +++--------- samples/Normal Mapping.cgl | 66 +++++------- 43 files changed, 794 insertions(+), 1358 deletions(-) diff --git a/samples/Adv complexShader.cgl b/samples/Adv complexShader.cgl index b3f03a9..dafe8bb 100644 --- a/samples/Adv complexShader.cgl +++ b/samples/Adv complexShader.cgl @@ -2,63 +2,46 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - input vec3 normal; - output vec4 color; - - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); - } + output vec2 fragTexCoord; + output vec3 fragPosition; void main() { - // Pass texture coordinates and normals to the fragment shader - color.xy = texCoord; - color.zw = normal; - - // Simulate normal map calculation in the vertex shader - vec3 mapNormal = normalize(vec3(texCoord, 1.0) * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(normal, mapNormal); - - // Pass perturbed normal to the fragment shader - color.rgb = vec3(perturbedNormal); - color.a = 1.0; // Alpha channel + fragTexCoord = texCoord; + fragPosition = position; // Directly pass position to fragment shader + gl_Position = vec4(position, 1.0); } } fragment { - input vec4 color; - input vec2 texCoord; + input vec2 fragTexCoord; + input vec3 fragPosition; output vec4 fragColor; - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) { - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); - return spec * vec3(1.0, 1.0, 1.0); - } - - void main() { - vec3 perturbedNormal = normalize(color.rgb); - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position - vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position - vec3 lightDir = normalize(lightPos - vec3(color)); - vec3 viewDir = normalize(viewPos - vec3(color)); + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Default light color - // Ambient component - vec3 ambient = vec3(0.1, 0.1, 0.1); // Example ambient color + float diff = max(dot(normal, lightDir), 0.0); + vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Default light color - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * vec3(0.8, 0.8, 0.8); // Example diffuse color + float specularStrength = 0.5; + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Default light color - // Specular component - float shininess = 32.0; // Example shininess - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir, shininess); + return ambient + diffuse + specular; + } - // Combine lighting components - vec3 lighting = ambient + diffuse + specular; + void main() { + vec4 texColor = texture2D(textureSampler, fragTexCoord); // Assumed default sampler + vec3 normal = normalize(cross(dFdx(fragPosition), dFdy(fragPosition))); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Default light direction + vec3 viewDir = normalize(-fragPosition); // Simple view direction - vec4 finalColor = vec4(lighting, 1.0); + vec3 lighting = calculateLighting(normal, lightDir, viewDir); + vec4 color = vec4(lighting, 1.0) * texColor; - fragColor = finalColor; + fragColor = color; } } -} +} \ No newline at end of file diff --git a/samples/Advanced Post-Processing Effects.cgl b/samples/Advanced Post-Processing Effects.cgl index ffc5528..7ad3a1d 100644 --- a/samples/Advanced Post-Processing Effects.cgl +++ b/samples/Advanced Post-Processing Effects.cgl @@ -1,29 +1,17 @@ shader main { vertex { - input vec2 texCoord; - output vec4 color; - - void main() { - // Pass through texture coordinates as color - color = vec4(texCoord, 0.0, 1.0); - } + // Vertex shader logic goes here. } fragment { - input vec4 color; + input vec2 texCoord; output vec4 fragColor; - vec4 textureSample(vec2 texCoord) { - // Example function to simulate texture sampling - return vec4(texCoord.x, texCoord.y, 0.5, 1.0); - } - void main() { - float brightness = textureSample(color.xy); + float brightness = 0.0; // Placeholder for brightness calculation float bloom = max(0.0, brightness - 0.5); - vec3 texColor = textureSample(color.xy); - vec3 colorWithBloom = texColor + vec3(bloom); - fragColor = vec4(colorWithBloom, 1.0); + vec3 color = vec3(0.0); // Placeholder for texture color + fragColor = vec4(color + vec3(bloom), 1.0); } } -} +} \ No newline at end of file diff --git a/samples/Advanced Shader.cgl b/samples/Advanced Shader.cgl index f911fbd..ab68ef3 100644 --- a/samples/Advanced Shader.cgl +++ b/samples/Advanced Shader.cgl @@ -2,57 +2,48 @@ shader advancedShader { vertex { input vec3 position; input vec2 texCoord; - output vec4 color; + output vec2 fragTexCoord; + output vec3 fragPosition; - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) { + void main() { + fragTexCoord = texCoord; + fragPosition = position; + gl_Position = vec4(position, 1.0); + } + } + + fragment { + input vec2 fragTexCoord; + input vec3 fragPosition; + output vec4 fragColor; + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess return spec * vec3(1.0, 1.0, 1.0); // White specular highlight } void main() { - // Pass texture coordinates to the fragment shader - color.xy = texCoord; - - // Simulate ambient, light position, view position, and shininess - vec3 ambientColor = vec3(0.1, 0.1, 0.1); - vec3 lightPos = vec3(10.0, 10.0, 10.0); - vec3 viewPos = vec3(0.0, 0.0, 10.0); - float shininess = 32.0; + vec4 texColor = texture2D(textureSampler, fragTexCoord); // Default sampler - // Calculate normal in the vertex shader - vec3 norm = normalize(cross(dFdx(position), dFdy(position))); - vec3 lightDir = normalize(lightPos - position); - vec3 viewDir = normalize(viewPos - position); + vec3 norm = normalize(cross(dFdx(fragPosition), dFdy(fragPosition))); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position + vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position // Ambient component - vec3 ambient = ambientColor; + vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color // Diffuse component float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // White diffuse light // Specular component - vec3 specular = calculateSpecular(norm, lightDir, viewDir, shininess); + vec3 specular = calculateSpecular(norm, lightDir, viewDir); vec3 lighting = ambient + diffuse + specular; - color.rgb = lighting; - color.a = 1.0; // Alpha channel - } - } - - fragment { - input vec4 color; - output vec4 fragColor; - - vec4 textureSample(vec2 texCoord) { - // Example function to simulate texture sampling - return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Simulated texture color - } + vec4 color = vec4(lighting, 1.0) * texColor; - void main() { - vec4 texColor = textureSample(color.xy); - fragColor = vec4(color.rgb * texColor.rgb, 1.0); + fragColor = color; } } -} +} \ No newline at end of file diff --git a/samples/Advanced calculateLighting.cgl b/samples/Advanced calculateLighting.cgl index a469a29..ad1a9f4 100644 --- a/samples/Advanced calculateLighting.cgl +++ b/samples/Advanced calculateLighting.cgl @@ -2,50 +2,60 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - output vec4 color; + input vec3 normal; + output vec2 fragTexCoord; + output vec3 fragPosition; + output vec3 fragNormal; - vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { - float ambientStrength = 0.1; - vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Example ambient color + void main() { + fragTexCoord = texCoord; + fragPosition = position; + fragNormal = normal; + gl_Position = vec4(position, 1.0); + } + } - float diff = max(dot(normal, lightDir), 0.0); - vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Example diffuse color + fragment { + input vec2 fragTexCoord; + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; - float specularStrength = 0.5; + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Example specular color + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess + return spec * vec3(1.0, 1.0, 1.0) * 1.0; // Default specular intensity + } - return ambient + diffuse + specular; + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); } void main() { - // Pass texture coordinates to the fragment shader - color.xy = texCoord; + vec4 diffuseColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler + vec4 specularColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler + vec4 normalMapColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - // Calculate normal in the vertex shader - vec3 normal = normalize(cross(dFdx(position), dFdy(position))); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Example light direction - vec3 viewDir = normalize(-position); // Example view direction + vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); - vec3 lighting = calculateLighting(normal, lightDir, viewDir); - color.rgb = lighting; - color.a = 1.0; // Alpha channel - } - } + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position + vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position - fragment { - input vec4 color; - output vec4 fragColor; + // Ambient component + vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color - vec4 textureSample(vec2 texCoord) { - // Example function to simulate texture sampling - return vec4(texCoord.x, texCoord.y, 1.0 - texCoord.x, 1.0); - } + // Diffuse component + float diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * diffuseColor.rgb; - void main() { - vec4 texColor = textureSample(color.xy); // Simulated texture sampling - fragColor = vec4(color.rgb * texColor.rgb, 1.0); + // Specular component + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + + vec3 lighting = ambient + diffuse + specular; + vec4 color = vec4(lighting, 1.0) * diffuseColor; + + fragColor = color; } } } diff --git a/samples/Anaglyph 3D.cgl b/samples/Anaglyph 3D.cgl index 8bc6c96..99b196f 100644 --- a/samples/Anaglyph 3D.cgl +++ b/samples/Anaglyph 3D.cgl @@ -1,28 +1,21 @@ shader main { - vertex { - input vec2 texCoord; - output vec4 color; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - // Pass through texture coordinates as color - color = vec4(texCoord, 0.0, 1.0); - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec4 color; - output vec4 fragColor; - - // Define a simulated texture sampling function - vec4 textureSample(vec2 texCoord) { - // Simulated texture sampling: using simple color mapping - return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Sample color with brightness in the red channel - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - vec4 leftColor = textureSample(color.xy - vec2(0.1)); // Simulate left texture sample - vec4 rightColor = textureSample(color.xy + vec2(0.1)); // Simulate right texture sample - fragColor = vec4(leftColor.r, rightColor.g, rightColor.b, 1.0); - } + void main() { + vec4 leftColor = texture(texture, fragTexCoord - eyeOffset); + vec4 rightColor = texture(texture, fragTexCoord + eyeOffset); + fragColor = vec4(leftColor, rightColor, rightColor, 1.0); } } +} \ No newline at end of file diff --git a/samples/Atmospheric Scattering.cgl b/samples/Atmospheric Scattering.cgl index 79fefcc..da6a9ad 100644 --- a/samples/Atmospheric Scattering.cgl +++ b/samples/Atmospheric Scattering.cgl @@ -1,7 +1,11 @@ shader main { vertex { + // Vertex shader logic goes here. + } + + fragment { input vec3 position; - output vec4 color; + output vec4 fragColor; vec3 rayleighScattering(float theta) { return vec3(0.5, 0.7, 1.0) * pow(max(0.0, 1.0 - theta * theta), 3.0); @@ -9,17 +13,8 @@ shader main { void main() { float theta = position.y; - vec3 colorComputed = rayleighScattering(theta); - color = vec4(colorComputed, 1.0); - } - } - - fragment { - input vec4 color; - output vec4 fragColor; - - void main() { - fragColor = color; + vec3 color = rayleighScattering(theta); + fragColor = vec4(color, 1.0); } } -} +} \ No newline at end of file diff --git a/samples/Basic Fog Shader.cgl b/samples/Basic Fog Shader.cgl index 3e2a33d..cecec25 100644 --- a/samples/Basic Fog Shader.cgl +++ b/samples/Basic Fog Shader.cgl @@ -17,23 +17,11 @@ shader main { input vec3 fragPosition; output vec4 fragColor; - // Simulate texture sampling function - vec4 textureSample(vec2 texCoord) { - // Simulated texture color based on texCoord - return vec4(texCoord, 0.5, 1.0); // Example color with depth information in the red channel - } - - void main() { - - vec3 baseColor = textureSample(fragTexCoord); - - // Calculate depth based on position (simple distance from origin) + float depth = length(fragPosition); float fogFactor = exp(-fogDensity * depth); - - // Compute final color with fog effect - vec3 finalColor = mix(fogColor, baseColor.rgb, fogFactor); + vec3 finalColor = mix(fogColor, baseColor, fogFactor); fragColor = vec4(finalColor, 1.0); } } diff --git a/samples/Basic Reflection Shader.cgl b/samples/Basic Reflection Shader.cgl index ec75a33..494ed7e 100644 --- a/samples/Basic Reflection Shader.cgl +++ b/samples/Basic Reflection Shader.cgl @@ -17,31 +17,12 @@ shader main { input vec3 fragNormal; output vec4 fragColor; - // Simulate texture sampling function - vec4 textureSample(vec2 texCoord) { - // Example simulated texture color based on texCoord - return vec4(texCoord, 0.5, 1.0); // Simulated texture color - } - - // Simulate environment map sampling function - vec4 cubeTextureSample(vec3 direction) { - // Example simulated cube map color based on direction - return vec4(abs(direction), 1.0); // Simulated environment map color - } - - - void main() { - // Simulate view position - vec3 viewPos = vec3(0.0, 0.0, 5.0); // Example view position vec3 norm = normalize(fragNormal); vec3 viewDir = normalize(viewPos - fragPosition); vec3 reflectDir = reflect(-viewDir, norm); - vec3 environmentColor = cubeTextureSample(reflectDir); - vec3 objectColor = textureSample(fragPosition.xy); - - fragColor = vec4(mix(objectColor.rgb, environmentColor.rgb, 0.5), 1.0); + fragColor = vec4(mix(objectColor, environmentColor, 0.5), 1.0); } } -} +} \ No newline at end of file diff --git a/samples/Basic Texture Shade.cgl b/samples/Basic Texture Shade.cgl index f0b4a5b..b6ec320 100644 --- a/samples/Basic Texture Shade.cgl +++ b/samples/Basic Texture Shade.cgl @@ -5,10 +5,7 @@ shader main { output vec2 fragTexCoord; void main() { - // Pass texture coordinates to the fragment shader fragTexCoord = texCoord; - - // Set the position of the vertex gl_Position = vec4(position, 1.0); } } @@ -17,15 +14,8 @@ shader main { input vec2 fragTexCoord; output vec4 fragColor; - // Simulate texture sampling function - vec4 textureSample(vec2 texCoord) { - // Example simulated texture color based on texCoord - return vec4(texCoord, 0.5, 1.0); // Simulated texture color - } - void main() { - // Sample the texture using the simulated texture sampling function - fragColor = textureSample(fragTexCoord); + fragColor = textureColor; } } } diff --git a/samples/Blinn-Phong Shading.cgl b/samples/Blinn-Phong Shading.cgl index ca1bb34..c6e8efd 100644 --- a/samples/Blinn-Phong Shading.cgl +++ b/samples/Blinn-Phong Shading.cgl @@ -1,41 +1,41 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; - - // Inputs for vertex shader - void main() { - // Pass position and normal to fragment shader - fragPosition = position; - fragNormal = normal; - - // Set the position of the vertex - gl_Position = vec4(position, 1.0); - } +vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; + + void main() { + fragPosition = position; + fragNormal = normal; } +} + +fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; + + void main() { + vec3 norm = normalize(fragNormal); + + // Hardcoded light and view positions + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // View position + + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + + // Calculate the halfway vector + vec3 viewDir = normalize(viewPos - fragPosition); + vec3 halfwayDir = normalize(lightDir + viewDir); + + // Calculate specular lighting + float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); - fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - - - void main() { - // Calculate lighting - // Define constant light and view positions for this shader - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position - vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position - vec3 norm = normalize(fragNormal); - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 halfwayDir = normalize(lightDir + viewPos - fragPosition); - float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); - vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; - - // Output the final color - fragColor = vec4(color, 1.0); - } + // Combine diffuse and specular components + vec3 color = vec3(0.5, 0.5, 0.5) * diff + vec3(1.0, 1.0, 1.0) * spec; + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Bloom Effect.cgl b/samples/Bloom Effect.cgl index f47262d..acc85e3 100644 --- a/samples/Bloom Effect.cgl +++ b/samples/Bloom Effect.cgl @@ -1,41 +1,22 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; +vertex { + input vec2 texCoord; + output vec2 fragCoord; - // Inputs for vertex shader - void main() { - // Pass position and normal to fragment shader - fragPosition = position; - fragNormal = normal; - - // Set the position of the vertex - gl_Position = vec4(position, 1.0); - } + // Pass through the texture coordinates to the fragment shader + void main() { + fragCoord = texCoord; } +} - fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - - - void main() { - // Calculate lighting - // Define constant light and view positions for this shader - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position - vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position - vec3 norm = normalize(fragNormal); - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 wayDir = normalize(lightDir + viewPos - fragPosition); - float spec = pow(max(dot(norm, wayDir), 0.0), 32.0); - vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; +fragment { + input vec2 fragCoord; + output vec4 fragColor; - // Output the final color - fragColor = vec4(color, 1.0); - } + void main() { + vec4 color = texture(texture, fragCoord); + vec4 bloom = max(color - threshold, 0.0); + fragColor = color + bloom; } } +} \ No newline at end of file diff --git a/samples/Bump Mapping.cgl b/samples/Bump Mapping.cgl index 8af45a1..d5bd4cf 100644 --- a/samples/Bump Mapping.cgl +++ b/samples/Bump Mapping.cgl @@ -1,48 +1,28 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec3 fragNormal; - output vec2 fragTexCoord; +vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec3 fragPosition; + output vec2 fragTexCoord; - void main() { - fragPosition = position; - fragNormal = normal; - fragTexCoord = texCoord; - - // Set the position of the vertex - gl_Position = vec4(position, 1.0); - } + void main() { + fragPosition = position; + fragTexCoord = texCoord; } +} - fragment { - input vec3 fragPosition; - input vec3 fragNormal; - input vec2 fragTexCoord; - output vec4 fragColor; +fragment { + input vec3 fragPosition; + input vec2 fragTexCoord; + output vec4 fragColor; - - void main() { - // Hardcoded texture and normal map values (you may need to adapt this to your setup) - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position - vec3 textureColor = vec3(1.0, 1.0, 1.0); // Example texture color - vec3 normalMapColor = vec3(0.5, 0.5, 1.0); // Example normal map color - // Fetch and normalize the normal from the normal map - vec3 norm = normalize(normalMapColor * 2.0 - 1.0); - - // Calculate the direction from the fragment to the light - vec3 lightDir = normalize(lightPos - fragPosition); - - // Compute the diffuse lighting term - float diff = max(dot(norm, lightDir), 0.0); - - // Fetch the color from the texture and apply the diffuse lighting - vec3 color = textureColor * diff; - - // Output the final color with full opacity - fragColor = vec4(color, 1.0); - } + void main() { + vec3 norm = normalize(texture(normalMap, fragTexCoord) * 2.0 - 1.0); + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = texture(texture, fragTexCoord) * diff; + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Calculate Lighting.cgl b/samples/Calculate Lighting.cgl index a34dd79..27aa059 100644 --- a/samples/Calculate Lighting.cgl +++ b/samples/Calculate Lighting.cgl @@ -2,41 +2,64 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - output vec4 color; - - - float calculateLighting(vec3 normal, vec3 lightDir) { - float diff = max(dot(normal, lightDir), 0.0); - return diff; - } - - vec4 textureColor(vec2 coord) { - // Hardcoded texture sampling (replace with actual sampling if needed) - return vec4(coord, 0.5, 1.0); // Simulated texture color - } + input vec3 normal; + output vec2 fragTexCoord; + output vec3 fragPosition; + output vec3 fragNormal; void main() { - // Hardcoded values for lighting and texture operations - vec3 hardcodedNormal = vec3(0.0, 0.0, 1.0); - vec3 hardcodedLightDir = vec3(1.0, 1.0, 1.0); - vec3 normal = hardcodedNormal; - vec3 lightDir = hardcodedLightDir; - float lightIntensity = calculateLighting(normal, lightDir); - - vec4 texColor = textureColor(texCoord); - color = texColor * lightIntensity; // Apply lighting to color - - // Pass texture coordinates to fragment shader - color.xy = texCoord; + fragTexCoord = texCoord; + fragPosition = position; + fragNormal = normal; + gl_Position = vec4(position, 1.0); } } fragment { - input vec4 color; + input vec2 fragTexCoord; + input vec3 fragPosition; + input vec3 fragNormal; output vec4 fragColor; + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), defaultShininess); + return spec * vec3(1.0, 1.0, 1.0) * defaultSpecularIntensity; + } + + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); + } + void main() { + + vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); + + vec3 lightDir = normalize(defaultLightPos - fragPosition); + vec3 viewDir = normalize(defaultViewPos - fragPosition); + + // Ambient component + vec3 ambient = defaultAmbientColor; + + // Diffuse component + float diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * diffuseColor.rgb; + + // Specular component + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + + // Loop to modify lighting based on iterations + vec3 lighting = ambient + diffuse + specular; + for (int i = 0; i < iterations; i++) { + lighting *= 0.9; + } + + // Ternary operator to conditionally modify color + vec4 color = (diff > 0.5) ? vec4(lighting, 1.0) * diffuseColor : vec4(lighting * 0.5, 1.0) * diffuseColor; + fragColor = color; } } -} +} \ No newline at end of file diff --git a/samples/Cartoon Outline.cgl b/samples/Cartoon Outline.cgl index 627bb9a..e2a6eaf 100644 --- a/samples/Cartoon Outline.cgl +++ b/samples/Cartoon Outline.cgl @@ -1,44 +1,31 @@ shader main { - vertex { - input vec2 texCoord; - output vec4 fragColor; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - - - void main() { - // Pass texture coordinates to fragment shader - float outlineWidth = 0.01; // Example value for outline width - fragColor.xy = texCoord; - fragColor.zw = vec2(outlineWidth, 0.0); // Store outline width in the z component for later use - gl_Position = vec4(texCoord, 0.0, 1.0); // Example position - } + // Pass through texCoord to the fragment shader + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 fragTexCoord; - input vec2 fragOutlineWidth; - output vec4 fragColor; - - // Simulated texture sampling - vec4 textureColor(vec2 coord) { - // Simulate texture sampling (replace with actual sampling if needed) - return vec4(coord, 0.5, 1.0); // Simulated texture color - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - float outlineWidth = fragOutlineWidth.x; // Extract outline width - vec4 color = textureColor(fragTexCoord); - vec4 north = textureColor(fragTexCoord + vec2(0.0, outlineWidth)); - vec4 south = textureColor(fragTexCoord - vec2(0.0, outlineWidth)); - vec4 east = textureColor(fragTexCoord + vec2(outlineWidth, 0.0)); - vec4 west = textureColor(fragTexCoord - vec2(outlineWidth, 0.0)); + void main() { + vec4 color = texture(texture, fragTexCoord); + vec4 north = texture(texture, fragTexCoord + vec2(0.0, outlineWidth)); + vec4 south = texture(texture, fragTexCoord - vec2(0.0, outlineWidth)); + vec4 east = texture(texture, fragTexCoord + vec2(outlineWidth, 0.0)); + vec4 west = texture(texture, fragTexCoord - vec2(outlineWidth, 0.0)); - float edge = length((north + south + east + west) / 4.0 - color); - if (edge > 0.1) { - fragColor = vec4(0.0, 0.0, 0.0, 1.0); - } else { - fragColor = color; - } + float edge = length((north + south + east + west) / 4.0 - color); + if (edge > 0.1) { + fragColor = vec4(0.0, 0.0, 0.0, 1.0); + } else { + fragColor = color; } } } +} \ No newline at end of file diff --git a/samples/Cel Shading.cgl b/samples/Cel Shading.cgl index c7a3783..6de128e 100644 --- a/samples/Cel Shading.cgl +++ b/samples/Cel Shading.cgl @@ -1,32 +1,23 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - output vec4 color; +vertex { + input vec3 position; + input vec3 normal; + output vec4 fragColor; - vec3 celShading(vec3 normal, vec3 lightDir) { - float inensity = dot(normal, lightDir); - if (inensity > 0.95) {return vec3(1.0, 1.0, 1.0);} - if (inensity > 0.5) {return vec3(0.8, 0.8, 0.8);} - if (inensity > 0.25) {return vec3(0.6, 0.6, 0.6);} - else {return vec3(0.4, 0.4, 0.4);} - } - - void main() { - vec3 norm = normalize(normal); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); - vec3 shadingColor = celShading(norm, lightDir); - - color = vec4(shadingColor, 1.0); // Alpha channel - } - } + // Vertex shader code, if needed + // For this example, there's no actual vertex shader code +} - fragment { - input vec4 color; - output vec4 fragColor; +fragment { + input vec3 position; + input vec3 normal; + output vec4 fragColor; void main() { - fragColor = color; - } + vec3 norm = normalize(normal); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); // Example light position + + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Chromakey Shader.cgl b/samples/Chromakey Shader.cgl index acbddad..aa49ada 100644 --- a/samples/Chromakey Shader.cgl +++ b/samples/Chromakey Shader.cgl @@ -1,35 +1,28 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 fragTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - fragTexCoord = texCoord; - gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // This should be set properly for your use case - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - vec4 textureColor(vec2 coord) { - // Example function to simulate texture sampling - return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - vec3 keyColor = vec3(0.0, 1.0, 0.0); // Example key color - float threshold = 0.1; // Example threshold + void main() { + vec4 color = texture(texture, fragTexCoord); + vec3 keyColor = vec3(1.0, 0.0, 0.0); // Example key color + float threshold = 0.1; // Example threshold - vec4 color = textureColor(fragTexCoord); - float diff = length(color.rgb - keyColor); - if (diff < threshold) { - // Discard fragment - fragColor = vec4(0.0, 0.0, 0.0, 0.0); - } else { - fragColor = color; - } + float diff = length(color - keyColor); + if (diff < threshold) { + discard; + } else { + fragColor = color; } } } +} \ No newline at end of file diff --git a/samples/Chromatic Aberration.cgl b/samples/Chromatic Aberration.cgl index e4b2f67..4ceb3b8 100644 --- a/samples/Chromatic Aberration.cgl +++ b/samples/Chromatic Aberration.cgl @@ -1,34 +1,24 @@ shader main { - vertex { - input vec2 texCoord; - output vec4 color; +vertex { + input vec2 texCoord; + output vec2 fragCoord; - // Pass through the texture coordinate - void main() { - color = vec4(texCoord, 0.0, 1.0); - } + // Pass through the texture coordinates to the fragment shader + void main() { + fragCoord = texCoord; } +} - fragment { - input vec4 color; // This contains the texture coordinate - output vec4 fragColor; - - vec4 textureColor(vec2 coord) { - // Example function to simulate texture sampling - return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color - } - - void main() { - vec2 texCoord = color.xy; // Extract the texture coordinate - - float aberration = 0.005; // Example aberration value - vec2 offset = vec2(aberration / 512.0); - - vec4 red = textureColor(texCoord + offset); - vec4 green = textureColor(texCoord); - vec4 blue = textureColor(texCoord - offset); +fragment { + input vec2 fragCoord; + output vec4 fragColor; - fragColor = vec4(red.r, green.g, blue.b, 1.0); - } + void main() { + vec2 offset = vec2(aberration / 512.0); + vec4 red = texture(texture, fragCoord + offset); + vec4 green = texture(texture, fragCoord); + vec4 blue = texture(texture, fragCoord - offset); + fragColor = vec4(red.r, green.g, blue.b, 1.0); } } +} diff --git a/samples/Color Inversion.cgl b/samples/Color Inversion.cgl index 0ff4d0b..9cd7eb0 100644 --- a/samples/Color Inversion.cgl +++ b/samples/Color Inversion.cgl @@ -1,25 +1,21 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - passTexCoord = texCoord; - } + // Pass through texCoord to the fragment shader + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - output vec4 fragColor; - - vec4 textureColor(vec2 coord) { - // Example function to simulate texture sampling - return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - vec4 color = textureColor(passTexCoord); - fragColor = vec4(vec3(1.0) - color.rgb, 1.0); - } + void main() { + vec4 color = texture(texture, fragTexCoord); + fragColor = vec4(vec3(1.0) - color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Complex Shader.cgl b/samples/Complex Shader.cgl index b001e9a..d8b82c0 100644 --- a/samples/Complex Shader.cgl +++ b/samples/Complex Shader.cgl @@ -1,99 +1,31 @@ shader complexShader { vertex { + // Vertex shader logic goes here. + } + + fragment { input vec3 position; input vec2 texCoord; - input vec3 normal; - input vec3 diffuseTexture; - input vec3 specularTexture; - input vec3 normalMap; - input vec3 lightPos; - input vec3 viewPos; - input vec3 ambientColor; - input float shininess; - input float specularIntensity; - input float shininess; - output vec4 color; - output vec2 passTexCoord; - - - - - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); - return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; - } - - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); - } - - void main() { - passTexCoord = texCoord; - if (normalMap >= vec3(0.0, 0.0, 0.0)) { - test += 1.0; - test -= 1.0; - test *= 1.0; - test /= 1.0; - float test = 1.0; - float test = 1.0; - // No normal map - color.rgb = normal; - color.a = 1.0; // Alpha channel - } - // Calculate normal map in the vertex shader - vec4 normalMapColor = texture(normalMap, texCoord); - vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(normal, mapNormal); + output vec4 fragColor; - // Pass perturbed normal to the fragment shader - color.rgb = vec3(perturbedNormal); - color.a = 1.0; // Alpha channel + float calculateLighting(vec3 normal, vec3 lightDir) { + float diff = max(dot(normal, lightDir), 0.0); + return diff; } - } - fragment { - input vec4 color; // Perturbed normal - input vec2 passTexCoord; - - - // Simulated textures and lighting parameters - input vec3 diffuseTexture; - input vec3 specularTexture; - input vec3 lightPos; - input vec3 viewPos; - input vec3 ambientColor; - input float shininess; - input float specularIntensity; - output vec4 fragColor; - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); - return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; + vec4 textureColor(vec2 coord) { + return texture2D(textureSampler, coord); } void main() { - vec4 diffuseColor = texture(diffuseTexture, passTexCoord); - vec4 specularColor = texture(specularTexture, passTexCoord); - - vec3 perturbedNormal = normalize(color.rgb); - vec3 lightDir = normalize(lightPos - position); - vec3 viewDir = normalize(viewPos - position); - - // Ambient component - vec3 ambient = ambientColor; - - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * diffuseColor.rgb; - - // Specular component - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); - - vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * diffuseColor; + vec3 normal = normalize(vec3(0.0, 0.0, 1.0)); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); + float lightIntensity = calculateLighting(normal, lightDir); + + vec4 color = textureColor(texCoord); + color *= lightIntensity; // Corrected error: multiplying RGB components fragColor = color; } } -} +} \ No newline at end of file diff --git a/samples/Depth Fog.cgl b/samples/Depth Fog.cgl index 92facb9..f4d2635 100644 --- a/samples/Depth Fog.cgl +++ b/samples/Depth Fog.cgl @@ -1,37 +1,30 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec2 passTexCoord; - output float passDepth; +vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec3 fragPosition; + output vec2 fragTexCoord; - void main() { - passTexCoord = texCoord; - passDepth = length(position); - } + void main() { + fragPosition = position; + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - input float passDepth; - // Simulated input values - input float texture; - input float fogDensity; - input vec3 fogColor; - output vec4 fragColor; +fragment { + input vec3 fragPosition; + input vec2 fragTexCoord; + output vec4 fragColor; - // Simulated texture function - vec4 get_texture(float tex, vec2 coord) { - return vec4(coord.x, coord.y, 0.5, 1.0); - } - void main() { - vec3 baseColor = get_texture(texture, passTexCoord); - float fogFactor = exp(-fogDensity * passDepth); - vec3 finalColor = mix(fogColor, baseColor.rgb, fogFactor); + void main() { + vec3 baseColor = texture(texture, fragTexCoord); + float depth = length(fragPosition); + float fogFactor = exp(-fogDensity * depth); + vec3 finalColor = mix(fogColor, baseColor, fogFactor); - fragColor = vec4(finalColor, 1.0); - } + fragColor = vec4(finalColor, 1.0); } } +} \ No newline at end of file diff --git a/samples/Depth of Field.cgl b/samples/Depth of Field.cgl index 4badaaf..eb7d641 100644 --- a/samples/Depth of Field.cgl +++ b/samples/Depth of Field.cgl @@ -1,54 +1,31 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragCoord; - void main() { - passTexCoord = texCoord; - } + // Pass through the texture coordinates to the fragment shader + void main() { + fragCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - - - // Simulated input values - input vec4 textureData; // Example for a 512x512 texture - input vec4 depthTextureData; // Example for a 512x512 depth texture - input int textureWidth; - input int textureHeight; - input float focusDepth; - input float focusRange; - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, int width, int height) { - // Convert coord to texture indices - int x = int(coord.x * float(width)); - int y = int(coord.y * float(height)); - // Clamp the indices to texture dimensions - x = clamp(x, 0, width - 1); - y = clamp(y, 0, height - 1); - // Calculate the 1D array index - int index = y * width + x; - // Return the color from the texture data - return texData; - } +fragment { + input vec2 fragCoord; + output vec4 fragColor; - void main() { - vec2 texCoord = passTexCoord; // Extract the texture coordinate - float depth = texture(depthTextureData, texCoord, textureWidth, textureHeight); - float blur = clamp(abs(depth.r - focusDepth) / focusRange, 0.0, 1.0); + void main() { + float depth = texture(depthTexture, fragCoord); + float blur = clamp(abs(depth - focusDepth) / focusRange, 0.0, 1.0); - vec4 colorAccum = vec4(0.0); - int blurRadius = 4; - for (int i = -blurRadius; i <= blurRadius; i=i+1) { - for (int j = -blurRadius; j <= blurRadius; j=j+1) { - vec2 offset = vec2(float(i), float(j)) * blur / float(textureWidth); - colorAccum += texture(textureData, texCoord + offset, textureWidth, textureHeight); - } + vec4 color = vec4(0.0); + for (int i = -4; i < 4; i++) { + for (int j = -4; j < 4; j++) { + vec2 offset = vec2(i, j) * blur / 512.0; + color += texture(texture, fragCoord + offset); } - colorAccum /= float((2 * blurRadius + 1) * (2 * blurRadius + 1)); - fragColor = colorAccum; } + color /= 81.0; + fragColor = color; } } +} \ No newline at end of file diff --git a/samples/Displacement Mapping.cgl b/samples/Displacement Mapping.cgl index 673bf1a..251afe6 100644 --- a/samples/Displacement Mapping.cgl +++ b/samples/Displacement Mapping.cgl @@ -1,45 +1,22 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - passTexCoord = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - - - // Simulated input values - input vec4 textureData; // Example for a 512x512 texture - input vec4 displacementMapData; // Example for a 512x512 displacement map - input int textureWidth; - input int textureHeight; - input float displacementFactor; - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, int width, int height) { - // Convert coord to texture indices - int x = int(coord.x * float(width)); - int y = int(coord.y * float(height)); - // Clamp the indices to texture dimensions - x = clamp(x, 0, width - 1); - y = clamp(y, 0, height - 1); - // Calculate the 1D array index - int index = y * width + x; - // Return the color from the texture data - return texData; - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - vec2 texCoord = passTexCoord; - vec2 displacement = texture(displacementMapData, texCoord, textureWidth, textureHeight); - displacement = displacement.rg * 2.0 - 1.0; - vec2 uv = texCoord + displacement * displacementFactor; - vec4 color = texture(textureData, uv, textureWidth, textureHeight); - fragColor = vec4(color.rgb, 1.0); - } + void main() { + vec2 displacement = texture(displacementMap, fragTexCoord) * 2.0 - 1.0; + vec2 uv = fragTexCoord + displacement * displacementFactor; + vec4 color = texture(texture, uv); + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Dynamic Day-Night Cycle.cgl b/samples/Dynamic Day-Night Cycle.cgl index df71e9f..37aa01f 100644 --- a/samples/Dynamic Day-Night Cycle.cgl +++ b/samples/Dynamic Day-Night Cycle.cgl @@ -1,25 +1,20 @@ shader main { vertex { + // Vertex shader logic goes here. + } + + fragment { input vec3 position; - output vec4 color; + output vec4 fragColor; vec3 dayNightColor(float timeOfDay) { return mix(vec3(0.0, 0.5, 1.0), vec3(0.0, 0.0, 0.2), timeOfDay); } void main() { - float timeOfDay = mod(iTime / 24.0, 1.0); - vec3 colorComputed = dayNightColor(timeOfDay); - color = vec4(colorComputed, 1.0); - } - } - - fragment { - input vec4 color; - output vec4 fragColor; - - void main() { - fragColor = color; + float timeOfDay = 0.0; // Placeholder for time of day calculation + vec3 color = dayNightColor(timeOfDay); + fragColor = vec4(color, 1.0); } } } diff --git a/samples/Dynamic Water Shader.cgl b/samples/Dynamic Water Shader.cgl index d57297b..b3cefc8 100644 --- a/samples/Dynamic Water Shader.cgl +++ b/samples/Dynamic Water Shader.cgl @@ -1,26 +1,22 @@ shader main { vertex { + // Vertex shader logic goes here. + } + + fragment { input vec2 position; - output vec4 color; + output vec4 fragColor; float wave(vec2 p, float time) { - return sin(p.x * 10.0 + time * 2.0) * 0.1 + sin(p.y * 10.0 + time * 2.0) * 0.1; + return sin(p.x * 10.0) * 0.1 + sin(p.y * 10.0) * 0.1; } - + void main() { vec2 uv = position; - float waterHeight = wave(uv, iTime); - vec3 colorComputed = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); - color = vec4(colorComputed, 1.0); - } - } + float waterHeight = wave(uv, 0.0); // Placeholder for time value + vec3 color = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); - fragment { - input vec4 color; - output vec4 fragColor; - - void main() { - fragColor = color; + fragColor = vec4(color, 1.0); } } -} +} \ No newline at end of file diff --git a/samples/Edge Detection.cgl b/samples/Edge Detection.cgl index 84fda7a..1700e2a 100644 --- a/samples/Edge Detection.cgl +++ b/samples/Edge Detection.cgl @@ -1,45 +1,27 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragCoord; - void main() { - passTexCoord = texCoord; - } + // Pass through the texCoord as fragCoord for simplicity + void main() { + fragCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - // Simulated input values - input vec4 textureData; // Example for a 512x512 texture - input int textureWidth; - input int textureHeight; - output vec4 fragColor; +fragment { + input vec2 fragCoord; + output vec4 fragColor; - vec4 texture(vec4 texData, vec2 coord, int width, int height) { - // Convert coord to texture indices - int x = int(coord.x * float(width)); - int y = int(coord.y * float(height)); - // Clamp the indices to texture dimensions - x = clamp(x, 0, width - 1); - y = clamp(y, 0, height - 1); - // Calculate the 1D array index - int index = y * width + x; - // Return the color from the texture data - return texData; - } + void main() { + vec2 texCoord = fragCoord; + vec2 offset = vec2(1.0 / 512.0, 1.0 / 512.0); + vec3 color = texture(texture, texCoord); + vec3 colorRight = texture(texture, texCoord + vec2(offset.x, 0.0)); + vec3 colorUp = texture(texture, texCoord + vec2(0.0, offset.y)); - void main() { - vec2 texCoord = passTexCoord; // Extract the texture coordinate - vec2 offset = vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight)); - vec3 color = texture(textureData, texCoord, textureWidth, textureHeight); - vec3 colorRight = texture(textureData, texCoord + vec2(offset.x, 0.0), textureWidth, textureHeight); - vec3 colorUp = texture(textureData, texCoord + vec2(0.0, offset.y), textureWidth, textureHeight); - color = color.rgb; - colorRight = colorRight.rgb; - colorUp = colorUp.rgb; - vec3 edge = abs(color - colorRight) + abs(color - colorUp); - fragColor = vec4(edge, 1.0); - } + vec3 edge = abs(color - colorRight) + abs(color - colorUp); + fragColor = vec4(edge, 1.0); } } +} \ No newline at end of file diff --git a/samples/Emboss Effect.cgl b/samples/Emboss Effect.cgl index 84fda7a..08f620c 100644 --- a/samples/Emboss Effect.cgl +++ b/samples/Emboss Effect.cgl @@ -1,45 +1,39 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - passTexCoord = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - // Simulated input values - input vec4 textureData; // Example for a 512x512 texture - input int textureWidth; - input int textureHeight; - output vec4 fragColor; +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - vec4 texture(vec4 texData, vec2 coord, int width, int height) { - // Convert coord to texture indices - int x = int(coord.x * float(width)); - int y = int(coord.y * float(height)); - // Clamp the indices to texture dimensions - x = clamp(x, 0, width - 1); - y = clamp(y, 0, height - 1); - // Calculate the 1D array index - int index = y * width + x; - // Return the color from the texture data - return texData; - } + void main() { + vec2 texOffset = vec2(1.0 / 512.0); - void main() { - vec2 texCoord = passTexCoord; // Extract the texture coordinate - vec2 offset = vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight)); - vec3 color = texture(textureData, texCoord, textureWidth, textureHeight); - vec3 colorRight = texture(textureData, texCoord + vec2(offset.x, 0.0), textureWidth, textureHeight); - vec3 colorUp = texture(textureData, texCoord + vec2(0.0, offset.y), textureWidth, textureHeight); - color = color.rgb; - colorRight = colorRight.rgb; - colorUp = colorUp.rgb; - vec3 edge = abs(color - colorRight) + abs(color - colorUp); - fragColor = vec4(edge, 1.0); - } - } -} + vec3 center = texture(texture, fragTexCoord); + vec3 north = texture(texture, fragTexCoord + vec2(0.0, texOffset)); + vec3 south = texture(texture, fragTexCoord - vec2(0.0, texOffset)); + vec3 east = texture(texture, fragTexCoord + vec2(texOffset, 0.0)); + vec3 west = texture(texture, fragTexCoord - vec2(texOffset, 0.0)); + vec3 northwest = texture(texture, fragTexCoord + vec2(-texOffset, texOffset)); + vec3 northeast = texture(texture, fragTexCoord + vec2(texOffset, texOffset)); + vec3 southwest = texture(texture, fragTexCoord + vec2(-texOffset, -texOffset)); + vec3 southeast = texture(texture, fragTexCoord + vec2(texOffset, -texOffset)); + + vec3 color = center * -1.0 + + north * -1.0 + + south * -1.0 + + east * -1.0 + + west * -1.0 + + northwest * 1.0 + + northeast * 1.0 + + southwest * 1.0 + + southeast * 1.0; + + fragColor = vec4(color + vec3(0.5), 1.0); + } \ No newline at end of file diff --git a/samples/Flat Shading.cgl b/samples/Flat Shading.cgl index 629079e..69508a0 100644 --- a/samples/Flat Shading.cgl +++ b/samples/Flat Shading.cgl @@ -1,21 +1,24 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - output vec3 passNormal; +vertex { + input vec3 position; + input vec3 normal; + output vec3 fragNormal; - void main() { - passNormal = normal; - } + void main() { + fragNormal = normal; } +} - fragment { - input vec3 passNormal; - output vec4 fragColor; +fragment { + input vec3 fragNormal; + output vec4 fragColor; - void main() { - vec3 color = vec3(0.5, 0.5, 0.5) + 0.5 * passNormal; - fragColor = vec4(color, 1.0); - } + void main() { + // Base color + vec3 baseColor = vec3(0.5, 0.5, 0.5); + // Adjust color based on normal + vec3 color = baseColor + 0.5 * fragNormal; + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Fluid Dynamics Simulation.cgl b/samples/Fluid Dynamics Simulation.cgl index acfb3c7..0917ce1 100644 --- a/samples/Fluid Dynamics Simulation.cgl +++ b/samples/Fluid Dynamics Simulation.cgl @@ -1,25 +1,20 @@ shader main { vertex { + // Vertex shader logic goes here. + } + + fragment { input vec3 position; - output vec4 color; + output vec4 fragColor; float fluidMotion(vec3 p) { - return sin(p.x * 10.0 + iTime) * 0.5 + 0.5; + return sin(p.x * 10.0) * 0.5 + 0.5; } void main() { float motion = fluidMotion(position); - vec3 colorComputed = vec3(0.0, 0.0, 1.0) * motion; - color = vec4(colorComputed, 1.0); - } - } - - fragment { - input vec4 color; - output vec4 fragColor; - - void main() { - fragColor = color; + vec3 color = vec3(0.0, 0.0, 1.0) * motion; + fragColor = vec4(color, 1.0); } } -} +} \ No newline at end of file diff --git a/samples/Fog Effect.cgl b/samples/Fog Effect.cgl index c413250..37f0c9e 100644 --- a/samples/Fog Effect.cgl +++ b/samples/Fog Effect.cgl @@ -1,28 +1,23 @@ shader main { - vertex { - input vec3 position; - output vec3 passPosition; +vertex { + input vec3 position; + output vec3 fragPosition; - // Pass through the position - void main() { - passPosition = position; - } + // Pass through the position to the fragment shader + void main() { + fragPosition = position; } +} - fragment { - input vec3 passPosition; // This contains the position - - input vec3 cameraPos; // Simulated uniform - input vec3 fogColor; // Simulated uniform - input float fogDensity; // Simulated uniform - output vec4 fragColor; +fragment { + input vec3 fragPosition; + output vec4 fragColor; - void main() { - vec3 position = passPosition; // Extract the position - float distance = length(position - cameraPos); - float fogFactor = exp(-pow(distance * fogDensity, 2.0)); - vec3 finalColor = mix(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); - fragColor = vec4(finalColor, 1.0); - } + void main() { + float distance = length(fragPosition - cameraPos); + float fogFactor = exp(-pow(distance * fogDensity, 2.0)); + vec3 color = mix(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Fresnel Effect.cgl b/samples/Fresnel Effect.cgl index 25c6300..a9dc6e7 100644 --- a/samples/Fresnel Effect.cgl +++ b/samples/Fresnel Effect.cgl @@ -1,39 +1,28 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - input vec3 viewDir; // Simulated uniform +vertex { + input vec3 position; + input vec3 normal; + output vec4 fragColor; - output vec3 passNormal; - output vec3 passViewDir; + // Vertex shader code, if needed + // For this example, there's no actual vertex shader code +} - - float fresnelEffect(vec3 normal, vec3 viewDir) { - return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); - } +fragment { + input vec3 normal; + output vec4 fragColor; - void main() { - passNormal = normal; - passViewDir = viewDir; - } + float fresnelEffect(vec3 normal, vec3 viewDir) { + return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); } - fragment { - input vec3 passNormal; - input vec3 passViewDir; - output vec4 fragColor; - - float fresnelEffect(vec3 normal, vec3 viewDir) { - return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); - } - - void main() { - vec3 norm = normalize(passNormal); - vec3 viewDir = normalize(passViewDir); - float fresnel = fresnelEffect(norm, viewDir); - vec3 color = vec3(0.1, 0.5, 1.0) * fresnel; + void main() { + vec3 norm = normalize(normal); + vec3 viewDir = vec3(0.0, 0.0, 1.0); // Example view direction + float fresnel = fresnelEffect(norm, normalize(viewDir)); + vec3 color = vec3(0.1, 0.5, 1.0) * fresnel; - fragColor = vec4(color, 1.0); - } + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Glitch Effect.cgl b/samples/Glitch Effect.cgl index f878c64..86fb1b5 100644 --- a/samples/Glitch Effect.cgl +++ b/samples/Glitch Effect.cgl @@ -1,49 +1,25 @@ shader main { - vertex { - input vec2 texCoord; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - // Pass through the texture coordinate - void main() { - passTexCoord = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - - - // Simulated inputs - input vec4 textureData; // Example for a 512x512 texture - input int textureWidth; - input int textureHeight; - input float time; // Simulated uniform - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, int width, int height) { - // Convert coord to texture indices - int x = int(coord.x * float(width)); - int y = int(coord.y * float(height)); - // Clamp the indices to texture dimensions - x = clamp(x, 0, width - 1); - y = clamp(y, 0, height - 1); - // Calculate the 1D array index - int index = y * width + x; - // Return the color from the texture data - return texData; - } - - void main() { - vec2 uv = passTexCoord; +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - // Apply the time-based distortion - if (fract(time * 10.0) < 0.5) { - uv.x += sin(passTexCoord.y * 10.0) * 0.1; - } + void main() { + vec2 uv = fragTexCoord; - // Sample the texture using the simulated texture function - vec4 color = texture(textureData, uv, textureWidth, textureHeight); - fragColor = color; + if (fract(time * 10.0) < 0.5) { + uv += sin(fragTexCoord * 10.0) * 0.1; } + vec4 color = texture(texture, uv); + fragColor = color; } } +} \ No newline at end of file diff --git a/samples/Gouraud Shading.cgl b/samples/Gouraud Shading.cgl index 962dd81..48c41fd 100644 --- a/samples/Gouraud Shading.cgl +++ b/samples/Gouraud Shading.cgl @@ -1,32 +1,28 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - // Simulated light position - input vec3 lightPos; - output vec3 passPosition; - output vec3 passNormal; - output vec3 passLightPos; +vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; - void main() { - passPosition = position; - passNormal = normal; - passLightPos = lightPos; - } + void main() { + fragPosition = position; + fragNormal = normal; } +} - fragment { - input vec3 passPosition; - input vec3 passNormal; - input vec3 passLightPos; - output vec4 fragColor; +fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; - void main() { - vec3 norm = normalize(passNormal); - vec3 lightDir = normalize(passLightPos - passPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = vec3(0.5, 0.5, 0.5) + diff; - fragColor = vec4(color, 1.0); - } + void main() { + vec3 norm = normalize(fragNormal); + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Hardcoded light position + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) * diff; // Diffuse color + fragColor = vec4(color, 1.0); } } +} diff --git a/samples/Gradient Background.cgl b/samples/Gradient Background.cgl index 0ab6bc7..9d06de1 100644 --- a/samples/Gradient Background.cgl +++ b/samples/Gradient Background.cgl @@ -1,27 +1,20 @@ shader main { - vertex { - input vec2 texCoord; - // Simulated topColor and bottomColor - input vec3 topColor; - input vec3 bottomColor; - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - - void main() { - passTexCoord = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - // Simulated topColor and bottomColor - input vec3 topColor; - input vec3 bottomColor; - output vec4 fragColor; - - void main() { - vec3 color = mix(bottomColor, topColor, passTexCoord.y); - fragColor = vec4(color, 1.0); - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; + + void main() { + vec3 color = mix(bottomColor, topColor, fragTexCoord.y); + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Halftone Effect.cgl b/samples/Halftone Effect.cgl index 95fadfe..49096e4 100644 --- a/samples/Halftone Effect.cgl +++ b/samples/Halftone Effect.cgl @@ -1,58 +1,24 @@ shader main { - vertex { - input vec2 texCoord; - // Simulated inputs - input vec2 textureSize; // Size of the texture (width, height) - input float dotSize; - input vec4 textureData; // Example for a 512x512 texture - output vec2 passTexCoord; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - - void main() { - passTexCoord = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - // Simulated inputs - input vec2 textureSize; - input float dotSize; - input vec4 textureData; // Example for a 512x512 texture - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, vec2 size) { - // Convert coord to texture indices - int x = int(coord.x * size.x); - int y = int(coord.y * size.y); - // Clamp the indices to texture dimensions - x = clamp(x, 0, int(size.x) - 1); - y = clamp(y, 0, int(size.y) - 1); - // Calculate the 1D array index - int index = y * int(size.x) + x; - // Return the color from the texture data - return texData; - } - - void main() { - // Scale texture coordinates by dotSize - vec2 uv = passTexCoord * dotSize; - - // Calculate angle and radius for the dot pattern - float angle = mod(uv.x + uv.y, 2.0) * 3.14159265; - float radius = length(fract(uv) - vec2(0.5)); - - // Sample color from texture using the simulated texture function - vec4 color = texture(textureData, passTexCoord, textureSize); - - // Calculate intensity based on luminance - float intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); - - // Compute dot pattern using smoothstep for edge softness - float dot = smoothstep(0.4, 0.5, sin(angle) * 0.5 + radius); - - // Set fragment color with dot pattern applied - fragColor = vec4(vec3(dot * intensity), 1.0); - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; + + void main() { + float angle = mod(uv + uv, 2.0) * 3.14159265; + float radius = length(fract(uv) - vec2(0.5)); + vec4 color = texture(texture, fragTexCoord); + float itensity = dot(color, vec3(0.299, 0.587, 0.114)); + float dot = smoothstep(0.4, 0.5, sin(angle) * 0.5 + radius); + fragColor = vec4(vec3(dot * itensity), 1.0); } } +} \ No newline at end of file diff --git a/samples/Hatching shader.cgl b/samples/Hatching shader.cgl index 680c000..0aff9c6 100644 --- a/samples/Hatching shader.cgl +++ b/samples/Hatching shader.cgl @@ -1,66 +1,36 @@ shader main { - vertex { - input vec2 texCoord; - // Simulated thresholds - input float threshold1; - input float threshold2; - input float threshold3; - +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - - // Simulated texture data - input vec4 textureData; // Example for a 512x512 texture - input vec2 textureSize; // Size of the texture (width, height) - output vec2 passTexCoord; - - void main() { - passTexCoord = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 passTexCoord; - - - // Simulated thresholds - input float threshold1; - input float threshold2; - input float threshold3; +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - // Simulated texture data - input vec4 textureData; // Example for a 512x512 texture - input vec2 textureSize; // Size of the texture (width, height) - output vec4 fragColor; + void main() { + vec4 color = texture(texture, fragTexCoord); + float itensity = dot(color, vec3(0.299, 0.587, 0.114)); - vec4 texture(vec4 texData, vec2 coord, vec2 size) { - // Convert coord to texture indices - int x = int(coord.x * size.x); - int y = int(coord.y * size.y); - // Clamp the indices to texture dimensions - x = clamp(x, 0, int(size.x) - 1); - y = clamp(y, 0, int(size.y) - 1); - // Calculate the 1D array index - int index = y * int(size.x) + x; - // Return the color from the texture data - return texData; + vec3 hatching = vec3(1.0); + if (itensity < threshold1) { + hatching = vec3(0.8); + } + if (itensity < threshold2) { + hatching = vec3(0.6); } - - void main() { - // Sample color from texture using the simulated texture function - vec4 color = texture(textureData, passTexCoord, textureSize); - - // Calculate intensity based on luminance - float intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); - - // Determine hatching color based on thresholds - vec3 hatching = vec3(1.0); - if (intensity < threshold1) {hatching = vec3(0.8);} - if (intensity < threshold2) {hatching = vec3(0.6);} - if (intensity < threshold3) {hatching = vec3(0.4);} - else {hatching = vec3(0.2);} - - // Set the fragment color - fragColor = vec4(hatching, 1.0); + if (itensity < threshold3) { + hatching = vec3(0.4); } + else { + hatching = vec3(0.2); + } + + fragColor = vec4(hatching, 1.0); } } +} \ No newline at end of file diff --git a/samples/Heat Haze.cgl b/samples/Heat Haze.cgl index f470b95..7b65273 100644 --- a/samples/Heat Haze.cgl +++ b/samples/Heat Haze.cgl @@ -1,47 +1,22 @@ shader main { - vertex { - input vec2 texCoord; - // Simulated input for time and texture data - input float time; - input vec4 textureData; // Example for a 512x512 texture - input vec2 textureSize; // Size of the texture (width, height) - output vec2 texCoordOut; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - - void main() { - texCoordOut = texCoord; - } + // Pass through texCoord to the fragment shader + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 texCoordOut; - - // Simulated input for time and texture data - input float time; - input vec4 textureData; // Example for a 512x512 texture - input vec2 textureSize; // Size of the texture (width, height) - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, vec2 size) { - // Convert coord to texture indices - int x = int(coord.x * size.x); - int y = int(coord.y * size.y); - // Clamp the indices to texture dimensions - x = clamp(x, 0, int(size.x) - 1); - y = clamp(y, 0, int(size.y) - 1); - // Calculate the 1D array index - int index = y * int(size.x) + x; - // Return the color from the texture data - return texData; - } - - void main() { - vec2 uv = texCoordOut + sin(texCoordOut.y * 10.0 + time) * 0.01; - - // Sample color from texture using the simulated texture function - vec4 color = texture(textureData, uv, textureSize); +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - fragColor = vec4(color.rgb, 1.0); - } + void main() { + vec2 uv = fragTexCoord + sin(fragTexCoord.y * 10.0 + time) * 0.01; + vec4 color = texture(texture, uv); + fragColor = vec4(color.rgb, 1.0); } } +} \ No newline at end of file diff --git a/samples/Kaleidoscope Effect.cgl b/samples/Kaleidoscope Effect.cgl index 41006b5..fd7aede 100644 --- a/samples/Kaleidoscope Effect.cgl +++ b/samples/Kaleidoscope Effect.cgl @@ -1,59 +1,24 @@ shader main { - vertex { - input vec2 texCoord; - - // Simulated input for texture data and segments - input vec4 textureData; // Example for a 512x512 texture - input float segments; - input vec2 textureSize; // Size of the texture (width, height) - output vec2 texCoordOut; +vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - - void main() { - texCoordOut = texCoord; - } + void main() { + fragTexCoord = texCoord; } +} - fragment { - input vec2 texCoordOut; - - // Simulated input for texture data and segments - input vec4 textureData; // Example for a 512x512 texture - input float segments; - input vec2 textureSize; // Size of the texture (width, height) - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, vec2 size) { - // Convert coord to texture indices - int x = int(coord.x * size.x); - int y = int(coord.y * size.y); - // Clamp the indices to texture dimensions - x = clamp(x, 0, int(size.x) - 1); - y = clamp(y, 0, int(size.y) - 1); - // Calculate the 1D array index - int index = y * int(size.x) + x; - // Return the color from the texture data - return texData; - } +fragment { + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - // Compute the angle of the current texCoord relative to the center - float angle = atan(texCoordOut.y - 0.5, texCoordOut.x - 0.5); - - // Compute the radius from the center of the texture - float radius = length(texCoordOut - vec2(0.5)); - - // Determine the segment index based on the angle and number of segments - float segment = floor(segments * angle / (2.0 * 3.14159265)); - - // Compute the new angle corresponding to the segment - float newAngle = segment * 2.0 * 3.14159265 / segments; - - // Compute the new texture coordinates based on the new angle - vec2 newTexCoord = vec2(0.5) + radius * vec2(cos(newAngle), sin(newAngle)); - - // Sample the texture at the new texture coordinates - fragColor = texture(textureData, newTexCoord, textureSize); - } + void main() { + float angle = atan(fragTexCoord.y - 0.5, fragTexCoord.x - 0.5); + float radius = length(fragTexCoord - vec2(0.5)); + float segment = floor(segments * angle / (2.0 * 3.14159265)); + float newAngle = segment * 2.0 * 3.14159265 / segments; + vec2 newTexCoord = vec2(0.5) + radius * vec2(cos(newAngle), sin(newAngle)); + fragColor = texture(texture, newTexCoord); } } +} \ No newline at end of file diff --git a/samples/Lambertian Shading.cgl b/samples/Lambertian Shading.cgl index 4690a5c..5cf9a10 100644 --- a/samples/Lambertian Shading.cgl +++ b/samples/Lambertian Shading.cgl @@ -1,35 +1,28 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - // Simulated input for light position - input vec3 lightPos; - output vec4 color; +vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; - - void main() { - // Pass the position and normal to the fragment shader - color.xyz = position; - color.w = 1.0; // Alpha channel - } + void main() { + fragPosition = position; + fragNormal = normal; } +} - fragment { - input vec4 color; // This contains the position - input vec3 normal; // Pass the normal directly - // Simulated input for light position - input vec3 lightPos; - output vec4 fragColor; - - +fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; - void main() { - vec3 pos = color.xyz; // Extract position - vec3 norm = normalize(normal); - vec3 lightDir = normalize(lightPos - pos); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = vec3(0.5, 0.5, 0.5) * diff; - fragColor = vec4(color, 1.0); - } + void main() { + vec3 norm = normalize(fragNormal); + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Hardcoded light position + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) * diff; + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Light Shaft Effect.cgl b/samples/Light Shaft Effect.cgl index e148501..827a472 100644 --- a/samples/Light Shaft Effect.cgl +++ b/samples/Light Shaft Effect.cgl @@ -1,55 +1,27 @@ shader main { - vertex { - input vec2 texCoord; - - // Simulated inputs for texture data and light position - input vec4 textureData; // Example for a 512x512 texture - input vec2 lightPos; // Simulated light position - input vec2 textureSize; // Size of the texture (width, height) - output vec2 texCoordOut; - - void main() { - texCoordOut = texCoord; - } +vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; + + // Pass through position and normal to the fragment shader + void main() { + fragPosition = position; + fragNormal = normal; } +} - fragment { - input vec2 texCoordOut; - - // Simulated inputs for texture data and light position - input vec4 textureData; // Example for a 512x512 texture - input vec2 lightPos; // Simulated light position - input vec2 textureSize; // Size of the texture (width, height) - output vec4 fragColor; - - vec4 texture(vec4 texData, vec2 coord, vec2 size) { - // Convert coord to texture indices - int x = int(coord.x * size.x); - int y = int(coord.y * size.y); - // Clamp the indices to texture dimensions - x = clamp(x, 0, int(size.x) - 1); - y = clamp(y, 0, int(size.y) - 1); - // Calculate the 1D array index - int index = y * int(size.x) + x; - // Return the color from the texture data - return texData; - } - - void main() { - vec2 uv = texCoordOut; - vec4 color = texture(textureData, uv, textureSize); - vec2 dir = uv - lightPos; - float dist = length(dir); - - // Compute offset based on distance - vec2 offset = dir * (1.0 - dist) / textureSize; // Normalize by textureSize - vec2 uvOffset = uv - offset; - - // Sample the texture at the offset position - vec4 shaftColor = texture(textureData, uvOffset, textureSize); +fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; - // Mix the original color with the shaft color - fragColor = mix(color, shaftColor, 0.5); - } + void main() { + vec3 viewDir = normalize(viewPos - fragPosition); + float rim = pow(1.0 - max(dot(fragNormal, viewDir), 0.0), rimPower); + vec3 color = vec3(0.5, 0.5, 0.5) + rim * rimColor; + fragColor = vec4(color, 1.0); } } +} diff --git a/samples/Metallic Shader.cgl b/samples/Metallic Shader.cgl index 1e7732b..7c35fef 100644 --- a/samples/Metallic Shader.cgl +++ b/samples/Metallic Shader.cgl @@ -1,37 +1,34 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - // Simulated inputs - input vec3 lightPos; - input vec3 viewPos; - input float metallic; - output vec4 color; +vertex { + input vec3 position; + input vec3 normal; + output vec4 fragColor; - vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { - float ambient = 0.1; - float diff = max(dot(normal, lightDir), 0.0); - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - return ambient + diff + metallic * spec; - } + // Vertex shader code, if needed + // For this example, there's no actual vertex shader code +} + +fragment { + input vec3 position; + input vec3 normal; + output vec4 fragColor; - void main() { - vec3 norm = normalize(normal); - vec3 lightDir = normalize(lightPos - position); - vec3 viewDir = normalize(viewPos - position); - vec3 lighting = calculateLighting(norm, lightDir, viewDir); - color.rgb = vec3(1.0, 0.8, 0.6) * lighting; - color.a = 1.0; // Alpha channel - } + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambient = 0.1; + float diff = max(dot(normal, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + return ambient + diff + 0.5 * spec; // Assuming metallic is set to 0.5 } - fragment { - input vec4 color; - output vec4 fragColor; + void main() { + vec3 norm = normalize(normal); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); // Example light position + vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - position); // Example view position + vec3 lighting = calculateLighting(norm, lightDir, viewDir); + vec3 color = vec3(1.0, 0.8, 0.6) * lighting; - void main() { - fragColor = color; - } + fragColor = vec4(color, 1.0); } } +} \ No newline at end of file diff --git a/samples/Motion Blur.cgl b/samples/Motion Blur.cgl index 61e32f2..4f0c143 100644 --- a/samples/Motion Blur.cgl +++ b/samples/Motion Blur.cgl @@ -1,36 +1,24 @@ shader main { - vertex { - input vec2 texCoord; - output vec4 color; +vertex { + input vec2 texCoord; + output vec2 fragCoord; - // Pass through the texture coordinate - void main() { - color = vec4(texCoord, 0.0, 1.0); - } + void main() { + fragCoord = texCoord; } +} - fragment { - input vec4 color; // This contains the texture coordinate - // Simulated inputs - input vec2 motionDirection; - input float blurAmount; - input float texture; // Simulated texture - output vec4 fragColor; - - vec4 sampleTexture(vec2 coord) { - // Simulate texture sampling (for demonstration purposes) - return vec4(texture, coord); - } +fragment { + input vec2 fragCoord; + output vec4 fragColor; void main() { - vec2 texCoord = color.xy; // Extract the texture coordinate - vec4 colorAccum = vec4(0.0); - for (int i = -4; i <= 4; i=i+1) { - vec2 offset = float(i) * motionDirection * blurAmount / 10.0; - colorAccum += sampleTexture(texCoord + offset); - } - colorAccum /= 9.0; - fragColor = colorAccum; + + for (int i = -4; i < 4; i++) { + color += texture(texture, fragCoord * motionDirection * blurAmount / 10.0); } + color /= 9.0; + fragColor = color; } } +} \ No newline at end of file diff --git a/samples/Nested Loop Shader.cgl b/samples/Nested Loop Shader.cgl index ca3e7cf..00cc5c9 100644 --- a/samples/Nested Loop Shader.cgl +++ b/samples/Nested Loop Shader.cgl @@ -3,62 +3,29 @@ shader nestedLoopsShader { input vec3 position; input vec2 texCoord; input vec3 normal; - output vec4 color; + output vec3 fragPosition; + output vec2 fragTexCoord; + output vec3 fragNormal; - // Pass the texture coordinates and normal to the fragment shader void main() { - color.xy = texCoord; - color.zw = normal; - color.a = 1.0; // Alpha channel + fragPosition = position; + fragTexCoord = texCoord; + fragNormal = normal; } } fragment { - input vec4 color; // Contains the texture coordinates and normal - input vec2 texCoord; // Texture coordinates from vertex shader - - - // Simulated inputs - input vec2 texture; // Simulated texture - input vec3 lightPos; - input vec3 viewPos; - input vec3 ambientColor; - input float shininess; - input float specularIntensity; - input int outerIterations; - input int innerIterations; + input vec3 fragPosition; + input vec2 fragTexCoord; + input vec3 fragNormal; output vec4 fragColor; - vec4 sampleTexture(vec2 coord) { - // Simulate texture sampling (for demonstration purposes) - return vec4(texture, coord); - } - - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); - return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; - } - void main() { - vec4 texColor = sampleTexture(texCoord); - vec3 lightDir = normalize(lightPos - color.xyz); - vec3 viewDir = normalize(viewPos - color.xyz); - - vec3 ambient = ambientColor; - vec3 diffuse = texColor.rgb; - vec3 specular = vec3(0.0); - - // Outer loop - for (int i = 0; i < outerIterations; i=i+1) { - // Inner loop - for (int j = 0; j < innerIterations; j=j+1) { - // Compute lighting - vec3 tempSpecular = calculateSpecular(color.zw, lightDir, viewDir); - specular += tempSpecular; - } - - // Reduce diffuse intensity progressively + + for (int i = 0; i < 0; i++) { + for (int j = 0; j < 0; j++) { + vec3 reflectDir = reflect(-lightDir, fragNormal); + } diffuse *= 0.9; } @@ -66,4 +33,4 @@ shader nestedLoopsShader { fragColor = vec4(lighting, 1.0); } } -} +} \ No newline at end of file diff --git a/samples/Normal Mapping.cgl b/samples/Normal Mapping.cgl index 840ecbd..553d259 100644 --- a/samples/Normal Mapping.cgl +++ b/samples/Normal Mapping.cgl @@ -1,44 +1,34 @@ -shader main { - vertex { - input vec3 position; - input vec3 normal; - output vec4 color; // To pass the normal and position +vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec2 fragTexCoord; + output vec3 fragPosition; + output vec3 fragNormal; - - void main() { - // Define constants for light and view positions - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position - vec3 viewPos = vec3(0.0, 0.0, 5.0); // Example view position - float shininess = 32.0; - vec3 norm = normalize(normal); - vec3 lightDir = normalize(lightPos - position); // Direction of light - vec3 viewDir = normalize(viewPos - position); // Direction of view - - // Calculate lighting - float ambient = 0.1; - float diff = max(dot(norm, lightDir), 0.0); - vec3 reflectDir = reflect(-lightDir, norm); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); - - // Combine lighting components - color.rgb = ambient + diff + spec; - color.a = 1.0; // Alpha channel - } + void main() { + fragTexCoord = texCoord; + fragPosition = position; + fragNormal = normal; } +} - fragment { - input vec4 color; // This contains the lighting information - input vec2 texCoord; - output vec4 fragColor; - - +fragment { + input vec2 fragTexCoord; + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; - void main() { - // Sample texture (assuming `texture` is a built-in function in this environment) - vec4 texColor = texture(texture, texCoord); // Sample texture using coordinates - vec3 ambient = vec3(0.1, 0.1, 0.1); // Hardcoded ambient color - vec3 finalColor = ambient + color.rgb * texColor.rgb; // Combine ambient and texture color - fragColor = vec4(finalColor, 1.0); // Final output color - } + + void main() { + vec3 norm = normalize(texture(normalMap, fragTexCoord) * 2.0 - 1.0); + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, norm); + vec3 viewDir = normalize(viewPos - fragPosition); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + vec3 color = texture(texture, fragTexCoord); + fragColor = vec4((0.1 + diff + spec) * color, 1.0); } } +} \ No newline at end of file From 31a61c29016798414fdc88098685ad7efb235098 Mon Sep 17 00:00:00 2001 From: samthakur587 Date: Thu, 8 Aug 2024 12:57:03 +0530 Subject: [PATCH 3/3] fix: updated the samples --- samples/Adv complexShader.cgl | 71 ++++++++----- samples/Advanced Post-Processing Effects.cgl | 24 +++-- samples/Advanced Shader.cgl | 59 ++++++----- samples/Advanced calculateLighting.cgl | 72 ++++++------- samples/Anaglyph 3D.cgl | 35 ++++--- samples/Atmospheric Scattering.cgl | 21 ++-- samples/Basic Fog Shader.cgl | 16 ++- samples/Basic Reflection Shader.cgl | 23 ++++- samples/Basic Texture Shade.cgl | 12 ++- samples/Blinn-Phong Shading.cgl | 72 ++++++------- samples/Bloom Effect.cgl | 49 ++++++--- samples/Bump Mapping.cgl | 62 +++++++---- samples/Calculate Lighting.cgl | 100 +++++++----------- samples/Cartoon Outline.cgl | 57 +++++++---- samples/Cel Shading.cgl | 41 +++++--- samples/Chromakey Shader.cgl | 45 ++++---- samples/Chromatic Aberration.cgl | 44 ++++---- samples/Color Inversion.cgl | 32 +++--- samples/Complex Shader.cgl | 102 +++++++++++++++---- samples/Depth Fog.cgl | 49 +++++---- samples/Depth of Field.cgl | 65 ++++++++---- samples/Displacement Mapping.cgl | 53 +++++++--- samples/Dynamic Day-Night Cycle.cgl | 21 ++-- samples/Dynamic Water Shader.cgl | 26 +++-- samples/Edge Detection.cgl | 56 ++++++---- samples/Emboss Effect.cgl | 72 +++++++------ samples/Flat Shading.cgl | 31 +++--- samples/Fluid Dynamics Simulation.cgl | 23 +++-- samples/Fog Effect.cgl | 37 ++++--- samples/Fresnel Effect.cgl | 49 +++++---- samples/Glitch Effect.cgl | 56 +++++++--- samples/Gouraud Shading.cgl | 46 +++++---- samples/Gradient Background.cgl | 35 ++++--- samples/Halftone Effect.cgl | 70 +++++++++---- samples/Hatching shader.cgl | 82 ++++++++++----- samples/Heat Haze.cgl | 55 +++++++--- samples/Kaleidoscope Effect.cgl | 69 +++++++++---- samples/Lambertian Shading.cgl | 49 +++++---- samples/Light Shaft Effect.cgl | 70 +++++++++---- samples/Metallic Shader.cgl | 55 +++++----- samples/Motion Blur.cgl | 42 +++++--- samples/Nested Loop Shader.cgl | 63 +++++++++--- samples/Normal Mapping.cgl | 66 +++++++----- 43 files changed, 1367 insertions(+), 810 deletions(-) diff --git a/samples/Adv complexShader.cgl b/samples/Adv complexShader.cgl index dafe8bb..b3f03a9 100644 --- a/samples/Adv complexShader.cgl +++ b/samples/Adv complexShader.cgl @@ -2,46 +2,63 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; + input vec3 normal; + output vec4 color; + + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); + } void main() { - fragTexCoord = texCoord; - fragPosition = position; // Directly pass position to fragment shader - gl_Position = vec4(position, 1.0); + // Pass texture coordinates and normals to the fragment shader + color.xy = texCoord; + color.zw = normal; + + // Simulate normal map calculation in the vertex shader + vec3 mapNormal = normalize(vec3(texCoord, 1.0) * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(normal, mapNormal); + + // Pass perturbed normal to the fragment shader + color.rgb = vec3(perturbedNormal); + color.a = 1.0; // Alpha channel } } fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; + input vec4 color; + input vec2 texCoord; output vec4 fragColor; - vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { - float ambientStrength = 0.1; - vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Default light color - - float diff = max(dot(normal, lightDir), 0.0); - vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Default light color - - float specularStrength = 0.5; + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) { vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Default light color - - return ambient + diffuse + specular; + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0); } void main() { - vec4 texColor = texture2D(textureSampler, fragTexCoord); // Assumed default sampler - vec3 normal = normalize(cross(dFdx(fragPosition), dFdy(fragPosition))); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Default light direction - vec3 viewDir = normalize(-fragPosition); // Simple view direction + vec3 perturbedNormal = normalize(color.rgb); + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position + vec3 lightDir = normalize(lightPos - vec3(color)); + vec3 viewDir = normalize(viewPos - vec3(color)); + + // Ambient component + vec3 ambient = vec3(0.1, 0.1, 0.1); // Example ambient color + + // Diffuse component + float diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * vec3(0.8, 0.8, 0.8); // Example diffuse color + + // Specular component + float shininess = 32.0; // Example shininess + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir, shininess); + + // Combine lighting components + vec3 lighting = ambient + diffuse + specular; - vec3 lighting = calculateLighting(normal, lightDir, viewDir); - vec4 color = vec4(lighting, 1.0) * texColor; + vec4 finalColor = vec4(lighting, 1.0); - fragColor = color; + fragColor = finalColor; } } -} \ No newline at end of file +} diff --git a/samples/Advanced Post-Processing Effects.cgl b/samples/Advanced Post-Processing Effects.cgl index 7ad3a1d..ffc5528 100644 --- a/samples/Advanced Post-Processing Effects.cgl +++ b/samples/Advanced Post-Processing Effects.cgl @@ -1,17 +1,29 @@ shader main { vertex { - // Vertex shader logic goes here. + input vec2 texCoord; + output vec4 color; + + void main() { + // Pass through texture coordinates as color + color = vec4(texCoord, 0.0, 1.0); + } } fragment { - input vec2 texCoord; + input vec4 color; output vec4 fragColor; + vec4 textureSample(vec2 texCoord) { + // Example function to simulate texture sampling + return vec4(texCoord.x, texCoord.y, 0.5, 1.0); + } + void main() { - float brightness = 0.0; // Placeholder for brightness calculation + float brightness = textureSample(color.xy); float bloom = max(0.0, brightness - 0.5); - vec3 color = vec3(0.0); // Placeholder for texture color - fragColor = vec4(color + vec3(bloom), 1.0); + vec3 texColor = textureSample(color.xy); + vec3 colorWithBloom = texColor + vec3(bloom); + fragColor = vec4(colorWithBloom, 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Advanced Shader.cgl b/samples/Advanced Shader.cgl index ab68ef3..f911fbd 100644 --- a/samples/Advanced Shader.cgl +++ b/samples/Advanced Shader.cgl @@ -2,48 +2,57 @@ shader advancedShader { vertex { input vec3 position; input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; + output vec4 color; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - gl_Position = vec4(position, 1.0); - } - } - - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - output vec4 fragColor; - - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) { vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); return spec * vec3(1.0, 1.0, 1.0); // White specular highlight } void main() { - vec4 texColor = texture2D(textureSampler, fragTexCoord); // Default sampler + // Pass texture coordinates to the fragment shader + color.xy = texCoord; + + // Simulate ambient, light position, view position, and shininess + vec3 ambientColor = vec3(0.1, 0.1, 0.1); + vec3 lightPos = vec3(10.0, 10.0, 10.0); + vec3 viewPos = vec3(0.0, 0.0, 10.0); + float shininess = 32.0; - vec3 norm = normalize(cross(dFdx(fragPosition), dFdy(fragPosition))); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position - vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position + // Calculate normal in the vertex shader + vec3 norm = normalize(cross(dFdx(position), dFdy(position))); + vec3 lightDir = normalize(lightPos - position); + vec3 viewDir = normalize(viewPos - position); // Ambient component - vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color + vec3 ambient = ambientColor; // Diffuse component float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // White diffuse light // Specular component - vec3 specular = calculateSpecular(norm, lightDir, viewDir); + vec3 specular = calculateSpecular(norm, lightDir, viewDir, shininess); vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * texColor; + color.rgb = lighting; + color.a = 1.0; // Alpha channel + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + vec4 textureSample(vec2 texCoord) { + // Example function to simulate texture sampling + return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Simulated texture color + } - fragColor = color; + void main() { + vec4 texColor = textureSample(color.xy); + fragColor = vec4(color.rgb * texColor.rgb, 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Advanced calculateLighting.cgl b/samples/Advanced calculateLighting.cgl index ad1a9f4..a469a29 100644 --- a/samples/Advanced calculateLighting.cgl +++ b/samples/Advanced calculateLighting.cgl @@ -2,60 +2,50 @@ shader complexShader { vertex { input vec3 position; input vec2 texCoord; - input vec3 normal; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; + output vec4 color; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; - gl_Position = vec4(position, 1.0); - } - } + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Example ambient color - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + float diff = max(dot(normal, lightDir), 0.0); + vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Example diffuse color - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + float specularStrength = 0.5; vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess - return spec * vec3(1.0, 1.0, 1.0) * 1.0; // Default specular intensity - } + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Example specular color - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); + return ambient + diffuse + specular; } void main() { - vec4 diffuseColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - vec4 specularColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - vec4 normalMapColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler - - vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); + // Pass texture coordinates to the fragment shader + color.xy = texCoord; - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position - vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position + // Calculate normal in the vertex shader + vec3 normal = normalize(cross(dFdx(position), dFdy(position))); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Example light direction + vec3 viewDir = normalize(-position); // Example view direction - // Ambient component - vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color - - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * diffuseColor.rgb; + vec3 lighting = calculateLighting(normal, lightDir, viewDir); + color.rgb = lighting; + color.a = 1.0; // Alpha channel + } + } - // Specular component - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + fragment { + input vec4 color; + output vec4 fragColor; - vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * diffuseColor; + vec4 textureSample(vec2 texCoord) { + // Example function to simulate texture sampling + return vec4(texCoord.x, texCoord.y, 1.0 - texCoord.x, 1.0); + } - fragColor = color; + void main() { + vec4 texColor = textureSample(color.xy); // Simulated texture sampling + fragColor = vec4(color.rgb * texColor.rgb, 1.0); } } } diff --git a/samples/Anaglyph 3D.cgl b/samples/Anaglyph 3D.cgl index 99b196f..8bc6c96 100644 --- a/samples/Anaglyph 3D.cgl +++ b/samples/Anaglyph 3D.cgl @@ -1,21 +1,28 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec4 color; - void main() { - fragTexCoord = texCoord; + void main() { + // Pass through texture coordinates as color + color = vec4(texCoord, 0.0, 1.0); + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec4 color; + output vec4 fragColor; + + // Define a simulated texture sampling function + vec4 textureSample(vec2 texCoord) { + // Simulated texture sampling: using simple color mapping + return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Sample color with brightness in the red channel + } - void main() { - vec4 leftColor = texture(texture, fragTexCoord - eyeOffset); - vec4 rightColor = texture(texture, fragTexCoord + eyeOffset); - fragColor = vec4(leftColor, rightColor, rightColor, 1.0); + void main() { + vec4 leftColor = textureSample(color.xy - vec2(0.1)); // Simulate left texture sample + vec4 rightColor = textureSample(color.xy + vec2(0.1)); // Simulate right texture sample + fragColor = vec4(leftColor.r, rightColor.g, rightColor.b, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Atmospheric Scattering.cgl b/samples/Atmospheric Scattering.cgl index da6a9ad..79fefcc 100644 --- a/samples/Atmospheric Scattering.cgl +++ b/samples/Atmospheric Scattering.cgl @@ -1,11 +1,7 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; - output vec4 fragColor; + output vec4 color; vec3 rayleighScattering(float theta) { return vec3(0.5, 0.7, 1.0) * pow(max(0.0, 1.0 - theta * theta), 3.0); @@ -13,8 +9,17 @@ shader main { void main() { float theta = position.y; - vec3 color = rayleighScattering(theta); - fragColor = vec4(color, 1.0); + vec3 colorComputed = rayleighScattering(theta); + color = vec4(colorComputed, 1.0); + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Basic Fog Shader.cgl b/samples/Basic Fog Shader.cgl index cecec25..3e2a33d 100644 --- a/samples/Basic Fog Shader.cgl +++ b/samples/Basic Fog Shader.cgl @@ -17,11 +17,23 @@ shader main { input vec3 fragPosition; output vec4 fragColor; + // Simulate texture sampling function + vec4 textureSample(vec2 texCoord) { + // Simulated texture color based on texCoord + return vec4(texCoord, 0.5, 1.0); // Example color with depth information in the red channel + } + + void main() { - + + vec3 baseColor = textureSample(fragTexCoord); + + // Calculate depth based on position (simple distance from origin) float depth = length(fragPosition); float fogFactor = exp(-fogDensity * depth); - vec3 finalColor = mix(fogColor, baseColor, fogFactor); + + // Compute final color with fog effect + vec3 finalColor = mix(fogColor, baseColor.rgb, fogFactor); fragColor = vec4(finalColor, 1.0); } } diff --git a/samples/Basic Reflection Shader.cgl b/samples/Basic Reflection Shader.cgl index 494ed7e..ec75a33 100644 --- a/samples/Basic Reflection Shader.cgl +++ b/samples/Basic Reflection Shader.cgl @@ -17,12 +17,31 @@ shader main { input vec3 fragNormal; output vec4 fragColor; + // Simulate texture sampling function + vec4 textureSample(vec2 texCoord) { + // Example simulated texture color based on texCoord + return vec4(texCoord, 0.5, 1.0); // Simulated texture color + } + + // Simulate environment map sampling function + vec4 cubeTextureSample(vec3 direction) { + // Example simulated cube map color based on direction + return vec4(abs(direction), 1.0); // Simulated environment map color + } + + + void main() { + // Simulate view position + vec3 viewPos = vec3(0.0, 0.0, 5.0); // Example view position vec3 norm = normalize(fragNormal); vec3 viewDir = normalize(viewPos - fragPosition); vec3 reflectDir = reflect(-viewDir, norm); - fragColor = vec4(mix(objectColor, environmentColor, 0.5), 1.0); + vec3 environmentColor = cubeTextureSample(reflectDir); + vec3 objectColor = textureSample(fragPosition.xy); + + fragColor = vec4(mix(objectColor.rgb, environmentColor.rgb, 0.5), 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Basic Texture Shade.cgl b/samples/Basic Texture Shade.cgl index b6ec320..f0b4a5b 100644 --- a/samples/Basic Texture Shade.cgl +++ b/samples/Basic Texture Shade.cgl @@ -5,7 +5,10 @@ shader main { output vec2 fragTexCoord; void main() { + // Pass texture coordinates to the fragment shader fragTexCoord = texCoord; + + // Set the position of the vertex gl_Position = vec4(position, 1.0); } } @@ -14,8 +17,15 @@ shader main { input vec2 fragTexCoord; output vec4 fragColor; + // Simulate texture sampling function + vec4 textureSample(vec2 texCoord) { + // Example simulated texture color based on texCoord + return vec4(texCoord, 0.5, 1.0); // Simulated texture color + } + void main() { - fragColor = textureColor; + // Sample the texture using the simulated texture sampling function + fragColor = textureSample(fragTexCoord); } } } diff --git a/samples/Blinn-Phong Shading.cgl b/samples/Blinn-Phong Shading.cgl index c6e8efd..ca1bb34 100644 --- a/samples/Blinn-Phong Shading.cgl +++ b/samples/Blinn-Phong Shading.cgl @@ -1,41 +1,41 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; - - void main() { - fragPosition = position; - fragNormal = normal; + vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; + + // Inputs for vertex shader + void main() { + // Pass position and normal to fragment shader + fragPosition = position; + fragNormal = normal; + + // Set the position of the vertex + gl_Position = vec4(position, 1.0); + } } -} - -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - - void main() { - vec3 norm = normalize(fragNormal); - - // Hardcoded light and view positions - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Light position - vec3 viewPos = vec3(0.0, 0.0, 1.0); // View position - - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - - // Calculate the halfway vector - vec3 viewDir = normalize(viewPos - fragPosition); - vec3 halfwayDir = normalize(lightDir + viewDir); - - // Calculate specular lighting - float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); - // Combine diffuse and specular components - vec3 color = vec3(0.5, 0.5, 0.5) * diff + vec3(1.0, 1.0, 1.0) * spec; - fragColor = vec4(color, 1.0); + fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; + + + void main() { + // Calculate lighting + // Define constant light and view positions for this shader + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position + vec3 norm = normalize(fragNormal); + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 halfwayDir = normalize(lightDir + viewPos - fragPosition); + float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; + + // Output the final color + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Bloom Effect.cgl b/samples/Bloom Effect.cgl index acc85e3..f47262d 100644 --- a/samples/Bloom Effect.cgl +++ b/samples/Bloom Effect.cgl @@ -1,22 +1,41 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec3 position; + input vec3 normal; + output vec3 fragPosition; + output vec3 fragNormal; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + // Inputs for vertex shader + void main() { + // Pass position and normal to fragment shader + fragPosition = position; + fragNormal = normal; + + // Set the position of the vertex + gl_Position = vec4(position, 1.0); + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec3 fragPosition; + input vec3 fragNormal; + output vec4 fragColor; + + + void main() { + // Calculate lighting + // Define constant light and view positions for this shader + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position + vec3 norm = normalize(fragNormal); + vec3 lightDir = normalize(lightPos - fragPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 wayDir = normalize(lightDir + viewPos - fragPosition); + float spec = pow(max(dot(norm, wayDir), 0.0), 32.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; - void main() { - vec4 color = texture(texture, fragCoord); - vec4 bloom = max(color - threshold, 0.0); - fragColor = color + bloom; + // Output the final color + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Bump Mapping.cgl b/samples/Bump Mapping.cgl index d5bd4cf..8af45a1 100644 --- a/samples/Bump Mapping.cgl +++ b/samples/Bump Mapping.cgl @@ -1,28 +1,48 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec2 fragTexCoord; + vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec3 fragPosition; + output vec3 fragNormal; + output vec2 fragTexCoord; - void main() { - fragPosition = position; - fragTexCoord = texCoord; + void main() { + fragPosition = position; + fragNormal = normal; + fragTexCoord = texCoord; + + // Set the position of the vertex + gl_Position = vec4(position, 1.0); + } } -} -fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec3 fragPosition; + input vec3 fragNormal; + input vec2 fragTexCoord; + output vec4 fragColor; - void main() { - vec3 norm = normalize(texture(normalMap, fragTexCoord) * 2.0 - 1.0); - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = texture(texture, fragTexCoord) * diff; - fragColor = vec4(color, 1.0); + + void main() { + // Hardcoded texture and normal map values (you may need to adapt this to your setup) + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 textureColor = vec3(1.0, 1.0, 1.0); // Example texture color + vec3 normalMapColor = vec3(0.5, 0.5, 1.0); // Example normal map color + // Fetch and normalize the normal from the normal map + vec3 norm = normalize(normalMapColor * 2.0 - 1.0); + + // Calculate the direction from the fragment to the light + vec3 lightDir = normalize(lightPos - fragPosition); + + // Compute the diffuse lighting term + float diff = max(dot(norm, lightDir), 0.0); + + // Fetch the color from the texture and apply the diffuse lighting + vec3 color = textureColor * diff; + + // Output the final color with full opacity + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Calculate Lighting.cgl b/samples/Calculate Lighting.cgl index 27aa059..600637e 100644 --- a/samples/Calculate Lighting.cgl +++ b/samples/Calculate Lighting.cgl @@ -1,65 +1,35 @@ -shader complexShader { - vertex { - input vec3 position; - input vec2 texCoord; - input vec3 normal; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; - - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; - gl_Position = vec4(position, 1.0); - } - } - - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - - - vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), defaultShininess); - return spec * vec3(1.0, 1.0, 1.0) * defaultSpecularIntensity; - } - - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); - } - - void main() { - - vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); - - vec3 lightDir = normalize(defaultLightPos - fragPosition); - vec3 viewDir = normalize(defaultViewPos - fragPosition); - - // Ambient component - vec3 ambient = defaultAmbientColor; - - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * diffuseColor.rgb; - - // Specular component - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); - - // Loop to modify lighting based on iterations - vec3 lighting = ambient + diffuse + specular; - for (int i = 0; i < iterations; i++) { - lighting *= 0.9; - } - - // Ternary operator to conditionally modify color - vec4 color = (diff > 0.5) ? vec4(lighting, 1.0) * diffuseColor : vec4(lighting * 0.5, 1.0) * diffuseColor; - - fragColor = color; - } - } -} \ No newline at end of file +#version 450 + + +// Vertex shader + +layout(location = 0) in vec3 position; +layout(location = 1) in vec2 texCoord; +out vec4 color; + +float calculateLighting(vec3 normal, vec3 lightDir) { + float diff = max(dot(normal, lightDir), 0.0); + return diff; +} +vec4 textureColor(vec2 coord) { + return vec4(coord, 0.5, 1.0); +} +void main() { + vec3 hardcodedNormal = vec3(0.0, 0.0, 1.0); + vec3 hardcodedLightDir = vec3(1.0, 1.0, 1.0); + vec3 normal = hardcodedNormal; + vec3 lightDir = hardcodedLightDir; + float lightIntensity = calculateLighting(normal, lightDir); + vec4 texColor = textureColor(texCoord); + color = (texColor * lightIntensity); + color = texCoord; +} + +// Fragment shader + +in vec4 color; +layout(location = 0) out vec4 fragColor; + +void main() { + fragColor = color; +} diff --git a/samples/Cartoon Outline.cgl b/samples/Cartoon Outline.cgl index e2a6eaf..627bb9a 100644 --- a/samples/Cartoon Outline.cgl +++ b/samples/Cartoon Outline.cgl @@ -1,31 +1,44 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec4 fragColor; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + + + void main() { + // Pass texture coordinates to fragment shader + float outlineWidth = 0.01; // Example value for outline width + fragColor.xy = texCoord; + fragColor.zw = vec2(outlineWidth, 0.0); // Store outline width in the z component for later use + gl_Position = vec4(texCoord, 0.0, 1.0); // Example position + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 fragTexCoord; + input vec2 fragOutlineWidth; + output vec4 fragColor; + + // Simulated texture sampling + vec4 textureColor(vec2 coord) { + // Simulate texture sampling (replace with actual sampling if needed) + return vec4(coord, 0.5, 1.0); // Simulated texture color + } - void main() { - vec4 color = texture(texture, fragTexCoord); - vec4 north = texture(texture, fragTexCoord + vec2(0.0, outlineWidth)); - vec4 south = texture(texture, fragTexCoord - vec2(0.0, outlineWidth)); - vec4 east = texture(texture, fragTexCoord + vec2(outlineWidth, 0.0)); - vec4 west = texture(texture, fragTexCoord - vec2(outlineWidth, 0.0)); + void main() { + float outlineWidth = fragOutlineWidth.x; // Extract outline width + vec4 color = textureColor(fragTexCoord); + vec4 north = textureColor(fragTexCoord + vec2(0.0, outlineWidth)); + vec4 south = textureColor(fragTexCoord - vec2(0.0, outlineWidth)); + vec4 east = textureColor(fragTexCoord + vec2(outlineWidth, 0.0)); + vec4 west = textureColor(fragTexCoord - vec2(outlineWidth, 0.0)); - float edge = length((north + south + east + west) / 4.0 - color); - if (edge > 0.1) { - fragColor = vec4(0.0, 0.0, 0.0, 1.0); - } else { - fragColor = color; + float edge = length((north + south + east + west) / 4.0 - color); + if (edge > 0.1) { + fragColor = vec4(0.0, 0.0, 0.0, 1.0); + } else { + fragColor = color; + } } } } -} \ No newline at end of file diff --git a/samples/Cel Shading.cgl b/samples/Cel Shading.cgl index 6de128e..c7a3783 100644 --- a/samples/Cel Shading.cgl +++ b/samples/Cel Shading.cgl @@ -1,23 +1,32 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vertex { + input vec3 position; + input vec3 normal; + output vec4 color; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} + vec3 celShading(vec3 normal, vec3 lightDir) { + float inensity = dot(normal, lightDir); + if (inensity > 0.95) {return vec3(1.0, 1.0, 1.0);} + if (inensity > 0.5) {return vec3(0.8, 0.8, 0.8);} + if (inensity > 0.25) {return vec3(0.6, 0.6, 0.6);} + else {return vec3(0.4, 0.4, 0.4);} + } + + void main() { + vec3 norm = normalize(normal); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); + vec3 shadingColor = celShading(norm, lightDir); + + color = vec4(shadingColor, 1.0); // Alpha channel + } + } -fragment { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + fragment { + input vec4 color; + output vec4 fragColor; void main() { - vec3 norm = normalize(normal); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); // Example light position - - fragColor = vec4(color, 1.0); + fragColor = color; + } } } -} \ No newline at end of file diff --git a/samples/Chromakey Shader.cgl b/samples/Chromakey Shader.cgl index aa49ada..acbddad 100644 --- a/samples/Chromakey Shader.cgl +++ b/samples/Chromakey Shader.cgl @@ -1,28 +1,35 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 fragTexCoord; - void main() { - fragTexCoord = texCoord; + void main() { + fragTexCoord = texCoord; + gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // This should be set properly for your use case + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 fragTexCoord; + output vec4 fragColor; + + vec4 textureColor(vec2 coord) { + // Example function to simulate texture sampling + return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color + } - void main() { - vec4 color = texture(texture, fragTexCoord); - vec3 keyColor = vec3(1.0, 0.0, 0.0); // Example key color - float threshold = 0.1; // Example threshold + void main() { + vec3 keyColor = vec3(0.0, 1.0, 0.0); // Example key color + float threshold = 0.1; // Example threshold - float diff = length(color - keyColor); - if (diff < threshold) { - discard; - } else { - fragColor = color; + vec4 color = textureColor(fragTexCoord); + float diff = length(color.rgb - keyColor); + if (diff < threshold) { + // Discard fragment + fragColor = vec4(0.0, 0.0, 0.0, 0.0); + } else { + fragColor = color; + } } } } -} \ No newline at end of file diff --git a/samples/Chromatic Aberration.cgl b/samples/Chromatic Aberration.cgl index 4ceb3b8..e4b2f67 100644 --- a/samples/Chromatic Aberration.cgl +++ b/samples/Chromatic Aberration.cgl @@ -1,24 +1,34 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec4 color; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + // Pass through the texture coordinate + void main() { + color = vec4(texCoord, 0.0, 1.0); + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the texture coordinate + output vec4 fragColor; + + vec4 textureColor(vec2 coord) { + // Example function to simulate texture sampling + return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color + } + + void main() { + vec2 texCoord = color.xy; // Extract the texture coordinate - void main() { - vec2 offset = vec2(aberration / 512.0); - vec4 red = texture(texture, fragCoord + offset); - vec4 green = texture(texture, fragCoord); - vec4 blue = texture(texture, fragCoord - offset); - fragColor = vec4(red.r, green.g, blue.b, 1.0); + float aberration = 0.005; // Example aberration value + vec2 offset = vec2(aberration / 512.0); + + vec4 red = textureColor(texCoord + offset); + vec4 green = textureColor(texCoord); + vec4 blue = textureColor(texCoord - offset); + + fragColor = vec4(red.r, green.g, blue.b, 1.0); + } } } -} diff --git a/samples/Color Inversion.cgl b/samples/Color Inversion.cgl index 9cd7eb0..0ff4d0b 100644 --- a/samples/Color Inversion.cgl +++ b/samples/Color Inversion.cgl @@ -1,21 +1,25 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + output vec4 fragColor; + + vec4 textureColor(vec2 coord) { + // Example function to simulate texture sampling + return vec4(coord.x, coord.y, 0.5, 1.0); // Simulated texture color + } - void main() { - vec4 color = texture(texture, fragTexCoord); - fragColor = vec4(vec3(1.0) - color, 1.0); + void main() { + vec4 color = textureColor(passTexCoord); + fragColor = vec4(vec3(1.0) - color.rgb, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Complex Shader.cgl b/samples/Complex Shader.cgl index d8b82c0..b001e9a 100644 --- a/samples/Complex Shader.cgl +++ b/samples/Complex Shader.cgl @@ -1,31 +1,99 @@ shader complexShader { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; input vec2 texCoord; - output vec4 fragColor; + input vec3 normal; + input vec3 diffuseTexture; + input vec3 specularTexture; + input vec3 normalMap; + input vec3 lightPos; + input vec3 viewPos; + input vec3 ambientColor; + input float shininess; + input float specularIntensity; + input float shininess; + output vec4 color; + output vec2 passTexCoord; + + + + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; + } + + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); + } + + void main() { + passTexCoord = texCoord; + if (normalMap >= vec3(0.0, 0.0, 0.0)) { + test += 1.0; + test -= 1.0; + test *= 1.0; + test /= 1.0; + float test = 1.0; + float test = 1.0; + // No normal map + color.rgb = normal; + color.a = 1.0; // Alpha channel + } + // Calculate normal map in the vertex shader + vec4 normalMapColor = texture(normalMap, texCoord); + vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(normal, mapNormal); - float calculateLighting(vec3 normal, vec3 lightDir) { - float diff = max(dot(normal, lightDir), 0.0); - return diff; + // Pass perturbed normal to the fragment shader + color.rgb = vec3(perturbedNormal); + color.a = 1.0; // Alpha channel } + } - vec4 textureColor(vec2 coord) { - return texture2D(textureSampler, coord); + fragment { + input vec4 color; // Perturbed normal + input vec2 passTexCoord; + + + // Simulated textures and lighting parameters + input vec3 diffuseTexture; + input vec3 specularTexture; + input vec3 lightPos; + input vec3 viewPos; + input vec3 ambientColor; + input float shininess; + input float specularIntensity; + output vec4 fragColor; + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; } void main() { - vec3 normal = normalize(vec3(0.0, 0.0, 1.0)); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); - float lightIntensity = calculateLighting(normal, lightDir); - - vec4 color = textureColor(texCoord); - color *= lightIntensity; // Corrected error: multiplying RGB components + vec4 diffuseColor = texture(diffuseTexture, passTexCoord); + vec4 specularColor = texture(specularTexture, passTexCoord); + + vec3 perturbedNormal = normalize(color.rgb); + vec3 lightDir = normalize(lightPos - position); + vec3 viewDir = normalize(viewPos - position); + + // Ambient component + vec3 ambient = ambientColor; + + // Diffuse component + float diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * diffuseColor.rgb; + + // Specular component + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + + vec3 lighting = ambient + diffuse + specular; + vec4 color = vec4(lighting, 1.0) * diffuseColor; fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Depth Fog.cgl b/samples/Depth Fog.cgl index f4d2635..92facb9 100644 --- a/samples/Depth Fog.cgl +++ b/samples/Depth Fog.cgl @@ -1,30 +1,37 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec2 fragTexCoord; + vertex { + input vec3 position; + input vec3 normal; + input vec2 texCoord; + output vec2 passTexCoord; + output float passDepth; - void main() { - fragPosition = position; - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + passDepth = length(position); + } } -} -fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + input float passDepth; + // Simulated input values + input float texture; + input float fogDensity; + input vec3 fogColor; + output vec4 fragColor; + // Simulated texture function + vec4 get_texture(float tex, vec2 coord) { + return vec4(coord.x, coord.y, 0.5, 1.0); + } - void main() { - vec3 baseColor = texture(texture, fragTexCoord); - float depth = length(fragPosition); - float fogFactor = exp(-fogDensity * depth); - vec3 finalColor = mix(fogColor, baseColor, fogFactor); + void main() { + vec3 baseColor = get_texture(texture, passTexCoord); + float fogFactor = exp(-fogDensity * passDepth); + vec3 finalColor = mix(fogColor, baseColor.rgb, fogFactor); - fragColor = vec4(finalColor, 1.0); + fragColor = vec4(finalColor, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Depth of Field.cgl b/samples/Depth of Field.cgl index eb7d641..4badaaf 100644 --- a/samples/Depth of Field.cgl +++ b/samples/Depth of Field.cgl @@ -1,31 +1,54 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + + + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input vec4 depthTextureData; // Example for a 512x512 depth texture + input int textureWidth; + input int textureHeight; + input float focusDepth; + input float focusRange; + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - void main() { - float depth = texture(depthTexture, fragCoord); - float blur = clamp(abs(depth - focusDepth) / focusRange, 0.0, 1.0); + void main() { + vec2 texCoord = passTexCoord; // Extract the texture coordinate + float depth = texture(depthTextureData, texCoord, textureWidth, textureHeight); + float blur = clamp(abs(depth.r - focusDepth) / focusRange, 0.0, 1.0); - vec4 color = vec4(0.0); - for (int i = -4; i < 4; i++) { - for (int j = -4; j < 4; j++) { - vec2 offset = vec2(i, j) * blur / 512.0; - color += texture(texture, fragCoord + offset); + vec4 colorAccum = vec4(0.0); + int blurRadius = 4; + for (int i = -blurRadius; i <= blurRadius; i=i+1) { + for (int j = -blurRadius; j <= blurRadius; j=j+1) { + vec2 offset = vec2(float(i), float(j)) * blur / float(textureWidth); + colorAccum += texture(textureData, texCoord + offset, textureWidth, textureHeight); + } } + colorAccum /= float((2 * blurRadius + 1) * (2 * blurRadius + 1)); + fragColor = colorAccum; } - color /= 81.0; - fragColor = color; } } -} \ No newline at end of file diff --git a/samples/Displacement Mapping.cgl b/samples/Displacement Mapping.cgl index 251afe6..673bf1a 100644 --- a/samples/Displacement Mapping.cgl +++ b/samples/Displacement Mapping.cgl @@ -1,22 +1,45 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + + + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input vec4 displacementMapData; // Example for a 512x512 displacement map + input int textureWidth; + input int textureHeight; + input float displacementFactor; + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - void main() { - vec2 displacement = texture(displacementMap, fragTexCoord) * 2.0 - 1.0; - vec2 uv = fragTexCoord + displacement * displacementFactor; - vec4 color = texture(texture, uv); - fragColor = vec4(color, 1.0); + void main() { + vec2 texCoord = passTexCoord; + vec2 displacement = texture(displacementMapData, texCoord, textureWidth, textureHeight); + displacement = displacement.rg * 2.0 - 1.0; + vec2 uv = texCoord + displacement * displacementFactor; + vec4 color = texture(textureData, uv, textureWidth, textureHeight); + fragColor = vec4(color.rgb, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Dynamic Day-Night Cycle.cgl b/samples/Dynamic Day-Night Cycle.cgl index 37aa01f..df71e9f 100644 --- a/samples/Dynamic Day-Night Cycle.cgl +++ b/samples/Dynamic Day-Night Cycle.cgl @@ -1,20 +1,25 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; - output vec4 fragColor; + output vec4 color; vec3 dayNightColor(float timeOfDay) { return mix(vec3(0.0, 0.5, 1.0), vec3(0.0, 0.0, 0.2), timeOfDay); } void main() { - float timeOfDay = 0.0; // Placeholder for time of day calculation - vec3 color = dayNightColor(timeOfDay); - fragColor = vec4(color, 1.0); + float timeOfDay = mod(iTime / 24.0, 1.0); + vec3 colorComputed = dayNightColor(timeOfDay); + color = vec4(colorComputed, 1.0); + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } } diff --git a/samples/Dynamic Water Shader.cgl b/samples/Dynamic Water Shader.cgl index b3cefc8..d57297b 100644 --- a/samples/Dynamic Water Shader.cgl +++ b/samples/Dynamic Water Shader.cgl @@ -1,22 +1,26 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec2 position; - output vec4 fragColor; + output vec4 color; float wave(vec2 p, float time) { - return sin(p.x * 10.0) * 0.1 + sin(p.y * 10.0) * 0.1; + return sin(p.x * 10.0 + time * 2.0) * 0.1 + sin(p.y * 10.0 + time * 2.0) * 0.1; } - + void main() { vec2 uv = position; - float waterHeight = wave(uv, 0.0); // Placeholder for time value - vec3 color = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); + float waterHeight = wave(uv, iTime); + vec3 colorComputed = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); + color = vec4(colorComputed, 1.0); + } + } - fragColor = vec4(color, 1.0); + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Edge Detection.cgl b/samples/Edge Detection.cgl index 1700e2a..84fda7a 100644 --- a/samples/Edge Detection.cgl +++ b/samples/Edge Detection.cgl @@ -1,27 +1,45 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - // Pass through the texCoord as fragCoord for simplicity - void main() { - fragCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input int textureWidth; + input int textureHeight; + output vec4 fragColor; - void main() { - vec2 texCoord = fragCoord; - vec2 offset = vec2(1.0 / 512.0, 1.0 / 512.0); - vec3 color = texture(texture, texCoord); - vec3 colorRight = texture(texture, texCoord + vec2(offset.x, 0.0)); - vec3 colorUp = texture(texture, texCoord + vec2(0.0, offset.y)); + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - vec3 edge = abs(color - colorRight) + abs(color - colorUp); - fragColor = vec4(edge, 1.0); + void main() { + vec2 texCoord = passTexCoord; // Extract the texture coordinate + vec2 offset = vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight)); + vec3 color = texture(textureData, texCoord, textureWidth, textureHeight); + vec3 colorRight = texture(textureData, texCoord + vec2(offset.x, 0.0), textureWidth, textureHeight); + vec3 colorUp = texture(textureData, texCoord + vec2(0.0, offset.y), textureWidth, textureHeight); + color = color.rgb; + colorRight = colorRight.rgb; + colorUp = colorUp.rgb; + vec3 edge = abs(color - colorRight) + abs(color - colorUp); + fragColor = vec4(edge, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Emboss Effect.cgl b/samples/Emboss Effect.cgl index 08f620c..84fda7a 100644 --- a/samples/Emboss Effect.cgl +++ b/samples/Emboss Effect.cgl @@ -1,39 +1,45 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + void main() { + passTexCoord = texCoord; + } } -} - -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - vec2 texOffset = vec2(1.0 / 512.0); - vec3 center = texture(texture, fragTexCoord); - vec3 north = texture(texture, fragTexCoord + vec2(0.0, texOffset)); - vec3 south = texture(texture, fragTexCoord - vec2(0.0, texOffset)); - vec3 east = texture(texture, fragTexCoord + vec2(texOffset, 0.0)); - vec3 west = texture(texture, fragTexCoord - vec2(texOffset, 0.0)); - vec3 northwest = texture(texture, fragTexCoord + vec2(-texOffset, texOffset)); - vec3 northeast = texture(texture, fragTexCoord + vec2(texOffset, texOffset)); - vec3 southwest = texture(texture, fragTexCoord + vec2(-texOffset, -texOffset)); - vec3 southeast = texture(texture, fragTexCoord + vec2(texOffset, -texOffset)); + fragment { + input vec2 passTexCoord; + // Simulated input values + input vec4 textureData; // Example for a 512x512 texture + input int textureWidth; + input int textureHeight; + output vec4 fragColor; - vec3 color = center * -1.0 + - north * -1.0 + - south * -1.0 + - east * -1.0 + - west * -1.0 + - northwest * 1.0 + - northeast * 1.0 + - southwest * 1.0 + - southeast * 1.0; + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } - fragColor = vec4(color + vec3(0.5), 1.0); - } \ No newline at end of file + void main() { + vec2 texCoord = passTexCoord; // Extract the texture coordinate + vec2 offset = vec2(1.0 / float(textureWidth), 1.0 / float(textureHeight)); + vec3 color = texture(textureData, texCoord, textureWidth, textureHeight); + vec3 colorRight = texture(textureData, texCoord + vec2(offset.x, 0.0), textureWidth, textureHeight); + vec3 colorUp = texture(textureData, texCoord + vec2(0.0, offset.y), textureWidth, textureHeight); + color = color.rgb; + colorRight = colorRight.rgb; + colorUp = colorUp.rgb; + vec3 edge = abs(color - colorRight) + abs(color - colorUp); + fragColor = vec4(edge, 1.0); + } + } +} diff --git a/samples/Flat Shading.cgl b/samples/Flat Shading.cgl index 69508a0..629079e 100644 --- a/samples/Flat Shading.cgl +++ b/samples/Flat Shading.cgl @@ -1,24 +1,21 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragNormal; + vertex { + input vec3 position; + input vec3 normal; + output vec3 passNormal; - void main() { - fragNormal = normal; + void main() { + passNormal = normal; + } } -} -fragment { - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec3 passNormal; + output vec4 fragColor; - void main() { - // Base color - vec3 baseColor = vec3(0.5, 0.5, 0.5); - // Adjust color based on normal - vec3 color = baseColor + 0.5 * fragNormal; - fragColor = vec4(color, 1.0); + void main() { + vec3 color = vec3(0.5, 0.5, 0.5) + 0.5 * passNormal; + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Fluid Dynamics Simulation.cgl b/samples/Fluid Dynamics Simulation.cgl index 0917ce1..acfb3c7 100644 --- a/samples/Fluid Dynamics Simulation.cgl +++ b/samples/Fluid Dynamics Simulation.cgl @@ -1,20 +1,25 @@ shader main { vertex { - // Vertex shader logic goes here. - } - - fragment { input vec3 position; - output vec4 fragColor; + output vec4 color; float fluidMotion(vec3 p) { - return sin(p.x * 10.0) * 0.5 + 0.5; + return sin(p.x * 10.0 + iTime) * 0.5 + 0.5; } void main() { float motion = fluidMotion(position); - vec3 color = vec3(0.0, 0.0, 1.0) * motion; - fragColor = vec4(color, 1.0); + vec3 colorComputed = vec3(0.0, 0.0, 1.0) * motion; + color = vec4(colorComputed, 1.0); + } + } + + fragment { + input vec4 color; + output vec4 fragColor; + + void main() { + fragColor = color; } } -} \ No newline at end of file +} diff --git a/samples/Fog Effect.cgl b/samples/Fog Effect.cgl index 37f0c9e..c413250 100644 --- a/samples/Fog Effect.cgl +++ b/samples/Fog Effect.cgl @@ -1,23 +1,28 @@ shader main { -vertex { - input vec3 position; - output vec3 fragPosition; + vertex { + input vec3 position; + output vec3 passPosition; - // Pass through the position to the fragment shader - void main() { - fragPosition = position; + // Pass through the position + void main() { + passPosition = position; + } } -} -fragment { - input vec3 fragPosition; - output vec4 fragColor; + fragment { + input vec3 passPosition; // This contains the position + + input vec3 cameraPos; // Simulated uniform + input vec3 fogColor; // Simulated uniform + input float fogDensity; // Simulated uniform + output vec4 fragColor; - void main() { - float distance = length(fragPosition - cameraPos); - float fogFactor = exp(-pow(distance * fogDensity, 2.0)); - vec3 color = mix(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); - fragColor = vec4(color, 1.0); + void main() { + vec3 position = passPosition; // Extract the position + float distance = length(position - cameraPos); + float fogFactor = exp(-pow(distance * fogDensity, 2.0)); + vec3 finalColor = mix(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); + fragColor = vec4(finalColor, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Fresnel Effect.cgl b/samples/Fresnel Effect.cgl index a9dc6e7..25c6300 100644 --- a/samples/Fresnel Effect.cgl +++ b/samples/Fresnel Effect.cgl @@ -1,28 +1,39 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vertex { + input vec3 position; + input vec3 normal; + input vec3 viewDir; // Simulated uniform - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} + output vec3 passNormal; + output vec3 passViewDir; -fragment { - input vec3 normal; - output vec4 fragColor; + + float fresnelEffect(vec3 normal, vec3 viewDir) { + return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + } - float fresnelEffect(vec3 normal, vec3 viewDir) { - return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + void main() { + passNormal = normal; + passViewDir = viewDir; + } } - void main() { - vec3 norm = normalize(normal); - vec3 viewDir = vec3(0.0, 0.0, 1.0); // Example view direction - float fresnel = fresnelEffect(norm, normalize(viewDir)); - vec3 color = vec3(0.1, 0.5, 1.0) * fresnel; + fragment { + input vec3 passNormal; + input vec3 passViewDir; + output vec4 fragColor; + + float fresnelEffect(vec3 normal, vec3 viewDir) { + return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + } + + void main() { + vec3 norm = normalize(passNormal); + vec3 viewDir = normalize(passViewDir); + float fresnel = fresnelEffect(norm, viewDir); + vec3 color = vec3(0.1, 0.5, 1.0) * fresnel; - fragColor = vec4(color, 1.0); + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Glitch Effect.cgl b/samples/Glitch Effect.cgl index 86fb1b5..f878c64 100644 --- a/samples/Glitch Effect.cgl +++ b/samples/Glitch Effect.cgl @@ -1,25 +1,49 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + // Pass through the texture coordinate + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + + + // Simulated inputs + input vec4 textureData; // Example for a 512x512 texture + input int textureWidth; + input int textureHeight; + input float time; // Simulated uniform + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, int width, int height) { + // Convert coord to texture indices + int x = int(coord.x * float(width)); + int y = int(coord.y * float(height)); + // Clamp the indices to texture dimensions + x = clamp(x, 0, width - 1); + y = clamp(y, 0, height - 1); + // Calculate the 1D array index + int index = y * width + x; + // Return the color from the texture data + return texData; + } + + void main() { + vec2 uv = passTexCoord; - void main() { - vec2 uv = fragTexCoord; + // Apply the time-based distortion + if (fract(time * 10.0) < 0.5) { + uv.x += sin(passTexCoord.y * 10.0) * 0.1; + } - if (fract(time * 10.0) < 0.5) { - uv += sin(fragTexCoord * 10.0) * 0.1; + // Sample the texture using the simulated texture function + vec4 color = texture(textureData, uv, textureWidth, textureHeight); + fragColor = color; } - vec4 color = texture(texture, uv); - fragColor = color; } } -} \ No newline at end of file diff --git a/samples/Gouraud Shading.cgl b/samples/Gouraud Shading.cgl index 48c41fd..962dd81 100644 --- a/samples/Gouraud Shading.cgl +++ b/samples/Gouraud Shading.cgl @@ -1,28 +1,32 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + vertex { + input vec3 position; + input vec3 normal; + // Simulated light position + input vec3 lightPos; + output vec3 passPosition; + output vec3 passNormal; + output vec3 passLightPos; - void main() { - fragPosition = position; - fragNormal = normal; + void main() { + passPosition = position; + passNormal = normal; + passLightPos = lightPos; + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec3 passPosition; + input vec3 passNormal; + input vec3 passLightPos; + output vec4 fragColor; - void main() { - vec3 norm = normalize(fragNormal); - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Hardcoded light position - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = vec3(0.5, 0.5, 0.5) * diff; // Diffuse color - fragColor = vec4(color, 1.0); + void main() { + vec3 norm = normalize(passNormal); + vec3 lightDir = normalize(passLightPos - passPosition); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff; + fragColor = vec4(color, 1.0); + } } } -} diff --git a/samples/Gradient Background.cgl b/samples/Gradient Background.cgl index 9d06de1..0ab6bc7 100644 --- a/samples/Gradient Background.cgl +++ b/samples/Gradient Background.cgl @@ -1,20 +1,27 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated topColor and bottomColor + input vec3 topColor; + input vec3 bottomColor; + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - vec3 color = mix(bottomColor, topColor, fragTexCoord.y); - fragColor = vec4(color, 1.0); + fragment { + input vec2 passTexCoord; + // Simulated topColor and bottomColor + input vec3 topColor; + input vec3 bottomColor; + output vec4 fragColor; + + void main() { + vec3 color = mix(bottomColor, topColor, passTexCoord.y); + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Halftone Effect.cgl b/samples/Halftone Effect.cgl index 49096e4..95fadfe 100644 --- a/samples/Halftone Effect.cgl +++ b/samples/Halftone Effect.cgl @@ -1,24 +1,58 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated inputs + input vec2 textureSize; // Size of the texture (width, height) + input float dotSize; + input vec4 textureData; // Example for a 512x512 texture + output vec2 passTexCoord; - void main() { - fragTexCoord = texCoord; + + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - float angle = mod(uv + uv, 2.0) * 3.14159265; - float radius = length(fract(uv) - vec2(0.5)); - vec4 color = texture(texture, fragTexCoord); - float itensity = dot(color, vec3(0.299, 0.587, 0.114)); - float dot = smoothstep(0.4, 0.5, sin(angle) * 0.5 + radius); - fragColor = vec4(vec3(dot * itensity), 1.0); + fragment { + input vec2 passTexCoord; + // Simulated inputs + input vec2 textureSize; + input float dotSize; + input vec4 textureData; // Example for a 512x512 texture + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } + + void main() { + // Scale texture coordinates by dotSize + vec2 uv = passTexCoord * dotSize; + + // Calculate angle and radius for the dot pattern + float angle = mod(uv.x + uv.y, 2.0) * 3.14159265; + float radius = length(fract(uv) - vec2(0.5)); + + // Sample color from texture using the simulated texture function + vec4 color = texture(textureData, passTexCoord, textureSize); + + // Calculate intensity based on luminance + float intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); + + // Compute dot pattern using smoothstep for edge softness + float dot = smoothstep(0.4, 0.5, sin(angle) * 0.5 + radius); + + // Set fragment color with dot pattern applied + fragColor = vec4(vec3(dot * intensity), 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Hatching shader.cgl b/samples/Hatching shader.cgl index 0aff9c6..680c000 100644 --- a/samples/Hatching shader.cgl +++ b/samples/Hatching shader.cgl @@ -1,36 +1,66 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated thresholds + input float threshold1; + input float threshold2; + input float threshold3; + - void main() { - fragTexCoord = texCoord; + + // Simulated texture data + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec2 passTexCoord; + + void main() { + passTexCoord = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 passTexCoord; + - void main() { - vec4 color = texture(texture, fragTexCoord); - float itensity = dot(color, vec3(0.299, 0.587, 0.114)); + // Simulated thresholds + input float threshold1; + input float threshold2; + input float threshold3; - vec3 hatching = vec3(1.0); - if (itensity < threshold1) { - hatching = vec3(0.8); - } - if (itensity < threshold2) { - hatching = vec3(0.6); - } - if (itensity < threshold3) { - hatching = vec3(0.4); - } - else { - hatching = vec3(0.2); + // Simulated texture data + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; } - fragColor = vec4(hatching, 1.0); + void main() { + // Sample color from texture using the simulated texture function + vec4 color = texture(textureData, passTexCoord, textureSize); + + // Calculate intensity based on luminance + float intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); + + // Determine hatching color based on thresholds + vec3 hatching = vec3(1.0); + if (intensity < threshold1) {hatching = vec3(0.8);} + if (intensity < threshold2) {hatching = vec3(0.6);} + if (intensity < threshold3) {hatching = vec3(0.4);} + else {hatching = vec3(0.2);} + + // Set the fragment color + fragColor = vec4(hatching, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Heat Haze.cgl b/samples/Heat Haze.cgl index 7b65273..f470b95 100644 --- a/samples/Heat Haze.cgl +++ b/samples/Heat Haze.cgl @@ -1,22 +1,47 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + // Simulated input for time and texture data + input float time; + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec2 texCoordOut; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + + void main() { + texCoordOut = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 texCoordOut; + + // Simulated input for time and texture data + input float time; + input vec4 textureData; // Example for a 512x512 texture + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } + + void main() { + vec2 uv = texCoordOut + sin(texCoordOut.y * 10.0 + time) * 0.01; + + // Sample color from texture using the simulated texture function + vec4 color = texture(textureData, uv, textureSize); - void main() { - vec2 uv = fragTexCoord + sin(fragTexCoord.y * 10.0 + time) * 0.01; - vec4 color = texture(texture, uv); - fragColor = vec4(color.rgb, 1.0); + fragColor = vec4(color.rgb, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Kaleidoscope Effect.cgl b/samples/Kaleidoscope Effect.cgl index fd7aede..41006b5 100644 --- a/samples/Kaleidoscope Effect.cgl +++ b/samples/Kaleidoscope Effect.cgl @@ -1,24 +1,59 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + vertex { + input vec2 texCoord; + + // Simulated input for texture data and segments + input vec4 textureData; // Example for a 512x512 texture + input float segments; + input vec2 textureSize; // Size of the texture (width, height) + output vec2 texCoordOut; - void main() { - fragTexCoord = texCoord; + + void main() { + texCoordOut = texCoord; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + input vec2 texCoordOut; + + // Simulated input for texture data and segments + input vec4 textureData; // Example for a 512x512 texture + input float segments; + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } - void main() { - float angle = atan(fragTexCoord.y - 0.5, fragTexCoord.x - 0.5); - float radius = length(fragTexCoord - vec2(0.5)); - float segment = floor(segments * angle / (2.0 * 3.14159265)); - float newAngle = segment * 2.0 * 3.14159265 / segments; - vec2 newTexCoord = vec2(0.5) + radius * vec2(cos(newAngle), sin(newAngle)); - fragColor = texture(texture, newTexCoord); + void main() { + // Compute the angle of the current texCoord relative to the center + float angle = atan(texCoordOut.y - 0.5, texCoordOut.x - 0.5); + + // Compute the radius from the center of the texture + float radius = length(texCoordOut - vec2(0.5)); + + // Determine the segment index based on the angle and number of segments + float segment = floor(segments * angle / (2.0 * 3.14159265)); + + // Compute the new angle corresponding to the segment + float newAngle = segment * 2.0 * 3.14159265 / segments; + + // Compute the new texture coordinates based on the new angle + vec2 newTexCoord = vec2(0.5) + radius * vec2(cos(newAngle), sin(newAngle)); + + // Sample the texture at the new texture coordinates + fragColor = texture(textureData, newTexCoord, textureSize); + } } } -} \ No newline at end of file diff --git a/samples/Lambertian Shading.cgl b/samples/Lambertian Shading.cgl index 5cf9a10..4690a5c 100644 --- a/samples/Lambertian Shading.cgl +++ b/samples/Lambertian Shading.cgl @@ -1,28 +1,35 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + vertex { + input vec3 position; + input vec3 normal; + // Simulated input for light position + input vec3 lightPos; + output vec4 color; - void main() { - fragPosition = position; - fragNormal = normal; + + void main() { + // Pass the position and normal to the fragment shader + color.xyz = position; + color.w = 1.0; // Alpha channel + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the position + input vec3 normal; // Pass the normal directly + // Simulated input for light position + input vec3 lightPos; + output vec4 fragColor; + + - void main() { - vec3 norm = normalize(fragNormal); - vec3 lightPos = vec3(1.0, 1.0, 1.0); // Hardcoded light position - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 color = vec3(0.5, 0.5, 0.5) * diff; - fragColor = vec4(color, 1.0); + void main() { + vec3 pos = color.xyz; // Extract position + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - pos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) * diff; + fragColor = vec4(color, 1.0); + } } } -} \ No newline at end of file diff --git a/samples/Light Shaft Effect.cgl b/samples/Light Shaft Effect.cgl index 827a472..e148501 100644 --- a/samples/Light Shaft Effect.cgl +++ b/samples/Light Shaft Effect.cgl @@ -1,27 +1,55 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; - - // Pass through position and normal to the fragment shader - void main() { - fragPosition = position; - fragNormal = normal; + vertex { + input vec2 texCoord; + + // Simulated inputs for texture data and light position + input vec4 textureData; // Example for a 512x512 texture + input vec2 lightPos; // Simulated light position + input vec2 textureSize; // Size of the texture (width, height) + output vec2 texCoordOut; + + void main() { + texCoordOut = texCoord; + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec2 texCoordOut; + + // Simulated inputs for texture data and light position + input vec4 textureData; // Example for a 512x512 texture + input vec2 lightPos; // Simulated light position + input vec2 textureSize; // Size of the texture (width, height) + output vec4 fragColor; + + vec4 texture(vec4 texData, vec2 coord, vec2 size) { + // Convert coord to texture indices + int x = int(coord.x * size.x); + int y = int(coord.y * size.y); + // Clamp the indices to texture dimensions + x = clamp(x, 0, int(size.x) - 1); + y = clamp(y, 0, int(size.y) - 1); + // Calculate the 1D array index + int index = y * int(size.x) + x; + // Return the color from the texture data + return texData; + } - void main() { - vec3 viewDir = normalize(viewPos - fragPosition); - float rim = pow(1.0 - max(dot(fragNormal, viewDir), 0.0), rimPower); - vec3 color = vec3(0.5, 0.5, 0.5) + rim * rimColor; - fragColor = vec4(color, 1.0); + void main() { + vec2 uv = texCoordOut; + vec4 color = texture(textureData, uv, textureSize); + vec2 dir = uv - lightPos; + float dist = length(dir); + + // Compute offset based on distance + vec2 offset = dir * (1.0 - dist) / textureSize; // Normalize by textureSize + vec2 uvOffset = uv - offset; + + // Sample the texture at the offset position + vec4 shaftColor = texture(textureData, uvOffset, textureSize); + + // Mix the original color with the shaft color + fragColor = mix(color, shaftColor, 0.5); + } } } -} diff --git a/samples/Metallic Shader.cgl b/samples/Metallic Shader.cgl index 7c35fef..1e7732b 100644 --- a/samples/Metallic Shader.cgl +++ b/samples/Metallic Shader.cgl @@ -1,34 +1,37 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vertex { + input vec3 position; + input vec3 normal; + // Simulated inputs + input vec3 lightPos; + input vec3 viewPos; + input float metallic; + output vec4 color; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} - -fragment { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambient = 0.1; + float diff = max(dot(normal, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + return ambient + diff + metallic * spec; + } - vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { - float ambient = 0.1; - float diff = max(dot(normal, lightDir), 0.0); - vec3 reflectDir = reflect(-lightDir, normal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - return ambient + diff + 0.5 * spec; // Assuming metallic is set to 0.5 + void main() { + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - position); + vec3 viewDir = normalize(viewPos - position); + vec3 lighting = calculateLighting(norm, lightDir, viewDir); + color.rgb = vec3(1.0, 0.8, 0.6) * lighting; + color.a = 1.0; // Alpha channel + } } - void main() { - vec3 norm = normalize(normal); - vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - position); // Example light position - vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - position); // Example view position - vec3 lighting = calculateLighting(norm, lightDir, viewDir); - vec3 color = vec3(1.0, 0.8, 0.6) * lighting; + fragment { + input vec4 color; + output vec4 fragColor; - fragColor = vec4(color, 1.0); + void main() { + fragColor = color; + } } } -} \ No newline at end of file diff --git a/samples/Motion Blur.cgl b/samples/Motion Blur.cgl index 4f0c143..61e32f2 100644 --- a/samples/Motion Blur.cgl +++ b/samples/Motion Blur.cgl @@ -1,24 +1,36 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + vertex { + input vec2 texCoord; + output vec4 color; - void main() { - fragCoord = texCoord; + // Pass through the texture coordinate + void main() { + color = vec4(texCoord, 0.0, 1.0); + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the texture coordinate + // Simulated inputs + input vec2 motionDirection; + input float blurAmount; + input float texture; // Simulated texture + output vec4 fragColor; + + vec4 sampleTexture(vec2 coord) { + // Simulate texture sampling (for demonstration purposes) + return vec4(texture, coord); + } void main() { - - for (int i = -4; i < 4; i++) { - color += texture(texture, fragCoord * motionDirection * blurAmount / 10.0); + vec2 texCoord = color.xy; // Extract the texture coordinate + vec4 colorAccum = vec4(0.0); + for (int i = -4; i <= 4; i=i+1) { + vec2 offset = float(i) * motionDirection * blurAmount / 10.0; + colorAccum += sampleTexture(texCoord + offset); + } + colorAccum /= 9.0; + fragColor = colorAccum; } - color /= 9.0; - fragColor = color; } } -} \ No newline at end of file diff --git a/samples/Nested Loop Shader.cgl b/samples/Nested Loop Shader.cgl index 00cc5c9..ca3e7cf 100644 --- a/samples/Nested Loop Shader.cgl +++ b/samples/Nested Loop Shader.cgl @@ -3,29 +3,62 @@ shader nestedLoopsShader { input vec3 position; input vec2 texCoord; input vec3 normal; - output vec3 fragPosition; - output vec2 fragTexCoord; - output vec3 fragNormal; + output vec4 color; + // Pass the texture coordinates and normal to the fragment shader void main() { - fragPosition = position; - fragTexCoord = texCoord; - fragNormal = normal; + color.xy = texCoord; + color.zw = normal; + color.a = 1.0; // Alpha channel } } fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - input vec3 fragNormal; + input vec4 color; // Contains the texture coordinates and normal + input vec2 texCoord; // Texture coordinates from vertex shader + + + // Simulated inputs + input vec2 texture; // Simulated texture + input vec3 lightPos; + input vec3 viewPos; + input vec3 ambientColor; + input float shininess; + input float specularIntensity; + input int outerIterations; + input int innerIterations; output vec4 fragColor; + vec4 sampleTexture(vec2 coord) { + // Simulate texture sampling (for demonstration purposes) + return vec4(texture, coord); + } + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; + } + void main() { - - for (int i = 0; i < 0; i++) { - for (int j = 0; j < 0; j++) { - vec3 reflectDir = reflect(-lightDir, fragNormal); - } + vec4 texColor = sampleTexture(texCoord); + vec3 lightDir = normalize(lightPos - color.xyz); + vec3 viewDir = normalize(viewPos - color.xyz); + + vec3 ambient = ambientColor; + vec3 diffuse = texColor.rgb; + vec3 specular = vec3(0.0); + + // Outer loop + for (int i = 0; i < outerIterations; i=i+1) { + // Inner loop + for (int j = 0; j < innerIterations; j=j+1) { + // Compute lighting + vec3 tempSpecular = calculateSpecular(color.zw, lightDir, viewDir); + specular += tempSpecular; + } + + // Reduce diffuse intensity progressively diffuse *= 0.9; } @@ -33,4 +66,4 @@ shader nestedLoopsShader { fragColor = vec4(lighting, 1.0); } } -} \ No newline at end of file +} diff --git a/samples/Normal Mapping.cgl b/samples/Normal Mapping.cgl index 553d259..840ecbd 100644 --- a/samples/Normal Mapping.cgl +++ b/samples/Normal Mapping.cgl @@ -1,34 +1,44 @@ -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; +shader main { + vertex { + input vec3 position; + input vec3 normal; + output vec4 color; // To pass the normal and position - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; + + void main() { + // Define constants for light and view positions + vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position + vec3 viewPos = vec3(0.0, 0.0, 5.0); // Example view position + float shininess = 32.0; + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - position); // Direction of light + vec3 viewDir = normalize(viewPos - position); // Direction of view + + // Calculate lighting + float ambient = 0.1; + float diff = max(dot(norm, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, norm); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + + // Combine lighting components + color.rgb = ambient + diff + spec; + color.a = 1.0; // Alpha channel + } } -} -fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + input vec4 color; // This contains the lighting information + input vec2 texCoord; + output vec4 fragColor; + + - - void main() { - vec3 norm = normalize(texture(normalMap, fragTexCoord) * 2.0 - 1.0); - vec3 lightDir = normalize(lightPos - fragPosition); - float diff = max(dot(norm, lightDir), 0.0); - vec3 reflectDir = reflect(-lightDir, norm); - vec3 viewDir = normalize(viewPos - fragPosition); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); - vec3 color = texture(texture, fragTexCoord); - fragColor = vec4((0.1 + diff + spec) * color, 1.0); + void main() { + // Sample texture (assuming `texture` is a built-in function in this environment) + vec4 texColor = texture(texture, texCoord); // Sample texture using coordinates + vec3 ambient = vec3(0.1, 0.1, 0.1); // Hardcoded ambient color + vec3 finalColor = ambient + color.rgb * texColor.rgb; // Combine ambient and texture color + fragColor = vec4(finalColor, 1.0); // Final output color + } } } -} \ No newline at end of file