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

问题描述 投票:1回答:1

我需要绘制像素然后更改它们的大小,因此一个显示像素包含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:参数5 ::错误的类型

python opengl pyglet
1个回答
1
投票

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

EG

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

如果你仍然分别使用GLuint GL_UNSIGNED_INT,那么整数颜色通道必须在[0,2147483647]的范围内:

EG

data = [random.randint(0, 2147483647) for _ in range (0, H*W*3)]
glDrawPixels(W, H, GL_RGB, GL_UNSIGNED_INT, (GLuint * len(data))(*data)) 
© www.soinside.com 2019 - 2024. All rights reserved.