views:

1201

answers:

2

I have a texture which has only 1 channel as it's a grayscale image. When I pass the pixels in to glTexImage2D, it comes out red (obviously because channel 1 is red; RGB).

glTexImage2D(
 GL_TEXTURE_2D, 0, GL_RGBA,
 dicomImage->GetColumns(), dicomImage->GetRows(),
 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelArrayPtr);

Do I change GL_RGBA? If so, what to?

A: 

It appears that I should use GL_LUMINANCE instead of GL_RGBA for the 3rd argument.

Edit (in reply to comments):

When I set the 7th argument to GL_LUMINANCE (as well as the 3rd), the picture goes completely distorted. With the DICOM pixel format, it appears that the 7th argument must be GL_RGBA for some reason.

The strange behavior is because I'm using the DICOM standard. The particular DICOM reader I am using outputs integer pixel values (as pixel values may exceed the normal maximum of 255). For some strange reason the combination of telling OpenGL that I am using an RGBA format, but passing in integer values rendered a perfect image.

Because I was truncating the DICOM > 255 pixel values anyway, it seemed logical to copy the values in to a GLbyte array. However, after doing so, a SIGSEGV (segmentation fault) occurred when calling glTexImage2D. Changing the 7th parameter to GL_LUMINANCE (as is normally required) returned the functionality to normal.

Weird eh?

So, a note to all developers using the DICOM image format: You need to convert the integer array to a char array before passing it to glTexImage2D, or just set the 7th argument to GL_RGBA (the later is probably not recommended).

nbolton
It is argument #7 that counts here!
ypnos
Hmm, when change #7, it goes red... but #3 has the desired effect. Can you explain this please?
nbolton
As ypnos says, arguments 7 and 8 are what you're interested in. They tell OpenGL the format of the data which the pointer points to. The earlier arguments indicate the type of texture, as I recall.
Jay Kominek
Nick, argument #3 is for internal storage, #7 is for input format. GL will convert from #7 to #3. The effect you are getting can have several reasons I can't tell from here. In almost every case however you want both be the same anyway.
ypnos
My case is unusual, see my edit for explanation.
nbolton
+2  A: 

Change it to GL_LUMINANCE. See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html

ypnos