Search code examples
opengltexturesopengl-4texture1d

Creating and reading 1D textures in OpenGL 4.x


I have problems to use 1D textures in OpenGL 4.x.

I create my 1d texture this way (BTW: I removed my error checks to make the code more clear and shorter - usually after each gl call a BLUE_ASSERTEx(glGetError() == GL_NO_ERROR, "glGetError failed."); follows):

glGenTextures(1, &textureId_);

// bind texture
glBindTexture(GL_TEXTURE_1D, textureId_);

// tells OpenGL how the data that is going to be uploaded is aligned
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

BLUE_ASSERTEx(description.data, "Invalid data provided");

    glTexImage1D(
        GL_TEXTURE_1D,      // Specifies the target texture. Must be GL_TEXTURE_1D or GL_PROXY_TEXTURE_1D.
        0,                  // Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
        GL_RGBA32F,
        description.width,  
        0,                  // border: This value must be 0.
        GL_RGBA, 
        GL_FLOAT,
        description.data);
    BLUE_ASSERTEx(glGetError() == GL_NO_ERROR, "glGetError failed.");

// texture sampling/filtering operation.
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glBindTexture(GL_TEXTURE_1D, 0);

After the creation I try to read the pixel data of the created texture this way: const int width = width_; const int height = 1;

// Allocate memory for depth buffer screenshot
float* pixels = new float[width*height*sizeof(buw::vector4f)];

// bind texture
glBindTexture(GL_TEXTURE_1D, textureId_);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, pixels);
glBindTexture(GL_TEXTURE_1D, 0);

buw::Image_4f::Ptr img(new buw::Image_4f(width, height, pixels));

buw::storeImageAsFile(filename.toCString(), img.get());

delete pixels;

But the returned pixel data is different to the input pixel data (input: color ramp, ouptut: black image)

Any ideas how to solve the issue? Maybe I am using a wrong API call.


Solution

  • Replacing glReadPixels by glGetTexImage does fix the issue.