commit 9cbd64c0f6b6b78cade5f36c16eb80af3313dae6
parent 32fc153c144cc360d1f9255d17021f100123d231
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 10 Nov 2024 07:59:55 +0900
rotate light
Diffstat:
2 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -207,29 +207,31 @@ func main() {
keyboardCallback(window)
gl.ClearColor(0.1, 0.2, 0.2, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT)
+
+ lightModel := tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1.41421356/2, 1.41421356/2}).
+ Mul(tofu.Translate(tofu.Vec3{3, 0, 0}))
+ lightPos := tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1.41421356/2, 1.41421356/2}).
+ MulVec(tofu.Vec4{3, 0, 0, 0}).Vec3()
+
view := camera.View()
projection := tofu.Perspective(80*math.Pi/180, 800/600, 0.1, 100)
-
program.Use()
- program.SetVec3("lightPos", tofu.Vec3{3, 3, 3})
+ program.SetVec3("lightPos", lightPos)
program.SetMat4("view", view)
program.SetMat4("projection", projection)
- model := tofu.Translate(tofu.Vec3{0, 0, 0}).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
- program.SetMat4("model", model)
program.SetVec3("lightCol", lightCol)
- program.SetVec3("objCol", objCol)
program.SetVec3("camPos", camera.Pos)
+ model := tofu.Translate(tofu.Vec3{0, 0, 0})
+// Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
+// Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
+ program.SetMat4("model", model)
+ program.SetVec3("objCol", objCol)
gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, nil)
lightProgram.Use()
lightProgram.SetMat4("view", view)
lightProgram.SetMat4("projection", projection)
- model = tofu.Translate(tofu.Vec3{3, 3, 3}).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 0, 1})).
- Mul(tofu.Rotate(float32(glfw.GetTime()), tofu.Vec3{0, 1, 0}))
- lightProgram.SetMat4("model", model)
+ lightProgram.SetMat4("model", lightModel)
lightProgram.SetVec3("lightCol", lightCol)
gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, nil)
diff --git a/math.go b/math.go
@@ -34,6 +34,12 @@ func (v Vec3) MulF(x float32) Vec3{
return Vec3{v[0]*x, v[1]*x, v[2]*x}
}
+type Vec4 [4]float32
+
+func (v Vec4) Vec3() Vec3 {
+ return Vec3{v[0], v[1], v[2]}
+}
+
type Mat3 [9]float32
func Rotate3(rad float32, axis Vec3) Mat3 {
@@ -69,6 +75,15 @@ func (m Mat4) Mul(n Mat4) Mat4 {
return dst
}
+func (m Mat4) MulVec(v Vec4) Vec4 {
+ return Vec4{
+ m[0]*v[0]+m[4]*v[1]+m[8]*v[2]+m[12]*v[3],
+ m[1]*v[0]+m[5]*v[1]+m[9]*v[2]+m[13]*v[3],
+ m[2]*v[0]+m[6]*v[1]+m[10]*v[2]+m[14]*v[3],
+ m[3]*v[0]+m[7]*v[1]+m[11]*v[2]+m[15]*v[3],
+ }
+}
+
func Translate(v Vec3) Mat4 {
var n Mat4
n[0] = 1