-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphongShading.lua
48 lines (40 loc) · 1.69 KB
/
phongShading.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
teapot = Transform{Sphere{}}
RelativeTo.World:addChild(teapot)
applyPhongToStateSet = function(stateset)
local vert = osg.Shader(osg.Shader.Type.VERTEX, [[
varying vec3 N;
varying vec3 v;
void main(void)
{
v = vec3(gl_ModelViewMatrix * gl_Vertex);
N = normalize(gl_NormalMatrix * gl_Normal);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
]])
local frag = osg.Shader(osg.Shader.Type.FRAGMENT, [[
varying vec3 N;
varying vec3 v;
void main (void)
{
vec3 L = normalize(gl_LightSource[0].position.xyz - v);
vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
vec3 R = normalize(-reflect(L,N));
//calculate Ambient Term:
vec4 Iamb = gl_FrontLightProduct[0].ambient;
//calculate Diffuse Term:
vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
Idiff = clamp(Idiff, 0.0, 1.0);
// calculate Specular Term:
vec4 Ispec = gl_FrontLightProduct[0].specular
* pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
Ispec = clamp(Ispec, 0.0, 1.0);
// write Total Color:
gl_FragColor = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec;
}
]])
local program = osg.Program()
program:addShader(vert)
program:addShader(frag)
stateset:setAttributeAndModes(program, osg.StateAttribute.Values.ON)
end
applyPhongToStateSet(teapot:getOrCreateStateSet())