summaryrefslogtreecommitdiff
path: root/main.go
blob: 672bb8d39c23543e9335cf8fee7e314a32b1d9e9 (plain)
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
// vim: shiftwidth=4 tabstop=4 noexpandtab

// stlscope is a file viewer for the STL file format. It is based on Go
// and OpenGL.
package main

import (
	"runtime"
	"log"
	"flag"
	"io/ioutil"
)

// cliArgs contains all data given via command line arguments.
type cliArgs struct {
	filePath string
	debugOutput bool
}

// main represents the whole functionality of stlscope.
func main() {

	// read command line arguments and mute log if necessary
	var args cliArgs
	args.read()
	if !args.debugOutput {
		log.SetOutput(ioutil.Discard)
	}

	// parse STL file
	stl, err := ReadBinaryStlFile(args.filePath)
	if err != nil {
		log.Fatal(err)
	}
	vertices,vertex_normals = stl.toVertices()

	// lock this program to one OS thread
	// (details: https://golang.org/pkg/runtime/#LockOSThread)
	log.Println("Locking OS thread")
	runtime.LockOSThread()

	// initialize application (includes GLFW/window)
	var app App = newApp(&stl)
	defer app.terminate()  // GLFW needs to be terminated!

	// initialize graphics
	var graphics Graphics = newGraphics()
	app.graphics = &graphics  // connect graphics to the app

	// main loop
	for !app.window.ShouldClose() {
		graphics.draw()
		app.handle()
	}
}

// read reads the command line arguments given to stlscope to a cliArgs struct.
func (args *cliArgs) read() {
	flag.BoolVar(&args.debugOutput, "debug", false,
		"enable to print log output")
	flag.Parse()
	args.filePath = flag.Arg(0)
}