forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate.go
244 lines (208 loc) · 5.2 KB
/
coordinate.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package canvas3d
import "C"
import (
"github.com/go-gl/mathgl/mgl64"
"github.com/gorustyt/fyne/v2"
"github.com/gorustyt/fyne/v2/canvas3d/context"
"math"
"github.com/go-gl/mathgl/mgl32"
"github.com/gorustyt/fyne/v2/internal/painter/gl"
)
const (
ProjectName = "projection"
ViewName = "view"
ModelName = "model"
)
const (
Forward = iota
Backward
Left
Right
)
const (
Yaw = -90.0
Pitch = 0.0
Speed = 3.0
Sensitivy = 0.01
)
const (
Near = 0.1
Far = 100
)
var (
_ gl.Canvas3D = (*Coordinate)(nil)
_ fyne.Scrollable = (*Coordinate)(nil)
_ fyne.Draggable = (*Coordinate)(nil)
_ gl.Canvas3DBeforePainter = (*Coordinate)(nil)
)
type Coordinate struct {
*ViewConfig
*ProjectConfig
*ModelConfig
frameSize fyne.Size
firstMouse bool
}
func (c *Coordinate) NeedShader() bool {
return true
}
func (c *Coordinate) UpdateFrameSize(frameSize fyne.Size) {
if c.frameSize != frameSize {
c.frameSize = frameSize
c.LastX = frameSize.Width / 2
c.LastY = frameSize.Height / 2
c.firstMouse = true
}
}
func (c *Coordinate) BeforeDraw(p context.Painter, pos fyne.Position, frame fyne.Size) {
c.UpdateFrameSize(frame)
project := mgl32.Perspective(mgl32.DegToRad(c.Fov), frame.Width/frame.Height, c.Near, c.Far)
p.UniformMatrix4fv(ProjectName, project)
}
func (c *Coordinate) Dragged(event *fyne.DragEvent) {
xPos := event.AbsolutePosition.X
yPos := event.AbsolutePosition.Y
if c.firstMouse {
c.LastX = xPos
c.LastY = yPos
c.firstMouse = false
}
xOffset := float64(xPos - c.LastX)
yOffset := float64(c.LastY - yPos)
c.LastX = xPos
c.LastY = yPos
xOffset *= c.MouseSensitivity
yOffset *= c.MouseSensitivity
c.Yaw += xOffset
c.Pitch += yOffset
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if c.ConstrainPitch {
if c.Pitch > 89.0 {
c.Pitch = 89.0
}
if c.Pitch < -89.0 {
c.Pitch = -89.0
}
}
}
func (c *Coordinate) DragEnd() {
x := float32(math.Cos(mgl64.DegToRad(c.Yaw)) * math.Cos(mgl64.DegToRad(c.Pitch)))
y := float32(math.Sin(mgl64.DegToRad(c.Pitch)))
z := float32(math.Sin(mgl64.DegToRad(c.Yaw)) * math.Cos(mgl64.DegToRad(c.Pitch)))
front := mgl32.Vec3{x, y, z}
c.Front = front.Normalize()
// Also re-calculate the Right and Up vector
// Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
c.Right = front.Cross(c.WorldUp).Normalize()
c.Up = c.Right.Cross(c.Front).Normalize()
}
func (c *Coordinate) Scrolled(event *fyne.ScrollEvent) {
yOffset := event.Scrolled.DY
if c.Fov >= 1.0 && c.Fov <= 45. {
c.Fov -= yOffset
if c.Fov <= 1.0 {
c.Fov = 1.0
}
if c.Fov >= 45.0 {
c.Fov = 45.0
}
}
}
func (c *Coordinate) InitOnce(p *gl.Painter3D) {
}
func (c *Coordinate) Init(p *gl.Painter3D) {
p.UniformMatrix4fv(ViewName, c.GetView())
p.UniformMatrix4fv(ModelName, c.GetModel())
}
func (c *Coordinate) After(p *gl.Painter3D) {
}
func NewCoordinate() *Coordinate {
c := &Coordinate{
firstMouse: true,
ModelConfig: NewModelConfig(),
ProjectConfig: &ProjectConfig{
Near: Near,
Far: Far,
Fov: 45,
},
ViewConfig: &ViewConfig{
Yaw: Yaw,
Pitch: Pitch,
Front: mgl32.Vec3{0.0, 0.0, -1.0},
Position: mgl32.Vec3{0.0, 0.0, 3},
WorldUp: mgl32.Vec3{0, 1, 0},
MovementSpeed: Speed,
MouseSensitivity: Sensitivy,
ConstrainPitch: true,
},
}
c.Right = c.Front.Cross(c.WorldUp).Normalize()
c.Up = c.Right.Cross(c.Front).Normalize()
return c
}
type ViewConfig struct {
Position mgl32.Vec3
Front mgl32.Vec3
Up mgl32.Vec3
Right mgl32.Vec3
WorldUp mgl32.Vec3
Yaw float64
Pitch float64
LastX float32
LastY float32
MovementSpeed float64
MouseSensitivity float64
ConstrainPitch bool
}
func (c *ViewConfig) ProcessKeyboard(direction int, deltaTime float64) {
velocity := float32(c.MovementSpeed * deltaTime)
if direction == Forward {
c.Position = c.Position.Add(c.Front.Mul(velocity))
}
if direction == Backward {
c.Position = c.Position.Sub(c.Front.Mul(velocity))
}
if direction == Left {
c.Position = c.Position.Sub(c.Right.Mul(velocity))
}
if direction == Right {
c.Position = c.Position.Add(c.Right.Mul(velocity))
}
}
func (c *ViewConfig) GetView() mgl32.Mat4 {
eye := c.Position
return mgl32.LookAtV(eye, eye.Add(c.Front), c.Up)
}
type ProjectConfig struct {
Near float32
Far float32
Fov float32
}
type ModelConfig struct {
Model mgl32.Mat4
translate mgl32.Mat4
scale mgl32.Mat4
rotate mgl32.Mat4
}
func NewModelConfig() *ModelConfig {
return &ModelConfig{
Model: mgl32.Ident4(),
translate: mgl32.Ident4(),
scale: mgl32.Ident4(),
rotate: mgl32.Ident4(),
}
}
func (m *ModelConfig) GetModel() mgl32.Mat4 {
return m.Model.Mul4(m.translate).Mul4(m.scale).Mul4(m.rotate)
}
func (m *ModelConfig) TranslateXYZ(x, y, z float32) {
m.translate = mgl32.Translate3D(x, y, z)
}
func (m *ModelConfig) TranslateVec3(vec mgl32.Vec3) {
m.translate = mgl32.Translate3D(vec.X(), vec.Y(), vec.Z())
}
func (m *ModelConfig) Rotate(angle float32, axis mgl32.Vec3) {
m.rotate = mgl32.HomogRotate3D(mgl32.DegToRad(angle), axis)
}
func (m *ModelConfig) Scale(x, y, z float32) {
m.scale = mgl32.Scale3D(x, y, z)
}