Reading texture colors values

Member
Posts: 105
Joined: 2007.03
Post: #1
I'm working on enabling the player to select objects(textured quads) in my 2d game. So first, I check if a mousedown has occurred in the bounding rect of the quad, and if it has, I want to check the corresponding color value of the texture's pixel which I clicked, for transparency(the alpha value). How do I do this(I'm using tga)?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
The file format doesn't make a difference. If you keep the buffer you pass to glTexImage2D around, you can simply examing the alpha value at the appropriate pixel location.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #3
ThemsAllTook Wrote:The file format doesn't make a difference. If you keep the buffer you pass to glTexImage2D around, you can simply examing the alpha value at the appropriate pixel location.

I'm not sure I understand quite what you're saying(my understanding of opengl is terrible). Are you saying for me to use the coord on the selected quad as an index to it's texture's image data? And if so, how do I parse through that data to retrieve the alpha values?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
you can just call glReadPixels to get the color of a pixel on screen.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #5
unknown Wrote:you can just call glReadPixels to get the color of a pixel on screen.

Yeah but when Im trying to get the alpha value of a sprite, ill end up getting the alpha value of the tile under the sprite, right?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
Yeah, you don't want to call glReadPixels. You want to read it out of your original image.

Assuming that your texture is a vanilla 32bit RGBA image, then you would do the following.

Code:
unsigned char *pixels; // pointer to your texture data
unsigned char alpha = pixels[4*(y*width + x) + 3];

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #7
Skorche Wrote:
Code:
unsigned char *pixels; // pointer to your texture data
unsigned char alpha = pixels[4*(y*width + x) + 3];

That's what I was looking for, thanks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  passing values from vertex to fragment shader Sumaleth 6 11,274 Feb 18, 2011 01:54 AM
Last Post: Holmes
  Changing Pixel Values using CG LIPH700 1 4,635 Nov 25, 2010 03:17 PM
Last Post: SethWillits
  Lighting and changing texture colors in OpenGL agreendev 2 7,867 Aug 13, 2010 03:47 PM
Last Post: agreendev
  "Inconsistent set of values to create NSBitmapImageRep" Help! Joseph Duchesne 2 6,032 Sep 30, 2005 05:32 PM
Last Post: Joseph Duchesne
  Passing values to SDL_LoadBMP strangeness Lunatic 0 2,541 Sep 7, 2005 08:49 PM
Last Post: Lunatic