Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure Examples #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 82 additions & 31 deletions samples/Adv complexShader.cgl
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
39 changes: 30 additions & 9 deletions samples/Advanced Post-Processing Effects.cgl
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
77 changes: 47 additions & 30 deletions samples/Advanced Shader.cgl
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
96 changes: 48 additions & 48 deletions samples/Advanced calculateLighting.cgl
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading