Public Group

# [OpenGL] clarification about pixel data

This topic is 3680 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello guys, in some examples on the redbook pixel data is created and stored in arrays like this: GLubyte image[width][height][3] (or [4] if there is the a-component). I think about it in this way: for the i,j element (or texel) [w][h][0] [w][h][1] [w][h][2] store the r,g and b components (8bit each since GLubyte) for the i,j element. Later image is used for example in glTextImage2D or glDrawPixels. I am having some fun with DevIL lib and in some example code found here and there, after loading the image, they use: glTexImage2D(........,ilGetData()); The last parameter of the glTexImage2D is GLvoid* wich is obviosly the same type returned by ilGetData(). IlGetData() exactly returns a pointer to the 1D array containing pixel data of the current binded image. How is it possible i can pass to glTexImage2D both image (multi-dim array) and ilGetData() and it works? I khnow they are both GLubyte* but... i think i am missing something. Hope to have been clear Thanks guys

##### Share on other sites
All OpenGL wants is a memory address where the image is stored. On that memory address, and with increasing addresses, the image data is stored. The image is stored pixel by pixel for each row, row by row for the whole image. Each pixel's content is defined by the second and third last parameter to glTexImage.

So, for eample: glTexImage(..., 8, 16, 0, GL_RGB, GL_UNSIGED_BYTE, ptr).
This means at the location pointer to by ptr, there are 16 rows of pixels, each row being 8 pixels wide. Each pixel consists of a red, green and blue channel, and each channel is described using a GLubyte.

As long as the physical storage of the image follow the rules set by OpenGL and the parameters you pass, you can pass any data type you like.

##### Share on other sites
Hey Bro thanks for the reply.

I will try with ur suggestions. I will ask again if needed. Thanks Bob!

##### Share on other sites
Bob, you were right obviously. It's not important the data type u use, as the location of pixel data in memory is always the same. RedBook should focus more on these kind of tips.

An other question: since i store every channel in a GLubyte there are 8 bit for channel. So they range from 0 to 255.

if i run:

GLubyte pixeldata[h*w*3];
...fill it correctly.... (i khnow i did it right coz i drawned it later)
cout << pixeldata[0];

i should see printed the r channel of the first pixel right?
Why do i see only strange symbols?
thanks
bro

##### Share on other sites
Quote:
 Original post by broadyGLubyte pixeldata[h*w*3]; ...fill it correctly.... (i khnow i did it right coz i drawned it later)cout << pixeldata[0];i should see printed the r channel of the first pixel right?Why do i see only strange symbols?thanksbro

You're printing out a char (or unsigned char), so std::cout shows you the corresponding character. If you want the number, then convert it to an int (or unsigned int or anything but a char [wink]):
std::cout << int(pixeldata[0]);

(a char is mostly a byte)

##### Share on other sites
If I may ask, how long have you been programming?
Don't take this as an offense but I think you should focus on c++ first before diving in something as 3D rendering.
If you question yourself something like that, that usually means you don't yet have enough understanding of the programming language or programming in general.

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5

• 13
• 14
• 65
• 14
• 15
• ### Forum Statistics

• Total Topics
632130
• Total Posts
3004286

×