opengl-deferred/types/shader/shader.go

147 lines
3.4 KiB
Go
Raw Permalink Normal View History

2024-10-11 16:38:21 +00:00
package shader
import (
"errors"
2024-10-12 12:31:15 +00:00
"fmt"
2024-12-16 12:24:09 +00:00
"os"
2024-10-11 16:38:21 +00:00
"strings"
2024-10-12 12:31:15 +00:00
"git.tek.govt.hu/dowerx/opengl-deferred/types/light"
2024-10-11 16:38:21 +00:00
"github.com/go-gl/gl/v4.6-core/gl"
2024-10-11 23:02:12 +00:00
"github.com/go-gl/mathgl/mgl32"
2024-10-11 16:38:21 +00:00
)
type Shader struct {
2024-10-12 12:31:15 +00:00
program uint32
uniforms map[string]int32
lightCount int32
2024-10-11 16:38:21 +00:00
}
func (s *Shader) Use() {
gl.UseProgram(s.program)
2024-10-12 12:31:15 +00:00
s.lightCount = 0
2024-10-11 16:38:21 +00:00
}
2024-12-17 00:16:17 +00:00
func (s Shader) Delete() {
2024-10-11 16:38:21 +00:00
gl.DeleteProgram(s.program)
}
2024-10-11 23:02:12 +00:00
func (s *Shader) getUniformLocation(name string) int32 {
if val, ok := s.uniforms[name]; ok {
return val
}
uniform := gl.GetUniformLocation(s.program, gl.Str(name+"\x00"))
s.uniforms[name] = uniform
return uniform
}
func (s *Shader) Mat4(name string, mat mgl32.Mat4) {
gl.UniformMatrix4fv(s.getUniformLocation(name), 1, false, &mat[0])
}
func (s *Shader) Int(name string, val int32) {
gl.Uniform1i(s.getUniformLocation(name), val)
}
2024-10-12 12:31:15 +00:00
func (s *Shader) Float(name string, val float32) {
gl.Uniform1f(s.getUniformLocation(name), val)
}
func (s *Shader) Vec3(name string, val *float32) {
gl.Uniform3fv(s.getUniformLocation(name), 1, val)
}
func (s *Shader) Light(l *light.Light) error {
if s.lightCount >= light.MAX_LIGHT_COUNT {
return errors.New("too many lights")
}
s.Vec3(fmt.Sprintf("lights[%d].color", s.lightCount), &l.Color[0])
s.Vec3(fmt.Sprintf("lights[%d].position", s.lightCount), &l.Position[0])
s.Vec3(fmt.Sprintf("lights[%d].direction", s.lightCount), &l.Direction[0])
s.Float(fmt.Sprintf("lights[%d].intensity", s.lightCount), l.Intensity)
s.Int(fmt.Sprintf("lights[%d].type", s.lightCount), l.Type)
s.lightCount++
s.Int("light_count", s.lightCount)
return nil
}
2024-10-11 16:38:21 +00:00
func compileShader(source string, shaderType uint32) (uint32, error) {
shader := gl.CreateShader(shaderType)
2024-10-15 14:52:21 +00:00
csource, free := gl.Strs(source + "\x00")
2024-10-11 16:38:21 +00:00
gl.ShaderSource(shader, 1, csource, nil)
free()
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
return 0, errors.New(log)
}
return shader, nil
}
2024-12-16 12:24:09 +00:00
func New(vertexSource string, fragmentSource string) (shader Shader, _ error) {
2024-10-11 23:02:12 +00:00
shader.uniforms = make(map[string]int32)
2024-10-11 16:38:21 +00:00
vertexShader, err := compileShader(vertexSource, gl.VERTEX_SHADER)
if err != nil {
return shader, err
}
2024-12-16 12:24:09 +00:00
fragmentShader, err := compileShader(fragmentSource, gl.FRAGMENT_SHADER)
2024-10-11 16:38:21 +00:00
if err != nil {
return shader, err
}
shader.program = gl.CreateProgram()
gl.AttachShader(shader.program, vertexShader)
2024-12-16 12:24:09 +00:00
gl.AttachShader(shader.program, fragmentShader)
2024-10-11 16:38:21 +00:00
gl.LinkProgram(shader.program)
defer gl.DeleteShader(vertexShader)
2024-12-16 12:24:09 +00:00
defer gl.DeleteShader(fragmentShader)
2024-10-11 16:38:21 +00:00
var status int32
gl.GetProgramiv(shader.program, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(shader.program, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(shader.program, logLength, nil, gl.Str(log))
return shader, errors.New(log)
}
return shader, nil
}
2024-12-16 12:24:09 +00:00
func FromFiles(vertexPath, fragmentPath string) (shader Shader, _ error) {
vertexSource, err := os.ReadFile(vertexPath)
if err != nil {
return shader, err
}
fragmentSource, err := os.ReadFile(fragmentPath)
if err != nil {
return shader, err
}
shader, err = New(string(vertexSource), string(fragmentSource))
if err != nil {
return shader, err
}
return shader, nil
}