Discussion:
Get pixel RGB values from a texture
David Lara
2013-08-27 21:53:21 UTC
Permalink
Hi all,

In SDL2, there's a way to get the RGB values of a pixel that are in a
loaded texture ?

In SDL 1.2 I use the "SDL_Color get_pixel32" routine to get this
values from a Surface.
--
Thanks
Sam Lantinga
2013-08-29 04:12:21 UTC
Permalink
Nope, textures are write-only, with the exception of render targets, which
you can read back with the slow SDL_RenderReadPixels() function.
Post by David Lara
Hi all,
In SDL2, there's a way to get the RGB values of a pixel that are in a
loaded texture ?
In SDL 1.2 I use the "SDL_Color get_pixel32" routine to get this
values from a Surface.
--
Thanks
_______________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Nathaniel J Fries
2013-08-29 04:31:03 UTC
Permalink
Nope, textures are write-only, with the exception of render targets, which you can read back with the slow SDL_RenderReadPixels() function.
Post by David Lara
Hi all,
In SDL2, there's a way to get the RGB values of a pixel that are in a
loaded texture ?
In SDL 1.2 I use the "SDL_Color get_pixel32" routine to get this
values from a Surface.
--
Thanks
_______________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org (http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
Also, streaming textures, which are useful for things like dynamically generated minimaps.
However, I'm not sure why you'd want to read pixels from a texture unless you were doing graphics processing. If you are, it would be better to use surfaces than textures.
That, or you're trying to rig a system so that the user can change the renderer at runtime. A good idea, but since you have to maintain a list of textures to do this anyway, you might as well identify where each texture was loaded from in that list.

------------------------
Nate Fries
David Lara
2013-08-29 05:29:33 UTC
Permalink
***
I'm not sure why you'd want to read pixels from a texture unless you
were doing graphics processing.
***

I'm trying to do a pixel-collision (The game has a lot of irregular
objects). I'm trying to do "Check if the RGB of this pixel is 255" or
"Check alpha value of this pixel".
Kai Sterker
2013-08-29 08:12:27 UTC
Permalink
Post by David Lara
I'm trying to do a pixel-collision (The game has a lot of irregular
objects). I'm trying to do "Check if the RGB of this pixel is 255" or
"Check alpha value of this pixel".
If you're only interested whether a pixel is 100% transparent, it
might be more efficient to keep a separate bitmap around for each
object (where each pixel can be represented by a single bit).

Other than that, streaming textures work fine, but you are basically
keeping a complete copy of the pixel data in memory. Use
SDL_LockTexture to access that pixel data at a later point. That's
what I do, so that I can compose images off-screen.

Render Targets might work, but from my own experience they are much
slower than streaming textures.

Kai
Nathaniel J Fries
2013-08-29 14:02:12 UTC
Permalink
Post by Nathaniel J Fries
***
I'm not sure why you'd want to read pixels from a texture unless you
were doing graphics processing.
***
I'm trying to do a pixel-collision (The game has a lot of irregular
objects). I'm trying to do "Check if the RGB of this pixel is 255" or
"Check alpha value of this pixel".
_______________________________________________
SDL mailing list
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
You wouldn't want to download from GPU memory each time you wanted to do collision detection anyway. Your code would be so slow as to basically render it useless, and there'd be little you can do about it.
What you want to do is hold a separate collision copy. Not only does this prevent having to constantly download from the GPU, but it allows for useful things like compression by eliminating color data or using an 8-bit palette to enable different types of collisions.

However, you might still find that pixel-perfect collision is slow, especially if non-player entities can collide with other non-player entities (the case with most great games). A rectangle or quadtree approach should yield better performance, even if a little accuracy is lost.

------------------------
Nate Fries

Loading...