forked from vova616/GarageEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Render.go
158 lines (117 loc) · 3.58 KB
/
Render.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
package engine
import (
"github.com/go-gl/gl"
)
var (
defaultBuffer VBO
defaultIndexBuffer VBO
)
func initDefaultPlane() {
defaultBuffer = GenBuffer()
//Triagles
data := make([]float32, 20)
data[0] = -0.5
data[1] = -0.5
data[2] = 1
data[3] = 0.5
data[4] = -0.5
data[5] = 1
data[6] = 0.5
data[7] = 0.5
data[8] = 1
data[9] = -0.5
data[10] = 0.5
data[11] = 1
// UV
data[12] = 0
data[13] = 1
data[14] = 1
data[15] = 1
data[16] = 1
data[17] = 0
data[18] = 0
data[19] = 0
defaultBuffer.Bind(gl.ARRAY_BUFFER)
gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, data, gl.STATIC_DRAW)
}
func InsideScreen(ratio float32, position Vector, scale Vector) bool {
return CurrentCamera().InsideScreen(ratio, position, scale)
}
func DrawSprite(tex *Texture, uv UV, position Vector, scale Vector, rotation float32, aling Align, color Color) {
if !InsideScreen(uv.Ratio, position, scale) {
return
}
internalMaterial.Begin(nil)
mp := internalMaterial.ProjMatrix
mv := internalMaterial.ViewMatrix
mm := internalMaterial.ModelMatrix
tx := internalMaterial.Texture
ac := internalMaterial.AddColor
ti := internalMaterial.Tiling
of := internalMaterial.Offset
defaultBuffer.Bind(gl.ARRAY_BUFFER)
internalMaterial.Verts.EnableArray()
internalMaterial.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
internalMaterial.UV.EnableArray()
internalMaterial.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(12*4))
v := aling.Vector()
v.X *= uv.Ratio
camera := GetScene().SceneBase().Camera
view := camera.InvertedMatrix()
model := Identity()
model.Translate(v.X, v.Y, 0)
model.Scale(scale.X*uv.Ratio, scale.Y, scale.Z)
model.RotateZ(rotation, -1)
model.Translate(position.X+0.75, position.Y+0.75, position.Z)
mv.UniformMatrix4fv(false, view)
mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))
mm.UniformMatrix4fv(false, model)
ac.Uniform4i(int(color.R), int(color.G), int(color.B), int(color.A))
ti.Uniform2f(uv.U2-uv.U1, uv.V2-uv.V1)
of.Uniform2f(uv.U1, uv.V1)
tx.Uniform1i(0)
tex.Bind()
gl.DrawArrays(gl.QUADS, 0, 4)
internalMaterial.End(nil)
}
func DrawSprites(tex *Texture, uvs []UV, positions []Vector, scales []Vector, rotations []float32, alings []Align, colors []Color) {
internalMaterial.Begin(nil)
mp := internalMaterial.ProjMatrix
mv := internalMaterial.ViewMatrix
mm := internalMaterial.ModelMatrix
tx := internalMaterial.Texture
ac := internalMaterial.AddColor
ti := internalMaterial.Tiling
of := internalMaterial.Offset
defaultBuffer.Bind(gl.ARRAY_BUFFER)
internalMaterial.Verts.EnableArray()
internalMaterial.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
internalMaterial.UV.EnableArray()
internalMaterial.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(12*4))
camera := GetScene().SceneBase().Camera
view := camera.InvertedMatrix()
mv.UniformMatrix4fv(false, view)
mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))
tex.Bind()
tx.Uniform1i(0)
for i := 0; i < len(uvs); i++ {
uv, position, scale := uvs[i], positions[i], scales[i]
if !InsideScreen(uv.Ratio, position, scale) {
continue
}
rotation, aling, color := rotations[i], alings[i], colors[i]
v := aling.Vector()
v.X *= uv.Ratio
model := Identity()
model.Translate(v.X, v.Y, 0)
model.Scale((scale.X * uv.Ratio), scale.Y, scale.Z)
model.RotateZ(rotation, -1)
model.Translate(position.X+0.75, position.Y+0.75, position.Z)
mm.UniformMatrix4fv(false, model)
ac.Uniform4f(color.R, color.G, color.B, color.A)
ti.Uniform2f(uv.U2-uv.U1, uv.V2-uv.V1)
of.Uniform2f(uv.U1, uv.V1)
gl.DrawArrays(gl.QUADS, 0, 4)
}
internalMaterial.End(nil)
}