如何使用opengl/pyglet在python中绘制/使用像素并更改这些像素的大小?

2024-04-26 06:41:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要画像素,然后改变它们的大小,所以一个显示像素包含9个程序像素

import random
from pyglet.gl import *
from OpenGL.GLUT import *

win = pyglet.window.Window()

@win.event
def on_draw():
    W = 200
    H = 200
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT)
    data = [[[0] * 3 for j in range(W)] for i in range(H)]
    for y in range (0, H):
      for x in range (0, W):
          data[y][x][0] = random.randint(0, 255)
          data[y][x][1] = random.randint(0, 255)
          data[y][x][2] = random.randint(0, 255)

    glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_INT, data)


    glutSwapBuffers()

 pyglet.app.run()

我得到这个错误

glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_INT, data)
ctypes.ArgumentError: argument 5: : wrong type


Tags: infromimportfordatarangerandomrgb
1条回答
网友
1楼 · 发布于 2024-04-26 06:41:41

传递给glDrawPixels的数据必须是GLuint值的数组,而不是嵌套的值列表。
如果要通过[0,255]范围内的整数值定义颜色通道,则必须使用数据类型GLubyte和相应的OpenGL枚举器常量GL_UNSIGNED_BYTE,而不是GL_UNSIGNED_INT。你知道吗

例如

data = [random.randint(0, 255) for _ in range (0, H*W*3)]
glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_BYTE, (GLubyte * len(data))(*data))

如果要分别使用GLuintGL_UNSIGNED_INT,则积分色通道必须在[0,2147483647]范围内:

例如

data = [random.randint(0, 2147483647) for _ in range (0, H*W*3)]
glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_INT, (GLuint * len(data))(*data)) 

相关问题 更多 >