-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeometry.go
482 lines (415 loc) · 13 KB
/
geometry.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package glitch
import (
"math"
)
// Note: For caching purposes
var geomQuadIndices = []uint32{
0, 1, 3,
1, 2, 3,
}
type RectFill struct {
bounds Rect
}
// func (s Rect) GetBuffer() *VertexBuffer {
// return nil
// }
// func (s Rect) Fill(pool *BufferPool, mat glMat4, mask RGBA) *VertexBuffer {
// numVerts := 4
// vertexBuffer := pool.Reserve(geomQuadIndices, numVerts, pool.shader.tmpBuffers)
// bounds := s
// destBuffs := pool.shader.tmpBuffers
// for bufIdx, attr := range pool.shader.attrFmt {
// // TODO - I'm not sure of a good way to break up this switch statement
// switch attr.Swizzle {
// case shaders.PositionXYZ:
// bounds := bounds.Box()
// min := bounds.Min.gl()
// max := bounds.Max.gl()
// if mat != glMat4Ident {
// min = mat.Apply(min)
// max = mat.Apply(max)
// }
// // TODO: Depth? Right now I just do min[2] b/c max and min should be on same Z axis
// posBuf := *(destBuffs[bufIdx]).(*[]glVec3)
// posBuf[0] = glVec3{float32(max[0]), float32(max[1]), float32(min[2])}
// posBuf[1] = glVec3{float32(max[0]), float32(min[1]), float32(min[2])}
// posBuf[2] = glVec3{float32(min[0]), float32(min[1]), float32(min[2])}
// posBuf[3] = glVec3{float32(min[0]), float32(max[1]), float32(min[2])}
// case shaders.ColorRGBA:
// colBuf := *(destBuffs[bufIdx]).(*[]glVec4)
// color := mask.gl()
// colBuf[0] = color
// colBuf[1] = color
// colBuf[2] = color
// colBuf[3] = color
// case shaders.TexCoordXY:
// uvBounds := bounds
// texBuf := *(destBuffs[bufIdx]).(*[]glVec2)
// texBuf[0] = glVec2{float32(uvBounds.Max.X), float32(uvBounds.Min.Y)}
// texBuf[1] = glVec2{float32(uvBounds.Max.X), float32(uvBounds.Max.Y)}
// texBuf[2] = glVec2{float32(uvBounds.Min.X), float32(uvBounds.Max.Y)}
// texBuf[3] = glVec2{float32(uvBounds.Min.X), float32(uvBounds.Min.Y)}
// default:
// panic("Unsupported")
// }
// }
// return vertexBuffer
// }
type GeomDraw struct {
color RGBA
Divisions int
mesh *Mesh
// defaultMaterial Material
}
func NewGeomDraw() *GeomDraw {
return &GeomDraw{
color: RGBA{1, 1, 1, 1},
Divisions: 100,
mesh: NewMesh(),
// defaultMaterial: DefaultMaterial(WhiteTexture()),
}
}
func (g *GeomDraw) SetColor(color RGBA) {
g.color = color
}
// func (g *GeomDraw) DrawRect(target BatchTarget, rect Rect, mat Mat4, mask RGBA) {
// pass, ok := target.(*RenderPass)
// if ok {
// // TODO: Doesn't handle depth calculation
// cmdPtr := pass.commands[pass.currentLayer].Add(false, drawCommand{
// // rect,
// matrix: mat,
// mask: mask,
// state: BufferState{g.defaultMaterial, pass.blendMode},
// })
// cmdPtr.filler = rect
// } else {
// target.Add(rect, mat, mask, DefaultMaterial(), false)
// }
// }
// func (g *GeomDraw) Clear() {
// g.mesh.Clear()
// }
// func (g *GeomDraw) Draw(target BatchTarget, mat Mat4) {
// // pass, ok := target.(*RenderPass)
// // if ok {
// // bufferedMesh := g.mesh.Buffer(pass, DefaultMaterial(), false)
// // g.mesh.buffer = bufferedMesh.buffer
// // }
// // TODO: mask? bufferstate, transparent
// target.Add(g.mesh, mat, White, BufferState{DefaultMaterial(), BlendModeNormal}, false)
// }
// func (g *GeomDraw) DrawRect2(rect Rect, mat Mat4, mask RGBA) {
// g.SetColor(mask)
// g.Rectangle2(g.mesh, rect, mat.gl())
// }
// if width == 0, then fill the rect
func (g *GeomDraw) Rectangle2(mesh *Mesh, rect Rect, width float64) {
if width <= 0 {
g.FillRect2(mesh, rect, glMat4Ident)
}
t := rect.CutTop(width)
b := rect.CutBottom(width)
l := rect.CutLeft(width)
r := rect.CutRight(width)
g.FillRect2(mesh, t, glMat4Ident)
g.FillRect2(mesh, b, glMat4Ident)
g.FillRect2(mesh, l, glMat4Ident)
g.FillRect2(mesh, r, glMat4Ident)
}
func (g *GeomDraw) FillRect2(mesh *Mesh, rect Rect, mat glMat4) {
currentElement := uint32(len(mesh.positions))
for i := range geomQuadIndices {
mesh.indices = append(mesh.indices, currentElement+geomQuadIndices[i])
}
{
bounds := rect.Box()
min := glv3(bounds.Min)
max := glv3(bounds.Max)
if mat != glMat4Ident {
min = mat.Apply(min)
max = mat.Apply(max)
}
// TODO: Depth? Right now I just do min[2] b/c max and min should be on same Z axis
mesh.positions = append(mesh.positions,
glVec3{float32(max[0]), float32(max[1]), float32(min[2])},
glVec3{float32(max[0]), float32(min[1]), float32(min[2])},
glVec3{float32(min[0]), float32(min[1]), float32(min[2])},
glVec3{float32(min[0]), float32(max[1]), float32(min[2])},
)
}
{
color := glc4(g.color)
mesh.colors = append(mesh.colors,
color,
color,
color,
color,
)
}
{
uvBounds := rect // TODO: idk
mesh.texCoords = append(mesh.texCoords,
glVec2{float32(uvBounds.Max.X), float32(uvBounds.Min.Y)},
glVec2{float32(uvBounds.Max.X), float32(uvBounds.Max.Y)},
glVec2{float32(uvBounds.Min.X), float32(uvBounds.Max.Y)},
glVec2{float32(uvBounds.Min.X), float32(uvBounds.Min.Y)},
)
}
}
func (g *GeomDraw) FillRect(rect Rect) *Mesh {
positions := []glVec3{
glVec3{float32(rect.Min.X), float32(rect.Max.Y), 0},
glVec3{float32(rect.Min.X), float32(rect.Min.Y), 0},
glVec3{float32(rect.Max.X), float32(rect.Min.Y), 0},
glVec3{float32(rect.Max.X), float32(rect.Max.Y), 0},
}
colors := []glVec4{
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
}
texCoords := []glVec2{
glVec2{1, 0},
glVec2{1, 1},
glVec2{0, 1},
glVec2{0, 0},
}
inds := []uint32{
0, 1, 3,
1, 2, 3,
}
return &Mesh{
positions: positions,
colors: colors,
texCoords: texCoords,
indices: inds,
}
}
// if width == 0, then fill the rect
func (g *GeomDraw) Rectangle(rect Rect, width float64) *Mesh {
if width <= 0 {
return g.FillRect(rect)
}
t := rect.CutTop(width)
b := rect.CutBottom(width)
l := rect.CutLeft(width)
r := rect.CutRight(width)
m := NewMesh()
m.Append(g.FillRect(t))
m.Append(g.FillRect(b))
m.Append(g.FillRect(l))
m.Append(g.FillRect(r))
return m
}
func (g *GeomDraw) Circle(mesh *Mesh, center Vec3, radius float64, width float64) {
g.Ellipse(mesh, center, Vec2{radius, radius}, 0, width)
}
func (g *GeomDraw) Ellipse(mesh *Mesh, center Vec3, size Vec2, rotation float64, width float64) {
if width <= 0 {
panic("TODO - Fill Ellipse")
}
alpha := rotation
a := math.Max(size.X, size.Y) // SemiMajorAxis
b := math.Min(size.X, size.Y) // SemiMinorAxis
// TODO - Rotate pi/2 if width < height?
e := math.Sqrt(1 - (b*b)/(a*a)) // Eccintricity
points := make([]Vec3, g.Divisions, g.Divisions)
radians := 0.0
for i := range points {
eCos := (e * math.Cos(radians))
r := b / (math.Sqrt(1 - (eCos * eCos)))
points[i] = center.Add(Vec3{
r * math.Cos(radians-alpha),
r * math.Sin(radians-alpha),
0,
})
radians += 2 * math.Pi / float64(g.Divisions)
}
// Append last point
{
eCos := (e * math.Cos(radians))
r := b / (math.Sqrt(1 - (eCos * eCos)))
// r := a * (1 - e * e) / (1 + (e * math.Cos(radians - alpha)))
// r := l / (1 + (e * math.Cos(radians - alpha)))
lastPoint := center.Add(Vec3{
r * math.Cos(radians-alpha),
r * math.Sin(radians-alpha),
0,
})
points = append(points, lastPoint)
}
// // Circle only
// points := make([]Vec3, g.Divisions, g.Divisions)
// radians := 0.0
// for i := range points {
// points[i] = center.Add(Vec3{
// radius * float32(math.Cos(radians)),
// (22.0/32.0) * radius * float32(math.Sin(radians)),
// 0,
// })
// radians += 2 * math.Pi / float64(g.Divisions)
// }
// // Append last point
// points = append(points, center.Add(Vec3{radius, 0, 0}))
g.LineStrip(mesh, points, width)
}
func (g *GeomDraw) LineStrip(mesh *Mesh, points []Vec3, width float64) {
// fmt.Println("Points:", points)
c := points[0]
for i := 0; i < len(points); i++ {
b := points[i]
if i+1 < len(points) {
c = points[i+1]
}
// Note: Divide by 2 because each connection spills over have the midpoint of the joint
g.Line(mesh, b, c, 0, 0, width)
}
}
func (g *GeomDraw) Polygon2D(mesh *Mesh, points []Vec2, width float64) {
v3Points := make([]Vec3, len(points))
for i := range points {
v3Points[i] = points[i].Vec3()
}
g.Polygon(mesh, v3Points, width)
}
// TODO - remake linestrip but don't have the looping indexes (ie modulo). This is technically for polygons
func (g *GeomDraw) Polygon(mesh *Mesh, points []Vec3, width float64) {
// fmt.Println("Points:", points)
// for i := 0; i < len(points)-1; i++ {
a := points[len(points)-1]
for i := 0; i < len(points); i++ {
if i > 0 {
a = points[i-1]
}
b := points[i]
c := points[(i+1)%len(points)]
d := points[(i+2)%len(points)]
v0 := b.Sub(a)
v1 := c.Sub(b)
v2 := d.Sub(c)
// fmt.Println("Index:", i, v0, v1, v2)
// Note: Divide by 2 because each connection spills over have the midpoint of the joint
g.Line(mesh, b, c, v0.Angle(v1)/2, v1.Angle(v2)/2, width)
// m := NewMesh()
// m.Append(g.Line(b, c, v0.Angle(v1) / 2, v1.Angle(v2) / 2, width))
// m.Append(g.Line(points[i], points[i+1], v0.Angle(v1), v1.Angle(v2), width))
// m.Append(g.Line(points[i], points[i+1], width))
}
// fmt.Println("Positions:" m.positions)
}
// TODO different line endings
func (g *GeomDraw) Line(mesh *Mesh, a, b Vec3, lastAngle, nextAngle float64, width float64) {
// fmt.Println("Angles:", lastAngle, nextAngle)
line := b.Sub(a)
lineAngle := line.Theta()
lastAngle = (lineAngle - (math.Pi / 2)) - lastAngle
nextAngle += (lineAngle - (math.Pi / 2))
// fmt.Println("LineAngle:", lineAngle, lastAngle, nextAngle)
// // Shift the point over by width
// a = a.Add(line.Scaled(width/2, width/2, width/2))
// b = b.Sub(line.Scaled(width/2, width/2, width/2))
// A line along the width of the line
// (x, y) rotated 90 degrees around (0, 0) is (-y, x)
// TODO - 3D version of this 90 degree rotation?
// wLineUp := Vec3{-line[1], line[0], line[2]}.Scaled(width/2, width/2, width/2)
// wLineDown := wLineUp.Scaled(-1, -1, -1)
// a1 := a.Add(wLineUp)
// a2 := a.Add(wLineDown)
// b1 := b.Add(wLineUp)
// b2 := b.Add(wLineDown)
// wLineUp := Vec3{-line[1], line[0], line[2]}.Scaled(width/2, width/2, width/2)
// wLineUp := Vec3{-line[1], line[0], 0}.Scaled(width/2, width/2, 1)
wLineUp := Vec3{1, 0, 0}.Rotate2D(lastAngle).Scaled(width/2, width/2, width/2)
wLineDown := wLineUp.Scaled(-1, -1, -1)
a1 := a.Add(wLineUp)
a2 := a.Add(wLineDown)
// // Track the outer and inner a1 and a2
// if a1.Len() < a2.Len() {
// // swap a1 and a2
// tmp := a1
// a1 = a2
// a2 = tmp
// }
wLineUp2 := Vec3{1, 0, 0}.Rotate2D(nextAngle).Scaled(width/2, width/2, width/2)
wLineDown2 := wLineUp2.Scaled(-1, -1, 1)
b1 := b.Add(wLineUp2)
b2 := b.Add(wLineDown2)
// // Track the outer and inner b1 and b2
// if b1.Len() < b2.Len() {
// // swap b1 bnd b2
// tmp := b1
// b1 = b2
// b2 = tmp
// }
positions := []glVec3{
glv3(b1),
glv3(b2),
glv3(a2),
glv3(a1),
}
// fmt.Println("Positions:", positions)
colors := []glVec4{
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
glVec4{float32(g.color.R), float32(g.color.G), float32(g.color.B), float32(g.color.A)},
}
// TODO - Finalize what these should be
texCoords := []glVec2{
glVec2{1, 0},
glVec2{1, 1},
glVec2{0, 1},
glVec2{0, 0},
}
inds := []uint32{
0, 1, 3,
1, 2, 3,
// 0, 1, 2,
// 2, 3, 0,
}
currentElement := uint32(len(mesh.positions))
for i := range inds {
mesh.indices = append(mesh.indices, currentElement+inds[i])
}
mesh.positions = append(mesh.positions, positions...)
mesh.colors = append(mesh.colors, colors...)
mesh.texCoords = append(mesh.texCoords, texCoords...)
// mesh.bounds = mesh.bounds.Union(bounds.ToBox()) // TODO - add back
}
// Point generation functions:
func EllipsePoints(size Vec2, rotation float64, divisions int) []Vec3 {
alpha := rotation
a := math.Max(size.X, size.Y) // SemiMajorAxis
b := math.Min(size.X, size.Y) // SemiMinorAxis
// TODO - Rotate pi/2 if width < height?
e := math.Sqrt(1 - (b*b)/(a*a)) // Eccintricity
points := make([]Vec3, divisions, divisions)
radians := 0.0
for i := range points {
eCos := (e * math.Cos(radians))
r := b / (math.Sqrt(1 - (eCos * eCos)))
points[i] = Vec3{
r * math.Cos(radians-alpha),
r * math.Sin(radians-alpha),
0,
}
radians += 2 * math.Pi / float64(divisions)
}
// TODO - not needed when doing polygon
// // Append last point
// {
// eCos := (e * math.Cos(radians))
// r := b / (math.Sqrt(1 - (eCos * eCos)))
// // r := a * (1 - e * e) / (1 + (e * math.Cos(radians - alpha)))
// // r := l / (1 + (e * math.Cos(radians - alpha)))
// lastPoint := Vec3{
// float32(r * math.Cos(radians - alpha)),
// float32(r * math.Sin(radians - alpha)),
// 0,
// }
// points = append(points, lastPoint)
// }
return points
}