2

I would like a simple way of achieving this, but seem to be bjorking the parameters to glTexImage2D. I have an std::vector<uint16_t> depth_buffer that, on a frame-by-frame basis has depth measurements coming from a kinect. There are exactly 640 x 480 of them, one depth measurement per pixel. If the world went my way, the call should be

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, 640, 480, 0, GL_LUMINANCE16, GL_UNSIGNED_SHORT, depth_buffer.data());

Where internalFormat (third parameter) is GL_LUMINANCE16 because they are 16 bit unsigned integers, and format is the same because that is exactly how the data is coming in. The type parameter should be GL_UNSIGNED_SHORT...because these are shorts and not bytes.

Surprisingly, if I change it to be

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, 640, 480, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, depth_buffer.data());

where internalFormat is still GL_LUMINANCE16, format is just GL_LUMINANCE without the 16, and type is GL_UNSIGNED_BYTE, then I get something. Things are clearly being skipped, but just changing to GL_UNSIGNED_SHORT doesn't cut it.

Depending on which documentation you read, format (the second GL_LUMINANCE) may or may not allow the 16 after it (anybody know why? experimentation seems to confirm this). But my chief concern here is why GL_UNSIGNED_**SHORT** seems to be invalid (either all black or all white) depending on the internalFormat -- format combination.

I've tried an obscene amount of combinations here, and am looking for the right approach. Anybody have some advice for achieving this? I'm not opposed to using fbo's, but would really like to avoid it if possible...since it definitely should be doable.

2 Answers 2

4

I wouldn't bother with GL_LUMINANCE, it's an obsolete feature from old versions of OpenGL (no, seriously, don't use it). In a modern setting, you would use:

  • Internal format GL_R16. All this means is "one channel, 16 bits, normalized".

  • Format GL_RED. (Formats are not sized, so GL_LUMINANCE16 is illegal here, and GL_R16 is also illegal.)

  • Type GL_UNSIGNED_SHORT.

5
  • Hmmmm. What you have suggested makes sense in theory, and your format agrees with datenwolf's response. Using those parameters, though, gives me all black :/ Changing to GL_UNSIGNED_BYTE gives the identical skipping pattern as before, only now everything is red. Which makes sense? I did some toying around with GL_R16UI and some others, but no dice there either. Any other thoughts? Regardless, thanks for the response and tip about GL_LUMINANCE :)
    – svenevs
    Aug 4, 2015 at 12:26
  • What is the minimum and maximum value in the texture? Is it possible that it's not black, but just really dark? Aug 4, 2015 at 12:33
  • hmmm you are definitely on to something here. It should be min=0 and max=65535 since these are ushorts. I inverted the data to be depth[i] = 65535 - depth[i] and get something seemingly legitimate, though very very red in nature ;) I thought maybe changing glDepthRange to be 0 - 65535 could fix it, but that wasn't any help. Should I be trying to find a way to map things to 0-1 with GL? I really don't want to modify the depth data itself, since that is used later for the actual project ;)
    – svenevs
    Aug 4, 2015 at 12:43
  • I was about to answer, but to be honest, it sounds like you are not reading any documentation, but relying on function and constant names. Please read the docs. For example, in the Kinect documentation, it will explain what it puts in the depth buffer (freenect gives physical mm or raw 11-bit data, but never normalized 16-bit data). The OpenGL documentation explains that glDepthRange() changes the depth output of the rasterizer. And the glTexImage2D() documentation explains which formats and types are permissible or not permissible. I know these things because I read the docs. Aug 4, 2015 at 12:58
  • I'm sorry you feel I was wasting your time. I have been working on this for about 4 days before asking a question. I use 11-bit mode, but this gets padded to 16 by libfreenect (as does 10bit). 65535 is possible (indicates bad measurements e.g. lost packet). 0 is given for out of range. In any case, you have given me enough. I'll keep tinkering with it. glDepthRange() was an oversight, mainly out of desperation x0. Please note that I specifically cited the documentation, and asked why format could not have 16. Anyway, thank you for helping me get things in a forward direction, Dietrich.
    – svenevs
    Aug 5, 2015 at 5:23
2

The (second) format parameter is only for telling what is contained in the data, not how it's laid out. Therefore GL_LUMINANCE16 is an invalid token to pass to the format parameter (it's allowed only for the internalformat parameter).

The layout from which the data shall be unpacked is controlled by the type parameter to glTexImage and the pixel store settings for unpacking set with glPixelStorei for the GL_UNPACK_… parameters. Most likely your "skipping" is due to mismatched pixel store unpack parameters.

1
  • Ok, that makes sense for the format parameter in some senses. Indeed, the skipping comes from me telling it to use bytes when they are shorts.
    – svenevs
    Aug 4, 2015 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.