我正在尝试在GLTF中创建一个简单的多维数据集。我正在使用Python脚本将索引和顶点缓冲区写入二进制数据。这是我的代码;
import base64
import struct
indices = [
0, 1, 2, 0, 2, 3, # Front
4, 5, 6, 4, 6, 7, # Right
8, 9, 10, 8, 10, 11, # Back
12, 13, 14, 12, 14, 15, # Left
16, 17, 18, 16, 18, 19, # Upper
20, 21, 22, 20, 22, 23 # Bottom
]
faces = [
[ # Front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0
],
[ # Right
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0
],
[ # Back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0
],
[ # Left
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
],
[ # Upper
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0
],
[ # Bottom
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0
]
]
length = 2 * len(indices) + 4 * len(faces) * len(faces[0])
buffer = bytearray(length)
offset = 0
# Write index array.
for index in indices:
struct.pack_into('H', buffer, offset, index)
offset = offset + 1
# Write vertex array.
for face in faces:
for vertex in face:
struct.pack_into('f', buffer, offset, vertex)
offset = offset + 1
# Test
index_format = 'H' * 36
vertex_format = 'f' * 72
data_format = index_format + vertex_format
print(data_format)
print(struct.unpack(data_format, buffer))
# Write to base64.
b64 = base64.b64encode(buffer)
print(b64)
问题在于,当我解压缩数据时,它会给出从何处冒出来的怪异值:
(256, 2, 770, 1284, 1030, 1798, 2312, 2058, 2826, 3340, 3086, 3854, 4368, 4114, 4882, 5396, 5142, 5910, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.831554006032442e-39, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
我不知道我在做什么错。如果我要使用与解压缩相同的格式来打包数据,为什么这些值会有所不同?
答案 0 :(得分:1)
在我从事我的工作时,您已经发布了答案。但是,这是一个变体:
code.py :
#!/usr/bin/env python3
import sys
import base64
import struct
def main():
indices = [
0, 1, 2, 0, 2, 3, # Front
4, 5, 6, 4, 6, 7, # Right
8, 9, 10, 8, 10, 11, # Back
12, 13, 14, 12, 14, 15, # Left
16, 17, 18, 16, 18, 19, # Upper
20, 21, 22, 20, 22, 23 # Bottom
]
faces = [
[ # Front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0
],
[ # Right
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0
],
[ # Back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0
],
[ # Left
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
],
[ # Upper
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0
],
[ # Bottom
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0
]
]
# @TODO - cfati: Relevant code is below (3 lines)
format = "H" * len(indices) + "f" * sum((len(face) for face in faces))
buf = struct.pack(format, *indices + [i for face in faces for i in face])
buf_b64 = base64.b64encode(buf)
print("Format: {:}".format(format))
print("\nBuffer unpacked: {:}".format(struct.unpack(format, buf)))
print("\nBuffer base64'ed: {:}".format(buf_b64))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("\nDone.")
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056530761]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code.py Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 Format: HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff Buffer unpacked: (0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0) Buffer base64'ed: b'AAABAAIAAAACAAMABAAFAAYABAAGAAcACAAJAAoACAAKAAsADAANAA4ADAAOAA8AEAARABIAEAASABMAFAAVABYAFAAWABcAAACAvwAAgL8AAIA/AACAPwAAgL8AAIA/AACAPwAAgD8AAIA/AACAvwAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwAAgD8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIA/AACAvwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgD8AAIC/AACAvwAAgD8AAIC/AACAvwAAgL8AAIC/AACAvwAAgL8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIC/AACAPwAAgD8AAIA/AACAvwAAgD8AAIA/AACAvwAAgD8AAIC/AACAPwAAgD8AAIC/AACAvwAAgL8AAIC/AACAPwAAgL8AAIC/AACAPwAAgL8AAIA/AACAvwAAgL8AAIA/' Done.
答案 1 :(得分:0)
我想出了解决方案。问题是我只将offset
增加1。写unsigned short
时,我应该增加2。写float
时,我应增加4。< / p>
正确的代码:
# Write index array.
for index in indices:
struct.pack_into('H', buffer, offset, index)
offset = offset + 2
# Write vertex array.
for face in faces:
for vertex in face:
struct.pack_into('f', buffer, offset, vertex)
offset = offset + 4