diff options
-rw-r--r-- | app.go | 55 | ||||
-rw-r--r-- | graphics.go | 33 | ||||
-rw-r--r-- | main.go | 32 |
3 files changed, 71 insertions, 49 deletions
@@ -0,0 +1,55 @@ +// vim: shiftwidth=4 tabstop=4 noexpandtab + +package main + +import ( + "log" + + "github.com/go-gl/glfw/v3.3/glfw" +) + +const ( + WINDOW_WIDTH = 500 + WINDOW_HEIGHT = 500 + WINDOW_TITLE = "stlscope" +) + +type App struct { + window *glfw.Window +} + +func newApp() App { + + var app App = App{} + var err error + + log.Println("GLFW init") + if err := glfw.Init(); err != nil { + panic(err) + } + + glfw.WindowHint(glfw.Resizable, glfw.False) + glfw.WindowHint(glfw.ContextVersionMajor, 4) + glfw.WindowHint(glfw.ContextVersionMinor, 6) + glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) + glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) + glfw.WindowHint(glfw.Samples, 16) // anti-aliasing + + log.Println("Creating Window") + app.window, err = glfw.CreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, nil, nil) + if err != nil { + panic(err) + } + app.window.MakeContextCurrent() + + return app +} + +func (application App) handle() { + glfw.PollEvents() + application.window.SwapBuffers() +} + +func (application App) terminate() { + glfw.Terminate() +} diff --git a/graphics.go b/graphics.go index 812be3a..6cb3400 100644 --- a/graphics.go +++ b/graphics.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/go-gl/gl/v4.6-core/gl" - "github.com/go-gl/glfw/v3.3/glfw" ) var ( @@ -21,10 +20,6 @@ var ( ) const ( - WINDOW_WIDTH = 500 - WINDOW_HEIGHT = 500 - WINDOW_TITLE = "stlscope" - // vertex shader to draw points VERTEX_SHADER = ` #version 410 @@ -44,29 +39,6 @@ const ( ` + "\x00" ) -func initGlfw() *glfw.Window { - log.Println("GLFW init") - - if err := glfw.Init(); err != nil { - panic(err) - } - - glfw.WindowHint(glfw.Resizable, glfw.False) - glfw.WindowHint(glfw.ContextVersionMajor, 4) - glfw.WindowHint(glfw.ContextVersionMinor, 6) - glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) - glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) - glfw.WindowHint(glfw.Samples, 16) // anti-aliasing - - window, err := glfw.CreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, nil, nil) - if err != nil { - panic(err) - } - window.MakeContextCurrent() - - return window -} - // initOpenGL initializes OpenGL and returns an intiialized program func initOpenGL() uint32 { log.Println("OpenGL init") @@ -93,15 +65,12 @@ func initOpenGL() uint32 { return prog } -func draw(vao uint32, window *glfw.Window, program uint32) { +func draw(vao uint32, program uint32) { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(program) gl.BindVertexArray(vao) gl.DrawArrays(gl.TRIANGLES, 0, int32(len(triangle)/3)) - - glfw.PollEvents() - window.SwapBuffers() } // makeVao initializes and returns a vertex array from the points provided. @@ -6,18 +6,19 @@ import ( "runtime" "log" "flag" - - "github.com/go-gl/glfw/v3.3/glfw" ) -var ( - stlFilePath string -) +func init() { + // lock this program to one OS thread (details: https://golang.org/pkg/runtime/#LockOSThread) + log.Println("Locking OS thread") + runtime.LockOSThread() +} func main() { // read command line arguments - parseFlags() + var stlFilePath string + parseFlags(&stlFilePath) // parse STL file _, err := ReadBinaryStlFile(stlFilePath) @@ -25,13 +26,9 @@ func main() { log.Fatal(err) } - // lock this program to one OS thread (details: https://golang.org/pkg/runtime/#LockOSThread) - log.Println("Locking OS thread") - runtime.LockOSThread() - - // init GLFW and assert termination at end of main - window := initGlfw() - defer glfw.Terminate() + // initialize application (includes GLFW/window) + var app App = newApp() + defer app.terminate() // GLFW needs to be terminated! // init OpenGL program := initOpenGL() @@ -39,12 +36,13 @@ func main() { vao := makeVao(triangle) // main loop - for !window.ShouldClose() { - draw(vao, window, program) + for !app.window.ShouldClose() { + app.handle() + draw(vao, program) } } -func parseFlags() { - flag.StringVar(&stlFilePath, "file", "myfile.stl", "path to the binary STL file") +func parseFlags(stlFilePath *string) { + flag.StringVar(stlFilePath, "file", "myfile.stl", "path to the binary STL file") flag.Parse() } |