Skip to content

Commit

Permalink
Merge pull request #85 from qmuntal/int32
Browse files Browse the repository at this point in the history
Use int instead of uint32 for all non-wire data
  • Loading branch information
qmuntal authored Jun 28, 2024
2 parents a47c7b9 + 750a77a commit a8f6223
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 155 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ gltf.Document{
}},
Nodes: []*gltf.Node{{Name: "Cube", Mesh: gltf.Index(0)}},
Scene: gltf.Index(0),
Scenes: []*gltf.Scene{{Name: "Root Scene", Nodes: []uint32{0}}},
Scenes: []*gltf.Scene{{Name: "Root Scene", Nodes: []int{0}}},
}
```

Expand Down Expand Up @@ -198,7 +198,7 @@ To implement a custom extension decoding, call [gltf.RegisterExtension](https://
const ExtensionName = "FAKE_Extension"

type Foo struct {
BufferView uint32 `json:"bufferView"`
BufferView int `json:"bufferView"`
Attributes gltf.Attributes `json:"attributes"`
}

Expand Down
6 changes: 3 additions & 3 deletions binary/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func BenchmarkNative(b *testing.B) {
var s uint32 = 1000
var s int = 1000
bs := make([]byte, s*gltf.SizeOfElement(gltf.ComponentFloat, gltf.AccessorVec3))
data := make([][3]float32, s)
b.ResetTimer()
Expand All @@ -21,7 +21,7 @@ func BenchmarkNative(b *testing.B) {
}

func BenchmarkWrite(b *testing.B) {
var s uint32 = 1000
var s int = 1000
bs := make([]byte, s*gltf.SizeOfElement(gltf.ComponentFloat, gltf.AccessorVec3))
data := make([][3]float32, s)
b.ResetTimer()
Expand All @@ -31,7 +31,7 @@ func BenchmarkWrite(b *testing.B) {
}

func BenchmarkWrite_builtint(b *testing.B) {
var s uint32 = 1000
var s int = 1000
bs := bytes.NewBuffer(make([]byte, s*gltf.SizeOfElement(gltf.ComponentFloat, gltf.AccessorVec3)))
data := make([][3]float32, s)
b.ResetTimer()
Expand Down
4 changes: 2 additions & 2 deletions binary/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
//
// Data should be a slice of glTF predefined fixed-size types.
// If data length is greater than the length of b, Read returns io.ErrShortBuffer.
func Read(b []byte, byteStride uint32, data any) error {
func Read(b []byte, byteStride int, data any) error {
c, t, n := Type(data)
size := gltf.SizeOfElement(c, t)
if byteStride == 0 {
Expand Down Expand Up @@ -222,7 +222,7 @@ func Read(b []byte, byteStride uint32, data any) error {
//
// Data must be a slice of glTF predefined fixed-size types,
// else it fallbacks to `encoding/binary.Write`.
func Write(b []byte, stride uint32, data any) error {
func Write(b []byte, stride int, data any) error {
c, t, n := Type(data)
if n == 0 {
return binary.Write(bytes.NewBuffer(b), binary.LittleEndian, data)
Expand Down
6 changes: 3 additions & 3 deletions binary/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func ExampleWrite() {
vertices := [][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}}

// Allocate buffer
sizeIndices := uint32(len(indices)) * gltf.SizeOfElement(gltf.ComponentUbyte, gltf.AccessorScalar)
sizeVertices := uint32(len(vertices)) * gltf.SizeOfElement(gltf.ComponentFloat, gltf.AccessorVec3)
sizeIndices := len(indices) * gltf.SizeOfElement(gltf.ComponentUbyte, gltf.AccessorScalar)
sizeVertices := len(vertices) * gltf.SizeOfElement(gltf.ComponentFloat, gltf.AccessorVec3)
b := make([]byte, sizeIndices+sizeVertices)

// Write
Expand All @@ -33,7 +33,7 @@ func ExampleRead() {

// Define buffer
b := []byte{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3, 0, 0, 44, 66, 0, 0, 44, 66, 0, 0, 0, 0, 0, 0, 166, 66, 0, 0, 44, 66, 0, 0, 0, 0, 0, 0, 124, 66, 0, 0, 124, 66, 0, 0, 32, 66, 0, 0, 44, 66, 0, 0, 166, 66, 0, 0, 0, 0, 0, 0, 166, 66, 0, 0, 166, 66, 0, 0, 0, 0}
sizeIndices := uint32(len(indices)) * gltf.SizeOfElement(gltf.ComponentUbyte, gltf.AccessorScalar)
sizeIndices := len(indices) * gltf.SizeOfElement(gltf.ComponentUbyte, gltf.AccessorScalar)

// Read
binary.Read(b, 0, indices)
Expand Down
10 changes: 5 additions & 5 deletions binary/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// MakeSliceBuffer returns the slice type associated with c and t and with the given element count.
// If the buffer is an slice which type matches with the expected by the acr then it will
// be used as backing slice.
func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count uint32, buffer any) any {
func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count int, buffer any) any {
if buffer == nil {
return MakeSlice(c, t, count)
}
Expand All @@ -32,7 +32,7 @@ func MakeSliceBuffer(c gltf.ComponentType, t gltf.AccessorType, count uint32, bu
// MakeSlice returns the slice type associated with c and t and with the given element count.
// For example, if c is gltf.ComponentFloat and t is gltf.AccessorVec3
// then MakeSlice(c, t, 5) is equivalent to make([][3]float32, 5).
func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) any {
func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count int) any {
var tp reflect.Type
switch c {
case gltf.ComponentUbyte:
Expand Down Expand Up @@ -63,17 +63,17 @@ func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) any {
case gltf.AccessorMat4:
tp = reflect.ArrayOf(4, reflect.ArrayOf(4, tp))
}
return reflect.MakeSlice(reflect.SliceOf(tp), int(count), int(count)).Interface()
return reflect.MakeSlice(reflect.SliceOf(tp), count, count).Interface()
}

// Type returns the associated glTF type data.
// It panics if data is not an slice.
func Type(data any) (c gltf.ComponentType, t gltf.AccessorType, count uint32) {
func Type(data any) (c gltf.ComponentType, t gltf.AccessorType, count int) {
v := reflect.ValueOf(data)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("gltf: binary.Type expecting a slice but got %s", v.Kind()))
}
count = uint32(v.Len())
count = v.Len()
switch data.(type) {
case []int8:
c, t = gltf.ComponentByte, gltf.AccessorScalar
Expand Down
4 changes: 2 additions & 2 deletions binary/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestMakeSlice(t *testing.T) {
type args struct {
c gltf.ComponentType
t gltf.AccessorType
count uint32
count int
}
tests := []struct {
name string
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestMakeSliceBuffer(t *testing.T) {
type args struct {
c gltf.ComponentType
t gltf.AccessorType
count uint32
count int
buffer any
}
tests := []struct {
Expand Down
10 changes: 5 additions & 5 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const (
)

// ByteSize returns the size of a component in bytes.
func (c ComponentType) ByteSize() uint32 {
return map[ComponentType]uint32{
func (c ComponentType) ByteSize() int {
return map[ComponentType]int{
ComponentByte: 1,
ComponentUbyte: 1,
ComponentShort: 2,
Expand Down Expand Up @@ -104,8 +104,8 @@ const (
)

// Components returns the number of components of an accessor type.
func (a AccessorType) Components() uint32 {
return map[AccessorType]uint32{
func (a AccessorType) Components() int {
return map[AccessorType]int{
AccessorScalar: 1,
AccessorVec2: 2,
AccessorVec3: 3,
Expand Down Expand Up @@ -161,7 +161,7 @@ const (
)

// PrimitiveAttributes is a map that each key corresponds to mesh attribute semantic and each value is the index of the accessor containing attribute's data.
type PrimitiveAttributes = map[string]uint32
type PrimitiveAttributes = map[string]int

// PrimitiveMode defines the type of primitives to render. All valid values correspond to WebGL enums.
type PrimitiveMode uint8
Expand Down
2 changes: 1 addition & 1 deletion decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (d *Decoder) decodeBinaryBuffer(buffer *Buffer) error {
if err != nil {
return err
}
if header.Type != glbChunkBIN || header.Length < buffer.ByteLength {
if header.Type != glbChunkBIN || header.Length < uint32(buffer.ByteLength) {
return errors.New("gltf: Invalid GLB BIN header")
}
buffer.Data = make([]byte, buffer.ByteLength)
Expand Down
10 changes: 5 additions & 5 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestOpen(t *testing.T) {
Nodes: []*Node{{Mesh: Index(0), Name: "Cube", Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}}},
Samplers: []*Sampler{{WrapS: WrapRepeat, WrapT: WrapRepeat}},
Scene: Index(0),
Scenes: []*Scene{{Nodes: []uint32{0}}},
Scenes: []*Scene{{Nodes: []int{0}}},
Textures: []*Texture{
{Sampler: Index(0), Source: Index(0)}, {Sampler: Index(0), Source: Index(1)},
},
Expand All @@ -77,7 +77,7 @@ func TestOpen(t *testing.T) {
{Camera: Index(1), Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}, Translation: [3]float64{0.5, 0.5, 3.0}},
},
Scene: nil,
Scenes: []*Scene{{Nodes: []uint32{0, 1, 2}}},
Scenes: []*Scene{{Nodes: []int{0, 1, 2}}},
}, false},
{args{"testdata/BoxVertexColors/glTF-Binary/BoxVertexColors.glb", ""}, &Document{
Accessors: []*Accessor{
Expand All @@ -99,14 +99,14 @@ func TestOpen(t *testing.T) {
Materials: []*Material{{Name: "Default", AlphaMode: AlphaOpaque, AlphaCutoff: Float(0.5), PBRMetallicRoughness: &PBRMetallicRoughness{BaseColorFactor: &[4]float64{0.8, 0.8, 0.8, 1}, MetallicFactor: Float(0.1), RoughnessFactor: Float(0.99)}}},
Meshes: []*Mesh{{Name: "Cube", Primitives: []*Primitive{{Indices: Index(0), Material: Index(0), Mode: PrimitiveTriangles, Attributes: PrimitiveAttributes{POSITION: 1, COLOR_0: 3, NORMAL: 2, TEXCOORD_0: 4}}}}},
Nodes: []*Node{
{Name: "RootNode", Children: []uint32{1, 2, 3}, Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}},
{Name: "RootNode", Children: []int{1, 2, 3}, Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}},
{Name: "Mesh", Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}},
{Name: "Cube", Mesh: Index(0), Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}},
{Name: "Texture Group", Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}},
},
Samplers: []*Sampler{{WrapS: WrapRepeat, WrapT: WrapRepeat}},
Scene: Index(0),
Scenes: []*Scene{{Name: "Root Scene", Nodes: []uint32{0}}},
Scenes: []*Scene{{Name: "Root Scene", Nodes: []int{0}}},
}, false},
{args{"testdata/Box With Spaces/glTF/Box With Spaces.gltf", ""}, &Document{
Accessors: []*Accessor{
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestOpen(t *testing.T) {
Meshes: []*Mesh{{Name: "Cube", Primitives: []*Primitive{{Indices: Index(3), Material: Index(0), Mode: PrimitiveTriangles, Attributes: PrimitiveAttributes{NORMAL: 1, POSITION: 0, TEXCOORD_0: 2}}}}},
Nodes: []*Node{{Mesh: Index(0), Name: "Cube", Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}}},
Scene: Index(0),
Scenes: []*Scene{{Name: "Scene", Nodes: []uint32{0}}},
Scenes: []*Scene{{Name: "Scene", Nodes: []int{0}}},
Textures: []*Texture{{Source: Index(0)}, {Source: Index(1)}, {Source: Index(2)}},
}, false},
}
Expand Down
6 changes: 3 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ func (e *Encoder) encodeBinary(doc *Document) (bool, error) {
}

hasBinChunk := len(doc.Buffers) > 0 && doc.Buffers[0].URI == ""
var binPaddedLength uint32
var binPaddedLength int
if hasBinChunk {
binPaddedLength = ((doc.Buffers[0].ByteLength + 3) / 4) * 4
header.Length += uint32(8) + binPaddedLength
header.Length += uint32(8 + binPaddedLength)
}

err = binary.Write(e.w, binary.LittleEndian, &header)
Expand All @@ -184,7 +184,7 @@ func (e *Encoder) encodeBinary(doc *Document) (bool, error) {
for i := range binPadding {
binPadding[i] = 0
}
binHeader := chunkHeader{Length: binPaddedLength, Type: glbChunkBIN}
binHeader := chunkHeader{Length: uint32(binPaddedLength), Type: glbChunkBIN}
binary.Write(e.w, binary.LittleEndian, &binHeader)
e.w.Write(binBuffer.Data)
_, err = e.w.Write(binPadding)
Expand Down
12 changes: 6 additions & 6 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ func TestEncoder_Encode(t *testing.T) {
}},
}}}, false},
{"withNodes", args{&Document{Nodes: []*Node{
{Extras: 8.0, Name: "n-1", Camera: Index(1), Children: []uint32{1, 2}, Skin: Index(3),
{Extras: 8.0, Name: "n-1", Camera: Index(1), Children: []int{1, 2}, Skin: Index(3),
Matrix: [16]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, Mesh: Index(15), Rotation: [4]float64{1.5, 1.3, 12, 0}, Scale: [3]float64{1, 3, 4}, Translation: [3]float64{0, 7.8, 9}, Weights: []float64{1, 3}},
{Extras: 8.0, Name: "n-2", Camera: Index(1), Children: []uint32{1, 2}, Skin: Index(3),
{Extras: 8.0, Name: "n-2", Camera: Index(1), Children: []int{1, 2}, Skin: Index(3),
Matrix: [16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, Mesh: Index(15), Rotation: [4]float64{0, 0, 0, 1}, Scale: [3]float64{1, 1, 1}},
}}}, false},
{"withSampler", args{&Document{Samplers: []*Sampler{
Expand All @@ -227,12 +227,12 @@ func TestEncoder_Encode(t *testing.T) {
}}}, false},
{"withScene", args{&Document{Scene: Index(1)}}, false},
{"withScenes", args{&Document{Scenes: []*Scene{
{Extras: 8.0, Name: "s_1", Nodes: []uint32{1, 2}},
{Extras: 8.0, Name: "s_2", Nodes: []uint32{2, 3}},
{Extras: 8.0, Name: "s_1", Nodes: []int{1, 2}},
{Extras: 8.0, Name: "s_2", Nodes: []int{2, 3}},
}}}, false},
{"withSkins", args{&Document{Skins: []*Skin{
{Extras: 8.0, Name: "skin_1", InverseBindMatrices: Index(2), Skeleton: Index(4), Joints: []uint32{5, 6}},
{Extras: 8.0, Name: "skin_2", InverseBindMatrices: Index(3), Skeleton: Index(4), Joints: []uint32{7, 8}},
{Extras: 8.0, Name: "skin_1", InverseBindMatrices: Index(2), Skeleton: Index(4), Joints: []int{5, 6}},
{Extras: 8.0, Name: "skin_2", InverseBindMatrices: Index(3), Skeleton: Index(4), Joints: []int{7, 8}},
}}}, false},
{"withTextures", args{&Document{Textures: []*Texture{
{Extras: 8.0, Name: "t_1", Sampler: Index(2), Source: Index(3)},
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func ExampleSave() {
}},
Meshes: []*gltf.Mesh{{Name: "Cube", Primitives: []*gltf.Primitive{{Indices: gltf.Index(0), Material: gltf.Index(0), Mode: gltf.PrimitiveTriangles, Attributes: gltf.PrimitiveAttributes{gltf.POSITION: 1, gltf.COLOR_0: 3, gltf.NORMAL: 2, gltf.TEXCOORD_0: 4}}}}},
Nodes: []*gltf.Node{
{Name: "RootNode", Children: []uint32{1, 2, 3}},
{Name: "RootNode", Children: []int{1, 2, 3}},
{Name: "Mesh"},
{Name: "Cube", Mesh: gltf.Index(0)},
{Name: "Texture Group"},
},
Samplers: []*gltf.Sampler{{WrapS: gltf.WrapRepeat, WrapT: gltf.WrapRepeat}},
Scene: gltf.Index(0),
Scenes: []*gltf.Scene{{Name: "Root Scene", Nodes: []uint32{0}}},
Scenes: []*gltf.Scene{{Name: "Root Scene", Nodes: []int{0}}},
}
if err := gltf.SaveBinary(doc, "./example.glb"); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion ext/lightspuntual/lightspuntual.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
)

// LightIndex is the id of the light referenced by this node.
type LightIndex uint32
type LightIndex int

// Spot defines the spot cone.
type Spot struct {
Expand Down
2 changes: 1 addition & 1 deletion ext/texturetransform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type TextureTranform struct {
Offset [2]float64 `json:"offset"`
Rotation float64 `json:"rotation,omitempty"`
Scale [2]float64 `json:"scale"`
TexCoord *uint32 `json:"texCoord,omitempty"`
TexCoord *int `json:"texCoord,omitempty"`
}

// ScaleOrDefault returns the node scale if it represents a valid scale factor, else return the default one.
Expand Down
Loading

0 comments on commit a8f6223

Please sign in to comment.