为什么 Android 上显示的纹理比 PC 更大且不完整?

Why are textures displayed on Android larger and icomplete compared to PC?

我的目标是用 C++ 为 Linux、Windows 和 Android 编写游戏。我使用 SDL,并且能够使用 OpenGL ES 2.0 和着色器绘制基本的几何形状。但是,当我尝试将纹理应用到这些形状时,我发现它们在 Android 上显得更大且不完整。在 PC 上它工作正常。我的代码无需更改即可针对 Android 进行编译。我使用 Ubuntu 14.10 并在其上以及使用 Android 5.0.1.

在我的 Nexus 5 上进行测试

我设置了一个正交投影矩阵,在 x 0.0 到 1.0 和 y 0.0 到 0.5625 的区域中得到一个 "surface",纵横比为 16:9。在这个区域我画了一个矩形来检查 "custom space":

//Clear
glClearColor(0.25, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Configure viewport
glViewport(0, 0, this->width, this->height);

//Update matrices
this->baseShader.bind();
this->baseShader.updateProjectionMatrix(this->matrix);
this->matrix.loadIdentity();
this->baseShader.updateModelViewMatrix(this->matrix);

//Draw rectangle
GLfloat vertices[] = {0.0, 0.0, 0.0, 0.5625, 1.0, 0.0, 1.0, 0.5625};

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisableVertexAttribArray(0);

结果如下:

TextureComparison1.png - Dropbox

然后我绘制一个正方形并为其映射纹理。这是代码:

//Enable blending
bw::Texture::enableAlpha();

//Matrices
this->textureShader.bind();
this->textureShader.updateProjectionMatrix(this->matrix);
this->matrix.loadIdentity();

this->matrix.translate(this->sW/2.0, this->sH/2.0, 0.0);
this->matrix.rotate(rot, 0.0, 0.0, 1.0);
this->matrix.translate(-this->sW/2.0, -this->sH/2.0, 0.0);

this->textureShader.updateModelViewMatrix(this->matrix);

//Coordinates
float x3 = this->sW/2.0-0.15, x4 = this->sW/2.0+0.15;
float y3 = this->sH/2.0-0.15, y4 = this->sH/2.0+0.15;
GLfloat vertices2[] = {x3, y3, x3, y4, x4, y3, x4, y4};
GLfloat texCoords[] = {0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0};

//Send coordinations to GPU
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices2);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
glEnableVertexAttribArray(1);

//Bind texture
int u1 = glGetUniformLocation(this->textureShader.getId(), "u_Texture");
glActiveTexture(GL_TEXTURE0);
this->spriteTexture.bind();
glUniform1i(u1, 0);

//Draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

但这给了我不同的结果:

TextureComparison2.png - Dropbox

然后我尝试修改和测试纹理坐标。如果我将它们减半,那么所有 1.0 到 0.5,应该只显示我的纹理的“1 个字段”?在 Linux 上是这样,但在 Android 上只是显示纹理的一些随机区域。

所以谁能告诉我我做错了什么?

我找到问题了。我将我的属性 a_Vertex 绑定到 0,将 a_TexCoordinate 绑定到 1。在 PC 上它会发生,但在 Android 上它是相反的。因此,我查看了有关 glBindAttribLocation 的 OpenGL ES 2.0 参考。在链接 程序之前,您必须绑定位置。现在它在 Android 上与在 PC 上完全一样。