-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
80 lines (63 loc) · 2.08 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32"
// "github.com/go-gl/mathgl/mgl32"
"strings"
)
func compileShader(source string, shaderType uint32) (uint32, error) {
shader := gl.CreateShader(shaderType)
csources, free := gl.Strs(source)
gl.ShaderSource(shader, 1, csources, 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, fmt.Errorf("failed to compile %v: %v", source, log)
}
return shader, nil
}
func newProg(vertShad, fragShad string) (uint32, error) {
vertexShader, err := compileShader(vertShad, gl.VERTEX_SHADER)
if err != nil {
panic(err)
}
fragmentShader, err := compileShader(fragShad, gl.FRAGMENT_SHADER)
if err != nil {
panic(err)
}
prog := gl.CreateProgram()
gl.AttachShader(prog, vertexShader)
gl.AttachShader(prog, fragmentShader)
gl.LinkProgram(prog)
var status int32
gl.GetProgramiv(prog, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(prog, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(prog, logLength, nil, gl.Str(log))
return 0, fmt.Errorf("failed to link prog: %v", log)
}
gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)
return prog, nil
}
func UpdateUniformMat4fv(name string, prog uint32, value *float32) {
UniformLocation := gl.GetUniformLocation(prog, gl.Str(name+"\x00"))
gl.UniformMatrix4fv(UniformLocation, 1, false, value)
}
func Refresh(w *glfw.Window) {
width, height := w.GetFramebufferSize()
gl.Viewport(0, 0, int32(width), int32(height))
projectionMat := mgl32.Perspective(mgl32.DegToRad(90),float32(width) / float32(height), 1/viewRange, viewRange)
UpdateUniformMat4fv("projection", program, &projectionMat[0])
fmt.Println(float32(width) / float32(height))
}