tofu

Making something with OpenGL in Go
Log | Files | Refs

commit 27fae4497819342e2bb2c16cbccf0e5002b6782a
parent 9ab1beb74baa87e5635793b2201c9d3716a4994f
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 14 Nov 2024 09:43:25 +0900

add sun and moving light

Diffstat:
Mcamera.go | 17+++++++++++++++--
Mcmd/sample/fragment.glsl | 44++++++++++++++++++++++++++++++++++----------
Mcmd/sample/main.go | 48++++++++++++++++++++++++++++++++++--------------
3 files changed, 83 insertions(+), 26 deletions(-)

diff --git a/camera.go b/camera.go @@ -1,7 +1,6 @@ package tofu import ( - "log" "math" ) @@ -31,10 +30,24 @@ func (cam *Camera) Move(right, up, back float32) { Add(cam.Front.MulF(-back)) } +func (cam *Camera) Move2(right, back float32) { + f := Vec3{float32(math.Cos(float64(cam.Yaw))), + 0, + float32(math.Sin(float64(cam.Yaw)))} + r := Vec3{-float32(math.Sin(float64(cam.Yaw))), + 0, + float32(math.Cos(float64(cam.Yaw)))} + cam.Pos = cam.Pos.Add(r.MulF(right)). + Add(f.MulF(-back)) +} + +func (cam *Camera) MoveVert(up float32) { + cam.Pos = cam.Pos.Add(Vec3{0, 1, 0}.MulF(up)) +} + // TODO: name func (cam *Camera) YawPitch(dy, dp float32) { const eps = 0.0000001 - log.Println(cam.Yaw, cam.Pitch, cam.Right) cam.Yaw -= dy cam.Pitch -= dp if cam.Pitch > math.Pi/2 { diff --git a/cmd/sample/fragment.glsl b/cmd/sample/fragment.glsl @@ -7,7 +7,7 @@ struct Material { }; struct Light { - vec3 direction; + vec3 vec; vec3 ambient; vec3 diffuse; vec3 specular; @@ -20,22 +20,46 @@ in vec2 texPos; uniform vec3 camPos; uniform vec3 objCol; uniform Material material; +uniform Light sun; uniform Light light; out vec4 fcol; void main() { - vec3 ambient = vec3(texture(material.diffuse, texPos)) * light.ambient; + vec3 ambient; + vec3 lightDir, diffuse; + vec3 viewDir, specular; + vec3 reflectDir; + float diff, spec; + + fcol = vec4(0, 0, 0, 1); + + ambient = vec3(texture(material.diffuse, texPos)) * light.ambient; + + lightDir = normalize(light.vec - fpos); + diff = max(dot(fnormal, lightDir), 0.0); + diffuse = diff * vec3(texture(material.diffuse, texPos)) * light.diffuse; + + viewDir = normalize(camPos - fpos); + reflectDir = reflect(-lightDir, fnormal); + spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness); + specular = vec3(texture(material.specular, texPos)) * spec * light.specular; + + fcol += vec4(ambient + diffuse + specular, 0); + + + ambient = vec3(texture(material.diffuse, texPos)) * sun.ambient; + + lightDir = normalize(-sun.vec); + diff = max(dot(fnormal, lightDir), 0.0); + diffuse = diff * vec3(texture(material.diffuse, texPos)) * sun.diffuse; - vec3 lightDir = normalize(-light.direction); - float diff = max(dot(fnormal, lightDir), 0.0); - vec3 diffuse = diff * vec3(texture(material.diffuse, texPos)) * light.diffuse; + viewDir = normalize(camPos - fpos); + reflectDir = reflect(-lightDir, fnormal); + spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness); + specular = vec3(texture(material.specular, texPos)) * spec * sun.specular; - vec3 viewDir = normalize(camPos - fpos); - vec3 reflectDir = reflect(-lightDir, fnormal); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness); - vec3 specular = vec3(texture(material.specular, texPos)) * spec * light.specular; + fcol += vec4(ambient + diffuse + specular, 0); - fcol = vec4(ambient + diffuse + specular, 1.0); } diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -40,22 +40,22 @@ func processInput(app *App) { } } if tofu.IsKeyPressed(tofu.KeyW) { - camera.Move(0, 0, -speed) + camera.Move2(0, -speed) } if tofu.IsKeyPressed(tofu.KeyS) { - camera.Move(0, 0, speed) + camera.Move2(0, speed) } if tofu.IsKeyPressed(tofu.KeyA) { - camera.Move(-speed, 0, 0) + camera.Move2(-speed, 0) } if tofu.IsKeyPressed(tofu.KeyD) { - camera.Move(speed, 0, 0) + camera.Move2(speed, 0) } if tofu.IsKeyPressed(tofu.KeySpace) { - camera.Move(0, speed, 0) + camera.MoveVert(speed) } if tofu.IsKeyPressed(tofu.KeyLeftShift) { - camera.Move(0, -speed, 0) + camera.MoveVert(-speed) } } @@ -164,26 +164,46 @@ func (app *App) Update() error { view := camera.View() projection := tofu.Perspective(80*math.Pi/180, 800/600, 0.1, 100) + + sunCol := tofu.Vec3{1, 1, 1} + sunDir := tofu.Vec3{-0.2, -1.0, -0.3} + lightCol := tofu.Vec3{1, 1, 1} - app.program.Use() - app.program.SetVec3("light.direction", tofu.Vec3{-0.2, -1.0, -0.3}) - app.program.SetVec3("light.ambient", lightCol.MulF(0.8)) - app.program.SetVec3("light.diffuse", lightCol.MulF(0.9)) - app.program.SetVec3("light.specular", lightCol) + lightModel := tofu.Rotate(now, tofu.Vec3{0, 1, 0}). + Mul(tofu.Translate(tofu.Vec3{5, 4, 0})). + Mul(tofu.Scale(0.3)) + lightPos := tofu.Rotate3(now, tofu.Vec3{0, 1, 0}). + MulV(tofu.Vec3{5, 4, 0}) + app.program.Use() app.program.SetMat4("view", view) app.program.SetMat4("projection", projection) app.program.SetVec3("camPos", camera.Pos) app.program.SetTexture("material.diffuse", app.texture) app.program.SetTexture("material.specular", app.specularMap) app.program.SetFloat32("material.shiness", 32) - for i, p := range cubePositions { - model := tofu.Translate(p.Inverse()). - Mul(tofu.Rotate(20*float32(i)+now, tofu.Vec3{math.Sqrt2 / 2, -math.Sqrt2 / 2, 0})) + app.program.SetVec3("sun.vec", sunDir) + app.program.SetVec3("sun.ambient", sunCol.MulF(0.8)) + app.program.SetVec3("sun.diffuse", sunCol.MulF(0.9)) + app.program.SetVec3("sun.specular", sunCol) + app.program.SetVec3("light.vec", lightPos) + app.program.SetVec3("light.ambient", lightCol.MulF(0.1)) + app.program.SetVec3("light.diffuse", lightCol.MulF(0.6)) + app.program.SetVec3("light.specular", lightCol) + for _, p := range cubePositions { + model := tofu.Translate(p.Inverse()) + // Mul(tofu.Rotate(20*float32(i)+now, tofu.Vec3{math.Sqrt2 / 2, -math.Sqrt2 / 2, 0})) app.program.SetMat4("model", model) object.Draw() } + app.lightProgram.Use() + app.program.SetMat4("view", view) + app.program.SetMat4("projection", projection) + app.program.SetMat4("model", lightModel) + app.lightProgram.SetVec3("lightCol", lightCol) + object.Draw() + return nil }