You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyOpenGL运行报错ValueError:期望12字节数组却得到8字节数组

Fix for "Expected 12 byte array, got 8 byte array" in OpenGL/Pygame Cube Code

Hey, I spotted the exact issue causing your error! Let's break this down:

What's Going Wrong?

When you run your cube code, you hit this frustrating error:

ValueError: ('Expected 12 byte array, got 8 byte array', (-1, 0), None)

Why This Happens

Look closely at your vertices array—there's a tiny syntax mistake in one of the tuples:

vertices = ((1, -1, -1), (1, 1, -1), (-1, 1 -1), (-1, -1, -1), ...)

The third entry (-1, 1 -1) is missing a comma between 1 and -1. Python interprets 1 -1 as a single value (0), so this tuple becomes (-1, 0)—only two elements instead of the three glVertex3fv expects. Since each float in the vertex takes 4 bytes, 2 elements equal 8 bytes, but OpenGL needs 3 elements (12 bytes) for a 3D vertex. That's exactly why you get this mismatch error.

The Simple Fix

Just add the missing comma to that vertex tuple:

# Corrected vertices array
vertices = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),  # Added comma here between 1 and -1
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1),
)

Test It Out

After fixing that line, re-run your code. OpenGL will now read all 3 coordinates for every vertex correctly, and your cube should render without any issues.

内容的提问来源于stack exchange,提问作者Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:57:45