tofu

Making something with OpenGL in Go
Log | Files | Refs

commit 351966b66e633903957361d86dccfce913eb9cb2
parent d03896801b04be5ebe14f96bb21683fa705f5c9d
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 10 Nov 2024 07:45:44 +0900

Phong lighting

Diffstat:
Mcmd/sample/fragment.glsl | 16+++++++++++++---
Mcmd/sample/main.go | 1+
Mcmd/sample/vertex.glsl | 2+-
3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl @@ -3,14 +3,24 @@ in vec3 fnormal; in vec3 fpos; uniform vec3 lightCol; uniform vec3 lightPos; +uniform vec3 camPos; uniform vec3 objCol; out vec4 fcol; void main() { float ambientStrength = 0.1; + float specularStrength = 0; + vec3 ambient = ambientStrength * lightCol; - vec3 dir = normalize(lightPos - fpos); - float diff = max(dot(fnormal, dir), 0.0); + + vec3 lightDir = normalize(lightPos - fpos); + float diff = max(dot(fnormal, lightDir), 0.0); vec3 diffuse = diff * lightCol; - fcol = vec4(objCol * (ambient + diffuse), 1.0); + + vec3 viewDir = normalize(camPos - fpos); + vec3 reflectDir = reflect(-lightDir, fnormal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); + vec3 specular = specularStrength * spec * lightCol; + + fcol = vec4(objCol * (ambient + diffuse + specular), 1.0); } diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -220,6 +220,7 @@ func main() { program.SetMat4("model", model) program.SetVec3("lightCol", lightCol) program.SetVec3("objCol", objCol) + program.SetVec3("camPos", camera.Pos) gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, nil) lightProgram.Use() diff --git a/cmd/sample/vertex.glsl b/cmd/sample/vertex.glsl @@ -8,7 +8,7 @@ out vec3 fnormal; out vec3 fpos; void main() { gl_Position = projection * view * model * vec4(pos, 1.0); - fnormal = vec3(model * vec4(normal, 1.0)); + fnormal = mat3(transpose(inverse(model))) * normal; fpos = vec3(model * vec4(pos, 1.0)); }