forked from vova616/GarageEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.go
223 lines (190 loc) · 5.47 KB
/
Camera.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
package engine
import (
"errors"
"github.com/go-gl/gl"
"github.com/LaPingvino/GarageEngine/engine/input"
)
type Rect struct {
Min, Max Vector
}
func (r *Rect) Overlaps(s Rect) bool {
return r.Min.X < s.Max.X && s.Min.X < r.Max.X &&
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
}
type Camera struct {
BaseComponent
Projection *Matrix
size float32
rect Rect
realRect Rect //
center Vector //Center of scale
sizeIsScale bool //Size is also the scale
autoScale bool //Scale together with window size
clearColor Color
}
func NewCamera() *Camera {
c := &Camera{BaseComponent: NewComponent(), Projection: NewIdentity(), size: 1, sizeIsScale: true, autoScale: true}
/*
c.rect.Min.X - left
c.rect.Max.X - right
c.rect.Min.Y - bottom
c.rect.Max.Y - top
*/
c.center = NewVector2(0, 0)
c.rect.Min.X = -float32(Width) / 2
c.rect.Max.X = float32(Width) / 2
c.rect.Min.Y = -float32(Height) / 2
c.rect.Max.Y = float32(Height) / 2
c.UpdateResolution()
return c
}
func (c *Camera) Update() {
c.LateUpdate()
}
func (c *Camera) Clear() {
gl.ClearColor(gl.GLclampf(c.clearColor.R), gl.GLclampf(c.clearColor.G), gl.GLclampf(c.clearColor.B), gl.GLclampf(c.clearColor.A))
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
}
func (c *Camera) LateUpdate() {
if c.sizeIsScale {
s := c.Transform().Scale()
if s.X != c.size || s.Y != c.size {
c.Transform().SetScalef(c.size, c.size)
}
}
}
//Setting the size and also scale if sizeIsScale is true
func (c *Camera) SetSize(size float32) error {
if size <= 0 {
return errors.New("Camera size cannot be 0 or less")
}
c.size = size
if c.sizeIsScale {
c.Transform().SetScalef(size, size)
}
c.UpdateResolution()
return nil
}
//Size of the camera screen
func (c *Camera) Size() float32 {
return c.size
}
//InvertedMatrix of the camera, this is needed because we will optimize it someday
func (c *Camera) InvertedMatrix() Matrix {
if c.sizeIsScale {
return c.Transform().InvertedMatrix()
} else {
x := c.Matrix()
return x.Invert()
}
}
//Returns Screen Size
func (c *Camera) ScreenSize() (Width, Height float32) {
if c.sizeIsScale {
return (c.realRect.Max.X - c.realRect.Min.X) * c.size, (c.realRect.Max.Y - c.realRect.Min.Y) * c.size
}
return c.realRect.Max.X - c.realRect.Min.X, c.realRect.Max.Y - c.realRect.Min.Y
}
//Screen resolution
func (c *Camera) ScreenResolution() (Width, Height float32) {
return c.rect.Max.X - c.rect.Min.X, c.rect.Max.Y - c.rect.Min.Y
}
//Matrix of the camera, this is needed because sometimes we control the matrix
func (c *Camera) Matrix() Matrix {
if c.sizeIsScale {
c.Transform().updateMatrix()
return c.Transform().matrix
}
m := Identity()
pos := c.Transform().WorldPosition()
r := c.Transform().WorldRotation()
m.Rotate(r.X, 1, 0, 0)
m.Rotate(r.Y, 0, 1, 0)
m.Rotate(r.Z, 0, 0, -1)
m.Translate(pos.X, pos.Y, pos.Z)
return m
}
//Checks if box is in the screen
func (c *Camera) InsideScreen(ratio float32, position Vector, scale Vector) bool {
cameraPos := c.Transform().WorldPosition()
bigScale := scale.X * ratio
if scale.Y > bigScale {
bigScale = scale.Y
}
//bigScale = -bigScale
r := c.realRect
if c.sizeIsScale {
r.Min = r.Min.Mul2(c.size)
r.Max = r.Max.Mul2(c.size)
}
r.Min = r.Min.Add(cameraPos)
r.Max = r.Max.Add(cameraPos)
r2 := Rect{}
r2.Min.X = (-float32(bigScale) / 2) + position.X
r2.Max.X = (float32(bigScale) / 2) + position.X
r2.Min.Y = (-float32(bigScale) / 2) + position.Y
r2.Max.Y = (float32(bigScale) / 2) + position.Y
return r.Overlaps(r2)
}
//Updates the Projection
func (c *Camera) UpdateResolution() {
if c.autoScale {
c.rect.Min.X = -float32(Width) / 2
c.rect.Max.X = float32(Width) / 2
c.rect.Min.Y = -float32(Height) / 2
c.rect.Max.Y = float32(Height) / 2
}
if c.sizeIsScale {
c.realRect.Min.X = c.center.X - (c.center.X - c.rect.Min.X)
c.realRect.Max.X = c.center.X - (c.center.X - c.rect.Max.X)
c.realRect.Min.Y = c.center.Y - (c.center.Y - c.rect.Min.Y)
c.realRect.Max.Y = c.center.Y - (c.center.Y - c.rect.Max.Y)
} else {
c.realRect.Min.X = c.center.X - (c.center.X-c.rect.Min.X)*c.size
c.realRect.Max.X = c.center.X - (c.center.X-c.rect.Max.X)*c.size
c.realRect.Min.Y = c.center.Y - (c.center.Y-c.rect.Min.Y)*c.size
c.realRect.Max.Y = c.center.Y - (c.center.Y-c.rect.Max.Y)*c.size
}
c.Projection.Ortho(c.realRect.Min.X, c.realRect.Max.X, c.realRect.Min.Y, c.realRect.Max.Y, -100000, 100000)
}
//Mouse world position
func (c *Camera) MouseWorldPosition() Vector {
v := c.MouseLocalPosition()
return c.ScreenToWorld(v.X, v.Y)
}
//Mouse local position
func (c *Camera) MouseLocalPosition() Vector {
xx, yy := input.MousePosition()
x, y := float32(xx), float32(yy)
if c.sizeIsScale {
x, y = x+c.realRect.Min.X, c.realRect.Max.Y-y
} else {
x, y = (x*c.size)+c.realRect.Min.X, c.realRect.Max.Y-(y*c.size)
}
return NewVector2(x, y)
}
//Takes a point on the screen and turns it into point on world
func (c *Camera) ScreenToWorld(x, y float32) Vector {
m := Identity()
if c.sizeIsScale {
m.Translate(float32(x), float32(y), 0)
m.Mul(c.Transform().Matrix())
} else {
m.Translate(float32(x)*c.size, float32(y)*c.size, 0)
pos := c.Transform().WorldPosition()
r := c.Transform().WorldRotation()
m.RotateXYZ(r.X, r.Y, r.Z)
m.Translate(pos.X, pos.Y, pos.Z)
}
return m.Translation()
}
//Forcing all the objects to render
func (c *Camera) Render() {
s := GetScene()
if s != nil {
tcam := s.SceneBase().Camera
s.SceneBase().Camera = c
depthMap.Iter(drawGameObject)
s.SceneBase().Camera = tcam
}
}