-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuffer.go
213 lines (179 loc) · 5.77 KB
/
buffer.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
package glitch
// type Buffer struct {
// buf *VertexBuffer
// }
// func NewBuffer() *Buffer {
// return &Buffer{}
// }
// func (b *Buffer) Add(mesh *Mesh, matrix Mat4, mask RGBA, material Material, translucent bool) {
// if b.material == nil {
// b.material = material
// } else {
// if b.material != material {
// panic("Materials must match inside a batch!")
// }
// }
// b.mesh.generation++
// // If anything translucent is added to the batch, then we will consider the entire thing translucent
// b.Translucent = b.Translucent || translucent
// mat := matrix.gl()
// // Append each index
// currentElement := uint32(len(b.mesh.positions))
// for i := range mesh.indices {
// b.mesh.indices = append(b.mesh.indices, currentElement + mesh.indices[i])
// }
// // Append each position
// for i := range mesh.positions {
// b.mesh.positions = append(b.mesh.positions, mat.Apply(mesh.positions[i]))
// }
// // Calculate the bounding box of the mesh we just merged in
// // Because we already figured out the first index of the new mesh (ie `currentElement`) we can just slice off the end of the new mesh
// posBuf := b.mesh.positions[int(currentElement):]
// min := posBuf[0]
// max := posBuf[0]
// for i := range posBuf {
// // X
// if posBuf[i][0] < min[0] {
// min[0] = posBuf[i][0]
// }
// if posBuf[i][0] > max[0] {
// max[0] = posBuf[i][0]
// }
// // Y
// if posBuf[i][1] < min[1] {
// min[1] = posBuf[i][1]
// }
// if posBuf[i][1] > max[1] {
// max[1] = posBuf[i][1]
// }
// // Z
// if posBuf[i][2] < min[2] {
// min[2] = posBuf[i][2]
// }
// if posBuf[i][2] > max[2] {
// max[2] = posBuf[i][2]
// }
// }
// newBounds := Box{
// Min: Vec3{float64(min[0]), float64(min[1]), float64(min[2])},
// Max: Vec3{float64(max[0]), float64(max[1]), float64(max[2])},
// }
// b.mesh.bounds = b.mesh.bounds.Union(newBounds)
// renormalizeMat := matrix.Inv().Transpose().gl()
// for i := range mesh.normals {
// b.mesh.normals = append(b.mesh.normals, renormalizeMat.Apply(mesh.normals[i]))
// }
// for i := range mesh.colors {
// // TODO - vec4 mult function
// b.mesh.colors = append(b.mesh.colors, glVec4{
// mesh.colors[i][0] * float32(mask.R),
// mesh.colors[i][1] * float32(mask.G),
// mesh.colors[i][2] * float32(mask.B),
// mesh.colors[i][3] * float32(mask.A),
// })
// }
// // TODO - is a copy faster?
// for i := range mesh.texCoords {
// b.mesh.texCoords = append(b.mesh.texCoords, mesh.texCoords[i])
// }
// // if b.material == nil {
// // b.material = material
// // } else {
// // if b.material != material {
// // panic("Materials must match inside a batch!")
// // }
// // }
// // mat := matrix.gl()
// // posBuf := make([]glVec3, len(mesh.positions))
// // for i := range mesh.positions {
// // posBuf[i] = mat.Apply(mesh.positions[i])
// // }
// // min := posBuf[0]
// // max := posBuf[0]
// // for i := range posBuf {
// // // X
// // if posBuf[i][0] < min[0] {
// // min[0] = posBuf[i][0]
// // }
// // if posBuf[i][0] > max[0] {
// // max[0] = posBuf[i][0]
// // }
// // // Y
// // if posBuf[i][1] < min[1] {
// // min[1] = posBuf[i][1]
// // }
// // if posBuf[i][1] > max[1] {
// // max[1] = posBuf[i][1]
// // }
// // // Z
// // if posBuf[i][2] < min[2] {
// // min[2] = posBuf[i][2]
// // }
// // if posBuf[i][2] > max[2] {
// // max[2] = posBuf[i][2]
// // }
// // }
// // newBounds := Box{
// // Min: Vec3{float64(min[0]), float64(min[1]), float64(min[2])},
// // Max: Vec3{float64(max[0]), float64(max[1]), float64(max[2])},
// // }
// // renormalizeMat := matrix.Inv().Transpose().gl()
// // normBuf := make([]glVec3, len(mesh.normals))
// // for i := range mesh.normals {
// // normBuf[i] = renormalizeMat.Apply(mesh.normals[i])
// // }
// // colBuf := make([]glVec4, len(mesh.colors))
// // for i := range mesh.colors {
// // // TODO - vec4 mult function
// // colBuf[i] = glVec4{
// // mesh.colors[i][0] * float32(mask.R),
// // mesh.colors[i][1] * float32(mask.G),
// // mesh.colors[i][2] * float32(mask.B),
// // mesh.colors[i][3] * float32(mask.A),
// // }
// // }
// // // TODO - is a copy faster?
// // texBuf := make([]glVec2, len(mesh.texCoords))
// // for i := range mesh.texCoords {
// // texBuf[i] = mesh.texCoords[i]
// // }
// // indices := make([]uint32, len(mesh.indices))
// // for i := range mesh.indices {
// // indices[i] = mesh.indices[i]
// // }
// // m2 := &Mesh{
// // positions: posBuf,
// // normals: normBuf,
// // colors: colBuf,
// // texCoords: texBuf,
// // indices: indices,
// // bounds: newBounds,
// // }
// // b.mesh.Append(m2)
// }
// func (b *Buffer) Clear() {
// b.mesh.Clear()
// b.material = nil
// b.Translucent = false
// }
// func (b *Buffer) Draw(target BatchTarget, matrix Mat4) {
// target.Add(b.mesh, matrix, RGBA{1.0, 1.0, 1.0, 1.0}, b.material, b.Translucent)
// }
// func (b *Buffer) DrawColorMask(target BatchTarget, matrix Mat4, color RGBA) {
// target.Add(b.mesh, matrix, color, b.material, b.Translucent)
// }
// func (b *Buffer) RectDraw(target BatchTarget, bounds Rect) {
// b.RectDrawColorMask(target, bounds, RGBA{1, 1, 1, 1})
// }
// // TODO: Generalize this rectdraw logic. Copy paseted from Sprite
// func (b *Buffer) RectDrawColorMask(target BatchTarget, bounds Rect, mask RGBA) {
// // pass.SetTexture(0, s.texture)
// // pass.Add(s.mesh, matrix, RGBA{1.0, 1.0, 1.0, 1.0}, s.material)
// batchBounds := b.Bounds().Rect()
// matrix := Mat4Ident
// matrix.Scale(bounds.W() / batchBounds.W(), bounds.H() / batchBounds.H(), 1).Translate(bounds.W()/2 + bounds.Min[0], bounds.H()/2 + bounds.Min[1], 0)
// target.Add(b.mesh, matrix, mask, b.material, false)
// }
// func (b *Buffer) Bounds() Box {
// return b.mesh.Bounds()
// }