鼠标中的glReadPixels [英] glReadPixels in mousePressed

查看:62
本文介绍了鼠标中的glReadPixels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击时,我试图获取JOGL中像素的颜色.如果将以下代码放入显示方法(来自GLEventListener),则可以正常工作.

I am trying to get the color of a pixel in JOGL when the user clicks on it. If I put the following code in the display method (coming from the GLEventListener), it works fine.

FloatBuffer buffer = FloatBuffer.allocate(4);

gl.glReadBuffer(GL3.GL_FRONT);
gl.glReadPixels(10, 10, 1, 1, GL3.GL_RGBA, GL3.GL_FLOAT, buffer);
float[] pixels = new float[3];
pixels = buffer.array();
float red = pixels[0];
float green = pixels[1];
float blue = pixels[2];
System.out.println(red + ", " + green + ", " + blue);

但是,如果将代码放入mousePressed方法中,则在检查成功时会收到无效的操作错误,并且颜色返回黑色(0、0、0).

However, if I put my code in the mousePressed method, I get an invalid operation error when checking for success and the color returns black (0, 0, 0).

是否有一种方法可以使它在我的mousePressed方法中工作,或者我必须存储选定的像素以及一个布尔值以指示选择(这样,我才不会在每一帧都调用glReadPixels)并执行选择在显示方法中?

Is there a way to get this to work in my mousePressed method, or will I have to store the selected pixel, along with a boolean to indicate selection (so that I don't call glReadPixels every frame) and do the selection in the display method?

P.s. (x,y)坐标=(10,10)只是为了确保所选内容确实在我的窗口内(之所以如此,因为在显示方法中像素颜色正确显示了出来.)

P.s. the (x,y) coords = (10,10) is just to make sure the selection is really inside my window (which it is, since the pixel color comes out correctly in the display method).

推荐答案

OpenGL上下文一次只能在单个线程中处于活动状态,如果您对鼠标按下的事件处理位于另一个线程上,则不会当时可以从OpenGL正确查询信息.一般来说,我会在读取显示功能中的像素值方面犯错.如果要避免每帧读取像素,则可以跟踪最近读取的位置,并且仅在要读取的位置发生更改时才调用glReadPixels.

An OpenGL context can only be active in a single thread at a time, if your event handling for mouse presses comes on a different thread, you won't be able to properly query information from OpenGL at that time. Generally speaking, I would err on the side of reading the pixel value in the display function. If you want to avoid reading pixels every frame, you could keep track of the last location you read from, and only call glReadPixels if the position to read from has changed.

如datenwolf所述,如果需要,可以停用和激活您的上下文以跨多个线程访问它.

As datenwolf mentions, it is possible to deactivate and activate your context to access it across multiple threads, if you need to do so.

这篇关于鼠标中的glReadPixels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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