commit 08820a0fe9c3eebac8fff31cbbbf55b4cdeaa958
parent fc6eaa5e3954f98dbe2255dbce581d24a3371d74
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 17 Oct 2024 21:38:59 +0900
link shaders
Diffstat:
M | cmd/sample/main.go | | | 83 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 83 insertions(+), 0 deletions(-)
diff --git a/cmd/sample/main.go b/cmd/sample/main.go
@@ -1,9 +1,11 @@
package main
import (
+ "fmt"
"log"
"math"
"runtime"
+ "strings"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
@@ -30,6 +32,65 @@ func processInput(w *glfw.Window) {
}
}
+func newShader(src string, xtype uint32) (shader uint32, err error) {
+ shader = gl.CreateShader(xtype)
+ cstrs, free := gl.Strs(src + "\x00")
+ defer free()
+ gl.ShaderSource(shader, 1, cstrs, nil)
+ gl.CompileShader(shader)
+
+ var success int32
+ gl.GetShaderiv(shader, gl.COMPILE_STATUS, &success)
+ if success == gl.FALSE {
+ var logLength int32
+ gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
+ l := gl.Str(strings.Repeat("\x00", int(logLength)))
+ gl.GetShaderInfoLog(shader, logLength, nil, l)
+ gl.DeleteShader(shader)
+ return 0, fmt.Errorf("%v", gl.GoStr(l))
+ }
+ return shader, nil
+}
+
+func linkShaders(vertexShader, fragmentShader uint32) (shader uint32, err error) {
+ shader = gl.CreateProgram()
+ gl.AttachShader(shader, vertexShader)
+ gl.AttachShader(shader, fragmentShader)
+ gl.LinkProgram(shader)
+
+ var success int32
+ gl.GetProgramiv(shader, gl.LINK_STATUS, &success)
+ if success == gl.FALSE {
+ var logLength int32
+ gl.GetProgramiv(shader, gl.INFO_LOG_LENGTH, &logLength)
+ l := gl.Str(strings.Repeat("\x00", int(logLength)))
+ gl.GetProgramInfoLog(shader, logLength, nil, l)
+ return 0, fmt.Errorf("%v", gl.GoStr(l))
+ }
+ return shader, nil
+}
+
+var vertices = []float32{
+ -0.5, -0.5, 0.0,
+ 0.5, -0.5, 0.0,
+ 0.0, 0.5, 0.0,
+}
+
+const vertexShaderSource = `#version 330 core
+layout (location = 0) in vec3 aPos;
+void main() {
+ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
+}
+`
+
+const fragmentShaderSource = `#version 330 core
+out vec4 FragColor;
+
+void main() {
+ FragColor = vec4(1.0, 0.5, 0.2, 1.0);
+}
+`
+
func main() {
err := glfw.Init()
if err != nil {
@@ -50,8 +111,30 @@ func main() {
}
gl.Viewport(0, 0, winW, winH)
+ vertexShader, err := newShader(vertexShaderSource, gl.VERTEX_SHADER)
+ if err != nil {
+ log.Fatalf("crete vertex shader: %v", err)
+ }
+ fragmentShader, err := newShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
+ if err != nil {
+ log.Fatalf("crete fragment shader: %v", err)
+ }
+ shaderProgram, err := linkShaders(vertexShader, fragmentShader)
+ if err != nil {
+ log.Fatalf("link shaders: %v", err)
+ }
+ gl.UseProgram(shaderProgram)
+ gl.DeleteShader(vertexShader)
+ gl.DeleteShader(fragmentShader)
+
window.SetFramebufferSizeCallback(framebufferSizeCallback)
+ var VBO uint32
+ gl.GenBuffers(1, &VBO)
+ gl.BindBuffer(gl.ARRAY_BUFFER, VBO)
+ gl.BufferData(gl.ARRAY_BUFFER, len(vertices) * 4,
+ gl.Ptr(vertices), gl.STATIC_DRAW)
+
for !window.ShouldClose() {
processInput(window)
gl.ClearColor(0.2, 0.3, 0.3, 1.0)