Skip to content

Commit

Permalink
Fixed bug in article 07 GLSL
Browse files Browse the repository at this point in the history
Some drivers were complaining that "samplers cannot be structure or 
block members", which is true according to the GLSL 1.5 spec.
  • Loading branch information
tomdalling committed Mar 8, 2014
1 parent d01ad81 commit a84b7d3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
15 changes: 7 additions & 8 deletions linux/07_more_lighting/resources/fragment-shader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
uniform mat4 model;
uniform vec3 cameraPosition;

uniform struct Material {
sampler2D tex;
float shininess;
vec3 specularColor;
} material;
// material settings
uniform sampler2D materialTex;
uniform float materialShininess;
uniform vec3 materialSpecularColor;

uniform struct Light {
vec3 position;
Expand All @@ -25,7 +24,7 @@ out vec4 finalColor;
void main() {
vec3 normal = normalize(transpose(inverse(mat3(model))) * fragNormal);
vec3 surfacePos = vec3(model * vec4(fragVert, 1));
vec4 surfaceColor = texture(material.tex, fragTexCoord);
vec4 surfaceColor = texture(materialTex, fragTexCoord);
vec3 surfaceToLight = normalize(light.position - surfacePos);
vec3 surfaceToCamera = normalize(cameraPosition - surfacePos);

Expand All @@ -39,8 +38,8 @@ void main() {
//specular
float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), material.shininess);
vec3 specular = specularCoefficient * material.specularColor * light.intensities;
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), materialShininess);
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;

//attenuation
float distanceToLight = length(light.position - surfacePos);
Expand Down
6 changes: 3 additions & 3 deletions linux/07_more_lighting/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ static void RenderInstance(const ModelInstance& inst) {
//set the shader uniforms
shaders->setUniform("camera", gCamera.matrix());
shaders->setUniform("model", inst.transform);
shaders->setUniform("material.tex", 0); //set to 0 because the texture will be bound to GL_TEXTURE0
shaders->setUniform("material.shininess", asset->shininess);
shaders->setUniform("material.specularColor", asset->specularColor);
shaders->setUniform("materialTex", 0); //set to 0 because the texture will be bound to GL_TEXTURE0
shaders->setUniform("materialShininess", asset->shininess);
shaders->setUniform("materialSpecularColor", asset->specularColor);
shaders->setUniform("light.position", gLight.position);
shaders->setUniform("light.intensities", gLight.intensities);
shaders->setUniform("light.attenuation", gLight.attenuation);
Expand Down
15 changes: 7 additions & 8 deletions osx/07_more_lighting/resources/fragment-shader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
uniform mat4 model;
uniform vec3 cameraPosition;

uniform struct Material {
sampler2D tex;
float shininess;
vec3 specularColor;
} material;
// material settings
uniform sampler2D materialTex;
uniform float materialShininess;
uniform vec3 materialSpecularColor;

uniform struct Light {
vec3 position;
Expand All @@ -25,7 +24,7 @@ out vec4 finalColor;
void main() {
vec3 normal = normalize(transpose(inverse(mat3(model))) * fragNormal);
vec3 surfacePos = vec3(model * vec4(fragVert, 1));
vec4 surfaceColor = texture(material.tex, fragTexCoord);
vec4 surfaceColor = texture(materialTex, fragTexCoord);
vec3 surfaceToLight = normalize(light.position - surfacePos);
vec3 surfaceToCamera = normalize(cameraPosition - surfacePos);

Expand All @@ -39,8 +38,8 @@ void main() {
//specular
float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), material.shininess);
vec3 specular = specularCoefficient * material.specularColor * light.intensities;
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), materialShininess);
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;

//attenuation
float distanceToLight = length(light.position - surfacePos);
Expand Down
6 changes: 3 additions & 3 deletions osx/07_more_lighting/source/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ static void RenderInstance(const ModelInstance& inst) {
//set the shader uniforms
shaders->setUniform("camera", gCamera.matrix());
shaders->setUniform("model", inst.transform);
shaders->setUniform("material.tex", 0); //set to 0 because the texture will be bound to GL_TEXTURE0
shaders->setUniform("material.shininess", asset->shininess);
shaders->setUniform("material.specularColor", asset->specularColor);
shaders->setUniform("materialTex", 0); //set to 0 because the texture will be bound to GL_TEXTURE0
shaders->setUniform("materialShininess", asset->shininess);
shaders->setUniform("materialSpecularColor", asset->specularColor);
shaders->setUniform("light.position", gLight.position);
shaders->setUniform("light.intensities", gLight.intensities);
shaders->setUniform("light.attenuation", gLight.attenuation);
Expand Down
15 changes: 7 additions & 8 deletions windows/07_more_lighting/resources/fragment-shader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
uniform mat4 model;
uniform vec3 cameraPosition;

uniform struct Material {
sampler2D tex;
float shininess;
vec3 specularColor;
} material;
// material settings
uniform sampler2D materialTex;
uniform float materialShininess;
uniform vec3 materialSpecularColor;

uniform struct Light {
vec3 position;
Expand All @@ -25,7 +24,7 @@ out vec4 finalColor;
void main() {
vec3 normal = normalize(transpose(inverse(mat3(model))) * fragNormal);
vec3 surfacePos = vec3(model * vec4(fragVert, 1));
vec4 surfaceColor = texture(material.tex, fragTexCoord);
vec4 surfaceColor = texture(materialTex, fragTexCoord);
vec3 surfaceToLight = normalize(light.position - surfacePos);
vec3 surfaceToCamera = normalize(cameraPosition - surfacePos);

Expand All @@ -39,8 +38,8 @@ void main() {
//specular
float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), material.shininess);
vec3 specular = specularCoefficient * material.specularColor * light.intensities;
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), materialShininess);
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;

//attenuation
float distanceToLight = length(light.position - surfacePos);
Expand Down
6 changes: 3 additions & 3 deletions windows/07_more_lighting/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ static void RenderInstance(const ModelInstance& inst) {
//set the shader uniforms
shaders->setUniform("camera", gCamera.matrix());
shaders->setUniform("model", inst.transform);
shaders->setUniform("material.tex", 0); //set to 0 because the texture will be bound to GL_TEXTURE0
shaders->setUniform("material.shininess", asset->shininess);
shaders->setUniform("material.specularColor", asset->specularColor);
shaders->setUniform("materialTex", 0); //set to 0 because the texture will be bound to GL_TEXTURE0
shaders->setUniform("materialShininess", asset->shininess);
shaders->setUniform("materialSpecularColor", asset->specularColor);
shaders->setUniform("light.position", gLight.position);
shaders->setUniform("light.intensities", gLight.intensities);
shaders->setUniform("light.attenuation", gLight.attenuation);
Expand Down

0 comments on commit a84b7d3

Please sign in to comment.