From 7ebf34c3e0c75f33ff642da27d75834934170646 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Thu, 21 Jan 2021 18:03:50 -0600 Subject: [PATCH] Phong lighting, flat shader --- demo/main.lua | 8 +++++--- demo/trough.glb | Bin 75748 -> 75748 bytes shaders/flat.frag | 18 ++---------------- shaders/flat.vert | 31 ++++++++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/demo/main.lua b/demo/main.lua index cf8ecfb..9154ec3 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -27,10 +27,11 @@ function init() camera.transform:Translate(0.0, 0.0, 10.0) 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.ambient = 0.4 + light.ambient = 0.2 light.diffuse = 1.0 + light.specular = 0.0001 couch.Node.GetRoot().children:Append(light) ball = couch.Mesh.FromFile("cube.glb") @@ -112,9 +113,10 @@ function onkey(key, code, action, mod) end 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 light.ambient = light.ambient + 0.1 + print(light.ambient) end end diff --git a/demo/trough.glb b/demo/trough.glb index 9ad428addbb957b28ac3c1dbfb675d5c39319731..aa416a0ef71a71953b454fd6921283225b6a75f3 100644 GIT binary patch delta 1157 zcmaEIp5@7TmJMIn>u<2s?bq@CXUo9A(7?dJz>qg*o*ipO@qQ4SfuX@3B#uK3NDd?o zqVegq$6`K64@hr4h|zF^r4FuWN3YEmn8hGRl+T&Bk0qnnK6BP`6PWllmO6V~?|*wi zCV)n7RKNOFcn@_9Gi~jiqisvc)jV&>V-N28ZJyYU&vn8gRLe zL__x@M-#f4&}g+sR|B#i90j0YgeuyPY%wl_kkueZA1-r{)dU^xw$qw!WFK_6n*+?K z?30{a&n0RR#gb|t)3MD?@j9p7*+?z>%05YZiTV$4KKs53dxnP^_8@g2@yC~E?sHT( zvY&j0OQPPZ=eJ$Lt1P?8)j9UtR)4UIZu@9AL(<$HMo;OJw4dWDVgLSE)!r$0%k5w^ z$dVnaKiGLxckGo}?raaE-)36bABgL-gVAz{j`lac)Z0({plRRq!pUe z_8WH0*eCXTirwTl+*0)#40;SK40;Tl4BQL`48{!H48{x`4D1X{46F?746F=X3=9mK z3~CGv3~CI_3~UUB4B8BA4B8Bg4AKmI3_1)(4EhX84ANkh5tt>;pvs`gAkLu3Ai|)` zAj2TZpv)l2AkQGnpu!-=Aj=@eAiyBRAi*HYAjBZ5#=y@Y#~{eS%OJ b!ywF{z@W>Z4rU31Sk0?>wy)-4w3G$_ES_1F delta 1173 zcmaEIp5@7TmJMIn>lwhHVLt-{Lj#DJJ7?ZL){J8N8!UD9I^O^G!o(REFx2cvR&#@; zZa)F_AT!a`)T3Lx{~Al(eqHZ>wjhhMXD#1?Y;pOVd3G!r#rv~oEjNLQBdZ5_0Hy}S z{{O$;9%K#IoVIGJpYQ5H1H@W2v);IT~a!4oAS%z|E?R+TMV_g9;yMwVq^`-@rte)su$fdcpT$UZ(rFbX&=+E%`WJ0cZi+V zbR+wq!`)yyiX{~+v2VVJN5uiH`O+zSP^l&9t&V5Z7l1qo;k)v~PN0XAd&hMPI@m%8#@M zX_(?FVSj5|;6A1F33ij?9qpI6O4x5mns5Iuy4NntH+|ovb{DXxKpH26&b2p?ceZ~q z#n|4(dAZ%Da%p=tds#StUAeTq$nPrq$!oYJ6gKRbu}|#x6uZCW_VzONvi9W2gc$@Fgw+_t7-Sfv82A`u82A_j8Tc8r f8Ppln84MT{8AKWQ!E8ka(dN}W+gI~2T1o=|3#(>K diff --git a/shaders/flat.frag b/shaders/flat.frag index 4abea14..10c6970 100644 --- a/shaders/flat.frag +++ b/shaders/flat.frag @@ -2,6 +2,7 @@ in vec3 UV; in vec3 NORMAL; +in vec3 LIGHT; out vec4 FragColor; @@ -14,16 +15,7 @@ struct Material { bool unshaded; }; -struct DirectionalLight { - vec3 direction; - vec3 color; - float ambient; - float diffuse; - float specular; -}; - uniform Material material; -uniform DirectionalLight directionalLight; void main() { FragColor = vec4(0.0); @@ -38,12 +30,6 @@ void main() { } if (!material.unshaded) { - vec3 ambient = directionalLight.ambient * directionalLight.color; - 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); + FragColor *= vec4(LIGHT, 1.0); } } diff --git a/shaders/flat.vert b/shaders/flat.vert index 89102ab..486f2f2 100644 --- a/shaders/flat.vert +++ b/shaders/flat.vert @@ -10,10 +10,39 @@ uniform mat4 PROJECTION; out vec3 UV; out vec3 NORMAL; +out vec3 LIGHT; + +struct DirectionalLight { + vec3 direction; + vec3 color; + float ambient; + float diffuse; + float specular; +}; + +uniform DirectionalLight directionalLight; void main() { vec4 vertex = PROJECTION * VIEW * MODEL * vec4(pos, 1.0); gl_Position = vertex; 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; }