Permalink
Browse files

Initial work on mouse controlling rotation

  • Loading branch information...
1 parent c65c7b0 commit d17edb7fc85a7d1bafec09e04d173491e1bac12b @andrebq committed Jun 18, 2012
Showing with 61 additions and 40 deletions.
  1. +12 −0 model.go
  2. +49 −40 testdata/viewer/main.go
View
@@ -10,6 +10,18 @@ func (v *Vertex) Same(other *Vertex) bool {
return v.X == other.X && v.Y == other.Y && v.Z == other.Z
}
+// Subtract one for another
+func (v *Vertex) Sub(other *Vertex) (ret *Vertex) {
+ ret = &Vertex{other.X - v.X, other.Y - v.Y, other.Z - v.Z}
+ return
+}
+
+// Add on vector to another
+func (v *Vertex) Add(other *Vertex) (ret *Vertex) {
+ ret = &Vertex{other.X + v.X, other.Y + v.Y, other.Z + v.Z}
+ return
+}
+
// Represent a vertex list
type VertexList []Vertex
View
@@ -33,19 +33,29 @@ var (
speed float32 = 0.5
)
+type DragInfo struct {
+ Start wfobj.Vertex
+ End wfobj.Vertex
+ IsDrag bool
+}
+
type State struct {
- Keys map[int]bool
- yRot, xRot, zRot float32
- speed float32
- light [4]float32
- wheel float32
+ Keys map[int]bool
+ Rot wfobj.Vertex
+ speed float32
+ light [4]float32
+ wheel float32
+ Mouse map[int]bool
+ MousePos wfobj.Vertex
+ Drag DragInfo
}
var (
globalState = &State{Keys: make(map[int]bool), speed: 1}
)
func main() {
+ globalState.Mouse = make(map[int]bool)
flag.Parse()
var err error
if err = glfw.Init(); err != nil {
@@ -85,6 +95,8 @@ func main() {
glfw.SetKeyCallback(onKey)
glfw.SetCharCallback(onChar)
glfw.SetMouseWheelCallback(onWheel)
+ glfw.SetMouseButtonCallback(onMouseButton)
+ glfw.SetMousePosCallback(onMousePos)
initGL()
@@ -120,7 +132,34 @@ func onKey(key, state int) {
}
func onWheel(delta int) {
- globalState.wheel = float32(delta)
+ globalState.MousePos.Z = float32(delta)
+}
+
+func onMouseButton(button, state int) {
+ globalState.Mouse[button] = state == glfw.KeyPress
+ if globalState.Mouse[glfw.Mouse1] {
+ x, y := glfw.MousePos()
+ globalState.Drag.Start.X, globalState.Drag.Start.Y = float32(x), float32(y)
+ globalState.Drag.IsDrag = true
+ } else {
+ if globalState.Drag.IsDrag {
+ globalState.Drag.IsDrag = false
+ x, y := glfw.MousePos()
+ globalState.Drag.End.X, globalState.Drag.End.Y = float32(x), float32(y)
+ }
+ }
+}
+
+func onMousePos(x, y int) {
+ globalState.MousePos.X = float32(x)
+ globalState.MousePos.Y = float32(y)
+ if globalState.Drag.IsDrag {
+ // println("Start: ", fmt.Sprintf("%v", globalState.Drag.Start))
+ // println("End: ", fmt.Sprintf("%v", globalState.Drag.End))
+ // println("Sub: ", fmt.Sprintf("%v", globalState.Drag.End.Sub(&globalState.Drag.Start)))
+ globalState.Drag.End.X = float32(x)
+ globalState.Drag.End.Y = float32(y)
+ }
}
func onChar(key, state int) {
@@ -148,36 +187,6 @@ func initGL() {
}
func handleInput() {
- if globalState.Keys[glfw.KeyLeft] {
- globalState.yRot -= speed
- }
- if globalState.Keys[glfw.KeyRight] {
- globalState.yRot += speed
- }
- if globalState.Keys[glfw.KeyUp] {
- globalState.xRot += speed
- }
- if globalState.Keys[glfw.KeyDown] {
- globalState.xRot -= speed
- }
- if globalState.Keys[KeyW] {
- globalState.zRot += speed
- }
- if globalState.Keys[KeyS] {
- globalState.zRot -= speed
- }
- if globalState.Keys[KeyPlus] {
- speed += 0.5
- }
- if globalState.Keys[KeyMinus] {
- speed -= 0.5
- }
- if globalState.Keys[glfw.KeyPageup] {
- globalState.speed += 0.5
- }
- if globalState.Keys[glfw.KeyPagedown] {
- globalState.speed -= 0.5
- }
if globalState.speed < 1 {
globalState.speed = 1
}
@@ -193,11 +202,11 @@ func drawScene() {
gl.LoadIdentity()
- gl.Translatef(0, 0, -20+globalState.wheel*globalState.speed)
+ gl.Translatef(0, 0, -20+globalState.MousePos.Z*globalState.speed)
- gl.Rotatef(globalState.xRot, 1, 0, 0)
- gl.Rotatef(globalState.yRot, 0, 1, 0)
- gl.Rotatef(globalState.zRot, 0, 0, 1)
+ gl.Rotatef(globalState.Rot.X, 1, 0, 0)
+ gl.Rotatef(globalState.Rot.Y, 0, 1, 0)
+ gl.Rotatef(globalState.Rot.Z, 0, 0, 1)
if globalState.speed != 1 {
gl.Scalef(globalState.speed, globalState.speed, globalState.speed)

0 comments on commit d17edb7

Please sign in to comment.