0

I have problems generating a Float texture with depth data in it.

Since I think its more an OpenGL ES (OES 2.0) problem instead of the the SDK i am using, I try to get help here considering the OpenGL ES part.

The error i am getting is just that my app crashes, this is why I think, that the error is a wrong Texture and/or wrong usage of the float Texture.

Just a few annotations about the data i am using:

-The depthFrame I am getting is in Float
-With the use of depthInMillimeters, it is possible to get actual mm values in Float 
-Since i am working on an iPhone 5 GL_HALF_FLOAT_OES should be available to use
-If someone wants to know, the data comes from the StructureSensor

This is my function to generate the Texture from the streamed depth data. I am using nearly the same function to generate RGB Textures from images, but with other data input and values.

    -(GLuint) generateDepthTexture: (STDepthFrame*)depthFrame{

    //data from depth in mm
    NSData *data = [NSData dataWithBytes:depthFrame.depthInMillimeters length:depthFrame.width*depthFrame.height*4];

    //data to texture
    UIImage* depthImage = [[UIImage alloc] initWithData:data];

    CGImageRef spriteImage = [depthImage CGImage];
    if(!spriteImage){
        NSLog(@"Failed to load image depth");
        exit(1);
    }

    size_t width = CGImageGetWidth(spriteImage);
    size_t height = CGImageGetHeight(spriteImage);

    GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte)); // *4


    CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4,
                                                   CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);

    CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
    CGContextRelease(spriteContext);


    GLuint texName;

    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_FLOAT, (GLsizei)width,(GLsizei)height, 0, GL_FLOAT, GL_HALF_FLOAT_OES, spriteData);

    free(spriteData);

    return texName;


    }

This is the important part of my render method:

    //depth
    //depthFrame is given every call
    _depthTexture = [self generateDepthTexture:depthFrame];

    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, _depthTexture);
    glUniform1i(_depthUniform, 2);

I am using it in the shader with this:

    //texture 
    uniform sampler2D DepthTexture;

    //usage of the Texture
    mediump vec4 depthV = texture2D(DepthTexture,TexCoordsIn);

I am not sure about the right usage here, because i would rather get the values in a float, than in a vec4.

And again, i would like to get some help considering the OpenGL ES part with the float texture. If its completely right, the problem must be with the data I am getting. If thats the case, I will ask the people working with the sensor.

The error might be only a wrong texture size or memory.

I really hope, someone has an answer to this.

1 Answer 1

2

The OP seems to be aware of this already, but just to send it ahead for clarity: float textures are not a standard ES 2.0 feature. They are only supported if the OES_texture_float and/or OES_texture_half_float extensions are present. This is fairly obvious for the half-float case, since the extension introduces the GL_HALF_FLOAT_OES value, but extension support is also required for the float case, even though the extension does not introduce new enum values.

Your attempt at using the extension has a number of issues:

glTexImage2D(GL_TEXTURE_2D, 0, GL_FLOAT, (GLsizei)width, (GLsizei)height, 0,
             GL_FLOAT, GL_HALF_FLOAT_OES, spriteData);

You're trying to pass GL_FLOAT/GL_HALF_FLOAT values to the internalformat, format and type arguments of glTexImage2D(). But the extension only introduces new values for the type argument. So the correct call to allocate a 1-component float texture is:

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, (GLsizei)width, (GLsizei)height, 0,
             GL_LUMINANCE, GL_FLOAT, spriteData);

Note that ES 2.0, unlike newer version of OpenGL, still uses GL_LUMINANCE or GL_ALPHA for the internalformat and format of 1-component textures.

For a half-float texture, the call is:

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, (GLsizei)width, (GLsizei)height, 0,
             GL_LUMINANCE, GL_HALF_FLOAT_OES, spriteData);

For this half-float case, it's important to understand that you need to pass half-float (16-bit per value) data into the call. If your original data is in float format, you'll have to write (or find) code that converts it into half-float format.

This all looks different in ES 3.0 and later, where the sized internal formats from more modern OpenGL versions were introduced. There, the call to allocate a 1-component float texture would be:

glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, (GLsizei)width, (GLsizei)height, 0,
             GL_RED, GL_FLOAT, spriteData);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.