LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Lotho on August 29, 2014, 09:34:53

Title: Quicker way than glreadpixels?
Post by: Lotho on August 29, 2014, 09:34:53
I need a way to store the pixels values currently on the screen and compare them to the values on the first frame. Right now I'm using glreadpixels as follows:


currentBuffer= BufferTools.reserveByteData(mapSize);
glReadPixels(mapStartX, mapStartY, mapWidth, mapHeight, GL_BLUE, GL_UNSIGNED_BYTE, currentBuffer);

for (int i = 0; i < mapSize; i++) {
   if (currentBuffer.get(i) != baseBuffer.get(i)) {
      //Do nothing
      continue;
   }
   //Do something
}


This works perfectly fine but turns out to be a real bottleneck, dropping the fps to a third of what it was. Is there any quicker way? All I'm after is speed, I don't even need to show it on the screen if the comparison is made "behind the scene". I believe binding to texture is the way to go but I'm not really sure how to approach it. How do I get pixel values from a bound texture?
Title: Re: Quicker way than glreadpixels?
Post by: Cornix on August 29, 2014, 11:06:02
Read up on some tutorials about FBO's and offscreen rendering.
Basically it allows you to render to a texture, then you can read image data from a texture which can be a lot faster on certain hardware.
Title: Re: Quicker way than glreadpixels?
Post by: quew8 on August 29, 2014, 18:21:44
Here is a tutorial on FBOs: http://lwjgl.org/wiki/index.php?title=Using_Frame_Buffer_Objects_(FBO) (http://lwjgl.org/wiki/index.php?title=Using_Frame_Buffer_Objects_(FBO))
And glGetTexImage() reads the contents of a texture into a CPU side buffer: http://www.opengl.org/sdk/docs/man4/html/glGetTexImage.xhtml (http://www.opengl.org/sdk/docs/man4/html/glGetTexImage.xhtml)