Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML and OpenGL: getting pixels from RenderTexture  (Read 2307 times)

0 Members and 1 Guest are viewing this topic.

Daerst

  • Newbie
  • *
  • Posts: 12
    • View Profile
SFML and OpenGL: getting pixels from RenderTexture
« on: March 07, 2014, 01:22:35 pm »
Hey there,

I'm trying to get a single pixel value from a RenderTexture. The image is grayscale, so I really only need one of the channels. The current solution uses
uint8_t px = renderTex.getTexture().copyToImage().getPixel(x, y).r;
which, as you can imagine, is pretty damn slow - but at least it ensures that renderTex is initialized and looks like what I expect it to.

To speed it up I tried to use raw OpenGL (which I'm a total noob at, SFML has shielded me from that arcane knowledge for years) to get the pixel like so:
uint8_t px;
sf::Texture::bind(renderTex.getTexture());
glReadPixels(x, renderTex.getTexture().getSize().y - y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &px);
assert(glGetError() == GL_NO_ERROR);

Sadly, glReadPixels doesn't write to px at all. If I initialize it with some value, this value is never overwritten. The assertion is always true, so glReadPixels doesn't throw an error either.

What's wrong here? Any hint is appreciated.

Thanks,
Daerst

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: SFML and OpenGL: getting pixels from RenderTexture
« Reply #1 on: March 07, 2014, 06:48:27 pm »
Why do you need a single color value? I hope you dont call that code in a loop, because then you should cache the texture reference and image.
Maybe its enough if you change your code to use the texture data directly by creating a sprite from it or reading from inside a shader?

Daerst

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML and OpenGL: getting pixels from RenderTexture
« Reply #2 on: March 14, 2014, 10:03:28 am »
For each pixel of a texture, I save some data in another texture (renderTex). I then need to acces various of the pixels erratically to use them in CPU code.

Since I need to access various pixels, I can cache the image and only update if when someone modifies the texture. This leads to a considerable (and sufficient) speedup, but if someknow I'd still be interested in how the OpenGL stuff works.

Thanks,
Daerst

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML and OpenGL: getting pixels from RenderTexture
« Reply #3 on: March 14, 2014, 10:23:02 am »
glReadPixels reads from the current color buffer (the area where you're drawing to), not from the currently active texture(s).

To read from a texture, you must call glGetTexImage.
Laurent Gomila - SFML developer

 

anything