从OpenGL获取数据glReadPixels(使用Pyglet) [英] Get Data from OpenGL glReadPixels(using Pyglet)

查看:862
本文介绍了从OpenGL获取数据glReadPixels(使用Pyglet)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Python中的Pyglet(和OpenGL),我试图使用glReadPixels获取一组像素的RGBA值。这是我的理解,OpenGL将数据作为打包整数返回,因为这是它们存储在硬件上的方式。然而,由于显而易见的原因,我想把它变成正常的格式来处理。基于一些阅读,我已经提出了这个: http://dpaste.com/99206/ ,但是它会因IndexError失败。我该如何去做这件事?

I'm using Pyglet(and OpenGL) in Python on an application, I'm trying to use glReadPixels to get the RGBA values for a set of pixels. It's my understanding that OpenGL returns the data as packed integers, since that's how they are stored on the hardware. However for obvious reasons I'd like to get it into a normal format for working with. Based on some reading I've come up with this: http://dpaste.com/99206/ , however it fails with an IndexError. How would I go about doing this?

推荐答案

您必须先创建一个正确类型的数组,然后将其传递给glReadPixels:

You must first create an array of the correct type, then pass it to glReadPixels:

a = (GLuint * 1)(0)
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_INT, a)

为了测试这个,在Pygletopengl.py示例中插入以下内容:

To test this, insert the following in the Pyglet "opengl.py" example:

@window.event
def on_mouse_press(x, y, button, modifiers):
    a = (GLuint * 1)(0)
    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_INT, a)
    print a[0]

无论何时在应用程序窗口中点击某处,您都应该可以看到鼠标光标下的像素的颜色代码。

Now you should see the color code for the pixel under the mouse cursor whenever you click somewhere in the app window.

这篇关于从OpenGL获取数据glReadPixels(使用Pyglet)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆