Phong lighting, flat shader
This commit is contained in:
parent
96f1e23a54
commit
7ebf34c3e0
@ -27,10 +27,11 @@ function init()
|
|||||||
camera.transform:Translate(0.0, 0.0, 10.0)
|
camera.transform:Translate(0.0, 0.0, 10.0)
|
||||||
|
|
||||||
light = couch.DirectionalLight.new()
|
light = couch.DirectionalLight.new()
|
||||||
light.direction = couch.Vector3(0.0, 0.0, -1.0)
|
light.direction = couch.Vector3(0.0, -1.0, 0.0)
|
||||||
light.color = couch.Vector3(1.0, 1.0, 1.0)
|
light.color = couch.Vector3(1.0, 1.0, 1.0)
|
||||||
light.ambient = 0.4
|
light.ambient = 0.2
|
||||||
light.diffuse = 1.0
|
light.diffuse = 1.0
|
||||||
|
light.specular = 0.0001
|
||||||
couch.Node.GetRoot().children:Append(light)
|
couch.Node.GetRoot().children:Append(light)
|
||||||
|
|
||||||
ball = couch.Mesh.FromFile("cube.glb")
|
ball = couch.Mesh.FromFile("cube.glb")
|
||||||
@ -112,9 +113,10 @@ function onkey(key, code, action, mod)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if key == couch.KEY_DOWN and action == couch.ACTION_PRESS then
|
if key == couch.KEY_DOWN and action == couch.ACTION_PRESS then
|
||||||
light.ambient = light.ambient - 0.1
|
light.ambient = max(light.ambient - 0.1, 0.0)
|
||||||
elseif key == couch.KEY_UP and action == couch.ACTION_PRESS then
|
elseif key == couch.KEY_UP and action == couch.ACTION_PRESS then
|
||||||
light.ambient = light.ambient + 0.1
|
light.ambient = light.ambient + 0.1
|
||||||
|
print(light.ambient)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
BIN
demo/trough.glb
BIN
demo/trough.glb
Binary file not shown.
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
in vec3 UV;
|
in vec3 UV;
|
||||||
in vec3 NORMAL;
|
in vec3 NORMAL;
|
||||||
|
in vec3 LIGHT;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
@ -14,16 +15,7 @@ struct Material {
|
|||||||
bool unshaded;
|
bool unshaded;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DirectionalLight {
|
|
||||||
vec3 direction;
|
|
||||||
vec3 color;
|
|
||||||
float ambient;
|
|
||||||
float diffuse;
|
|
||||||
float specular;
|
|
||||||
};
|
|
||||||
|
|
||||||
uniform Material material;
|
uniform Material material;
|
||||||
uniform DirectionalLight directionalLight;
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
FragColor = vec4(0.0);
|
FragColor = vec4(0.0);
|
||||||
@ -38,12 +30,6 @@ void main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!material.unshaded) {
|
if (!material.unshaded) {
|
||||||
vec3 ambient = directionalLight.ambient * directionalLight.color;
|
FragColor *= vec4(LIGHT, 1.0);
|
||||||
ambient = max(ambient, 0.0);
|
|
||||||
|
|
||||||
vec3 diffuse = directionalLight.diffuse * dot(directionalLight.direction, NORMAL) * directionalLight.color;
|
|
||||||
diffuse = max(diffuse, 0.0);
|
|
||||||
|
|
||||||
FragColor *= vec4(ambient + diffuse, 1.0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,39 @@ uniform mat4 PROJECTION;
|
|||||||
|
|
||||||
out vec3 UV;
|
out vec3 UV;
|
||||||
out vec3 NORMAL;
|
out vec3 NORMAL;
|
||||||
|
out vec3 LIGHT;
|
||||||
|
|
||||||
|
struct DirectionalLight {
|
||||||
|
vec3 direction;
|
||||||
|
vec3 color;
|
||||||
|
float ambient;
|
||||||
|
float diffuse;
|
||||||
|
float specular;
|
||||||
|
};
|
||||||
|
|
||||||
|
uniform DirectionalLight directionalLight;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
|
vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0);
|
||||||
gl_Position = vertex;
|
gl_Position = vertex;
|
||||||
UV = vec3(uv * vertex.z, vertex.z);
|
UV = vec3(uv * vertex.z, vertex.z);
|
||||||
NORMAL = (PROJECTION * VIEW * MODEL * vec4(normal, 0.0)).xyz;
|
|
||||||
|
NORMAL = (VIEW * MODEL * vec4(normal, 0.0)).xyz;
|
||||||
|
|
||||||
|
// Flat shading, we compute light per vertex
|
||||||
|
vec3 ambient = directionalLight.ambient * directionalLight.color;
|
||||||
|
|
||||||
|
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
|
||||||
|
float diff = dot(normalize(direction), normalize(NORMAL));
|
||||||
|
diff = max(diff, 0.0);
|
||||||
|
vec3 diffuse = directionalLight.diffuse * diff * directionalLight.color;
|
||||||
|
|
||||||
|
vec3 viewDir = (VIEW * MODEL * vec4(pos, 1.0)).xyz;
|
||||||
|
vec3 reflectionDir = reflect(normalize(direction), normalize(NORMAL));
|
||||||
|
float spec = dot(viewDir, reflectionDir);
|
||||||
|
spec = max(spec, 0.0);
|
||||||
|
spec = pow(spec, 2);
|
||||||
|
vec3 specular = directionalLight.specular * spec * directionalLight.color;
|
||||||
|
|
||||||
|
LIGHT = ambient + diffuse + specular;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user