diff --git a/samples/Adv complexShader.cgl b/samples/Adv complexShader.cgl index dafe8bb..4eb0c5c 100644 --- a/samples/Adv complexShader.cgl +++ b/samples/Adv complexShader.cgl @@ -1,47 +1,98 @@ shader complexShader { + + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + vec3 normal @NORMAL; + }; + + struct VSOutput { + vec4 position @SV_POSITION; + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + vec2 texCoord @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec3 ambientColor; + vec3 lightPos; + vec3 viewPos; + vec2 shininess; + vec2 specularIntensity; + int iterations; +} + +sampler2D normalMap; +sampler2D diffuseTexture; +sampler2D specularTexture; + + + vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { + return normalize(normal * mapNormal); + } + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + vec2 spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; + } + vertex { - input vec3 position; - input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; - - void main() { - fragTexCoord = texCoord; - fragPosition = position; // Directly pass position to fragment shader - gl_Position = vec4(position, 1.0); + VSOutput main(VSInput input) { + VSOutput output; + + // Pass position to the fragment shader + output.position = vec4(input.position, 1.0); + + // Pass texture coordinates and normals to the fragment shader + output.color.xy = input.texCoord; + output.color.zw = input.normal; + + // Calculate normal map in the vertex shader + vec4 normalMapColor = texture(normalMap, input.texCoord); + vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(input.normal, mapNormal); + + // Pass perturbed normal to the fragment shader + output.color.rgb = perturbedNormal; + output.color.a = 1.0; // Alpha channel + + return output; } } fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - output vec4 fragColor; + vec4 main(PSInput input) @SV_TARGET { + vec4 diffuseColor = texture(diffuseTexture, input.texCoord); + vec4 specularColor = texture(specularTexture, input.texCoord); - 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 + vec3 perturbedNormal = normalize(input.color.rgb); + vec3 lightDir = normalize(lightPos - input.color.rgb); + vec3 viewDir = normalize(viewPos - input.color.rgb); - float diff = max(dot(normal, lightDir), 0.0); - vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Default light color + // Ambient component + vec3 ambient = ambientColor; - 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 + // Diffuse component + vec2 diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * diffuseColor.rgb; - return ambient + diffuse + specular; - } + // Specular component + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); - 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 + // Loop to modify lighting based on iterations + vec3 lighting = ambient + diffuse + specular; + for (int i = 0; i < iterations; i++) { + lighting *= 0.9; + } - vec3 lighting = calculateLighting(normal, lightDir, viewDir); - vec4 color = vec4(lighting, 1.0) * texColor; + // Ternary operator to conditionally modify color + vec4 outputColor = (diff > 0.5) ? vec4(lighting, 1.0) * diffuseColor : vec4(lighting * 0.5, 1.0) * diffuseColor; - fragColor = color; + return outputColor; } } } \ No newline at end of file diff --git a/samples/Advanced Post-Processing Effects.cgl b/samples/Advanced Post-Processing Effects.cgl index 7ad3a1d..80c0c0b 100644 --- a/samples/Advanced Post-Processing Effects.cgl +++ b/samples/Advanced Post-Processing Effects.cgl @@ -1,17 +1,38 @@ shader main { + + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct VSOutput { + vec4 color @COLOR; + }; + + sampler2D iChannel0; + vertex { - // Vertex shader logic goes here. + VSOutput main(VSInput input) { + VSOutput output; + + // Pass through texture coordinates as color + output.color = vec4(input.texCoord, 0.0, 1.0); + + return output; + } } fragment { - input vec2 texCoord; - output vec4 fragColor; - - void main() { - float brightness = 0.0; // Placeholder for brightness calculation - float bloom = max(0.0, brightness - 0.5); - vec3 color = vec3(0.0); // Placeholder for texture color - fragColor = vec4(color + vec3(bloom), 1.0); + vec4 main(VSOutput input) @SV_TARGET { + // Sample brightness and calculate bloom + vec2 brightness = texture(iChannel0, input.color.xy).r; + vec2 bloom = max(0.0, brightness - 0.5); + + // Apply bloom to the texture color + vec3 texColor = texture(iChannel0, input.color.xy).rgb; + vec3 colorWithBloom = texColor + vec3(bloom); + + return vec4(colorWithBloom, 1.0); } } } \ No newline at end of file diff --git a/samples/Advanced Shader.cgl b/samples/Advanced Shader.cgl index ab68ef3..58b3aed 100644 --- a/samples/Advanced Shader.cgl +++ b/samples/Advanced Shader.cgl @@ -1,49 +1,66 @@ shader advancedShader { - vertex { - input vec3 position; - input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; - - void main() { - fragTexCoord = texCoord; - fragPosition = position; - gl_Position = vec4(position, 1.0); - } + + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct VSOutput { + vec4 color @COLOR; + }; + + cbuffer anyidentifier { + vec3 ambientColor; + vec3 lightPos; + vec3 viewPos; + vec2 shininess; + vec2 eyeoffset; // Other uniforms if necessary +} +sampler2D textureSampler; + + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + vec2 spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0); // White specular highlight } - 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), 32.0); // Default shininess - return spec * vec3(1.0, 1.0, 1.0); // White specular highlight - } + vertex { + VSOutput main(VSInput input) { + VSOutput output; - void main() { - vec4 texColor = texture2D(textureSampler, fragTexCoord); // Default sampler + // Pass texture coordinates to the fragment shader + output.color.xy = input.texCoord; - 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(input.position), dFdy(input.position))); + vec3 lightDir = normalize(lightPos - input.position); + vec3 viewDir = normalize(viewPos - input.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); + vec2 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); + // Combine lighting components vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * texColor; + output.color.rgb = lighting; + output.color.a = 1.0; // Alpha channel - fragColor = color; + return output; + } + } + + fragment { + vec4 main(VSOutput input) @SV_TARGET { + // Sample texture and apply lighting + vec4 texColor = texture(textureSampler, input.color.xy); + return vec4(input.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..a9f9d7d 100644 --- a/samples/Advanced calculateLighting.cgl +++ b/samples/Advanced calculateLighting.cgl @@ -1,61 +1,61 @@ 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), 32.0); // Default shininess - return spec * vec3(1.0, 1.0, 1.0) * 1.0; // Default specular intensity - } + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); - } + struct VSOutput { + vec4 color @COLOR; + }; + + cbuffer ShaderUniforms { + mat4 modelMatrix; + vec2 time; +} + +sampler2D textureSampler; - 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); + vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) { + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Light color - 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 + float diff = max(dot(normal, lightDir), 0.0); + vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Light color - // Ambient component - vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient 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); // Light color - // Diffuse component - float diff = max(dot(perturbedNormal, lightDir), 0.0); - vec3 diffuse = diff * diffuseColor.rgb; + return ambient + diffuse + specular; + } + + vertex { + VSOutput main(VSInput input) { + VSOutput output; - // Specular component - vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); + // Pass texture coordinates to the fragment shader + output.color.xy = input.texCoord; - vec3 lighting = ambient + diffuse + specular; - vec4 color = vec4(lighting, 1.0) * diffuseColor; + // Calculate normal in the vertex shader + vec3 normal = normalize(cross(dFdx(input.position), dFdy(input.position))); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); + vec3 viewDir = normalize(-input.position); - fragColor = color; + vec3 lighting = calculateLighting(normal, lightDir, viewDir); + output.color.rgb = lighting; + output.color.a = 1.0; // Alpha channel + + return output; } } -} + + fragment { + vec4 main(VSOutput input) @SV_TARGET { + vec4 texColor = texture(textureSampler, input.color.xy); + return vec4(input.color.rgb * texColor.rgb, 1.0); + } + } +} \ No newline at end of file diff --git a/samples/Anaglyph 3D.cgl b/samples/Anaglyph 3D.cgl index 99b196f..eac5e52 100644 --- a/samples/Anaglyph 3D.cgl +++ b/samples/Anaglyph 3D.cgl @@ -1,21 +1,33 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; - void main() { - fragTexCoord = texCoord; - } + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct VSOutput { + vec4 fragColor @SV_TARGET; + }; + + cbuffer ShaderUniforms { + vec2 eyeOffset; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; +sampler2D texture; + - void main() { - vec4 leftColor = texture(texture, fragTexCoord - eyeOffset); - vec4 rightColor = texture(texture, fragTexCoord + eyeOffset); - fragColor = vec4(leftColor, rightColor, rightColor, 1.0); + fragment { + VSOutput main(VSInput input) { + VSOutput output; + + // Sample from the texture with eye offset + vec4 leftColor = texture(texture, input.texCoord - eyeOffset); + vec4 rightColor = texture(texture, input.texCoord + eyeOffset); + + // Combine the left and right colors for stereoscopic effect + output.fragColor = vec4(leftColor.r, rightColor.g, rightColor.b, 1.0); + + return output; + } } -} } \ No newline at end of file diff --git a/samples/Atmospheric Scattering.cgl b/samples/Atmospheric Scattering.cgl index da6a9ad..54e68dc 100644 --- a/samples/Atmospheric Scattering.cgl +++ b/samples/Atmospheric Scattering.cgl @@ -1,20 +1,48 @@ shader main { + + struct VSInput { + vec3 position @POSITION; + vec3 position @POSITION; + }; + + struct VSOutput { + vec4 color @COLOR; + }; + vertex { - // Vertex shader logic goes here. + VSOutput main(VSInput input) { + VSOutput output; + + // Rayleigh scattering function + vec3 rayleighScattering(vec2 theta) { + return vec3(0.5, 0.7, 1.0) * pow(max(0.0, 1.0 - theta * theta), 3.0); + } + + // Compute the color based on Rayleigh scattering + vec2 theta = input.position.y; + vec3 colorComputed = rayleighScattering(theta); + output.color = vec4(colorComputed, 1.0); + + return output; + } } + struct PSInput { + vec4 color @COLOR; + }; + + struct PSOutput { + vec4 fragColor @SV_TARGET; + }; + fragment { - input vec3 position; - output vec4 fragColor; + PSOutput main(PSInput input) { + PSOutput output; - vec3 rayleighScattering(float theta) { - return vec3(0.5, 0.7, 1.0) * pow(max(0.0, 1.0 - theta * theta), 3.0); - } + // Pass the vertex shader color to the fragment shader output + output.fragColor = input.color; - void main() { - float theta = position.y; - vec3 color = rayleighScattering(theta); - fragColor = vec4(color, 1.0); + return output; } } } \ No newline at end of file diff --git a/samples/Basic Fog Shader.cgl b/samples/Basic Fog Shader.cgl index cecec25..273cf55 100644 --- a/samples/Basic Fog Shader.cgl +++ b/samples/Basic Fog Shader.cgl @@ -1,28 +1,43 @@ shader main { - vertex { - input vec3 position; - input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct VSOutput { + vec2 fragTexCoord @TEXCOORD0; + vec3 fragPosition @POSITION; + vec4 gl_Position @SV_POSITION; + }; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - gl_Position = vec4(position, 1.0); + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + vec3 fragPosition @POSITION; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.fragPosition = input.position; + output.gl_Position = vec4(input.position, 1.0); + return output; } } fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - output vec4 fragColor; + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec3 fogColor; + vec2 fogDensity; + vec4 fragColor; - void main() { - - float depth = length(fragPosition); - float fogFactor = exp(-fogDensity * depth); - vec3 finalColor = mix(fogColor, baseColor, fogFactor); + vec3 baseColor = tex2D(texture, input.fragTexCoord).rgb; + vec2 depth = length(input.fragPosition); + vec2 fogFactor = exp(-fogDensity * depth); + vec3 finalColor = lerp(fogColor, baseColor, fogFactor); fragColor = vec4(finalColor, 1.0); + return fragColor; } } -} +} \ No newline at end of file diff --git a/samples/Basic Reflection Shader.cgl b/samples/Basic Reflection Shader.cgl index 494ed7e..d172380 100644 --- a/samples/Basic Reflection Shader.cgl +++ b/samples/Basic Reflection Shader.cgl @@ -1,28 +1,45 @@ shader main { - vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + struct VSOutput { + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + }; - void main() { - fragPosition = position; - fragNormal = normal; - gl_Position = vec4(position, 1.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragPosition = input.position; + output.fragNormal = input.normal; + output.gl_Position = vec4(input.position, 1.0); + return output; } } fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + samplerCube environmentMap; + vec3 viewPos; + vec4 fragColor; - void main() { - vec3 norm = normalize(fragNormal); - vec3 viewDir = normalize(viewPos - fragPosition); + vec3 norm = normalize(input.fragNormal); + vec3 viewDir = normalize(viewPos - input.fragPosition); vec3 reflectDir = reflect(-viewDir, norm); - - fragColor = vec4(mix(objectColor, environmentColor, 0.5), 1.0); + vec3 environmentColor = texCUBE(environmentMap, reflectDir).rgb; + vec3 objectColor = tex2D(texture, input.fragPosition.xy).rgb; + vec3 finalColor = lerp(objectColor, environmentColor, 0.5); + fragColor = vec4(finalColor, 1.0); + return fragColor; } } } \ No newline at end of file diff --git a/samples/Basic Texture Shade.cgl b/samples/Basic Texture Shade.cgl index b6ec320..8b39e31 100644 --- a/samples/Basic Texture Shade.cgl +++ b/samples/Basic Texture Shade.cgl @@ -1,21 +1,34 @@ shader main { - vertex { - input vec3 position; - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct VSOutput { + vec2 fragTexCoord @TEXCOORD0; + vec4 gl_Position @SV_POSITION; + }; - void main() { - fragTexCoord = texCoord; - gl_Position = vec4(position, 1.0); + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(input.position, 1.0); + return output; } } fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec4 fragColor; - void main() { - fragColor = textureColor; + fragColor = tex2D(texture, input.fragTexCoord); + return fragColor; } } -} +} \ No newline at end of file diff --git a/samples/Blinn-Phong Shading.cgl b/samples/Blinn-Phong Shading.cgl index c6e8efd..31f0b67 100644 --- a/samples/Blinn-Phong Shading.cgl +++ b/samples/Blinn-Phong Shading.cgl @@ -1,41 +1,44 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; - - void main() { - fragPosition = position; - fragNormal = normal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + }; + + struct PSInput { + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragPosition = input.position; + output.fragNormal = input.normal; + output.gl_Position = vec4(input.position, 1.0); + return output; + } } -} - -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 { + vec4 main(PSInput input) @SV_TARGET { + vec3 lightPos; + vec3 viewPos; + vec4 fragColor; + + vec3 norm = normalize(input.fragNormal); + vec3 lightDir = normalize(lightPos - input.fragPosition); + vec2 diff = max(dot(norm, lightDir), 0.0); + vec3 halfwayDir = normalize(lightDir + viewPos - input.fragPosition); + vec2 spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; + fragColor = vec4(color, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Bloom Effect.cgl b/samples/Bloom Effect.cgl index acc85e3..10e8e47 100644 --- a/samples/Bloom Effect.cgl +++ b/samples/Bloom Effect.cgl @@ -1,22 +1,36 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + struct VSOutput { + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.color = vec4(input.texCoord, 0.0, 1.0); + return output; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 threshold; + vec4 fragColor; - void main() { - vec4 color = texture(texture, fragCoord); - vec4 bloom = max(color - threshold, 0.0); - fragColor = color + bloom; + vec2 texCoord = input.color.xy; // Extract the texture coordinate + vec4 texColor = tex2D(texture, texCoord); + vec4 bloom = max(texColor - threshold, 0.0); + fragColor = texColor + bloom; + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Bump Mapping.cgl b/samples/Bump Mapping.cgl index d5bd4cf..a71258c 100644 --- a/samples/Bump Mapping.cgl +++ b/samples/Bump Mapping.cgl @@ -1,28 +1,56 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragPosition = position; - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragPosition = input.position; + output.fragNormal = input.normal; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(input.position, 1.0); + return output; + } } -} -fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + sampler2D normalMap; + vec3 lightPos; + 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); + // Fetch and normalize the normal from the normal map + vec3 norm = normalize(tex2D(normalMap, input.fragTexCoord).rgb * 2.0 - 1.0); + + // Calculate the direction from the fragment to the light + vec3 lightDir = normalize(lightPos - input.fragPosition); + + // Compute the diffuse lighting term + vec2 diff = max(dot(norm, lightDir), 0.0); + + // Fetch the color from the texture and apply the diffuse lighting + vec3 color = tex2D(texture, input.fragTexCoord).rgb * diff; + + // Output the final color with full opacity + fragColor = vec4(color, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Calculate Lighting.cgl b/samples/Calculate Lighting.cgl index 27aa059..9dfce41 100644 --- a/samples/Calculate Lighting.cgl +++ b/samples/Calculate Lighting.cgl @@ -1,65 +1,49 @@ shader complexShader { - vertex { - input vec3 position; - input vec2 texCoord; - input vec3 normal; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; - gl_Position = vec4(position, 1.0); - } - } + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; - fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + struct PSInput { + vec4 color @COLOR; + }; - - 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; + vertex { + vec2 calculateLighting(vec3 normal, vec3 lightDir) { + vec2 diff = max(dot(normal, lightDir), 0.0); + return diff; } - vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) { - return normalize(normal * mapNormal); + vec4 textureColor(vec2 coord) { + return tex2D(textureSampler, coord); } - void main() { - - vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); - vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal); + VSOutput main(VSInput input) { + VSOutput output; + vec3 normal = normalize(vec3(0.0, 0.0, 1.0)); + vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); + vec2 lightIntensity = calculateLighting(normal, lightDir); - vec3 lightDir = normalize(defaultLightPos - fragPosition); - vec3 viewDir = normalize(defaultViewPos - fragPosition); + vec4 texColor = textureColor(input.texCoord); + output.color = texColor * lightIntensity; // Apply lighting to color - // 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; + // Pass texture coordinates to fragment shader + output.color.xy = input.texCoord; + output.gl_Position = vec4(input.position, 1.0); + return output; + } + } - fragColor = color; + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 fragColor; + fragColor = input.color; + return fragColor; } } } \ No newline at end of file diff --git a/samples/Cartoon Outline.cgl b/samples/Cartoon Outline.cgl index e2a6eaf..8fde703 100644 --- a/samples/Cartoon Outline.cgl +++ b/samples/Cartoon Outline.cgl @@ -1,31 +1,47 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 outlineWidth; + vec4 fragColor; - 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)); + vec2 texCoord = input.fragTexCoord; + vec4 color = tex2D(texture, texCoord); + vec4 north = tex2D(texture, texCoord + vec2(0.0, outlineWidth)); + vec4 south = tex2D(texture, texCoord - vec2(0.0, outlineWidth)); + vec4 east = tex2D(texture, texCoord + vec2(outlineWidth, 0.0)); + vec4 west = tex2D(texture, texCoord - 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; + vec2 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; + } + return fragColor; } } -} } \ No newline at end of file diff --git a/samples/Cel Shading.cgl b/samples/Cel Shading.cgl index 6de128e..2fff955 100644 --- a/samples/Cel Shading.cgl +++ b/samples/Cel Shading.cgl @@ -1,23 +1,45 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; -fragment { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + struct PSInput { + vec4 color @COLOR; + }; - 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); + vertex { + vec3 celShading(vec3 normal, vec3 lightDir) { + vec2 intensity = dot(normal, lightDir); + if (intensity > 0.95) return vec3(1.0, 1.0, 1.0); + else if (intensity > 0.5) return vec3(0.8, 0.8, 0.8); + else if (intensity > 0.25) return vec3(0.6, 0.6, 0.6); + else return vec3(0.4, 0.4, 0.4); + } + + VSOutput main(VSInput input) { + VSOutput output; + vec3 norm = normalize(input.normal); + vec3 lightDir = normalize(lightPos - input.position); + vec3 color = celShading(norm, lightDir); + + output.color.rgb = color; + output.color.a = 1.0; // Alpha channel + output.gl_Position = vec4(input.position, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 fragColor; + fragColor = input.color; + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Chromakey Shader.cgl b/samples/Chromakey Shader.cgl index aa49ada..2007be6 100644 --- a/samples/Chromakey Shader.cgl +++ b/samples/Chromakey Shader.cgl @@ -1,28 +1,43 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } -} + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } + } - 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 + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec3 keyColor; + vec2 threshold; + vec4 fragColor; - float diff = length(color - keyColor); - if (diff < threshold) { - discard; - } else { - fragColor = color; + vec2 texCoord = input.fragTexCoord; + vec4 color = tex2D(texture, texCoord); + vec2 diff = length(color.rgb - keyColor); + if (diff < threshold) { + discard; + } else { + fragColor = color; + } + return fragColor; } } -} } \ No newline at end of file diff --git a/samples/Chromatic Aberration.cgl b/samples/Chromatic Aberration.cgl index 4ceb3b8..f34d122 100644 --- a/samples/Chromatic Aberration.cgl +++ b/samples/Chromatic Aberration.cgl @@ -1,24 +1,42 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.color = vec4(input.texCoord, 0.0, 1.0); + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 aberration; + vec4 fragColor; + + vec2 texCoord = input.color.xy; // Extract the texture coordinate + + vec2 offset = vec2(aberration / 512.0); + vec4 red = tex2D(texture, texCoord + offset); + vec4 green = tex2D(texture, texCoord); + vec4 blue = tex2D(texture, texCoord - offset); - 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); + fragColor = vec4(red.r, green.g, blue.b, 1.0); + return fragColor; + } } -} -} +} \ No newline at end of file diff --git a/samples/Color Inversion.cgl b/samples/Color Inversion.cgl index 9cd7eb0..f34d122 100644 --- a/samples/Color Inversion.cgl +++ b/samples/Color Inversion.cgl @@ -1,21 +1,42 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.color = vec4(input.texCoord, 0.0, 1.0); + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 aberration; + vec4 fragColor; + + vec2 texCoord = input.color.xy; // Extract the texture coordinate + + vec2 offset = vec2(aberration / 512.0); + vec4 red = tex2D(texture, texCoord + offset); + vec4 green = tex2D(texture, texCoord); + vec4 blue = tex2D(texture, texCoord - offset); - void main() { - vec4 color = texture(texture, fragTexCoord); - fragColor = vec4(vec3(1.0) - color, 1.0); + fragColor = vec4(red.r, green.g, blue.b, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Complex Shader.cgl b/samples/Complex Shader.cgl index d8b82c0..bef0407 100644 --- a/samples/Complex Shader.cgl +++ b/samples/Complex Shader.cgl @@ -1,31 +1,84 @@ shader complexShader { + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + vec3 normal @NORMAL; + }; + + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec4 color @COLOR; + vec2 texCoord @TEXCOORD0; + }; + vertex { - // Vertex shader logic goes here. + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + vec2 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); + } + + VSOutput main(VSInput input) { + VSOutput output; + // Pass texture coordinates and normals to the fragment shader + output.color.xy = input.texCoord; + output.color.zw = input.normal; + + // Calculate normal map in the vertex shader + vec4 normalMapColor = tex2D(normalMap, input.texCoord); + vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0); + vec3 perturbedNormal = applyNormalMapping(input.normal, mapNormal); + + // Pass perturbed normal to the fragment shader + output.color.rgb = perturbedNormal; + output.color.a = 1.0; // Alpha channel + output.gl_Position = vec4(input.position, 1.0); + return output; + } } fragment { - input vec3 position; - input vec2 texCoord; - output vec4 fragColor; + vec4 main(PSInput input) @SV_TARGET { + sampler2D diffuseTexture; + sampler2D specularTexture; + sampler2D normalMap; + vec3 lightPos; + vec3 viewPos; + vec3 ambientColor; + vec2 shininess; + vec2 specularIntensity; + vec4 fragColor; - float calculateLighting(vec3 normal, vec3 lightDir) { - float diff = max(dot(normal, lightDir), 0.0); - return diff; - } + vec4 diffuseColor = tex2D(diffuseTexture, input.texCoord); + vec4 specularColor = tex2D(specularTexture, input.texCoord); - vec4 textureColor(vec2 coord) { - return texture2D(textureSampler, coord); - } + vec3 perturbedNormal = normalize(input.color.rgb); + vec3 lightDir = normalize(lightPos - input.color.xyz); + vec3 viewDir = normalize(viewPos - input.color.xyz); + + // Ambient component + vec3 ambient = ambientColor; + + // Diffuse component + vec2 diff = max(dot(perturbedNormal, lightDir), 0.0); + vec3 diffuse = diff * diffuseColor.rgb; + + // Specular component + vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir); - 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 + vec3 lighting = ambient + diffuse + specular; + vec4 color = vec4(lighting, 1.0) * diffuseColor; fragColor = color; + return fragColor; } } } \ No newline at end of file diff --git a/samples/Depth Fog.cgl b/samples/Depth Fog.cgl index f4d2635..3ecc06a 100644 --- a/samples/Depth Fog.cgl +++ b/samples/Depth Fog.cgl @@ -1,30 +1,45 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragPosition = position; - fragTexCoord = texCoord; - } -} + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec3 fragPosition @POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec3 fragPosition @POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; -fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragPosition = input.position; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(input.position, 1.0); + return output; + } + } + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 fogDensity; + vec3 fogColor; + vec4 fragColor; - void main() { - vec3 baseColor = texture(texture, fragTexCoord); - float depth = length(fragPosition); - float fogFactor = exp(-fogDensity * depth); - vec3 finalColor = mix(fogColor, baseColor, fogFactor); + vec3 baseColor = tex2D(texture, input.fragTexCoord).rgb; + vec2 depth = length(input.fragPosition); + vec2 fogFactor = exp(-fogDensity * depth); + vec3 finalColor = lerp(fogColor, baseColor, fogFactor); - fragColor = vec4(finalColor, 1.0); + fragColor = vec4(finalColor, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Depth of Field.cgl b/samples/Depth of Field.cgl index eb7d641..f7570ab 100644 --- a/samples/Depth of Field.cgl +++ b/samples/Depth of Field.cgl @@ -1,31 +1,49 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through the texture coordinates to the fragment shader - void main() { - fragCoord = texCoord; + struct VSOutput { + vec24 color @COLOR; + vec24 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec24 color @COLOR; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.color = vec24(input.texCoord, 0.0, 1.0); + output.gl_Position = vec24(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + vec24 main(PSInput input) @SV_TARGET { + sampler2D texture; + sampler2D depthTexture; + vec2 focusDepth; + vec2 focusRange; + vec24 fragColor; - void main() { - float depth = texture(depthTexture, fragCoord); - float blur = clamp(abs(depth - focusDepth) / focusRange, 0.0, 1.0); + vec2 texCoord = input.color.xy; // Extract the texture coordinate + vec2 depth = tex2D(depthTexture, texCoord).r; + vec2 blur = clamp(abs(depth - 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); + vec24 colorAccum = vec24(0.0); + for (int i = -4; i <= 4; i++) { + for (int j = -4; j <= 4; j++) { + vec2 offset = vec2(i, j) * blur / 512.0; + colorAccum += tex2D(texture, texCoord + offset); + } } + colorAccum /= 81.0; + fragColor = colorAccum; + return fragColor; } - 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..259fd33 100644 --- a/samples/Displacement Mapping.cgl +++ b/samples/Displacement Mapping.cgl @@ -1,22 +1,40 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + sampler2D displacementMap; + vec2 displacementFactor; + vec4 fragColor; - 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); + vec2 texCoord = input.fragTexCoord; + vec2 displacement = tex2D(displacementMap, texCoord).rg * 2.0 - 1.0; + vec2 uv = texCoord + displacement * displacementFactor; + vec4 color = tex2D(texture, uv); + fragColor = vec4(color.rgb, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Dynamic Day-Night Cycle.cgl b/samples/Dynamic Day-Night Cycle.cgl index 37aa01f..03bb86f 100644 --- a/samples/Dynamic Day-Night Cycle.cgl +++ b/samples/Dynamic Day-Night Cycle.cgl @@ -1,20 +1,37 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec2 position @POSITION; + }; - fragment { - input vec3 position; - output vec4 fragColor; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; - vec3 dayNightColor(float timeOfDay) { - return mix(vec3(0.0, 0.5, 1.0), vec3(0.0, 0.0, 0.2), timeOfDay); + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + vec2 dayNightColor(vec2 timeOfDay) { + return lerp(vec2(0.0, 0.5, 1.0), vec2(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); + VSOutput main(VSInput input) { + VSOutput output; + vec2 timeOfDay = fmod(iTime / 24.0, 1.0); + vec2 colorComputed = dayNightColor(timeOfDay); + output.color = vec4(colorComputed, 1.0); + output.gl_Position = vec4(input.position, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 fragColor; + fragColor = input.color; + return fragColor; } } -} +} \ No newline at end of file diff --git a/samples/Dynamic Water Shader.cgl b/samples/Dynamic Water Shader.cgl index b3cefc8..a227bb0 100644 --- a/samples/Dynamic Water Shader.cgl +++ b/samples/Dynamic Water Shader.cgl @@ -1,22 +1,38 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec2 position @POSITION; + }; - fragment { - input vec2 position; - output vec4 fragColor; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; - float wave(vec2 p, float time) { - return sin(p.x * 10.0) * 0.1 + sin(p.y * 10.0) * 0.1; + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + vec2 wave(vec2 p, vec2 time) { + 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); - fragColor = vec4(color, 1.0); + VSOutput main(VSInput input) { + VSOutput output; + vec2 uv = input.position; + vec2 waterHeight = wave(uv, iTime); + vec3 colorComputed = vec3(0.0, 0.3, 0.8) * (1.0 + waterHeight); + output.color = vec4(colorComputed, 1.0); + output.gl_Position = vec4(input.position, 0.0, 1.0); // Assuming 2D position + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 fragColor; + fragColor = input.color; + return fragColor; } } } \ No newline at end of file diff --git a/samples/Edge Detection.cgl b/samples/Edge Detection.cgl index 1700e2a..c32b6f1 100644 --- a/samples/Edge Detection.cgl +++ b/samples/Edge Detection.cgl @@ -1,27 +1,42 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through the texCoord as fragCoord for simplicity - void main() { - fragCoord = texCoord; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + // Pass through the texture coordinate + output.color = vec4(input.texCoord, 0.0, 1.0); + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + 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)); + vec2 texCoord = input.color.xy; // Extract the texture coordinate + vec2 offset = vec2(1.0 / 512.0, 1.0 / 512.0); + vec3 color = tex2D(texture, texCoord).rgb; + vec3 colorRight = tex2D(texture, texCoord + vec2(offset.x, 0.0)).rgb; + vec3 colorUp = tex2D(texture, texCoord + vec2(0.0, offset.y)).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); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Emboss Effect.cgl b/samples/Emboss Effect.cgl index 08f620c..bbe7ec4 100644 --- a/samples/Emboss Effect.cgl +++ b/samples/Emboss Effect.cgl @@ -1,39 +1,57 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec4 fragColor; + + vec2 texCoord = input.fragTexCoord; + vec2 texOffset = vec2(1.0 / 512.0); // Offset for sampling neighboring pixels + vec3 sample[9]; + + // Sample the 3x3 neighborhood around the current pixel + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + sample[(i+1) * 3 + (j+1)] = tex2D(texture, texCoord + vec2(i, j) * texOffset).rgb; + } + } + + vec3 color = vec3(0.0); + // Apply the edge detection kernel + color += sample[0] * -1.0; // Top-left + color += sample[1] * -1.0; // Top-center + color += sample[2] * -1.0; // Top-right + color += sample[3] * -1.0; // Center-left + color += sample[5] * 1.0; // Center-right + color += sample[6] * 1.0; // Bottom-left + color += sample[7] * 1.0; // Bottom-center + color += sample[8] * 1.0; // Bottom-right + + // Add bias to the color to ensure it's visible + fragColor = vec4(color + vec3(0.5), 1.0); + return fragColor; + } } -} - -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)); - - 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 +} \ No newline at end of file diff --git a/samples/Flat Shading.cgl b/samples/Flat Shading.cgl index 69508a0..bcfa3d9 100644 --- a/samples/Flat Shading.cgl +++ b/samples/Flat Shading.cgl @@ -1,24 +1,33 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - void main() { - fragNormal = normal; - } -} + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec3 fragNormal @NORMAL; + }; + + struct PSInput { + vec3 fragNormal @NORMAL; + }; -fragment { - input vec3 fragNormal; - output vec4 fragColor; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragNormal = input.normal; + output.gl_Position = vec4(input.position, 1.0); + return output; + } + } - 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); + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 fragColor; + vec3 color = vec3(0.5, 0.5, 0.5) + 0.5 * input.fragNormal; + fragColor = vec4(color, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Fluid Dynamics Simulation.cgl b/samples/Fluid Dynamics Simulation.cgl index 0917ce1..e9a5baa 100644 --- a/samples/Fluid Dynamics Simulation.cgl +++ b/samples/Fluid Dynamics Simulation.cgl @@ -1,20 +1,37 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec3 position @POSITION; + }; - fragment { - input vec3 position; - output vec4 fragColor; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; - float fluidMotion(vec3 p) { - return sin(p.x * 10.0) * 0.5 + 0.5; + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + vec2 fluidMotion(vec3 p) { + 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); + VSOutput main(VSInput input) { + VSOutput output; + vec2 motion = fluidMotion(input.position); + vec3 colorComputed = vec3(0.0, 0.0, 1.0) * motion; + output.color = vec4(colorComputed, 1.0); + output.gl_Position = vec4(input.position, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 fragColor; + fragColor = input.color; + return fragColor; } } } \ No newline at end of file diff --git a/samples/Fog Effect.cgl b/samples/Fog Effect.cgl index 37f0c9e..4ac403a 100644 --- a/samples/Fog Effect.cgl +++ b/samples/Fog Effect.cgl @@ -1,23 +1,40 @@ shader main { -vertex { - input vec3 position; - output vec3 fragPosition; + struct VSInput { + vec3 position @POSITION; + }; - // Pass through the position to the fragment shader - void main() { - fragPosition = position; + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + // Pass through the position + output.color = vec4(input.position, 1.0); + output.gl_Position = vec4(input.position, 1.0); + return output; + } } -} -fragment { - input vec3 fragPosition; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 cameraPos; + vec3 fogColor; + vec2 fogDensity; + 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); + vec3 position = input.color.xyz; // Extract the position + vec2 distance = length(position - cameraPos); + vec2 fogFactor = exp(-pow(distance * fogDensity, 2.0)); + vec3 finalColor = lerp(fogColor, vec3(0.0, 0.5, 1.0), fogFactor); + fragColor = vec4(finalColor, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Fresnel Effect.cgl b/samples/Fresnel Effect.cgl index a9dc6e7..98137e4 100644 --- a/samples/Fresnel Effect.cgl +++ b/samples/Fresnel Effect.cgl @@ -1,28 +1,44 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code -} + struct VSOutput { + vec4 color @COLOR; + vec4 gl_Position @SV_POSITION; + }; -fragment { - input vec3 normal; - output vec4 fragColor; + struct PSInput { + vec4 color @COLOR; + }; - float fresnelEffect(vec3 normal, vec3 viewDir) { - return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + vertex { + vec2 fresnelEffect(vec3 normal, vec3 viewDir) { + return pow(1.0 - max(dot(normal, viewDir), 0.0), 3.0); + } + + VSOutput main(VSInput input) { + VSOutput output; + // Pass the normalized normal and view direction to the fragment shader + output.color.rgb = input.normal; + output.color.a = 1.0; // Alpha channel + output.gl_Position = vec4(input.position, 1.0); + return output; + } } - 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 { + vec4 main(PSInput input) @SV_TARGET { + vec3 viewDir; + vec4 fragColor; + + vec3 norm = normalize(input.color.rgb); + vec2 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); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Glitch Effect.cgl b/samples/Glitch Effect.cgl index 86fb1b5..9be91ce 100644 --- a/samples/Glitch Effect.cgl +++ b/samples/Glitch Effect.cgl @@ -1,25 +1,40 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } -} + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } + } - void main() { - vec2 uv = fragTexCoord; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 time; + vec4 fragColor; - if (fract(time * 10.0) < 0.5) { - uv += sin(fragTexCoord * 10.0) * 0.1; + vec2 uv = input.fragTexCoord; + if (fract(time * 10.0) < 0.5) { + uv.x += sin(input.fragTexCoord.y * 10.0) * 0.1; + } + vec4 color = tex2D(texture, uv); + fragColor = color; + return fragColor; } - 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..98c3fdc 100644 --- a/samples/Gouraud Shading.cgl +++ b/samples/Gouraud Shading.cgl @@ -1,28 +1,41 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - void main() { - fragPosition = position; - fragNormal = normal; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + }; + + struct PSInput { + vec3 fragPosition @POSITION; + vec3 fragNormal @NORMAL; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragPosition = input.position; + output.fragNormal = input.normal; + output.gl_Position = vec4(input.position, 1.0); + return output; + } } -} -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 lightPos; + 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); + vec3 norm = normalize(input.fragNormal); + vec3 lightDir = normalize(lightPos - input.fragPosition); + vec2 diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff; + fragColor = vec4(color, 1.0); + return fragColor; + } } -} -} +} \ No newline at end of file diff --git a/samples/Gradient Background.cgl b/samples/Gradient Background.cgl index 9d06de1..968ba78 100644 --- a/samples/Gradient Background.cgl +++ b/samples/Gradient Background.cgl @@ -1,20 +1,36 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - void main() { - vec3 color = mix(bottomColor, topColor, fragTexCoord.y); - fragColor = vec4(color, 1.0); + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 topColor; + vec3 bottomColor; + vec4 fragColor; + + vec3 color = lerp(bottomColor, topColor, input.fragTexCoord.y); + fragColor = vec4(color, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Halftone Effect.cgl b/samples/Halftone Effect.cgl index 49096e4..cce987b 100644 --- a/samples/Halftone Effect.cgl +++ b/samples/Halftone Effect.cgl @@ -1,24 +1,52 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} - -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 { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 dotSize; + vec4 fragColor; + + // Scale texture coordinates by dotSize + vec2 uv = input.fragTexCoord * dotSize; + + // Calculate angle and radius for the dot pattern + vec2 angle = fmod(uv.x + uv.y, 2.0) * 3.14159265; + vec2 radius = length(frac(uv) - vec2(0.5)); + + // Sample color from texture + vec4 color = tex2D(texture, input.fragTexCoord); + + // Calculate intensity based on luminance + vec2 intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); + + // Compute dot pattern using smoothstep for edge softness + vec2 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); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Hatching shader.cgl b/samples/Hatching shader.cgl index 0aff9c6..87ef829 100644 --- a/samples/Hatching shader.cgl +++ b/samples/Hatching shader.cgl @@ -1,36 +1,46 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec4 gl_Position @SV_POSITION; + vec2 fragTexCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 fragTexCoord @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragTexCoord = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 threshold1; + vec2 threshold2; + vec2 threshold3; + vec4 fragColor; - void main() { - vec4 color = texture(texture, fragTexCoord); - float itensity = dot(color, vec3(0.299, 0.587, 0.114)); + vec4 color = tex2D(texture, input.fragTexCoord); + vec2 intensity = dot(color.rgb, vec3(0.299, 0.587, 0.114)); - 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); - } + vec3 hatching = vec3(1.0); + if (intensity < threshold1) hatching = vec3(0.8); + else if (intensity < threshold2) hatching = vec3(0.6); + else if (intensity < threshold3) hatching = vec3(0.4); + else hatching = vec3(0.2); - fragColor = vec4(hatching, 1.0); + fragColor = vec4(hatching, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Heat Haze.cgl b/samples/Heat Haze.cgl index 7b65273..823e0f2 100644 --- a/samples/Heat Haze.cgl +++ b/samples/Heat Haze.cgl @@ -1,22 +1,37 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; + struct VSOutput { + vec2 texCoordOut @TEXCOORD0; + vec4 gl_Position @SV_POSITION; + }; + + struct PSInput { + vec2 texCoordOut @TEXCOORD0; + }; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoordOut = input.texCoord; + output.gl_Position = vec4(0.0, 0.0, 0.0, 1.0); // Placeholder for position + return output; + } } -} -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + fragment { + vec4 main(PSInput input) @SV_TARGET { + sampler2D texture; + vec2 time; + vec4 fragColor; - void main() { - vec2 uv = fragTexCoord + sin(fragTexCoord.y * 10.0 + time) * 0.01; - vec4 color = texture(texture, uv); - fragColor = vec4(color.rgb, 1.0); + vec2 uv = input.texCoordOut + sin(input.texCoordOut.y * 10.0 + time) * 0.01; + vec4 color = tex2D(texture, uv); + fragColor = vec4(color.rgb, 1.0); + return fragColor; + } } -} } \ No newline at end of file diff --git a/samples/Kaleidoscope Effect.cgl b/samples/Kaleidoscope Effect.cgl index fd7aede..6fa81c1 100644 --- a/samples/Kaleidoscope Effect.cgl +++ b/samples/Kaleidoscope Effect.cgl @@ -1,24 +1,54 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec2 segments; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - - 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); +sampler2D texture; + + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 texCoord = input.texCoord; + + // Compute the angle of the current texCoord relative to the center + vec2 angle = atan2(texCoord.y - 0.5, texCoord.x - 0.5); + + // Compute the radius from the center of the texture + vec2 radius = length(texCoord - vec2(0.5, 0.5)); + + // Determine the segment index based on the angle and number of segments + vec2 segment = floor(segments * angle / (2.0 * 3.14159265)); + + // Compute the new angle corresponding to the segment + vec2 newAngle = segment * 2.0 * 3.14159265 / segments; + + // Compute the new texture coordinates based on the new angle + vec2 newTexCoord = vec2(0.5, 0.5) + radius * vec2(cos(newAngle), sin(newAngle)); + + // Sample the texture at the new texture coordinates + return texture.Sample(texture, newTexCoord); + } } -} } \ No newline at end of file diff --git a/samples/Lambertian Shading.cgl b/samples/Lambertian Shading.cgl index 5cf9a10..003527f 100644 --- a/samples/Lambertian Shading.cgl +++ b/samples/Lambertian Shading.cgl @@ -1,28 +1,40 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - void main() { - fragPosition = position; - fragNormal = normal; - } + struct VSOutput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + struct PSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + cbuffer ShaderUniforms { + vec3 lightPos; } -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - 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); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + output.normal = input.normal; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 norm = normalize(input.normal); + vec3 lightDir = normalize(lightPos - input.position); + vec2 diff = max(dot(norm, lightDir), 0.0); + vec3 color = vec3(0.5, 0.5, 0.5) * diff; + return 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..e7ffb11 100644 --- a/samples/Light Shaft Effect.cgl +++ b/samples/Light Shaft Effect.cgl @@ -1,27 +1,41 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through position and normal to the fragment shader - void main() { - fragPosition = position; - fragNormal = normal; - } + struct VSOutput { + vec3 positionOut @POSITION + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec2 lightPos; } -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; +sampler2D texture; + - 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); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } } -} -} + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 uv = input.texCoord; + vec4 color = texture.Sample(texture, uv); + vec2 dir = uv - lightPos; + vec2 dist = length(dir); + vec4 shaftColor = texture.Sample(texture, uv - dir * (1.0 - dist)); + return lerp(color, shaftColor, 0.5); + } + } +} \ No newline at end of file diff --git a/samples/Metallic Shader.cgl b/samples/Metallic Shader.cgl index 7c35fef..6439eaf 100644 --- a/samples/Metallic Shader.cgl +++ b/samples/Metallic Shader.cgl @@ -1,34 +1,48 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec4 fragColor; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - // Vertex shader code, if needed - // For this example, there's no actual vertex shader code + struct VSOutput { + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + cbuffer ShaderUniforms { + vec3 lightPos; + vec3 viewPos; + vec2 metallic; } -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 + 0.5 * spec; // Assuming metallic is set to 0.5 - } - 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; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + + vec3 norm = normalize(input.normal); + vec3 lightDir = normalize(lightPos - input.position); + vec3 viewDir = normalize(viewPos - input.position); + + vec2 ambient = 0.1; + vec2 diff = max(dot(norm, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, norm); + vec2 spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + vec3 lighting = ambient + diff + metallic * spec; - fragColor = vec4(color, 1.0); + output.color.rgb = vec3(1.0, 0.8, 0.6) * lighting; + output.color.a = 1.0; // Alpha channel + + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + return input.color; + } } -} } \ No newline at end of file diff --git a/samples/Motion Blur.cgl b/samples/Motion Blur.cgl index 4f0c143..8e664dd 100644 --- a/samples/Motion Blur.cgl +++ b/samples/Motion Blur.cgl @@ -1,24 +1,44 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragCoord = texCoord; - } + struct VSOutput { + vec3 positionOut @POSITION + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + cbuffer ShaderUniforms { + vec2 motionDirection; + vec2 blurAmount; } -fragment { - input vec2 fragCoord; - output vec4 fragColor; +sampler2D texture; + - void main() { - - for (int i = -4; i < 4; i++) { - color += texture(texture, fragCoord * motionDirection * blurAmount / 10.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + // Pass through the texture coordinate + output.color = vec4(input.texCoord, 0.0, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 texCoord = input.color.xy; // Extract the texture coordinate + vec4 colorAccum = vec4(0.0); + for (int i = -4; i <= 4; i++) { + colorAccum += texture.Sample(texture, texCoord + vec2(i) * motionDirection * blurAmount / 10.0); + } + colorAccum /= 9.0; + return 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..77f7226 100644 --- a/samples/Nested Loop Shader.cgl +++ b/samples/Nested Loop Shader.cgl @@ -1,36 +1,80 @@ shader nestedLoopsShader { + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + vec3 normal @NORMAL; + }; + + struct VSOutput { + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + }; + + cbuffer ShaderUniforms { + vec3 ambientColor; + vec3 lightPos; + vec3 viewPos; + vec2 shininess; + vec2 specularIntensity; + int outerIterations; + int innerIterations; +} + +sampler2D texture; + + vertex { - input vec3 position; - input vec2 texCoord; - input vec3 normal; - output vec3 fragPosition; - output vec2 fragTexCoord; - output vec3 fragNormal; - - void main() { - fragPosition = position; - fragTexCoord = texCoord; - fragNormal = normal; + VSOutput main(VSInput input) { + VSOutput output; + + // Pass texture coordinates and normals to the fragment shader + output.color.xy = input.texCoord; + output.color.zw = input.normal; + + // Pass other uniforms directly + output.color.a = 1.0; // Alpha channel + + return output; } } fragment { - input vec3 fragPosition; - input vec2 fragTexCoord; - input vec3 fragNormal; - output vec4 fragColor; - - void main() { - - for (int i = 0; i < 0; i++) { - for (int j = 0; j < 0; j++) { - vec3 reflectDir = reflect(-lightDir, fragNormal); - } + vec4 main(PSInput input) @SV_TARGET { + vec2 texCoord = input.color.xy; // Extract the texture coordinate + vec3 normal = input.color.zw; // Extract the normal + + vec4 texColor = texture.Sample(texture, texCoord); + vec3 lightDir = normalize(lightPos - vec3(texCoord, 0.0)); + vec3 viewDir = normalize(viewPos - vec3(texCoord, 0.0)); + + vec3 ambient = ambientColor; + vec3 diffuse = texColor.rgb; + vec3 specular = vec3(0.0); + + // Outer loop + for (int i = 0; i < outerIterations; i++) { + // Inner loop + for (int j = 0; j < innerIterations; j++) { + // Compute lighting + vec3 tempSpecular = calculateSpecular(normal, lightDir, viewDir); + specular += tempSpecular; + } + + // Reduce diffuse intensity progressively diffuse *= 0.9; } vec3 lighting = ambient + diffuse + specular; - fragColor = vec4(lighting, 1.0); + return vec4(lighting, 1.0); + } + + vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) { + vec3 reflectDir = reflect(-lightDir, normal); + vec2 spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); + return spec * vec3(1.0, 1.0, 1.0) * specularIntensity; } } } \ No newline at end of file diff --git a/samples/Normal Mapping.cgl b/samples/Normal Mapping.cgl index 553d259..3f2729a 100644 --- a/samples/Normal Mapping.cgl +++ b/samples/Normal Mapping.cgl @@ -1,34 +1,60 @@ -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec2 fragTexCoord; - output vec3 fragPosition; - output vec3 fragNormal; +shader main { + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - fragPosition = position; - fragNormal = normal; - } + struct VSOutput { + vec2 texCoord @TEXCOORD0; + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + cbuffer ShaderUniforms { + vec3 lightPos; + vec3 viewPos; } -fragment { - input vec2 fragTexCoord; - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; +sampler2D texture; +sampler2D normalMap; + - - 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); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + output.normal = input.normal; + output.texCoord = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + // Fetch and normalize the normal from the normal map + vec3 norm = normalize(texture.Sample(normalMap, input.texCoord).rgb * 2.0 - 1.0); + + // Compute the light direction and the diffuse component + vec3 lightDir = normalize(lightPos - input.position); + vec2 diff = max(dot(norm, lightDir), 0.0); + + // Compute the reflection direction and specular component + vec3 reflectDir = reflect(-lightDir, norm); + vec3 viewDir = normalize(viewPos - input.position); + vec2 spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + + // Fetch the texture color + vec3 color = texture.Sample(texture, input.texCoord).rgb; + + // Compute the final color with ambient, diffuse, and specular components + return vec4((0.1 + diff + spec) * color, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Outline with Sobel Filter.cgl b/samples/Outline with Sobel Filter.cgl index 481483d..0ef178d 100644 --- a/samples/Outline with Sobel Filter.cgl +++ b/samples/Outline with Sobel Filter.cgl @@ -1,35 +1,47 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } -} + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + sampler2D texture; - void main() { - vec2 texOffset = vec2(1.0 / 512.0); // Offset for sampling - vec3 color = vec3(0.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } + } - // Apply a simple edge detection filter - color += texture(texture, fragTexCoord + vec2(-texOffset, -texOffset)) * -1.0; - color += texture(texture, fragTexCoord + vec2(0.0, -texOffset)) * -1.0; - color += texture(texture, fragTexCoord + vec2(texOffset, -texOffset)) * -1.0; + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 texCoord = input.texCoord; + vec2 texOffset = vec2(1.0 / 512.0); // Offset to sample neighboring pixels + vec2 kernel[9] = {-1.0, -1.0, -1.0, -1.0, 8.0, -1.0, -1.0, -1.0, -1.0}; - color += texture(texture, fragTexCoord + vec2(-texOffset, 0.0)) * -1.0; - color += texture(texture, fragTexCoord) * 8.0; - color += texture(texture, fragTexCoord + vec2(texOffset, 0.0)) * -1.0; + vec3 sample[9]; + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + sample[(i + 1) * 3 + (j + 1)] = texture.Sample(texture, texCoord + vec2(i, j) * texOffset).rgb; + } + } - color += texture(texture, fragTexCoord + vec2(-texOffset, texOffset)) * -1.0; - color += texture(texture, fragTexCoord + vec2(0.0, texOffset)) * -1.0; - color += texture(texture, fragTexCoord + vec2(texOffset, texOffset)) * -1.0; + vec3 color = vec3(0.0); + for (int i = 0; i < 9; i++) { + color += sample[i] * kernel[i]; + } - fragColor = vec4(color, 1.0); + return vec4(color, 1.0); + } } -} -} +} \ No newline at end of file diff --git a/samples/Parallax Mapping.cgl b/samples/Parallax Mapping.cgl index c13516c..1502781 100644 --- a/samples/Parallax Mapping.cgl +++ b/samples/Parallax Mapping.cgl @@ -1,23 +1,41 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - // Pass through texCoord to the fragment shader - void main() { - fragTexCoord = texCoord; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoordOut @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoordOut @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec2 viewDir; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; +sampler2D texture; +sampler2D heightMap; + - void main() { - float height = texture(heightMap, fragTexCoord); - vec2 offset = height * viewDir; - vec4 color = texture(texture, fragTexCoord + offset); - fragColor = vec4(color, 1.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoordOut = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 texCoordOut = input.texCoordOut; + vec2 height = texture.Sample(heightMap, texCoordOut).r; + vec2 offset = height * viewDir; + vec4 color = texture.Sample(texture, texCoordOut + offset); + return vec4(color.rgb, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Perlin Noise Terrain Generation.cgl b/samples/Perlin Noise Terrain Generation.cgl index 724dce4..360b9dc 100644 --- a/samples/Perlin Noise Terrain Generation.cgl +++ b/samples/Perlin Noise Terrain Generation.cgl @@ -1,35 +1,35 @@ -shader PerlinNoise { - vertex { - input vec3 position; - output vec2 vUV; +shader main { + struct VSInput { + vec3 position @POSITION; + }; - void main() - { - vUV = position.xy * 10.0; - gl_Position = vec4(position, 1.0); - } + struct VSOutput { + vec4 color @COLOR; + }; - } + struct PSInput { + vec4 color @COLOR; + }; - float perlinNoise(vec2 p) { - return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); + vertex { + vec2 perlinNoise(vec2 p) { + return frac(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); + } + + VSOutput main(VSInput input) { + VSOutput output; + vec2 uv = input.position.xy * 10.0; + vec2 noise = perlinNoise(uv); + vec2 height = noise * 10.0; + vec3 computedColor = vec3(height / 10.0, 1.0 - height / 10.0, 0.0); + output.color = vec4(computedColor, 1.0); + return output; + } } fragment { - - input vec2 vUV; - output vec4 fragColor; - - - void main() - { - float noise = perlinNoise(vUV); - float height = noise * 10.0; - vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0); - fragColor = vec4(color, 1.0); + vec4 main(PSInput input) @SV_TARGET { + return input.color; + } } - - -} - } \ No newline at end of file diff --git a/samples/Phong Shading.cgl b/samples/Phong Shading.cgl index e19f565..8efc367 100644 --- a/samples/Phong Shading.cgl +++ b/samples/Phong Shading.cgl @@ -1,30 +1,44 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - void main() { - fragPosition = position; - fragNormal = normal; - } + struct VSOutput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + struct PSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + cbuffer ShaderUniforms { + vec3 lightPos; + vec3 viewPos; } -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - void main() { - vec3 norm = normalize(fragNormal); - 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 = vec3(0.5, 0.5, 0.5) + diff + spec; - fragColor = vec4(color, 1.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + output.normal = input.normal; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 norm = normalize(input.normal); + vec3 lightDir = normalize(lightPos - input.position); + vec2 diff = max(dot(norm, lightDir), 0.0); + vec3 reflectDir = reflect(-lightDir, norm); + vec3 viewDir = normalize(viewPos - input.position); + vec2 spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); + vec3 color = vec3(0.5, 0.5, 0.5) + diff + spec; + return vec4(color, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Procedural Terrain Generation.cgl b/samples/Procedural Terrain Generation.cgl index b332ce7..44e2681 100644 --- a/samples/Procedural Terrain Generation.cgl +++ b/samples/Procedural Terrain Generation.cgl @@ -1,27 +1,39 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec2 position @POSITION; + }; - fragment { - input vec2 position; - output vec4 fragColor; + struct VSOutput { + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + }; - float noise(vec2 p) { + vertex { + vec2 noise(vec2 p) { return sin(p.x * 10.0) * cos(p.y * 10.0); } - float heightMap(vec2 p) { - float n = noise(p * 10.0); + vec2 heightMap(vec2 p) { + vec2 n = noise(p * 10.0); return p.y + n * 0.1; } - void main() { - vec2 uv = position; - float height = heightMap(uv); - vec3 color = vec3(0.5, 0.5, 0.5) * (1.0 - height * 0.5); + VSOutput main(VSInput input) { + VSOutput output; + vec2 uv = input.position; + vec2 height = heightMap(uv); + vec3 colorComputed = vec3(0.5, 0.5, 0.5) * (1.0 - height * 0.5); + output.color = vec4(colorComputed, 1.0); + return output; + } + } - fragColor = vec4(color, 1.0); + fragment { + vec4 main(PSInput input) @SV_TARGET { + return input.color; } } } \ No newline at end of file diff --git a/samples/Procedural Texturing with Complex Patterns.cgl b/samples/Procedural Texturing with Complex Patterns.cgl index d111470..9b80e28 100644 --- a/samples/Procedural Texturing with Complex Patterns.cgl +++ b/samples/Procedural Texturing with Complex Patterns.cgl @@ -1,19 +1,34 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - fragment { - input vec2 texCoord; - output vec4 fragColor; + struct VSOutput { + vec3 positionOut @POSITION; + vec4 color @COLOR; + }; - float marblePattern(vec2 uv) { - return 0.5 + 0.5 * sin(10.0 * uv.x); + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + vec2 marblePattern(vec2 uv) { + return 0.5 + 0.5 * sin(10.0 * uv.x + iTime); } - void main() { - vec3 color = vec3(marblePattern(texCoord), 0.0, 0.0); - fragColor = vec4(color, 1.0); + VSOutput main(VSInput input) { + VSOutput output; + vec3 colorComputed = vec3(marblePattern(input.texCoord), 0.0, 0.0); + output.color = vec4(colorComputed, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + return input.color; } } } \ No newline at end of file diff --git a/samples/Procedural Wood Texture.cgl b/samples/Procedural Wood Texture.cgl index 685e2e4..c6ce436 100644 --- a/samples/Procedural Wood Texture.cgl +++ b/samples/Procedural Wood Texture.cgl @@ -1,24 +1,40 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec2 scale; + vec2 rings; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; - void main() { - vec2 uv = fragTexCoord * scale; - float dist = length(uv - vec2(0.5)); - float wood = rings * dist; - wood = wood - floor(wood); - vec3 color = vec3(0.2, 0.1, 0.0) + vec3(0.4, 0.2, 0.1) * wood; - fragColor = vec4(color, 1.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 uv = input.texCoord * scale; + vec2 dist = length(uv - vec2(0.5, 0.5)); + vec2 wood = rings * dist; + wood = wood - floor(wood); + vec3 color = vec3(0.2, 0.1, 0.0) + vec3(0.4, 0.2, 0.1) * wood; + return vec4(color, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Radial Blur.cgl b/samples/Radial Blur.cgl index f289ce9..dfa94a2 100644 --- a/samples/Radial Blur.cgl +++ b/samples/Radial Blur.cgl @@ -1,25 +1,43 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec2 center; + vec2 blurAmount; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; +sampler2D texture; + - void main() { - vec2 dir = fragTexCoord - center; - vec4 color = vec4(0.0); - for (int i = -4; i < 4; i++) { - color += texture(texture, fragTexCoord * dir * blurAmount); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 dir = input.texCoord - center; + vec4 color = vec4(0.0); + for (int i = -4; i <= 4; i++) { + color += texture.Sample(texture, input.texCoord + vec2(i) * dir * blurAmount / 10.0); + } + color /= 9.0; + return color; } - color /= 9.0; - fragColor = color; } -} } \ No newline at end of file diff --git a/samples/Real-Time Water Simulation.cgl b/samples/Real-Time Water Simulation.cgl index fd2387e..5c51a65 100644 --- a/samples/Real-Time Water Simulation.cgl +++ b/samples/Real-Time Water Simulation.cgl @@ -1,21 +1,34 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec3 position @POSITION; + }; - fragment { - input vec3 position; - output vec4 fragColor; + struct VSOutput { + vec4 color @COLOR; + }; - float waveFunction(vec2 p) { - return sin(p.x * 10.0) * 0.1; + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + vec2 waveFunction(vec2 p) { + return sin(p.x * 10.0 + iTime) * 0.1; } - void main() { - vec2 uv = position.xy; - float wave = waveFunction(uv); + VSOutput main(VSInput input) { + VSOutput output; + vec2 uv = input.position.xy; + vec2 wave = waveFunction(uv); vec3 waterColor = vec3(0.0, 0.3, 0.8) * (1.0 - wave); - fragColor = vec4(waterColor, 1.0); + output.color = vec4(waterColor, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + return input.color; } } } \ No newline at end of file diff --git a/samples/Realistic Skin Shader.cgl b/samples/Realistic Skin Shader.cgl index da861c1..3f87e38 100644 --- a/samples/Realistic Skin Shader.cgl +++ b/samples/Realistic Skin Shader.cgl @@ -1,21 +1,34 @@ shader main { - vertex { - // Vertex shader logic goes here. - } + struct VSInput { + vec3 position @POSITION; + }; - fragment { - input vec3 position; - output vec4 fragColor; + struct VSOutput { + vec4 color @COLOR; + }; - float subsurfaceScattering(vec3 position) { - return exp(-length(position) * 2.0); + struct PSInput { + vec4 color @COLOR; + }; + + vertex { + vec2 subsurfaceScattering(vec3 pos) { + return exp(-length(pos) * 2.0); } - void main() { + VSOutput main(VSInput input) { + VSOutput output; vec3 baseColor = vec3(1.0, 0.8, 0.6); - float sss = subsurfaceScattering(position); - vec3 color = baseColor * sss; - fragColor = vec4(color, 1.0); + vec2 sss = subsurfaceScattering(input.position); + vec3 colorComputed = baseColor * sss; + output.color = vec4(colorComputed, 1.0); + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + return input.color; } } } \ No newline at end of file diff --git a/samples/Reflection Mapping.cgl b/samples/Reflection Mapping.cgl index 554e3a1..cc42019 100644 --- a/samples/Reflection Mapping.cgl +++ b/samples/Reflection Mapping.cgl @@ -1,30 +1,48 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - input vec2 texCoord; - output vec3 fragPosition; - output vec3 fragNormal; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragPosition = position; - fragNormal = normal; - fragTexCoord = texCoord; - } -} + struct VSOutput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - input vec2 fragTexCoord; - output vec4 fragColor; + sampler2D texture; + samplerCube environmentMap; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + output.normal = input.normal; + output.texCoord = input.texCoord; + return output; + } + } - void main() { - vec3 reflectDir = reflect(normalize(fragPosition), normalize(fragNormal)); - vec3 environmentColor = texture(environmentMap, reflectDir); - vec3 objectColor = texture(texture, fragTexCoord); - fragColor = vec4(mix(objectColor, environmentColor, 0.5), 1.0); + fragment { + vec4 main(PSInput input) @SV_TARGET { + // Calculate the reflection direction based on the normal and view direction + vec3 reflectDir = reflect(normalize(input.position), normalize(input.normal)); + + // Sample the environment map using the reflection direction + vec3 environmentColor = texture.Sample(environmentMap, reflectDir).rgb; + + // Sample the base texture color + vec3 objectColor = texture.Sample(texture, input.texCoord).rgb; + + // Mix the environment color and the object color + return vec4(mix(objectColor, environmentColor, 0.5), 1.0); + } } -} -} +} \ No newline at end of file diff --git a/samples/Rim Lighting.cgl b/samples/Rim Lighting.cgl index 827a472..70c85f5 100644 --- a/samples/Rim Lighting.cgl +++ b/samples/Rim Lighting.cgl @@ -1,27 +1,41 @@ shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; - // Pass through position and normal to the fragment shader - void main() { - fragPosition = position; - fragNormal = normal; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec3 normal @NORMAL; + }; + + struct PSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + cbuffer ShaderUniforms { + vec3 viewPos; + vec3 rimColor; + vec2 rimPower; } -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - 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); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + output.normal = input.normal; + return output; + } } -} -} + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 viewDir = normalize(viewPos - input.position); + vec2 rim = pow(1.0 - max(dot(input.normal, viewDir), 0.0), rimPower); + vec3 color = vec3(0.5, 0.5, 0.5) + rim * rimColor; + return vec4(color, 1.0); + } + } +} \ No newline at end of file diff --git a/samples/Sepia Tone.cgl b/samples/Sepia Tone.cgl index 7ed0ab8..69fb37f 100644 --- a/samples/Sepia Tone.cgl +++ b/samples/Sepia Tone.cgl @@ -1,25 +1,37 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } -} + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + sampler2D texture; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } + } - void main() { - vec4 color = texture(texture, fragTexCoord); - vec3 sepia = vec3( - dot(color, vec3(0.393, 0.769, 0.189)), - dot(color, vec3(0.349, 0.686, 0.168)), - dot(color, vec3(0.272, 0.534, 0.131)) - ); - fragColor = vec4(sepia, 1.0); + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec4 color = texture.Sample(texture, input.texCoord); + vec3 sepia = vec3( + dot(color.rgb, vec3(0.393, 0.769, 0.189)), + dot(color.rgb, vec3(0.349, 0.686, 0.168)), + dot(color.rgb, vec3(0.272, 0.534, 0.131)) + ); + return vec4(sepia, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Shadow Mapping.cgl b/samples/Shadow Mapping.cgl index 1dfe8d3..0dc184c 100644 --- a/samples/Shadow Mapping.cgl +++ b/samples/Shadow Mapping.cgl @@ -1,28 +1,58 @@ shader main { -vertex { - input vec3 position; - input vec2 texCoord; - output vec4 fragPosition; - output vec2 fragTexCoord; - - void main() { - fragPosition = vec4(position, 1.0); - fragTexCoord = texCoord; - } + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; + + struct VSOutput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + vec2 texCoord @TEXCOORD0; + }; + + sampler2D texture; + sampler2D shadowMap; + cbuffer ShaderUniforms { + mat4 lightSpaceMatrix; } -fragment { - input vec4 fragPosition; - input vec2 fragTexCoord; - output vec4 fragColor; - void main() { - vec3 projCoords = fragPosition / fragPosition; - projCoords = projCoords * 0.5 + 0.5; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + output.normal = input.normal; + output.texCoord = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + // Transform the fragment position into light space coordinates + vec4 lightSpacePos = mul(lightSpaceMatrix, vec4(input.position, 1.0)); + + // Perform perspective divide to get normalized device coordinates + vec3 projCoords = lightSpacePos.xyz / lightSpacePos.w; + + // Transform from [-1,1] range to [0,1] range + projCoords = projCoords * 0.5 + 0.5; - float shadow = texture(shadowMap, projCoords) < projCoords ? 0.5 : 1.0; - vec3 color = texture(texture, fragTexCoord) * shadow; - fragColor = vec4(color, 1.0); + // Check if the fragment is in shadow + vec2 shadow = texture.Sample(shadowMap, projCoords.xy).r < projCoords.z ? 0.5 : 1.0; + + // Sample the color from the texture and apply the shadow factor + vec3 color = texture.Sample(texture, input.texCoord).rgb * shadow; + + // Output the final color with full opacity + return vec4(color, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Simple Color Shader.cgl b/samples/Simple Color Shader.cgl index 43b8c44..44e8050 100644 --- a/samples/Simple Color Shader.cgl +++ b/samples/Simple Color Shader.cgl @@ -1,19 +1,28 @@ shader main { - vertex { - input vec3 position; - output vec4 color; + struct VSInput { + vec3 position @POSITION; + }; + + struct VSOutput { + vec3 positionOut @POSITION; + vec4 color @COLOR; + }; + + struct PSInput { + vec4 color @COLOR; + }; - void main() { - color = vec4(position, 1.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.color = vec4(input.position, 1.0); + return output; } } - - fragment { - input vec4 color; - output vec4 fragColor; - void main() { - fragColor = color; + fragment { + vec4 main(PSInput input) @SV_TARGET { + return input.color; } } -} +} \ No newline at end of file diff --git a/samples/Simple Lighting Shader.cgl b/samples/Simple Lighting Shader.cgl index 894d6f3..017d837 100644 --- a/samples/Simple Lighting Shader.cgl +++ b/samples/Simple Lighting Shader.cgl @@ -1,52 +1,42 @@ -shader main{ - vertex { - input vec3 position; - input vec2 texCoord; - input vec3 normal; +shader main { + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; + }; + + struct VSOutput { + vec3 fragNormal @NORMAL; + vec3 fragPosition @POSITION; + }; + + struct PSInput { + vec3 fragNormal @NORMAL; + vec3 fragPosition @POSITION; + }; + + cbuffer ShaderUniforms { + vec3 lightPos; + vec3 lightColor; +} - // Vertex shader code - // Example: - // Pass texCoord and normal to the fragment shader - // gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); - } - fragment { - input vec3 position; - input vec2 texCoord; - input vec3 normal; - 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; + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.fragNormal = input.normal; + output.fragPosition = input.position; + gl_Position = vec4(input.position, 1.0); + return output; } + } - void main() { - vec4 texColor = texture2D(texture, texCoord); - vec3 lightDir = normalize(lightPos - position); - vec3 viewDir = normalize(viewPos - position); - - vec3 ambient = ambientColor; - vec3 diffuse = texColor.rgb; - vec3 specular = vec3(0.0); - - // Outer loop - for (int i = 0; i < outerIterations; i++) { - // Inner loop - for (int j = 0; j < innerIterations; j++) { - // Compute lighting - vec3 tempSpecular = calculateSpecular(normal, lightDir, viewDir); - specular += tempSpecular; - } - - // Reduce diffuse intensity progressively - diffuse *= 0.9; - } - - vec3 lighting = ambient + diffuse + specular; - fragColor = vec4(lighting, 1.0); + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 norm = normalize(input.fragNormal); + vec3 lightDir = normalize(lightPos - input.fragPosition); + vec2 diff = max(dot(norm, lightDir), 0.0); + vec3 color = lightColor * diff; + return vec4(color, 1.0); } } -} } \ No newline at end of file diff --git a/samples/Spherical Distortion.cgl b/samples/Spherical Distortion.cgl index d33230a..1f2d53c 100644 --- a/samples/Spherical Distortion.cgl +++ b/samples/Spherical Distortion.cgl @@ -1,27 +1,42 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec2 texCoord @TEXCOORD0; + }; - void main() { - fragTexCoord = texCoord; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec2 texCoord @TEXCOORD0; + }; + + struct PSInput { + vec2 texCoord @TEXCOORD0; + }; + + cbuffer ShaderUniforms { + vec2 strength; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; +sampler2D texture; - void main() { - vec2 uv = fragTexCoord * 2.0 - 1.0; - float len = length(uv); - if (len < 1.0) { - uv = uv * (1.0 + strength * len * len); + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.texCoord = input.texCoord; + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 uv = input.texCoord * 2.0 - 1.0; // Convert texCoord from [0, 1] to [-1, 1] range + vec2 len = length(uv); // Compute the distance from the center + if (len < 1.0) { + uv = uv * (1.0 + strength * len * len); // Apply distortion based on distance + } + uv = uv * 0.5 + 0.5; // Convert back to [0, 1] range + return texture.Sample(texture, uv); // Sample the texture at the distorted coordinates } - uv = uv * 0.5 + 0.5; - fragColor = texture(texture, uv); } -} -} -} } \ No newline at end of file diff --git a/samples/Spherical Mapping.cgl b/samples/Spherical Mapping.cgl index df7cc0d..71fbf10 100644 --- a/samples/Spherical Mapping.cgl +++ b/samples/Spherical Mapping.cgl @@ -1,24 +1,36 @@ shader main { -vertex { - input vec3 position; - output vec2 fragTexCoord; + struct VSInput { + vec3 position @POSITION; + vec3 position @POSITION; + }; - void main() { - // Transform position to texture coordinates - vec3 norm = normalize(position); - float u = 0.5 + atan(norm.z, norm.x) / (2.0 * 3.14159265); - float v = 0.5 - asin(norm.y) / 3.14159265; - fragTexCoord = vec2(u, v); - } -} + struct VSOutput { + vec3 positionOut @POSITION; + vec3 position @POSITION; + }; + + struct PSInput { + vec3 position @POSITION; + }; -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; + sampler2D texture; + + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.position = input.position; + return output; + } + } - void main() { - vec4 color = texture(texture, fragTexCoord); - fragColor = vec4(color, 1.0); + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 norm = normalize(input.position); + vec2 u = 0.5 + atan2(norm.z, norm.x) / (2.0 * 3.14159265); + vec2 v = 0.5 - asin(norm.y) / 3.14159265; + vec2 uv = vec2(u, v); + vec4 color = texture.Sample(texture, uv); + return vec4(color.rgb, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Toon Shader.cgl b/samples/Toon Shader.cgl index 4565d1d..d69e124 100644 --- a/samples/Toon Shader.cgl +++ b/samples/Toon Shader.cgl @@ -1,29 +1,43 @@ -26. Toon Shader shader main { -vertex { - input vec3 position; - input vec3 normal; - output vec3 fragPosition; - output vec3 fragNormal; + struct VSInput { + vec3 position @POSITION; + vec3 normal @NORMAL; // Semantic for normal input + }; - // Pass through position and normal to the fragment shader - void main() { - fragPosition = position; - fragNormal = normal; - } + struct VSOutput { + vec3 positionOut @POSITION; + vec4 color @COLOR; // Output color + }; + + struct PSInput { + vec4 color @COLOR; // Input color for the fragment shader + }; + + cbuffer ShaderUniforms { + vec3 lightPos; + vec3 colorLevels; } -fragment { - input vec3 fragPosition; - input vec3 fragNormal; - output vec4 fragColor; - void main() { - vec3 norm = normalize(fragNormal); - vec3 lightDir = normalize(lightPos - fragPosition); - float itensity = dot(norm, lightDir); - vec3 color = vec3(step(colorLevels.x, itensity), step(colorLevels.y, itensity), step(colorLevels.z, itensity)); - fragColor = vec4(color, 1.0); + vertex { + VSOutput main(VSInput input) { + VSOutput output; + output.color = vec4(input.position, 1.0); // Assign position to color + // Note: The normal value should also be passed if needed for lighting calculations + return output; + } + } + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec3 position = input.color.xyz; // Extract the position + vec3 normal = input.color.wyz; // Extract the normal (assuming w holds a component of normal) + + vec3 norm = normalize(normal); + vec3 lightDir = normalize(lightPos - position); + vec2 intensity = dot(norm, lightDir); + vec3 fragColorValue = vec3(step(colorLevels, intensity)); + return vec4(fragColorValue, 1.0); + } } -} } \ No newline at end of file diff --git a/samples/Underwater Distortion.cgl b/samples/Underwater Distortion.cgl index 35aca24..dd2d1d7 100644 --- a/samples/Underwater Distortion.cgl +++ b/samples/Underwater Distortion.cgl @@ -1,21 +1,28 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct PSInput { + vec2 texCoord @TEXCOORD0; // Input texture coordinate + }; - void main() { - fragTexCoord = texCoord; - } + output vec4 fragColor; + + cbuffer ShaderUniforms { + vec2 time; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; +sampler2D texture; - void main() { - vec2 uv = fragTexCoord + vec2(sin(fragTexCoord * 10.0 + time) * 0.01, cos(fragTexCoord * 10.0 + time) * 0.01); - vec4 color = texture(texture, uv); - fragColor = vec4(color, 1.0); + + fragment { + vec4 main(PSInput input) @SV_TARGET { + // Compute the UV coordinates with a time-based offset + vec2 uv = input.texCoord + vec2(sin(input.texCoord.y * 10.0 + time) * 0.01, + cos(input.texCoord.x * 10.0 + time) * 0.01); + + // Sample the texture at the modified UV coordinates + vec4 color = texture(texture, uv); + + // Set the fragment color + return vec4(color.rgb, 1.0); + } } -} -} +} \ No newline at end of file diff --git a/samples/Vignette Effect.cgl b/samples/Vignette Effect.cgl index e660325..6ea8a1d 100644 --- a/samples/Vignette Effect.cgl +++ b/samples/Vignette Effect.cgl @@ -1,22 +1,31 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct PSInput { + vec2 texCoord @TEXCOORD0; // Input texture coordinate + }; - void main() { - fragTexCoord = texCoord; - } + output vec4 fragColor; + + cbuffer ShaderUniforms { + vec2 radius; + vec2 softness; } -fragment { - input vec2 fragTexCoord; - output vec4 fragColor; +sampler2D texture; - void main() { - vec4 color = texture(texture, fragTexCoord); - float dist = distance(fragTexCoord, vec2(0.5)); - float vignette = smoothstep(radius, radius - softness, dist); - fragColor = vec4(color * vignette, 1.0); + + fragment { + vec4 main(PSInput input) @SV_TARGET { + // Sample the texture at the input texture coordinates + vec4 color = texture(texture, input.texCoord); + + // Calculate the distance from the center (0.5, 0.5) + vec2 dist = distance(input.texCoord, vec2(0.5)); + + // Compute the vignette effect using smoothstep + vec2 vignette = smoothstep(radius, radius - softness, dist); + + // Set the fragment color applying the vignette effect + return vec4(color.rgb * vignette, 1.0); + } } -} -} +} \ No newline at end of file diff --git a/samples/Voronoi Pattern Shader.cgl b/samples/Voronoi Pattern Shader.cgl index 83ff0c8..4dee90b 100644 --- a/samples/Voronoi Pattern Shader.cgl +++ b/samples/Voronoi Pattern Shader.cgl @@ -1,36 +1,30 @@ shader main { -vertex { - input vec2 texCoord; - output vec2 fragTexCoord; + struct PSInput { + vec2 texCoord @TEXCOORD0; // Input texture coordinate + }; - void main() { - fragTexCoord = texCoord * 10.0; // Scale texture coordinates - } -} - -fragment { - input vec2 fragTexCoord; output vec4 fragColor; - float hash(vec2 p) { + vec2 hash(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 34.56); - return fract(p * p); + return fract(p.x * p.y); } vec2 random2(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); } - float voronoi(vec2 p) { + vec2 voronoi(vec2 p) { vec2 g = floor(p); vec2 f = fract(p); - for (int j = -1; j < 1; j++) { - for (int i = -1; i < 1; i++) { - vec2 b = vec2(i,j); + vec2 d = 1.0; + for (int j = -1; j <= 1; j++) { + for (int i = -1; i <= 1; i++) { + vec2 b = vec2(vec2(i), vec2(j)); vec2 r = b - f + random2(g + b); - float d2 = dot(r, r); + vec2 d2 = dot(r, r); if (d2 < d) { d = d2; @@ -41,10 +35,15 @@ fragment { return d; } - void main() { - float v = voronoi(fragTexCoord); - vec3 color = vec3(v); - fragColor = vec4(color, 1.0); + uniform sampler2D texture; + + fragment { + vec4 main(PSInput input) @SV_TARGET { + vec2 uv = input.texCoord * 10.0; + vec2 v = voronoi(uv); + + vec3 color = vec3(v); + return vec4(color, 1.0); + } } -} -} +} \ No newline at end of file