commit 751fda512bd75b6b5e4f7fe8498e9d05270365b3
parent 9c5ed4a4abbb7bdcc8a11b363aacb456a5122b7b
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 21 Oct 2024 11:16:38 +0900
complete getting-started/shaders
Diffstat:
3 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -102,6 +102,15 @@ func main() {
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.BindVertexArray(VAO)
+ t := glfw.GetTime()
+ px := math.Sin(t)
+ py := math.Cos(t)
+ if err := shader.SetFloat32("px", float32(px)); err != nil {
+ log.Fatal(err)
+ }
+ if err := shader.SetFloat32("py", float32(py)); err != nil {
+ log.Fatal(err)
+ }
gl.DrawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, nil)
window.SwapBuffers()
glfw.PollEvents()
diff --git a/cmd/sample/vertex.glsl b/cmd/sample/vertex.glsl
@@ -2,8 +2,9 @@
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 col;
out vec3 vcol;
+uniform float px, py;
void main() {
- gl_Position = vec4(pos.x, -pos.y, pos.z, 1.0);
- vcol = col;
+ gl_Position = vec4(pos.x + px, pos.y + py, pos.z, 1.0);
+ vcol = gl_Position.xyz;
}
diff --git a/shader.go b/shader.go
@@ -85,3 +85,14 @@ func NewShader(vpath, fpath string) (*Shader, error) {
func (s *Shader) Use() {
gl.UseProgram(s.id)
}
+
+// SetFloat32 set the value of the uniform name.
+// TODO: Should I check error conditions?
+func (s *Shader) SetFloat32(name string, val float32) error {
+ l := gl.GetUniformLocation(s.id, gl.Str(name + "\x00"))
+ if l == -1 {
+ return fmt.Errorf("uniform %s not found", name)
+ }
+ gl.Uniform1f(l, val)
+ return nil
+}
+\ No newline at end of file