在golang中读取二进制.fbx文件

在golang中读取二进制.fbx文件,go,Go,与其他语言不同,我真的不知道如何将二进制文件读入字节数组,或者只是将其转换为ASCII字符串,这让我陷入了一个相当大的问题 我一直在使用的代码: func TestFBX(fileName string) { file, err := os.Open(fileName) if (err != nil) { log.Fatal(err) } defer file.Close() var content []byte scanner

与其他语言不同,我真的不知道如何将二进制文件读入字节数组,或者只是将其转换为ASCII字符串,这让我陷入了一个相当大的问题

我一直在使用的代码:

func TestFBX(fileName string) {
    file, err := os.Open(fileName)
    if (err != nil) {
        log.Fatal(err)
    }
    defer file.Close()

    var content []byte
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        text := []byte(scanner.Text())
        buf := bytes.NewReader(text)
        err := binary.Read(buf, binary.LittleEndian, &content)

        if (err != nil) {
            fmt.Println(err)
        }
    }

    fmt.Printf("%v", content)
    fmt.Println("")

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}
它最后所做的是打印出[],一个空切片。 现在,当我尝试将float64或int32替换为[]字节时,它确实打印出了不同的数字,但我仍然不知道如何读取整个文件,而不仅仅是开始时的数字。

您可能只需要?:

你可能只是想要


如果您仍然对golang FBX reader感兴趣,这里是我的实现。没有经过很好的测试,但应该有效

FBX内部具有以下结构:

type Header [27]byte
type FBX struct {
  Header *Header
  Top    *Node
  Nodes  []*Node
}
type Node struct {
  EndOffset       uint32
  NumProperties   uint32
  PropertyListLen uint32
  NameLen         uint8
  Name            string
  Properties      []*Property
  NestedNodes     []*Node
 }
 type Property struct {
  TypeCode byte
  Data     interface{}
 }
下面是一个使用示例:

f, _ := os.Open("cube.fbx")
defer f.Close()
fbxData, _ := fbx.ReadFrom(f)
ibo := fbxData.Filter(fbx.FilterName("PolygonVertexIndex"))[0]
fmt.Println(ibo)
输出将是

[0 2 -4 7 5 -5 4 1 -1 5 2 -2 2 7 -4 0 7 -5 0 1 -3 7 6 -6 4 5 -2 5 6 -3 2 6 -8 0 3 -8]
下面是一个示例,我如何获得其他属性

ibo := node.FilterName("PolygonVertexIndex")[0].Properties[0].AsInt32Slice()
vbo := node.FilterName("Vertices")[0].Properties[0].AsFloat64Slice()
normals := node.FilterName("Normals")[0].Properties[0].AsFloat64Slice()
uvIndex := node.FilterName("UVIndex")[0].Properties[0].AsInt32Slice()
uv := node.FilterName("UV")[0].Properties[0].AsFloat64Slice()

numFaces := len(ibo) / 3
for f := 0; f < numFaces; f++ {
    face := &Face{}

    index := f * 3
    a := int(ibo[index+0])
    b := int(ibo[index+1])
    c := int(ibo[index+2])*-1 - 1

    face.a.Position = getVec3(vbo, a)
    face.b.Position = getVec3(vbo, b)
    face.c.Position = getVec3(vbo, c)

    uva := int(uvIndex[index+0])
    uvb := int(uvIndex[index+1])
    uvc := int(uvIndex[index+2])
    face.a.UV = getVec2(uv, uva)
    face.b.UV = getVec2(uv, uvb)
    face.c.UV = getVec2(uv, uvc)

    face.a.Normal = getVec3(normals, f*3+0)
    face.b.Normal = getVec3(normals, f*3+1)
    face.c.Normal = getVec3(normals, f*3+2)

    faces[f] = face
}

type Vec3 struct {
    X, Y, Z float32
}
type Vec2 struct {
    X, Y float32
}
type Vertex struct {
    Position *Vec3
    Normal   *Vec3
    Color    *Vec3
    UV       *Vec2
}

type Face struct {
    a vertex.Vertex
    b vertex.Vertex
    c vertex.Vertex
}

func getVec3(data []float64, index int) *Vec3 {
    i := index * 3
    x := float32(data[i])
    y := float32(data[i+1])
    z := float32(data[i+2])
    return &Vec3{x, y, z}
}
ibo:=node.FilterName(“PolygonVertexIndex”)[0]。属性[0]。AsInt32Slice()
vbo:=node.FilterName(“顶点”)[0]。属性[0]。AsFloat64Slice()
法线:=node.FilterName(“法线”)[0]。属性[0]。AsFloat64Slice()
uvIndex:=node.FilterName(“uvIndex”)[0]。属性[0]。AsInt32Slice()
uv:=node.FilterName(“uv”)[0]。属性[0]。AsFloat64Slice()
numFaces:=len(ibo)/3
对于f:=0;f
如果您仍然对golang FBX reader感兴趣,这里是我的实现。没有经过很好的测试,但应该有效

FBX内部具有以下结构:

type Header [27]byte
type FBX struct {
  Header *Header
  Top    *Node
  Nodes  []*Node
}
type Node struct {
  EndOffset       uint32
  NumProperties   uint32
  PropertyListLen uint32
  NameLen         uint8
  Name            string
  Properties      []*Property
  NestedNodes     []*Node
 }
 type Property struct {
  TypeCode byte
  Data     interface{}
 }
下面是一个使用示例:

f, _ := os.Open("cube.fbx")
defer f.Close()
fbxData, _ := fbx.ReadFrom(f)
ibo := fbxData.Filter(fbx.FilterName("PolygonVertexIndex"))[0]
fmt.Println(ibo)
输出将是

[0 2 -4 7 5 -5 4 1 -1 5 2 -2 2 7 -4 0 7 -5 0 1 -3 7 6 -6 4 5 -2 5 6 -3 2 6 -8 0 3 -8]
下面是一个示例,我如何获得其他属性

ibo := node.FilterName("PolygonVertexIndex")[0].Properties[0].AsInt32Slice()
vbo := node.FilterName("Vertices")[0].Properties[0].AsFloat64Slice()
normals := node.FilterName("Normals")[0].Properties[0].AsFloat64Slice()
uvIndex := node.FilterName("UVIndex")[0].Properties[0].AsInt32Slice()
uv := node.FilterName("UV")[0].Properties[0].AsFloat64Slice()

numFaces := len(ibo) / 3
for f := 0; f < numFaces; f++ {
    face := &Face{}

    index := f * 3
    a := int(ibo[index+0])
    b := int(ibo[index+1])
    c := int(ibo[index+2])*-1 - 1

    face.a.Position = getVec3(vbo, a)
    face.b.Position = getVec3(vbo, b)
    face.c.Position = getVec3(vbo, c)

    uva := int(uvIndex[index+0])
    uvb := int(uvIndex[index+1])
    uvc := int(uvIndex[index+2])
    face.a.UV = getVec2(uv, uva)
    face.b.UV = getVec2(uv, uvb)
    face.c.UV = getVec2(uv, uvc)

    face.a.Normal = getVec3(normals, f*3+0)
    face.b.Normal = getVec3(normals, f*3+1)
    face.c.Normal = getVec3(normals, f*3+2)

    faces[f] = face
}

type Vec3 struct {
    X, Y, Z float32
}
type Vec2 struct {
    X, Y float32
}
type Vertex struct {
    Position *Vec3
    Normal   *Vec3
    Color    *Vec3
    UV       *Vec2
}

type Face struct {
    a vertex.Vertex
    b vertex.Vertex
    c vertex.Vertex
}

func getVec3(data []float64, index int) *Vec3 {
    i := index * 3
    x := float32(data[i])
    y := float32(data[i+1])
    z := float32(data[i+2])
    return &Vec3{x, y, z}
}
ibo:=node.FilterName(“PolygonVertexIndex”)[0]。属性[0]。AsInt32Slice()
vbo:=node.FilterName(“顶点”)[0]。属性[0]。AsFloat64Slice()
法线:=node.FilterName(“法线”)[0]。属性[0]。AsFloat64Slice()
uvIndex:=node.FilterName(“uvIndex”)[0]。属性[0]。AsInt32Slice()
uv:=node.FilterName(“uv”)[0]。属性[0]。AsFloat64Slice()
numFaces:=len(ibo)/3
对于f:=0;f
问题标题和问题内容之间的差异不匹配,导致对作者期望的内容有不同的解释。这个堆栈溢出问题的内容揭示了作者对周围环境的理解是多么的少

FBX文件格式是为3D模型/动画设计的二进制/文本规范。以文件格式读取的正式方式由Autodesk通过提供

在golang中,从文件中读取字节数据非常简单,并且它们通过一个单独的线性程序提供上述功能,即。作者说:“与其他语言不同,我真的不知道如何将二进制文件读入字节数组”,这一事实让我相信他们在提出问题之前没有使用谷歌


至于解释FBX文件,公认的答案是链接到一个个人库,即使是简单的FBX文件也会让人感到恐慌。创建这些导入器很困难,因为FBX规范非常通用,允许扩展供应商特定功能的格式。为了进一步解释,我建议阅读他们从探索格式中学到的东西。

问题标题和问题内容之间的差异不匹配,导致对作者期望的内容有不同的解释。这个堆栈溢出问题的内容揭示了作者对周围环境的理解是多么的少

FBX文件格式是为3D模型/动画设计的二进制/文本规范。以文件格式读取的正式方式由Autodesk通过提供

在golang中,从文件中读取字节数据非常简单,并且它们通过一个单独的线性程序提供上述功能,即。作者说:“与其他语言不同,我真的不知道如何将二进制文件读入字节数组”,这一事实让我想到了beli