Example #1
0
func unproject(glctx gl.Context, x, y float32) (mgl32.Vec3, mgl32.Vec3) {
	var wx, wy float32
	var viewport [4]int32
	glctx.GetIntegerv(viewport[:], gl.VIEWPORT)

	wx = x
	wy = float32(viewport[3]) - y

	posStart, err := mgl32.UnProject(mgl32.Vec3{wx, wy, 0}, arcball.getMtx(), projectionMtx, int(viewport[0]), int(viewport[1]), int(viewport[2]), int(viewport[3]))
	posEnd, err := mgl32.UnProject(mgl32.Vec3{wx, wy, 1}, arcball.getMtx(), projectionMtx, int(viewport[0]), int(viewport[1]), int(viewport[2]), int(viewport[3]))

	if err != nil {
		log.Printf("unable to unproject: %+v", err)
	}

	return posStart, posEnd
}
Example #2
0
func (c *Camera) GetMouseVector(windowSize mgl32.Vec2, mouse mgl32.Vec2) mgl32.Vec3 {
	v, err := mgl32.UnProject(
		mgl32.Vec3{mouse.X(), windowSize.Y() - mouse.Y(), 0.5},
		mgl32.LookAtV(c.Translation, c.Lookat, c.Up),
		mgl32.Perspective(mgl32.DegToRad(c.Angle), windowSize.X()/windowSize.Y(), c.Near, c.Far),
		0, 0, int(windowSize.X()), int(windowSize.Y()),
	)
	if err == nil {
		return v.Sub(c.Translation).Normalize()
	} else {
		log.Println("Error converting camera vector: ", err)
	}
	return c.Lookat
}