Newer
Older
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
package pixel
import "github.com/faiface/pixel/pixelgl"
// Warning: technical stuff below.
// VertexFormat is an internal format of the OpenGL vertex data.
//
// You can actually change this and all of the Pixel's functions will use the new format.
// Only change when you're implementing an OpenGL effect or something similar.
var VertexFormat = DefaultVertexFormat()
// DefaultVertexFormat returns the default vertex format used by Pixel.
func DefaultVertexFormat() pixelgl.VertexFormat {
return pixelgl.VertexFormat{
{Purpose: pixelgl.Position, Size: 2},
{Purpose: pixelgl.Color, Size: 4},
{Purpose: pixelgl.TexCoord, Size: 2},
}
}
// ConvertVertexData converts data in the oldFormat to the newFormat. Vertex attributes in the new format
// will be copied from the corresponding vertex attributes in the old format. If a vertex attribute in the new format
// has no corresponding attribute in the old format, it will be filled with zeros.
func ConvertVertexData(oldFormat, newFormat pixelgl.VertexFormat, data []float64) []float64 {
// calculate the mapping between old and new format
// if i is a start of a vertex attribute in the new format, then mapping[i] returns
// the index where the same attribute starts in the old format
mapping := make(map[int]int)
i := 0
for _, newAttr := range newFormat {
j := 0
for _, oldAttr := range oldFormat {
if newAttr == oldAttr {
mapping[i] = j
break
}
j += oldAttr.Size
}
i += newAttr.Size
}
oldData, newData := data, []float64{}
for i := 0; i < len(oldData); i += oldFormat.Size() {
j := 0
for _, attr := range newFormat {
if oldIndex, ok := mapping[j]; ok { // the attribute was found in the old format
newData = append(newData, oldData[i+oldIndex:i+oldIndex+attr.Size]...)
} else { // the attribute wasn't found in the old format, so fill with zeros
newData = append(newData, make([]float64, attr.Size)...)
}
j += attr.Size
}
}
return newData
}