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