与其他语言不同,我真的不知道如何将二进制文件读取为字节数组或仅将其转换为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时,它确实打印出了不同的数字,但是老实说,我仍然不知道如何读取整个文件,而不仅仅是开头的数字
答案 0 :(得分:2)
您可能只想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))
答案 1 :(得分:1)
如果您仍然对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
}
答案 2 :(得分:0)
问题标题和问题内容之间的差异不匹配,导致对作者的要求的不同解释。这个堆栈溢出问题的内容揭示了作者对他们所问的内容了解得很少。
FBX文件格式是为3D模型/动画设计的二进制/文本规范。 Autodesk通过SDK they provide提供了以文件格式读取的官方方法。
在golang中,从文件中读取字节数据非常简单,它们通过ioutil.ReadFile这样的单个衬里提供了上述功能。作者说:“与其他语言不同,我真的不知道如何将二进制文件读取为字节数组”,这使我相信他们在提出问题之前就没有使用过Google。 >
对于解释FBX文件,接受的答案链接到一个个人库,该库甚至对简单的FBX文件都感到恐慌。创建这些导入程序很困难,因为FBX规范非常通用,并且允许扩展格式以用于特定于供应商的功能。为了进一步解释,我建议阅读Blender's write up,以了解他们从探索格式中学到的知识。