登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-05 16:06:33 164浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《在 golang 中读取二进制 .fbx 文件》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

与其他语言不同,我真的不知道如何将二进制文件读入字节数组或将其转换为 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 而不是 []byte 时,它​​确实打印出了不同的数字,但说实话,我仍然不知道如何读取整个文件,而不仅仅是开头的数字.


解决方案


如果您仍然对 golang fbx 阅读器感兴趣,这里是我的实现 https://github.com/o5h/fbx。未经充分测试,但应该可以工作。

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}
}

您可能只想要 ioutil.ReadFile(...)?:

import "io/ioutil"

// ...

bs, err := ioutil.ReadFile("./myfile.fbx")
if err != nil {
  panic(err) // TODO: handle error?
}
// now "bs" has all the bytes in the file...
fmt.Printf("OK: read %d byte(s)\n", len(bs))

理论要掌握,实操不能落!以上关于《在 golang 中读取二进制 .fbx 文件》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>