tofu

Making something with OpenGL in Go
Log | Files | Refs

commit 73e44436dd889b8a44ef3d90175e20983653b7c2
parent 594a1db68f9a11df3a3a9b037f8a91e84474761c
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat,  2 Nov 2024 08:50:50 +0900

rotate

Diffstat:
Mcmd/sample/main.go | 7+++----
Mcmd/sample/vertex.glsl | 3++-
Amath.go | 44++++++++++++++++++++++++++++++++++++++++++++
Mshader.go | 12++++++++++--
4 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/cmd/sample/main.go b/cmd/sample/main.go @@ -122,18 +122,17 @@ func main() { if err := program.SetTexture(texture2, "texture2"); err != nil { log.Fatalf("set texture: %v", err) } -// gl.ActiveTexture(gl.TEXTURE0) -// gl.BindTexture(gl.TEXTURE_2D, 1) -// gl.ActiveTexture(gl.TEXTURE1) -// gl.BindTexture(gl.TEXTURE_2D, 2) window.SetFramebufferSizeCallback(framebufferSizeCallback) + transform := tofu.Rotate(math.Pi/4, tofu.Vec3{0, 0, 1}) + for !window.ShouldClose() { processInput(window) gl.ClearColor(0.2, 0.3, 0.3, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) program.SetFloat32("alpha", alpha) + program.SetMat4("transform", transform) gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil) window.SwapBuffers() glfw.PollEvents() diff --git a/cmd/sample/vertex.glsl b/cmd/sample/vertex.glsl @@ -4,8 +4,9 @@ layout (location = 1) in vec3 col; layout (location = 2) in vec2 vtexCoord; out vec3 vcol; out vec2 texCoord; +uniform mat4 transform; void main() { - gl_Position = vec4(pos, 1.0); + gl_Position = transform * vec4(pos, 1.0); vcol = col; texCoord = vtexCoord; } diff --git a/math.go b/math.go @@ -0,0 +1,44 @@ +package tofu + +import "math" + +type Vec3 [3]float32 + +// column major +type Mat4 [16]float32 + +// Mul multiplies m by n and returns newly allocated result. +func (m Mat4) Mul(n Mat4) Mat4 { + var dst Mat4 + for i := 0; i < 4; i++ { + for j := 0; j < 4; j++ { + for k := 0; k < 4; k++ { + dst[i+4*j] += m[i+4*k] * n[k+4*j] + } + } + } + return dst +} + +func Translate(v Vec3) Mat4 { + var n Mat4 + n[0] = 1 + n[5] = 1 + n[10] = 1 + n[12] = v[0] + n[13] = v[1] + n[14] = v[2] + n[15] = 1 + return n +} + +func Rotate(rad float32, axis Vec3) Mat4 { + x, y, z := axis[0], axis[1], axis[2] + c := float32(math.Cos(float64(rad))) + d := 1 - c + s := float32(math.Sin(float64(rad))) + return Mat4{c + x*x*d, y*x*d + z*s, z*x*d - y*s, 0, + x*y*d - z*s, c + y*y*d, z*y*d + x*s, 0, + x*z*d + y*s, y*z*d - x*s, c + z*z*d, 0, + 0, 0, 0, 1} +} diff --git a/shader.go b/shader.go @@ -98,6 +98,15 @@ func (p *Program) SetFloat32(name string, val float32) error { return nil } +func (p *Program) SetMat4(name string, val Mat4) error { + l := gl.GetUniformLocation(p.id, gl.Str(name + "\x00")) + if l == -1 { + return fmt.Errorf("uniform %s not found", name) + } + gl.UniformMatrix4fv(l, 1, false, &val[0]) + return nil +} + func (p *Program) SetTexture(t *Texture, name string) error { p.Use() gl.ActiveTexture(t.unit) @@ -108,4 +117,4 @@ func (p *Program) SetTexture(t *Texture, name string) error { } gl.Uniform1i(l, int32(t.unit - gl.TEXTURE0)) return nil -} -\ No newline at end of file +}