2014-08-28 34 views
0

我在使用OpenGL 4.x中的一維紋理時遇到了問題。在OpenGL 4.x中創建和讀取一維紋理

創建我的1D紋理這種方式(BTW:我取出我的錯誤檢查,以使代碼更清晰和更短 - 通常在每個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; 

但返回的像素數據輸入像素數據的不同(輸入:顏色漸變,ouptut:黑色圖像)

任何想法如何解決這一問題?也許我正在使用錯誤的API調用。

+4

'glReadPixels'從屏幕(或綁定的幀緩衝區)中讀取,而不是從紋理讀取。你想要'glGetTexImage'。 – 2014-08-28 13:53:39

回答

0

替換glReadPixels通過glGetTexImage解決了這個問題。