






Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!







World Space attribute vec3 inPosition; // Position vector received attribute vec3 inNormal; // Normal vector received varying vec3 fsPosition; // Position vector for FS varying vec3 fsNormal; // Normal vector for FS uniform mat4 wvpMatrix; // World,View,Project Matrix for the vertices uniform mat4 wMatrix; // World matrix to transform vertices in world space uniform mat3 nwMatrix; // Matrix to transform normal vectors in world space void main(void) { fsPosition = (wMatrix * vec4(inPosition, 1.0)).xyz; fsNormal = nwMatrix * inNormal; gl_Position = wvpMatrix * vec4(inPosition, 1.0); } Camera Space attribute vec3 inPosition; // Position vector received attribute vec3 inNormal; // Normal vector received varying vec3 fsPosition; // Position vector for FS varying vec3 fsNormal; // Normal vector for FS uniform mat4 wvpMatrix; // World,View,Project Matrix for the vertices uniform mat4 wvMatrix; // World,View Matrix to transform vertices in camera space uniform mat3 nwvMatrix; // Matrix to transform normal vectors in camera space void main(void) { fsPosition = (wvMatrix * vec4(inPosition, 1.0)).xyz; fsNormal = nwvMatrix * inPosition; gl_Position = wvpMatrix * vec4(inPosition, 1.0); } Static / Object Space attribute vec3 inPosition; // Position vector received attribute vec3 inNormal; // Normal vector received varying vec3 fsPosition; // Position vector for FS varying vec3 fsNormal; // Normal vector for FS uniform mat4 wvpMatrix; // World,View,Project Matrix for the vertices void main(void) { fsPosition = inPosition; fsNormal = inNormal; gl_Position = wvpMatrix * vec4(inPosition, 1.0);
Textures (add to the previous space) attribute vec2 inDiffUV; // UV Position vector for diffuse texture received attribute vec2 inSpecUV; // UV Position vector for specular texture received varying vec2 fsDiffUV; // UV Position vector for diffuse texture for FS varying vec2 fsSpecUV; // UV Position vector for specular texture for FS void main(void) { fsDiffUV = inDiffUV; // UVs never need to be transformed fsSpecUV = inSpecUV; // UVs never need to be transformed }
World/Object/Static Space precision highp float; varying vec3 fsPosition; // Position vector received from VS varying vec3 fsNormal; // Normal vector received from VS uniform vec3 eyePosition; // Position of the camera in the space coordinates system chosen // Material uniform vec4 mDiffColor; // Material's diffuse color … void main(void) { vec3 nNormal = normalize(fsNormal); // Normalized normal vec3 nEyeDirection = normalize(eyePosition - fsPosition); … } Camera Space precision highp float; varying vec3 fsPosition; // Position vector received from VS varying vec3 fsNormal; // Normal vector received from VS // Material uniform vec4 mDiffColor; // Material's diffuse color … void main(void) { vec3 nNormal = normalize(fsNormal); // Normalized normal vec3 nEyeDirection = normalize(- fsPosition); // Direction from this point toward the camera, which is in (0,0,0) …
// Diffuse component vec4 diffuse; diffuse = mDiffColor * lightColor * lightDecay * (max(dot(nNormal, nLightDirection), 0.0)); gl_FragColor = min(diffuse, vec4(1,1,1,1)); } Point light – Squared Decay // Lights uniform vec4 lightColor; // Color of the (primary) light uniform vec3 lightPosition; // Light source position vector uniform float lightDecayScale; // Light decay scale for computing the decay factor void main(void) { … // Light model vec3 nLightDirection = normalize(lightPosition - fsPosition); // Direction of the light source wrt the current position, normalized float lightDecay = lightDecayScale / pow(length(lightPosition - fsPosition), 2.0); // Diffuse component vec4 diffuse; diffuse = mDiffColor * lightColor * lightDecay * (max(dot(nNormal, nLightDirection), 0.0)); gl_FragColor = min(diffuse, vec4(1,1,1,1)); } Spot Light // Lights uniform vec4 lightColor; // Color of the (primary) light uniform vec3 lightPosition; // Light source position vector uniform vec3 spotLightDirection; // Constant incident spot light ray direction uniform float spotLightCosin; // Spot light cos-in parameter uniform float spotLightCosout; // Spot light cos-out parameter void main(void) { … // Light model vec3 nLightDirection = normalize(lightPosition - fsPosition); // Direction of the light source wrt the current position, normalized float lightDecay; float lightCone = - dot(nLightDirection, normalize(spotLightDirection)); if(lightCone < spotLightCosout){ lightDecay = 0.0;} else if (lightCone > spotLightCosin){ lightDecay = 1.0;} else{ lightDecay = (lightCone - spotLightCosout) / (spotLightCosin - spotLightCosout);} // Diffuse component vec4 diffuse; diffuse = mDiffColor * lightColor * lightDecay * (max(dot(nNormal, - normalize(spotLightDirection)), 0.0)); gl_FragColor = min(diffuse, vec4(1,1,1,1));
// Material uniform vec4 mEmitColor; // Material's emit color void main(void) { … gl_FragColor = min(diffuse + mEmitColor, vec4(1,1,1,1)); //aggiungo mEmitColor!! }
// Lights uniform vec4 ambLightColor; // Ambient light color void main(void) { … // Ambient component vec4 ambient = ambLightColor * mDiffColor; gl_FragColor = min(diffuse + ambient, vec4(1,1,1,1)); //aggiungo ambient!! }
// Material uniform vec4 mAmbientColor; // Material's specific ambient color // Lights uniform vec4 ambLightColor; // Ambient light color void main(void) { … // Ambient component vec4 ambient = ambLightColor * mAmbientColor; gl_FragColor = min(diffuse + ambient, vec4(1,1,1,1)); //aggiungo ambient!!
Phong algorithm (da integrare con eventuale texture) // Material uniform vec4 mSpecColor; // Material's specular color; uniform float mSpecPower; // Material's specular color power factor void main(void) { … // Specular component vec4 specular; vec3 refl = - reflect(nLightDirection, nNormal); // Reflection vector specular = mSpecColor * lightColor * pow(max(dot(refl, nEyeDirection), 0.0), mSpecPower); gl_FragColor = min(diffuse + specular, vec4(1,1,1,1)); //aggiungo specular!!! } Specular texture con Phong (bisogna aggiungerlo al codice sopra) varying vec2 fsSpecUV; // UV Position vector for specular texture received from VS // Textures uniform sampler2D mSpecTexture; // Material specular texture uniform float mSpecInf; // Material specular texture intensity: 0---->solid , 1---->texture void main(void) { … // Specular component vec4 specular; vec3 refl = - reflect(nLightDirection, nNormal); // Reflection vector vec4 specColorMixture = texture(mSpecTexture, fsSpecUV) * mSpecInf + mSpecColor * (1- mSpecInf); specular = specColorMixture * lightColor * pow(max(dot(refl, nEyeDirection), 0.0), mSpecPower); gl_FragColor = min(diffuse + specular, vec4(1,1,1,1));
1- VERTEX SHADER (tre luci) attribute vec3 inPosition; // Position vector received from VS attribute vec3 inNormal; // Normal vector received from VS // World,View,Projection Matrix for the vertices uniform mat4 wvpMatrix; // Position of the camera in the space coordinates system chosen uniform vec3 eyePosition; // Position of the camera in the space coordinates system chosen // Material uniform vec4 mDiffColor; // Material's diffuse color uniform vec4 mSpecColor; // Material's specular color; uniform float mSpecPower; // Material's specular color power factor // Lights uniform vec4 light1Color; // Color of the (primary) light uniform vec3 light1Direction; // Constant incident light ray direction uniform vec4 light2Color; // Color of the (secondary) light uniform vec3 light2Position; // Light source position vector uniform float light2DecayFactor; // Constant Light decay factor uniform vec4 light3Color; // Color of the (primary) light uniform vec3 light3Position; // Light source position vector uniform vec3 spotLightDirection; // Constant incident spot light ray direction uniform float spotLightCosin; // Spot light cos-in parameter uniform float spotLightCosout; // Spot light cos-out parameter varying vec4 goureaudColor; void main(void) { gl_Position = wvpMatrix * vec4(inPosition, 1.0); vec3 nNormal = normalize(inNormal); // Normalized normal vec3 nEyeDirection = normalize(eyePosition - inPosition); // Light model //Directional Light vec3 nLight1Direction = - normalize(light1Direction); //Point Light with linear decay vec3 nLight2Direction = normalize(light2Position - fsPosition); float light2Decay = light2DecayFactor; //Spot Light vec3 nLight3Direction = normalize(light3Position - fsPosition); float light3Decay; float light3Cone = - dot(nLight3Direction, normalize(spotLightDirection)); if(light3Cone < spotLightCosout){ light3Decay = 0.0;} else if (light3Cone > spotLightCosin){ light3Decay = 1.0;} else{ light3Decay = (light3Cone - spotLightCosout) /(spotLightCosin - spotLightCosout);}
void main(void) { fsDiffUV = inDiffUV; gl_Position = wvpMatrix * vec4(inPosition, 1.0); vec3 nNormal = normalize(inNormal); vec3 nEyeDirection = normalize(eyePosition – inPosition); vec3 nLightDirection = normalize(lightPosition - inPosition); float lightDecay = lightDecayFactor; out vec4 diffuse; diffuse = lightColor * lightDecay * (max(dot(nNormal, nLightDirection), 0.0)); out vec4 specular; vec3 hVec = normalize(nEyeDirection + nLightDirection); specular = mSpecColor * lightColor * lightDecay * pow(max(dot(nNormal, hVec), 0.0), mSpecPower); } 2- FRAGMENT SHADER in vec2 fsDiffUV; out vec4 outColor; in vec4 diffuse; in vec4 specular; uniform vec4 ambLightColor; uniform sampler2D mDiffTexture; uniform float mDiffInf; uniform vec4 mDiffColor; void main(void) { vec4 diffColorMixture = texture(mDiffTexture, fsDiffUV) * mDiffInf + mDiffColor * (1 - mDiffInf); vec4 ambient = ambLightColor * mDiffColor; outColor = min(diffuse + specular + ambient + diffColorMixture * (sum of other components)), vec4(1,1,1,1));