在OpenGL 4.x中创建和读取1D纹理



我在OpenGL 4.x中使用1D纹理时遇到问题。

我用这种方式创建我的1d纹理(顺便说一句:我删除了错误检查,使代码更清晰、更短——通常在每次gl调用BLUE_ASSERTEx(glGetError() == GL_NO_ERROR, "glGetError failed.");之后):

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);

创建后,我尝试以这种方式读取创建纹理的像素数据: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;

但返回的像素数据与输入的像素数据不同(输入:彩色渐变,输出:黑色图像)

有什么想法可以解决这个问题吗?也许我使用了错误的API调用。

glGetTexImage替换glReadPixels确实解决了这个问题。

相关内容

  • 没有找到相关文章