Jason McKesson avatar Jason McKesson committed c970ac3

Tut10: Changed the shader code to take the camera-space position instead of the reverse-computation method.

Comments (0)

Files changed (12)

Tut 10 Shinies/Blinn vs Phong Lighting.cpp

 
 	GLuint normalModelToCameraMatrixUnif;
 	GLuint cameraSpaceLightPosUnif;
-	GLuint clipToCameraMatrixUnif;
-	GLuint windowSizeUnif;
-	GLuint depthRangeUnif;
 	GLuint lightAttenuationUnif;
 	GLuint shininessFactorUnif;
 	GLuint baseDiffuseColorUnif;
 
-	void SetWindowData(const glm::mat4 cameraToClip, const glm::mat4 clipToCamera, int w, int h)
+	void SetWindowData(const glm::mat4 cameraToClip)
 	{
 		glUseProgram(theProgram);
 		glUniformMatrix4fv(cameraToClipMatrixUnif, 1, GL_FALSE,
 			glm::value_ptr(cameraToClip));
-		glUniform2i(windowSizeUnif, w, h);
-		glUniformMatrix4fv(clipToCameraMatrixUnif, 1, GL_FALSE,
-			glm::value_ptr(clipToCamera));
 		glUseProgram(0);
 	}
 };
 
 	data.normalModelToCameraMatrixUnif = glGetUniformLocation(data.theProgram, "normalModelToCameraMatrix");
 	data.cameraSpaceLightPosUnif = glGetUniformLocation(data.theProgram, "cameraSpaceLightPos");
-	data.clipToCameraMatrixUnif = glGetUniformLocation(data.theProgram, "clipToCameraMatrix");
-	data.windowSizeUnif = glGetUniformLocation(data.theProgram, "windowSize");
-	data.depthRangeUnif = glGetUniformLocation(data.theProgram, "depthRange");
 	data.lightAttenuationUnif = glGetUniformLocation(data.theProgram, "lightAttenuation");
 	data.shininessFactorUnif = glGetUniformLocation(data.theProgram, "shininessFactor");
 	data.baseDiffuseColorUnif = glGetUniformLocation(data.theProgram, "baseDiffuseColor");
 	glDepthRange(depthZNear, depthZFar);
 	glEnable(GL_DEPTH_CLAMP);
 
-	for(int iProg = 0; iProg < LM_MAX_LIGHTING_MODEL; iProg++)
-	{
-		glUseProgram(g_Programs[iProg].whiteProg.theProgram);
-		glUniform2f(g_Programs[iProg].whiteProg.depthRangeUnif,depthZNear, depthZFar);
-		glUseProgram(g_Programs[iProg].colorProg.theProgram);
-		glUniform2f(g_Programs[iProg].colorProg.depthRangeUnif,depthZNear, depthZFar);
-	}
-
 	glUseProgram(0);
 }
 
 
 	for(int iProg = 0; iProg < LM_MAX_LIGHTING_MODEL; iProg++)
 	{
-		g_Programs[iProg].whiteProg.SetWindowData(persMatrix.Top(), invMat, w, h);
-		g_Programs[iProg].colorProg.SetWindowData(persMatrix.Top(), invMat, w, h);
+		g_Programs[iProg].whiteProg.SetWindowData(persMatrix.Top());
+		g_Programs[iProg].colorProg.SetWindowData(persMatrix.Top());
 	}
 
 	g_Unlit.SetWindowData(persMatrix.Top());

Tut 10 Shinies/Gaussian Specular Lighting.cpp

 
 	GLuint normalModelToCameraMatrixUnif;
 	GLuint cameraSpaceLightPosUnif;
-	GLuint clipToCameraMatrixUnif;
-	GLuint windowSizeUnif;
-	GLuint depthRangeUnif;
 	GLuint lightAttenuationUnif;
 	GLuint shininessFactorUnif;
 	GLuint baseDiffuseColorUnif;
 
-	void SetWindowData(const glm::mat4 cameraToClip, const glm::mat4 clipToCamera, int w, int h)
+	void SetWindowData(const glm::mat4 cameraToClip)
 	{
 		glUseProgram(theProgram);
 		glUniformMatrix4fv(cameraToClipMatrixUnif, 1, GL_FALSE,
 			glm::value_ptr(cameraToClip));
-		glUniform2i(windowSizeUnif, w, h);
-		glUniformMatrix4fv(clipToCameraMatrixUnif, 1, GL_FALSE,
-			glm::value_ptr(clipToCamera));
 		glUseProgram(0);
 	}
 };
 
 	data.normalModelToCameraMatrixUnif = glGetUniformLocation(data.theProgram, "normalModelToCameraMatrix");
 	data.cameraSpaceLightPosUnif = glGetUniformLocation(data.theProgram, "cameraSpaceLightPos");
-	data.clipToCameraMatrixUnif = glGetUniformLocation(data.theProgram, "clipToCameraMatrix");
-	data.windowSizeUnif = glGetUniformLocation(data.theProgram, "windowSize");
-	data.depthRangeUnif = glGetUniformLocation(data.theProgram, "depthRange");
 	data.lightAttenuationUnif = glGetUniformLocation(data.theProgram, "lightAttenuation");
 	data.shininessFactorUnif = glGetUniformLocation(data.theProgram, "shininessFactor");
 	data.baseDiffuseColorUnif = glGetUniformLocation(data.theProgram, "baseDiffuseColor");
 	glDepthFunc(GL_LEQUAL);
 	glDepthRange(depthZNear, depthZFar);
 	glEnable(GL_DEPTH_CLAMP);
-
-	for(int iProg = 0; iProg < LM_MAX_LIGHTING_MODEL; iProg++)
-	{
-		glUseProgram(g_Programs[iProg].whiteProg.theProgram);
-		glUniform2f(g_Programs[iProg].whiteProg.depthRangeUnif,depthZNear, depthZFar);
-		glUseProgram(g_Programs[iProg].colorProg.theProgram);
-		glUniform2f(g_Programs[iProg].colorProg.depthRangeUnif,depthZNear, depthZFar);
-	}
-
-	glUseProgram(0);
 }
 
 static float g_fLightHeight = 1.5f;
 static float g_CylPitch = -90.0f;
 static float g_CylRoll = 0.0f;
 
-static int g_eLightModel = LM_GAUSSIAN_ONLY;
+static int g_eLightModel = LM_GAUSSIAN_SPECULAR;
 
 bool IsGuassianLightModel()
 {
 
 	for(int iProg = 0; iProg < LM_MAX_LIGHTING_MODEL; iProg++)
 	{
-		g_Programs[iProg].whiteProg.SetWindowData(persMatrix.Top(), invMat, w, h);
-		g_Programs[iProg].colorProg.SetWindowData(persMatrix.Top(), invMat, w, h);
+		g_Programs[iProg].whiteProg.SetWindowData(persMatrix.Top());
+		g_Programs[iProg].colorProg.SetWindowData(persMatrix.Top());
 	}
 
 	g_Unlit.SetWindowData(persMatrix.Top());

Tut 10 Shinies/Phong Lighting.cpp

 
 	GLuint normalModelToCameraMatrixUnif;
 	GLuint cameraSpaceLightPosUnif;
-	GLuint clipToCameraMatrixUnif;
-	GLuint windowSizeUnif;
-	GLuint depthRangeUnif;
 	GLuint lightAttenuationUnif;
 	GLuint shininessFactorUnif;
 	GLuint baseDiffuseColorUnif;
 
-	void SetWindowData(const glm::mat4 cameraToClip, const glm::mat4 clipToCamera, int w, int h)
+	void SetWindowData(const glm::mat4 cameraToClip)
 	{
 		glUseProgram(theProgram);
 		glUniformMatrix4fv(cameraToClipMatrixUnif, 1, GL_FALSE,
 			glm::value_ptr(cameraToClip));
-		glUniform2i(windowSizeUnif, w, h);
-		glUniformMatrix4fv(clipToCameraMatrixUnif, 1, GL_FALSE,
-			glm::value_ptr(clipToCamera));
 		glUseProgram(0);
 	}
 };
 
 	data.normalModelToCameraMatrixUnif = glGetUniformLocation(data.theProgram, "normalModelToCameraMatrix");
 	data.cameraSpaceLightPosUnif = glGetUniformLocation(data.theProgram, "cameraSpaceLightPos");
-	data.clipToCameraMatrixUnif = glGetUniformLocation(data.theProgram, "clipToCameraMatrix");
-	data.windowSizeUnif = glGetUniformLocation(data.theProgram, "windowSize");
-	data.depthRangeUnif = glGetUniformLocation(data.theProgram, "depthRange");
 	data.lightAttenuationUnif = glGetUniformLocation(data.theProgram, "lightAttenuation");
 	data.shininessFactorUnif = glGetUniformLocation(data.theProgram, "shininessFactor");
 	data.baseDiffuseColorUnif = glGetUniformLocation(data.theProgram, "baseDiffuseColor");
 	glDepthFunc(GL_LEQUAL);
 	glDepthRange(depthZNear, depthZFar);
 	glEnable(GL_DEPTH_CLAMP);
-
-	glUseProgram(g_WhiteNoPhong.theProgram);
-	glUniform2f(g_WhiteNoPhong.depthRangeUnif,depthZNear, depthZFar);
-	glUseProgram(g_WhitePhong.theProgram);
-	glUniform2f(g_WhitePhong.depthRangeUnif,depthZNear, depthZFar);
-	glUseProgram(g_WhitePhongOnly.theProgram);
-	glUniform2f(g_WhitePhongOnly.depthRangeUnif,depthZNear, depthZFar);
-
-	glUseProgram(g_ColorNoPhong.theProgram);
-	glUniform2f(g_ColorNoPhong.depthRangeUnif,depthZNear, depthZFar);
-	glUseProgram(g_ColorPhong.theProgram);
-	glUniform2f(g_ColorPhong.depthRangeUnif,depthZNear, depthZFar);
-	glUseProgram(g_ColorPhongOnly.theProgram);
-	glUniform2f(g_ColorPhongOnly.depthRangeUnif,depthZNear, depthZFar);
-	glUseProgram(0);
 }
 
 static float g_fLightHeight = 1.5f;
 	persMatrix.Perspective(45.0f, (h / (float)w), g_fzNear, g_fzFar);
 	const glm::mat4 &invMat = glm::inverse(persMatrix.Top());
 
-	g_WhiteNoPhong.SetWindowData(persMatrix.Top(), invMat, w, h);
-	g_ColorNoPhong.SetWindowData(persMatrix.Top(), invMat, w, h);
-	g_WhitePhong.SetWindowData(persMatrix.Top(), invMat, w, h);
-	g_ColorPhong.SetWindowData(persMatrix.Top(), invMat, w, h);
-	g_WhitePhongOnly.SetWindowData(persMatrix.Top(), invMat, w, h);
-	g_ColorPhongOnly.SetWindowData(persMatrix.Top(), invMat, w, h);
+	g_WhiteNoPhong.SetWindowData(persMatrix.Top());
+	g_ColorNoPhong.SetWindowData(persMatrix.Top());
+	g_WhitePhong.SetWindowData(persMatrix.Top());
+	g_ColorPhong.SetWindowData(persMatrix.Top());
+	g_WhitePhongOnly.SetWindowData(persMatrix.Top());
+	g_ColorPhongOnly.SetWindowData(persMatrix.Top());
 
 	g_Unlit.SetWindowData(persMatrix.Top());
 

Tut 10 Shinies/data/BlinnLighting.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 uniform vec3 modelSpaceLightPos;
 
 uniform vec4 lightIntensity;
-const vec4 specularIntensity = vec4(0.25, 0.25, 0.25, 1.0);
 uniform vec4 ambientIntensity;
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-const vec4 specularColor = vec4(1.0);
+const vec4 specularColor = vec4(0.25, 0.25, 0.25, 1.0);
 uniform float shininessFactor;
 
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 float CalcAttenuation(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	float atten = CalcAttenuation(cameraSpacePosition, lightDir);
 	vec4 attenIntensity = atten * lightIntensity;
 	float cosAngIncidence = dot(surfaceNormal, lightDir);
 	cosAngIncidence = clamp(cosAngIncidence, 0, 1);
 	
-	vec4 specularAttenIntensity = atten * specularIntensity;
 	vec3 viewDirection = normalize(-cameraSpacePosition);
 	
 	vec3 halfAngle = normalize(lightDir + viewDirection);
 	blinnTerm = pow(blinnTerm, shininessFactor);
 
 	outputColor = (diffuseColor * attenIntensity * cosAngIncidence) +
-		(specularColor * specularAttenIntensity * blinnTerm) +
+		(specularColor * attenIntensity * blinnTerm) +
 		(diffuseColor * ambientIntensity);
 }

Tut 10 Shinies/data/BlinnOnly.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 uniform vec3 modelSpaceLightPos;
 
 uniform vec4 lightIntensity;
-const vec4 specularIntensity = vec4(0.25, 0.25, 0.25, 1.0);
 uniform vec4 ambientIntensity;
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-const vec4 specularColor = vec4(1.0);
+const vec4 specularColor = vec4(0.25, 0.25, 0.25, 1.0);
 uniform float shininessFactor;
 
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 float CalcAttenuation(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	float atten = CalcAttenuation(cameraSpacePosition, lightDir);
 	vec4 attenIntensity = atten * lightIntensity;
 	
 	vec3 surfaceNormal = normalize(vertexNormal);
 	
-	vec4 specularAttenIntensity = atten * specularIntensity;
 	vec3 viewDirection = normalize(-cameraSpacePosition);
 	
 	vec3 halfAngle = normalize(lightDir + viewDirection);
 	blinnTerm = dot(surfaceNormal, lightDir) >= 0.0 ? blinnTerm : 0.0;
 	blinnTerm = pow(blinnTerm, shininessFactor);
 
-	outputColor = (specularColor * specularAttenIntensity * blinnTerm) +
+	outputColor = (specularColor * attenIntensity * blinnTerm) +
 		(diffuseColor * ambientIntensity);
 }

Tut 10 Shinies/data/GaussianLighting.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 uniform vec3 modelSpaceLightPos;
 
 uniform vec4 lightIntensity;
-const vec4 specularIntensity = vec4(0.25, 0.25, 0.25, 1.0);
 uniform vec4 ambientIntensity;
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-const vec4 specularColor = vec4(1.0);
+const vec4 specularColor = vec4(0.25, 0.25, 0.25, 1.0);
 uniform float shininessFactor;
 
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 float CalcAttenuation(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	float atten = CalcAttenuation(cameraSpacePosition, lightDir);
 	vec4 attenIntensity = atten * lightIntensity;
 	float cosAngIncidence = dot(surfaceNormal, lightDir);
 	cosAngIncidence = clamp(cosAngIncidence, 0, 1);
 	
-	vec4 specularAttenIntensity = atten * specularIntensity;
 	vec3 viewDirection = normalize(-cameraSpacePosition);
 	
 	vec3 halfAngle = normalize(lightDir + viewDirection);
 	gaussianTerm = dot(surfaceNormal, lightDir) >= 0.0 ? gaussianTerm : 0.0;
 
 	outputColor = (diffuseColor * attenIntensity * cosAngIncidence) +
-		(specularColor * specularAttenIntensity * gaussianTerm) +
+		(specularColor * attenIntensity * gaussianTerm) +
 		(diffuseColor * ambientIntensity);
 }

Tut 10 Shinies/data/GaussianOnly.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 uniform vec3 modelSpaceLightPos;
 
 uniform vec4 lightIntensity;
-const vec4 specularIntensity = vec4(0.25, 0.25, 0.25, 1.0);
 uniform vec4 ambientIntensity;
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-const vec4 specularColor = vec4(1.0);
+const vec4 specularColor = vec4(0.25, 0.25, 0.25, 1.0);
 uniform float shininessFactor;
 
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 float CalcAttenuation(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	float atten = CalcAttenuation(cameraSpacePosition, lightDir);
 	vec4 attenIntensity = atten * lightIntensity;
 	
 	vec3 surfaceNormal = normalize(vertexNormal);
 	
-	vec4 specularAttenIntensity = atten * specularIntensity;
 	vec3 viewDirection = normalize(-cameraSpacePosition);
 	
 	vec3 halfAngle = normalize(lightDir + viewDirection);
 
 	gaussianTerm = dot(surfaceNormal, lightDir) >= 0.0 ? gaussianTerm : 0.0;
 
-	outputColor = (specularColor * specularAttenIntensity * gaussianTerm) +
+	outputColor = (specularColor * attenIntensity * gaussianTerm) +
 		(diffuseColor * ambientIntensity);
 }

Tut 10 Shinies/data/NoPhong.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 vec4 ApplyLightIntensity(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	vec4 attenIntensity = ApplyLightIntensity(cameraSpacePosition, lightDir);
 

Tut 10 Shinies/data/PCN.vert

 
 out vec4 diffuseColor;
 out vec3 vertexNormal;
+out vec3 cameraSpacePosition;
 
 uniform mat4 cameraToClipMatrix;
 uniform mat4 modelToCameraMatrix;
 
 void main()
 {
-	gl_Position = cameraToClipMatrix * (modelToCameraMatrix * vec4(position, 1.0));
+	vec4 tempCamPosition = (modelToCameraMatrix * vec4(position, 1.0));
+	gl_Position = cameraToClipMatrix * tempCamPosition;
 
 	vertexNormal = normalModelToCameraMatrix * normal;
 	diffuseColor = inDiffuseColor;
+	cameraSpacePosition = vec3(tempCamPosition);
 }

Tut 10 Shinies/data/PN.vert

 
 out vec4 diffuseColor;
 out vec3 vertexNormal;
+out vec3 cameraSpacePosition;
 
 uniform mat4 cameraToClipMatrix;
 uniform mat4 modelToCameraMatrix;
 
 void main()
 {
-	gl_Position = cameraToClipMatrix * (modelToCameraMatrix * vec4(position, 1.0));
+	vec4 tempCamPosition = (modelToCameraMatrix * vec4(position, 1.0));
+	gl_Position = cameraToClipMatrix * tempCamPosition;
 
 	vertexNormal = normalModelToCameraMatrix * normal;
 	diffuseColor = baseDiffuseColor;
+	cameraSpacePosition = vec3(tempCamPosition);
 }

Tut 10 Shinies/data/PhongLighting.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 uniform vec3 modelSpaceLightPos;
 
 uniform vec4 lightIntensity;
-const vec4 specularIntensity = vec4(0.25, 0.25, 0.25, 1.0);
 uniform vec4 ambientIntensity;
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-const vec4 specularColor = vec4(1.0);
+const vec4 specularColor = vec4(0.25, 0.25, 0.25, 1.0);
 uniform float shininessFactor;
 
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 float CalcAttenuation(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	float atten = CalcAttenuation(cameraSpacePosition, lightDir);
 	vec4 attenIntensity = atten * lightIntensity;
 	float cosAngIncidence = dot(surfaceNormal, lightDir);
 	cosAngIncidence = clamp(cosAngIncidence, 0, 1);
 	
-	vec4 specularAttenIntensity = atten * specularIntensity;
 	vec3 viewDirection = normalize(-cameraSpacePosition);
 	vec3 reflectDir = reflect(-lightDir, surfaceNormal);
 	float phongTerm = dot(viewDirection, reflectDir);
 	
 
 	outputColor = (diffuseColor * attenIntensity * cosAngIncidence) +
-		(specularColor * specularAttenIntensity * phongTerm) +
+		(specularColor * attenIntensity * phongTerm) +
 		(diffuseColor * ambientIntensity);
 }

Tut 10 Shinies/data/PhongOnly.frag

 
 in vec4 diffuseColor;
 in vec3 vertexNormal;
+in vec3 cameraSpacePosition;
 
 out vec4 outputColor;
 
 uniform vec3 modelSpaceLightPos;
 
 uniform vec4 lightIntensity;
-const vec4 specularIntensity = vec4(0.25, 0.25, 0.25, 1.0);
 uniform vec4 ambientIntensity;
 
 uniform vec3 cameraSpaceLightPos;
 
-uniform mat4 clipToCameraMatrix;
-uniform ivec2 windowSize;
-uniform vec2 depthRange;
-
 uniform float lightAttenuation;
 
-const vec4 specularColor = vec4(1.0);
+const vec4 specularColor = vec4(0.25, 0.25, 0.25, 1.0);
 uniform float shininessFactor;
 
 
-vec3 CalcCameraSpacePosition()
-{
-	vec3 ndcPos;
-	ndcPos.xy = ((gl_FragCoord.xy / windowSize.xy) * 2.0) - 1.0;
-	ndcPos.z = 2.0 * (gl_FragCoord.z - depthRange.x - depthRange.y) / (depthRange.y - depthRange.x);
-	
-	vec4 clipPos;
-	clipPos.w = 1.0f / gl_FragCoord.w;
-	clipPos.xyz = ndcPos.xyz * clipPos.w;
-	
-	return vec3(clipToCameraMatrix * clipPos);
-}
-
 float CalcAttenuation(in vec3 cameraSpacePosition, out vec3 lightDirection)
 {
 	vec3 lightDifference =  cameraSpaceLightPos - cameraSpacePosition;
 
 void main()
 {
-	vec3 cameraSpacePosition = CalcCameraSpacePosition();
-
 	vec3 lightDir = vec3(0.0);
 	float atten = CalcAttenuation(cameraSpacePosition, lightDir);
-	
+	vec4 attenIntensity = atten * lightIntensity;
+
 	vec3 surfaceNormal = normalize(vertexNormal);
 	
-	vec4 specularAttenIntensity = atten * specularIntensity;
 	vec3 viewDirection = normalize(-cameraSpacePosition);
 	vec3 reflectDir = reflect(-lightDir, surfaceNormal);
 	float phongTerm = dot(viewDirection, reflectDir);
 	phongTerm = dot(reflectDir, surfaceNormal) > 0.0 ? phongTerm : 0.0;
 	phongTerm = pow(phongTerm, shininessFactor);
 	
-	outputColor = (specularColor * specularAttenIntensity * phongTerm) +
+	outputColor = (specularColor * attenIntensity * phongTerm) +
 		(diffuseColor * ambientIntensity);
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.