Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added point lighting pixel shader. Much better specular rendering... …
…but at a cost.
- Loading branch information
1 parent
341d996
commit e81a709
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#define MAX_LIGHTS 8 | ||
|
||
struct Light { | ||
vec4 position; | ||
vec4 ambientColor; | ||
vec4 diffuseColor; | ||
vec4 specularColor; | ||
float attenuation; | ||
}; | ||
|
||
struct Material { | ||
vec4 ambientColor; | ||
vec4 diffuseColor; | ||
vec4 specularColor; | ||
float shininess; | ||
}; | ||
|
||
uniform sampler2D s_texture; | ||
uniform bool u_useTexture; | ||
|
||
uniform Light u_light[MAX_LIGHTS]; | ||
uniform int u_lightEnabled[MAX_LIGHTS]; | ||
|
||
uniform Material u_material; | ||
uniform bool u_lightingEnabled; | ||
|
||
uniform bool u_includeSpecular; | ||
|
||
|
||
varying vec4 v_ambient; | ||
|
||
varying vec4 v_ecPosition3; | ||
varying vec3 v_normal; | ||
varying vec3 v_eye; | ||
varying vec2 v_texCoord; | ||
|
||
const float c_zero = 0.0; | ||
const float c_one = 1.0; | ||
|
||
vec4 color; | ||
vec4 specular; | ||
|
||
void pointLight(in int lightIndex, | ||
inout vec4 ambient, | ||
inout vec4 diffuse, | ||
inout vec4 specular) { | ||
|
||
float nDotVP; | ||
float eDotRV; | ||
float pf; | ||
float attenuation; | ||
float d; | ||
vec3 VP; | ||
vec3 reflectVector; | ||
|
||
|
||
// Vector between light position and vertex | ||
VP = vec3(u_light[lightIndex].position - v_ecPosition3); | ||
|
||
// Distance between the two | ||
d = length(VP); | ||
|
||
// Normalise | ||
VP = normalize(VP); | ||
|
||
// Calculate attenuation (only quadratic) | ||
attenuation = c_one / (c_one + u_light[lightIndex].attenuation * d * d); | ||
|
||
// angle between normal and light-vertex vector | ||
nDotVP = max(c_zero, dot(VP, v_normal)); | ||
|
||
if (nDotVP > c_zero) { | ||
diffuse += u_light[lightIndex].diffuseColor * nDotVP * attenuation; | ||
|
||
if (u_includeSpecular) { | ||
// reflected vector | ||
reflectVector = normalize(reflect(-VP, v_normal)); | ||
|
||
// angle between eye and reflected vector | ||
eDotRV = max(c_zero, dot(v_eye, reflectVector)); | ||
eDotRV = pow(eDotRV, 16.0); | ||
|
||
pf = pow(eDotRV, u_material.shininess); | ||
specular += u_light[lightIndex].specularColor * pf * attenuation; | ||
} | ||
} | ||
} | ||
|
||
void doLighting() { | ||
int i; | ||
vec4 amb = vec4(c_zero); | ||
vec4 diff = vec4(c_zero); | ||
vec4 spec = vec4(c_zero); | ||
|
||
if (u_lightingEnabled) { | ||
for (i = int(c_zero); i < MAX_LIGHTS; i++) { | ||
if (u_lightEnabled[i] == 1) { | ||
pointLight(i, amb, diff, spec); | ||
} | ||
} | ||
|
||
color = v_ambient + diff * u_material.diffuseColor; | ||
color.a = u_material.diffuseColor.a; | ||
specular = spec * u_material.specularColor; | ||
specular.a = u_material.specularColor.a; | ||
} else { | ||
color = u_material.diffuseColor; | ||
specular = spec; | ||
} | ||
} | ||
|
||
void main() { | ||
|
||
doLighting(); | ||
if (u_useTexture) { | ||
color = texture2D(s_texture, v_texCoord) * color; | ||
} | ||
gl_FragColor = color + specular; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#define MAX_LIGHTS 8 | ||
|
||
struct Material { | ||
vec4 ambientColor; | ||
vec4 diffuseColor; | ||
vec4 specularColor; | ||
float shininess; | ||
}; | ||
|
||
|
||
attribute vec4 a_vertex; | ||
attribute vec3 a_normal; | ||
attribute vec2 a_texCoord; | ||
|
||
uniform mat4 u_mvpMatrix; | ||
uniform mat4 u_mvMatrix; | ||
uniform mat3 u_normalMatrix; | ||
|
||
uniform vec4 u_sceneAmbientColor; | ||
|
||
uniform Material u_material; | ||
uniform bool u_lightingEnabled; | ||
|
||
varying vec4 v_ambient; | ||
varying vec2 v_texCoord; | ||
|
||
varying vec4 v_ecPosition3; | ||
varying vec3 v_normal; | ||
varying vec3 v_eye; | ||
|
||
|
||
void main(void) { | ||
v_ecPosition3 = u_mvMatrix * a_vertex; | ||
v_eye = -vec3(normalize(v_ecPosition3)); | ||
|
||
v_normal = u_normalMatrix * a_normal; | ||
v_normal = normalize(v_normal); | ||
|
||
if (u_lightingEnabled) { | ||
v_ambient = u_sceneAmbientColor * u_material.ambientColor; | ||
} | ||
|
||
gl_Position = u_mvpMatrix * a_vertex; | ||
v_texCoord = a_texCoord; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@import "GenericRenderer.j" | ||
|
||
@implementation GenericPixelRenderer : GenericRenderer { | ||
} | ||
|
||
- (id)initWithContext:(GLContext)context { | ||
self = [super initWithContext:context vertexShaderFile:@"Resources/shaders/pointLightingPixel.vsh" fragmentShaderFile:@"Resources/shaders/pointLightingPixel.fsh"]; | ||
|
||
if (self) { | ||
_lights = []; | ||
_sceneAmbient = [0.0, 0.0, 0.0, 1.0]; | ||
_lightingEnabled = YES; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
@end |