2015-09-23 54 views
0

我已經創建了一個紋理,並使用Android上的OpenGL ES 3.1(使用NDK)填充紫色。 問題是,着色器中的紋理查找無法按預期工作。GLSL紋理只適用於(0,0)

創建紋理就像這樣:

glGenTextures(1, &mGLTexture); 
glBindTexture(GL_TEXTURE_2D, mGLTexture); 
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, textureSize.x, textureSize.y); 

vector<GLubyte> img; int i = 0; 
img.resize(4 * textureSize.x * textureSize.y); 
for (unsigned x = 0; x < 64; ++x) { 
    for (unsigned y = 0; y < 64; ++y) { 
     img[i * 4 + 0] = 255; 
     img[i * 4 + 1] = 0; 
     img[i * 4 + 2] = 255; 
     img[i * 4 + 3] = 255; 
     // purple with full alpha 
    } 
} 

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 
       64, 64, 
       GL_RGBA, GL_UNSIGNED_BYTE, img.data()); 

沒有OpenGL的錯誤在此過程中拋出。

我然後綁定紋理:

glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, mGLTexture); 
glUniform1i(texLocation, 0); // got texLocation using glGetUniformLocation 

現在,出現問題:在我的着色器,當我擡頭只在(0,0)紋理,使用FragColor = texture(tex, vec2(0,0));它按預期工作,即事被塗成紫色。
對於所有其他紋理座標(例如vec2(0.5f,0.5f)),我只是到處都是黑色。

+1

您是否嘗試將'GL_MIN_FILTER'設置爲'GL_LINEAR'或'GL_NEAREST'?當沒有生成mipmap時,需要mipmap的插值模式將失敗。 – BDL

+0

不幸的是,GL_NEAREST和GL_LINEAR都不能解決問題。 – mrueg

+0

您只設置img的前四個值,因爲在每次迭代中i = 0。 – BDL

回答

2

在循環中,只分配img的前四個元素。您可能忘記更新索引變量i。

for (unsigned x = 0; x < 64; ++x) { 
    for (unsigned y = 0; y < 64; ++y) { 

     i = y * 64 + x; 

     img[i * 4 + 0] = 255; 
     img[i * 4 + 1] = 0; 
     img[i * 4 + 2] = 255; 
     img[i * 4 + 3] = 255; 
     // purple with full alpha 
    } 
}