我试图通过读取对象旁边像素的像素颜色来进行碰撞,但我对如何实现这一点感到困惑。
我阅读了有关 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/