diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-05-21 14:02:40 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-05-21 14:02:40 +0200 |
commit | feff22144689df9da5ddb8e9693326b77feb7949 (patch) | |
tree | cf8af3404cbf0bc749eeebd93ce9a120e62ab975 | |
parent | c8f8de20f5ff03d94a969e23cfe77d52e10b3ab4 (diff) | |
download | stlscope-feff22144689df9da5ddb8e9693326b77feb7949.tar stlscope-feff22144689df9da5ddb8e9693326b77feb7949.tar.zst stlscope-feff22144689df9da5ddb8e9693326b77feb7949.zip |
Refactor OpenGL-related Code
-rw-r--r-- | graphics.go | 55 | ||||
-rw-r--r-- | main.go | 8 |
2 files changed, 39 insertions, 24 deletions
diff --git a/graphics.go b/graphics.go index 6cb3400..91edcce 100644 --- a/graphics.go +++ b/graphics.go @@ -39,37 +39,54 @@ const ( ` + "\x00" ) -// initOpenGL initializes OpenGL and returns an intiialized program -func initOpenGL() uint32 { - log.Println("OpenGL init") +type Graphics struct { + version string + vao uint32 + vertexShader uint32 + fragmentShader uint32 + program uint32 +} + +func newGraphics() Graphics { - if err := gl.Init(); err != nil { - panic(err) + var graphics Graphics = Graphics{} + + // init OpenGL and save/log version + log.Println("OpenGL init") + err := gl.Init() + if err != nil { + log.Fatal(err) } - version := gl.GoStr(gl.GetString(gl.VERSION)) - log.Println("OpenGL version", version) + graphics.version = gl.GoStr(gl.GetString(gl.VERSION)) + log.Println("OpenGL version", graphics.version) - vertexShader, err := compileShader(VERTEX_SHADER, gl.VERTEX_SHADER) + // compile shaders + graphics.vertexShader, err = compileShader(VERTEX_SHADER, gl.VERTEX_SHADER) if err != nil { - panic(err) + log.Fatal(err) } - fragmentShader, err := compileShader(FRAGMENT_SHADER, gl.FRAGMENT_SHADER) + graphics.fragmentShader, err = compileShader(FRAGMENT_SHADER, gl.FRAGMENT_SHADER) if err != nil { - panic(err) + log.Fatal(err) } - prog := gl.CreateProgram() - gl.AttachShader(prog, vertexShader) - gl.AttachShader(prog, fragmentShader) - gl.LinkProgram(prog) - return prog + // create GL program + graphics.program = gl.CreateProgram() + gl.AttachShader(graphics.program, graphics.vertexShader) + gl.AttachShader(graphics.program, graphics.fragmentShader) + gl.LinkProgram(graphics.program) + + // create VAO + graphics.vao = makeVao(triangle) + + return graphics } -func draw(vao uint32, program uint32) { +func (graphics Graphics) draw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) - gl.UseProgram(program) + gl.UseProgram(graphics.program) - gl.BindVertexArray(vao) + gl.BindVertexArray(graphics.vao) gl.DrawArrays(gl.TRIANGLES, 0, int32(len(triangle)/3)) } @@ -30,15 +30,13 @@ func main() { var app App = newApp() defer app.terminate() // GLFW needs to be terminated! - // init OpenGL - program := initOpenGL() - - vao := makeVao(triangle) + // initialize graphics + var graphics Graphics = newGraphics() // main loop for !app.window.ShouldClose() { + graphics.draw() app.handle() - draw(vao, program) } } |