用OpenGL读取像素颜色?

时间:2012-12-22 00:13:06

标签: java opengl collision-detection lwjgl

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

我读到glreadpixels,但我并不真正理解参数,尤其是最后一个参数,它应该是ByteBuffer类型。任何人都可以向我解释如何实现这一点,或者更好的方法来进行简单的碰撞检测?

1 个答案:

答案 0 :(得分:2)

这肯定不是进行碰撞检测的最简单或最有效的方法,但要回答你的问题;

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

看起来很有用。