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

fixed some broken code and updated the few samples #5

Open
wants to merge 3 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
71 changes: 44 additions & 27 deletions samples/Adv complexShader.cgl
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,63 @@ shader complexShader {
vertex {
input vec3 position;
input vec2 texCoord;
output vec2 fragTexCoord;
output vec3 fragPosition;
input vec3 normal;
output vec4 color;

vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) {
return normalize(normal * mapNormal);
}

void main() {
fragTexCoord = texCoord;
fragPosition = position; // Directly pass position to fragment shader
gl_Position = vec4(position, 1.0);
// Pass texture coordinates and normals to the fragment shader
color.xy = texCoord;
color.zw = normal;

// Simulate normal map calculation in the vertex shader
vec3 mapNormal = normalize(vec3(texCoord, 1.0) * 2.0 - 1.0);
vec3 perturbedNormal = applyNormalMapping(normal, mapNormal);

// Pass perturbed normal to the fragment shader
color.rgb = vec3(perturbedNormal);
color.a = 1.0; // Alpha channel
}
}

fragment {
input vec2 fragTexCoord;
input vec3 fragPosition;
input vec4 color;
input vec2 texCoord;
output vec4 fragColor;

vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) {
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Default light color

float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Default light color

float specularStrength = 0.5;
vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) {
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Default light color

return ambient + diffuse + specular;
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
return spec * vec3(1.0, 1.0, 1.0);
}

void main() {
vec4 texColor = texture2D(textureSampler, fragTexCoord); // Assumed default sampler
vec3 normal = normalize(cross(dFdx(fragPosition), dFdy(fragPosition)));
vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Default light direction
vec3 viewDir = normalize(-fragPosition); // Simple view direction
vec3 perturbedNormal = normalize(color.rgb);
vec3 lightPos = vec3(1.0, 1.0, 1.0); // Example light position
vec3 viewPos = vec3(0.0, 0.0, 1.0); // Example view position
vec3 lightDir = normalize(lightPos - vec3(color));
vec3 viewDir = normalize(viewPos - vec3(color));

// Ambient component
vec3 ambient = vec3(0.1, 0.1, 0.1); // Example ambient color

// Diffuse component
float diff = max(dot(perturbedNormal, lightDir), 0.0);
vec3 diffuse = diff * vec3(0.8, 0.8, 0.8); // Example diffuse color

// Specular component
float shininess = 32.0; // Example shininess
vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir, shininess);

// Combine lighting components
vec3 lighting = ambient + diffuse + specular;

vec3 lighting = calculateLighting(normal, lightDir, viewDir);
vec4 color = vec4(lighting, 1.0) * texColor;
vec4 finalColor = vec4(lighting, 1.0);

fragColor = color;
fragColor = finalColor;
}
}
}
}
24 changes: 18 additions & 6 deletions samples/Advanced Post-Processing Effects.cgl
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
shader main {
vertex {
// Vertex shader logic goes here.
input vec2 texCoord;
output vec4 color;

void main() {
// Pass through texture coordinates as color
color = vec4(texCoord, 0.0, 1.0);
}
}

fragment {
input vec2 texCoord;
input vec4 color;
output vec4 fragColor;

vec4 textureSample(vec2 texCoord) {
// Example function to simulate texture sampling
return vec4(texCoord.x, texCoord.y, 0.5, 1.0);
}

void main() {
float brightness = 0.0; // Placeholder for brightness calculation
float brightness = textureSample(color.xy);
float bloom = max(0.0, brightness - 0.5);
vec3 color = vec3(0.0); // Placeholder for texture color
fragColor = vec4(color + vec3(bloom), 1.0);
vec3 texColor = textureSample(color.xy);
vec3 colorWithBloom = texColor + vec3(bloom);
fragColor = vec4(colorWithBloom, 1.0);
}
}
}
}
59 changes: 34 additions & 25 deletions samples/Advanced Shader.cgl
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,57 @@ shader advancedShader {
vertex {
input vec3 position;
input vec2 texCoord;
output vec2 fragTexCoord;
output vec3 fragPosition;
output vec4 color;

void main() {
fragTexCoord = texCoord;
fragPosition = position;
gl_Position = vec4(position, 1.0);
}
}

fragment {
input vec2 fragTexCoord;
input vec3 fragPosition;
output vec4 fragColor;

vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) {
vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir, float shininess) {
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
return spec * vec3(1.0, 1.0, 1.0); // White specular highlight
}

void main() {
vec4 texColor = texture2D(textureSampler, fragTexCoord); // Default sampler
// Pass texture coordinates to the fragment shader
color.xy = texCoord;

// Simulate ambient, light position, view position, and shininess
vec3 ambientColor = vec3(0.1, 0.1, 0.1);
vec3 lightPos = vec3(10.0, 10.0, 10.0);
vec3 viewPos = vec3(0.0, 0.0, 10.0);
float shininess = 32.0;

vec3 norm = normalize(cross(dFdx(fragPosition), dFdy(fragPosition)));
vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position
vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position
// Calculate normal in the vertex shader
vec3 norm = normalize(cross(dFdx(position), dFdy(position)));
vec3 lightDir = normalize(lightPos - position);
vec3 viewDir = normalize(viewPos - position);

// Ambient component
vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color
vec3 ambient = ambientColor;

// Diffuse component
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // White diffuse light

// Specular component
vec3 specular = calculateSpecular(norm, lightDir, viewDir);
vec3 specular = calculateSpecular(norm, lightDir, viewDir, shininess);

vec3 lighting = ambient + diffuse + specular;
vec4 color = vec4(lighting, 1.0) * texColor;
color.rgb = lighting;
color.a = 1.0; // Alpha channel
}
}

fragment {
input vec4 color;
output vec4 fragColor;

vec4 textureSample(vec2 texCoord) {
// Example function to simulate texture sampling
return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Simulated texture color
}

fragColor = color;
void main() {
vec4 texColor = textureSample(color.xy);
fragColor = vec4(color.rgb * texColor.rgb, 1.0);
}
}
}
}
72 changes: 31 additions & 41 deletions samples/Advanced calculateLighting.cgl
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,50 @@ shader complexShader {
vertex {
input vec3 position;
input vec2 texCoord;
input vec3 normal;
output vec2 fragTexCoord;
output vec3 fragPosition;
output vec3 fragNormal;
output vec4 color;

void main() {
fragTexCoord = texCoord;
fragPosition = position;
fragNormal = normal;
gl_Position = vec4(position, 1.0);
}
}
vec3 calculateLighting(vec3 normal, vec3 lightDir, vec3 viewDir) {
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * vec3(1.0, 1.0, 1.0); // Example ambient color

fragment {
input vec2 fragTexCoord;
input vec3 fragPosition;
input vec3 fragNormal;
output vec4 fragColor;
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * vec3(1.0, 1.0, 1.0); // Example diffuse color

vec3 calculateSpecular(vec3 normal, vec3 lightDir, vec3 viewDir) {
float specularStrength = 0.5;
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Default shininess
return spec * vec3(1.0, 1.0, 1.0) * 1.0; // Default specular intensity
}
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
vec3 specular = specularStrength * spec * vec3(1.0, 1.0, 1.0); // Example specular color

vec3 applyNormalMapping(vec3 normal, vec3 mapNormal) {
return normalize(normal * mapNormal);
return ambient + diffuse + specular;
}

void main() {
vec4 diffuseColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler
vec4 specularColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler
vec4 normalMapColor = texture2D(texture2D(fragTexCoord)); // Default texture sampler

vec3 mapNormal = normalize(normalMapColor.rgb * 2.0 - 1.0);
vec3 perturbedNormal = applyNormalMapping(fragNormal, mapNormal);
// Pass texture coordinates to the fragment shader
color.xy = texCoord;

vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0) - fragPosition); // Default light position
vec3 viewDir = normalize(vec3(0.0, 0.0, 1.0) - fragPosition); // Default view position
// Calculate normal in the vertex shader
vec3 normal = normalize(cross(dFdx(position), dFdy(position)));
vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); // Example light direction
vec3 viewDir = normalize(-position); // Example view direction

// Ambient component
vec3 ambient = vec3(0.1, 0.1, 0.1); // Default ambient color

// Diffuse component
float diff = max(dot(perturbedNormal, lightDir), 0.0);
vec3 diffuse = diff * diffuseColor.rgb;
vec3 lighting = calculateLighting(normal, lightDir, viewDir);
color.rgb = lighting;
color.a = 1.0; // Alpha channel
}
}

// Specular component
vec3 specular = calculateSpecular(perturbedNormal, lightDir, viewDir);
fragment {
input vec4 color;
output vec4 fragColor;

vec3 lighting = ambient + diffuse + specular;
vec4 color = vec4(lighting, 1.0) * diffuseColor;
vec4 textureSample(vec2 texCoord) {
// Example function to simulate texture sampling
return vec4(texCoord.x, texCoord.y, 1.0 - texCoord.x, 1.0);
}

fragColor = color;
void main() {
vec4 texColor = textureSample(color.xy); // Simulated texture sampling
fragColor = vec4(color.rgb * texColor.rgb, 1.0);
}
}
}
35 changes: 21 additions & 14 deletions samples/Anaglyph 3D.cgl
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
shader main {
vertex {
input vec2 texCoord;
output vec2 fragTexCoord;
vertex {
input vec2 texCoord;
output vec4 color;

void main() {
fragTexCoord = texCoord;
void main() {
// Pass through texture coordinates as color
color = vec4(texCoord, 0.0, 1.0);
}
}
}

fragment {
input vec2 fragTexCoord;
output vec4 fragColor;
fragment {
input vec4 color;
output vec4 fragColor;

// Define a simulated texture sampling function
vec4 textureSample(vec2 texCoord) {
// Simulated texture sampling: using simple color mapping
return vec4(texCoord.x, texCoord.y, 0.5, 1.0); // Sample color with brightness in the red channel
}

void main() {
vec4 leftColor = texture(texture, fragTexCoord - eyeOffset);
vec4 rightColor = texture(texture, fragTexCoord + eyeOffset);
fragColor = vec4(leftColor, rightColor, rightColor, 1.0);
void main() {
vec4 leftColor = textureSample(color.xy - vec2(0.1)); // Simulate left texture sample
vec4 rightColor = textureSample(color.xy + vec2(0.1)); // Simulate right texture sample
fragColor = vec4(leftColor.r, rightColor.g, rightColor.b, 1.0);
}
}
}
}
21 changes: 13 additions & 8 deletions samples/Atmospheric Scattering.cgl
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
shader main {
vertex {
// Vertex shader logic goes here.
}

fragment {
input vec3 position;
output vec4 fragColor;
output vec4 color;

vec3 rayleighScattering(float theta) {
return vec3(0.5, 0.7, 1.0) * pow(max(0.0, 1.0 - theta * theta), 3.0);
}

void main() {
float theta = position.y;
vec3 color = rayleighScattering(theta);
fragColor = vec4(color, 1.0);
vec3 colorComputed = rayleighScattering(theta);
color = vec4(colorComputed, 1.0);
}
}

fragment {
input vec4 color;
output vec4 fragColor;

void main() {
fragColor = color;
}
}
}
}
Loading