How Get RGB values from a image in memory?

alert
Unregistered
 
Post: #1
OPENGL

Hello::Smile

I have a image in memory:

"depthimage = (unsigned char *)malloc(600*600*sizeof(GL_FLOAT)); "

How can retrieve the RGB values from each pixel?... (to compare values)???

HELP Ninja


Alert
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
Provided that you've actually loaded the texture, I see two problem ahead. First of all, GL_FLOAT is most likely not what you have in a texture. You probably have the pixels as groups of bytes. If so, you should malloc (bits_per_pixel * 600*600*sizeof (char)); Now, how many is depending on the format of the image in memory. Where did you get this image from? Does it have alpha? If it does, bits_per_pixel above should be 4, else it should be 3. Anyway, if you assume that the image is in RGB format (which isn't always the case, mind you), then the first byte is the red value, the next is the green value and the next is the blue value. Then, the next pixel's three values and so on.

Now, please provide som more info on what you're doing - where does the image come from? Have you rendered to texture? Loaded from a file, and if so, what kind of file? Smile
Quote this message in a reply
alert
Unregistered
 
Post: #3
I´m trying developer an opengl program to detect collisions by a image of z-buffer values..

Code:

unsigned char *depthimage;

depthimage = (unsigned char *)malloc(600*600*sizeof(GL_FLOAT));
glReadPixels(0,0,600,600,GL_DEPTH_COMPONENT,GL_FLOAT,depthimage );

So...
how can access to the values of each pixel in this image ...to do a collision test!!!!


Alert
Quote this message in a reply
alert
Unregistered
 
Post: #4
Not retrieve the RGB values from each pixel but the depht vaues for each pixel!!! Smile
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #5
Ah, that's easy. Assume that your coordinates are x and y, you do:
float *depthValuePtr = (float *)(depthImage + (y * 800) + x);

Your value is now pointed to by depthValuePtr.
Quote this message in a reply
alert
Unregistered
 
Post: #6
y * 800... don´t understand the 800?????

for what?

Alert Grin
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
its supposed to be whatever your image's width is.

another thing: dont use "unsigned char*". If you dont know what an array's type is, use void*. But, since in this case you use GL_FLOAT, make it a GL_FLOAT*
Quote this message in a reply
alert
Unregistered
 
Post: #8
Sorry I continue without understanding....

I have a image (600*600) in memory...
I want retrieve the Z-buffer value for each pixel.....

I don´t now I I use the up code?....
Sorry.. please help again Huh

Alert
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #9
Oh, sorry - your image was 600 pixels wide, so it should 600 * y + x then.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #10
It's a little late to poke in here, but if you're using glReadPixels to do a collision test you're going to have some serious performance issues. You're probably better off doing collisions manually by tracking rects or something.

Anyway, this reminds me of a TRON game I wrote with friend in highschool, for the original classic mac ( with 496 kb of ram... ) where we didn't have enough memory to model collision detection so we read back pixel values from the screen buffer. Those were the days Wink
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #11
Shoot Things actually uses readback for collision detection, because it was the easiest way to do arbitrary concave object intersection. Check the source if you want to see how.

Readback is slow, but if you are careful it is not too bad. Runs at 60fps on anything newer than a Rage 128...
Quote this message in a reply
alert
Unregistered
 
Post: #12
ok.... Thanks all...
Mainly FENRIS.. thanks

Alert
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 10,663 Feb 18, 2011 01:54 AM
Last Post: Holmes
  Changing Pixel Values using CG LIPH700 1 4,441 Nov 25, 2010 03:17 PM
Last Post: SethWillits
  Reading texture colors values Leroy 6 4,573 Jul 24, 2007 10:06 PM
Last Post: Leroy
  "Inconsistent set of values to create NSBitmapImageRep" Help! Joseph Duchesne 2 5,742 Sep 30, 2005 05:32 PM
Last Post: Joseph Duchesne
  Passing values to SDL_LoadBMP strangeness Lunatic 0 2,456 Sep 7, 2005 08:49 PM
Last Post: Lunatic