java - 使用 OpenGL 读取像素颜色?

标签 java opengl collision-detection lwjgl

我试图通过读取对象旁边像素的像素颜色来进行碰撞,但我对如何实现这一点感到困惑。

我阅读了有关 glreadpixels 的内容,但我不太理解这些参数,尤其是最后一个参数,它应该是 ByteBuffer 类型。谁能向我解释一下如何实现这一点,或者可能是进行简单碰撞检测的更好方法?

最佳答案

这绝对不是进行碰撞检测的最简单或最有效的方法,而是回答您的问题;

ByteBuffer RGB = ByteBuffer.allocateDirect(3); //create a new byte buffer (r, g, b)

int x=1, y=1;

GL11.glReadPixels(x, y, //the x and y of the pixel you want the colour of
1, 1,                   //height, width of selection. 1 since you only want one pixel
GL11.GL_RGB,            //format method uses, get red green and blue
GL11.GL_UNSIGNED_BYTE,  //how the method is performed; using unsigned bytes
RGB);                   //the byte buffer to write to

float red, green, blue;

red = RGB.get(0)/255f,   //get the first byte
green = RGB.get(1)/255f, //the second
blue = RGB.get(2)/255f;  //and third

对于进行碰撞检测的最佳方法,快速搜索:Basic Collision Detection in 2D – Part 1 .

看起来很有用。

关于java - 使用 OpenGL 读取像素颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998836/

相关文章:

java - 在需要身份验证的 Controller 上运行单元测试

opengl - VBO : not drawing correctly. 手工绘图作品

opengl - 如何在GLSL程序中识别intel显卡?

swift - 从两个对象检测 SpriteKit 上的碰撞

java - 我的边缘碰撞算法无法正常工作

java - Jackson 将不同时区序列化为单一时区

java - 如何使用 VisualVM 获取方法的总时间

java - 为什么我不断收到 "Not a JSON Array"?

java - 2011 年新 Java/Clojure 项目的 OpenGL 工具集

多次注册 Swift SpriteKit 碰撞