2015-11-05 185 views
0
int j,k,t3,t4; 
for (int i = 0;i < width;i++) 
    for (j = 0;j < height;j++) 
    { 
     t3 = (i*height + j)*3 ; 
     t4 = (i*height + j) * 4; 
     for (k = 0;k < 3;k++) 
      texture[t4+k] = data[t3+k]; 
     texture[t4 + 3] = (data[t3 + 1]==255 && data[t3 + 2]==255 && data[t3]==255) ? 0 : 255; 
    } 


GLuint textureID; 
glGenTextures(1, &textureID); 

glBindTexture(GL_TEXTURE_2D, textureID); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, texture); 

这是一个位图文件,我用原始数据成功加载它。当手动添加alpha通道时,纹理在opengl中变成灰色

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data); 

但是,当我加入Alpha通道手动纹理losts其颜色啊画面下方安装。 After modification on texture data. Before Transparency is added.

+0

纹理和数据如何定义? – BDL

+0

@BDL data = new unsigned char [imageSize]; \t unsigned char * texture = new unsigned char [imageSize * 2];数据是从位图文件中读取的原始24位图像数据,而纹理是修改后的数据,我在第一个代码块中的循环后,在每个BGR数据之后附加了一个alpha通道。 –

+0

@BillSun:BMP文件通常以4字节的行对齐方式存储,默认情况下,OpenGL期望这种格式的数据。在访问源数据时,代码转换数据时不考虑这种对齐。 (当每个像素使用4个字节时,目标将始终对齐,所以这不成问题)。 – derhass

回答

1

我不能完全解释确切的症状,但在你的代码索引算法看起来关:

for (int i = 0;i < width;i++) 
    for (j = 0;j < height;j++) 
    { 
     t3 = (i*height + j)*3 ; 
     t4 = (i*height + j) * 4; 

由于图像是在内存中逐行正常布局,该指数是迭代在行中的像素应该是没有乘法器的像素,而另一个指数乘以width(而不是height)。这应该是:

for (j = 0; j < height; j++) 
    for (int i = 0; i < width; i++) 
    { 
     t3 = (j * width + i) * 3; 
     t4 = (j * width + i) * 4; 
相关问题