summaryrefslogtreecommitdiff
path: root/app.go
blob: a52802062912cef244457de4a02cd63a43a872b7 (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
64
65
66
67
68
69
70
71
72
73
74
75
// vim: shiftwidth=4 tabstop=4 noexpandtab

package main

import (
	"log"

	"github.com/go-gl/glfw/v3.3/glfw"
	"github.com/go-gl/mathgl/mgl32"
)

const (
	WINDOW_WIDTH  = 500
	WINDOW_HEIGHT = 500
	WINDOW_TITLE = "stlscope"
)

// App contains data about the OpenGL application including for example the
// window, OpenGL graphics and the current STL model.
type App struct {
	window *glfw.Window
	graphics *Graphics
	stl *StlModel
	homeTrafo mgl32.Mat4
}

// newApp creates a new App struct based on a given STL model.
func newApp(stl *StlModel) App {

	var app App = App{}
	app.stl = stl
	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()

	app.homeTrafo = stl.surface.getHomeView()

	return app
}

// handle has to be called frequently to let the App work.
func (application *App) handle() {
	glfw.PollEvents()
	application.window.SwapBuffers()

	// generate and set transformation
	trafo := application.homeTrafo
	trafo = mgl32.HomogRotate3D(
		float32(glfw.GetTime()) * 0.4 * 6.282,
		mgl32.Vec3{0.1, 1, 0}).Mul4(trafo)  // apply time-based rotation
	application.graphics.setTrafo(trafo)
}

// terminate closes the App.
func (application *App) terminate() {
	glfw.Terminate()
}