summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-05-25 12:51:50 +0200
committerxengineering <mail2xengineering@protonmail.com>2021-05-25 12:51:50 +0200
commit4ab710eba1708966f8ae9e637deafa5be060b6d5 (patch)
tree88c2a8b51a5d0963c1571563fc396500541fee0f
parente03455a979abf4444d4362dacbbf55c6915d3b69 (diff)
downloadstlscope-4ab710eba1708966f8ae9e637deafa5be060b6d5.tar
stlscope-4ab710eba1708966f8ae9e637deafa5be060b6d5.tar.zst
stlscope-4ab710eba1708966f8ae9e637deafa5be060b6d5.zip
Implement Home View
-rw-r--r--app.go13
-rw-r--r--geometry.go47
-rw-r--r--main.go2
3 files changed, 56 insertions, 6 deletions
diff --git a/app.go b/app.go
index 0591be9..22548a3 100644
--- a/app.go
+++ b/app.go
@@ -18,11 +18,14 @@ const (
type App struct {
window *glfw.Window
graphics *Graphics
+ stl *StlModel
+ homeTrafo mgl32.Mat4
}
-func newApp() App {
+func newApp(stl *StlModel) App {
var app App = App{}
+ app.stl = stl
var err error
log.Println("GLFW init")
@@ -44,6 +47,8 @@ func newApp() App {
}
app.window.MakeContextCurrent()
+ app.homeTrafo = stl.surface.getHomeView()
+
return app
}
@@ -52,10 +57,8 @@ func (application *App) handle() {
application.window.SwapBuffers()
// generate and set transformation
- var scale float32 = 0.01
- trafo := mgl32.Ident4()
- trafo = trafo.Mul4(mgl32.HomogRotate3D(float32(glfw.GetTime()) * 3, mgl32.Vec3{0, 1, 1}))
- trafo = trafo.Mul4(mgl32.Scale3D(scale, scale, scale))
+ trafo := application.homeTrafo
+ trafo = mgl32.HomogRotate3D(float32(glfw.GetTime()) * 3, mgl32.Vec3{0, 1, 0}).Mul4(trafo) // apply time-based rotation
application.graphics.setTrafo(trafo)
}
diff --git a/geometry.go b/geometry.go
index 04896dc..1fc516a 100644
--- a/geometry.go
+++ b/geometry.go
@@ -2,6 +2,10 @@
package main
+import (
+ "github.com/go-gl/mathgl/mgl32"
+)
+
// representation of a three-dimensional point in space
type Point struct {
scalars [3]float32 // x = scalars[0], y = scalars[1], z = scalars[2]
@@ -17,3 +21,46 @@ type Surface struct {
triangles []*Triangle
}
+// return a transformation to view the whole centered surface
+func (s Surface) getHomeView() mgl32.Mat4 {
+
+ // evaluating the smallest and biggest values for x,y,z of the surface
+ var min,max [3]float32
+ for _,triangle := range(s.triangles) {
+ for _,point := range(triangle.points) {
+ for index,_ := range(min) {
+ if point.scalars[index] < min[index] {
+ min[index] = point.scalars[index]
+ }
+ if point.scalars[index] > max[index] {
+ max[index] = point.scalars[index]
+ }
+ }
+ }
+ }
+
+ // calculate center point
+ var center Point
+ for index,_ := range(min) {
+ center.scalars[index] = (min[index] + max[index]) / 2
+ }
+
+ // calculate zoom
+ var zoom,zoomX,zoomY float32
+ const fillFactor float32 = 0.8
+ zoomX = (2 / (max[0] - min[0])) * fillFactor
+ zoomY = (2 / (max[1] - min[1])) * fillFactor
+ if zoomX > zoomY {
+ zoom = zoomY
+ } else {
+ zoom = zoomX
+ }
+
+ // calculate and return transformation
+ var trafo mgl32.Mat4 = mgl32.Ident4()
+ trafo = mgl32.Translate3D(-center.scalars[0], -center.scalars[1], -center.scalars[2]).Mul4(trafo)
+ trafo = mgl32.Scale3D(zoom, zoom, zoom).Mul4(trafo)
+
+ return trafo
+}
+
diff --git a/main.go b/main.go
index 1bf0b9d..e74cd1f 100644
--- a/main.go
+++ b/main.go
@@ -28,7 +28,7 @@ func main() {
vertices = stl.toVertices()
// initialize application (includes GLFW/window)
- var app App = newApp()
+ var app App = newApp(&stl)
defer app.terminate() // GLFW needs to be terminated!
// initialize graphics