commit 3f5554a7e0ecb3e1ed6a892bcae80cf65f621e17
parent 6b9ffa027b4ea588e1d24a27108ca97fcb10447c
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 29 Oct 2024 07:10:33 +0900
change name from Shader to Program
Diffstat:
2 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -104,16 +104,16 @@ func main() {
object.Load()
- shader, err := tofu.NewShader(vpath, fpath)
+ program, err := tofu.NewProgram(vpath, fpath)
if err != nil {
- log.Fatalf("create shader: %v", err)
+ log.Fatalf("create shader program: %v", err)
}
texture1, err := tofu.NewTexture(texpath1)
if err != nil {
log.Fatalf("create texture: %v", err)
}
- if err := shader.SetTexture(texture1, "texture1"); err != nil {
+ if err := program.SetTexture(texture1, "texture1"); err != nil {
log.Fatalf("set texture: %v", err)
}
@@ -121,11 +121,11 @@ func main() {
if err != nil {
log.Fatalf("create texture: %v", err)
}
- if err := shader.SetTexture(texture2, "texture2"); err != nil {
+ if err := program.SetTexture(texture2, "texture2"); err != nil {
log.Fatalf("set texture: %v", err)
}
- shader.Use()
+// program.Use()
window.SetFramebufferSizeCallback(framebufferSizeCallback)
@@ -133,7 +133,7 @@ func main() {
processInput(window)
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
- shader.SetFloat32("alpha", alpha)
+ program.SetFloat32("alpha", alpha)
gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil)
window.SwapBuffers()
glfw.PollEvents()
diff --git a/shader.go b/shader.go
@@ -57,13 +57,14 @@ func linkShaders(vsID, fsID uint32) (shaderID uint32, err error) {
return shaderID, nil
}
-type Shader struct {
+type Program struct {
id uint32
}
-// NewShader creates a shader program from a vertex shader source file specified by vpath and
-// a fragment shader source file specified by fpath, and returns the resulting Shader object.
-func NewShader(vpath, fpath string) (*Shader, error) {
+// NewProgram creates a shader program from a vertex shader source file
+// specified by vpath and a fragment shader source file
+// specified by fpath, and returns the resulting Program object.
+func NewProgram(vpath, fpath string) (*Program, error) {
vid, err := compileShader(vpath, gl.VERTEX_SHADER)
if err != nil {
return nil, fmt.Errorf("compile vertex shader: %v", err)
@@ -72,24 +73,24 @@ func NewShader(vpath, fpath string) (*Shader, error) {
if err != nil {
return nil, fmt.Errorf("compile fragment shader: %v", err)
}
- sid, err := linkShaders(vid, fid)
+ pid, err := linkShaders(vid, fid)
if err != nil {
return nil, fmt.Errorf("link shaders: %v", err)
}
gl.DeleteShader(vid)
gl.DeleteShader(fid)
- return &Shader{id: sid}, nil
+ return &Program{id: pid}, nil
}
// Use activates the shader.
-func (s *Shader) Use() {
- gl.UseProgram(s.id)
+func (p *Program) Use() {
+ gl.UseProgram(p.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"))
+func (p *Program) SetFloat32(name string, val float32) error {
+ l := gl.GetUniformLocation(p.id, gl.Str(name + "\x00"))
if l == -1 {
return fmt.Errorf("uniform %s not found", name)
}
@@ -97,13 +98,15 @@ func (s *Shader) SetFloat32(name string, val float32) error {
return nil
}
-func (s *Shader) SetTexture(t *Texture, name string) error {
- s.Use()
+func (p *Program) SetTexture(t *Texture, name string) error {
+ p.Use()
gl.ActiveTexture(t.unit)
- l := gl.GetUniformLocation(s.id, gl.Str(name + "\x00"))
+// gl.BindTexture(gl.TEXTURE_2D, t.id)
+ l := gl.GetUniformLocation(p.id, gl.Str(name + "\x00"))
if l == -1 {
return fmt.Errorf("no such uniform: %s", name)
}
+ fmt.Println("texture:", t.unit, t.unit-gl.TEXTURE0)
gl.Uniform1i(l, int32(t.unit - gl.TEXTURE0))
return nil
}
\ No newline at end of file