PDA

View Full Version : Textures larger and incomplete displayed on Android OpenGL ES 2.0



symla_
02-05-2015, 12:05 PM
My aim is to write a game in C++ for Linux, Windows and Android. I use SDL and am able to draw basic geometric shapes using OpenGL ES 2.0 and shaders. But when I try to apply textures to these shapes I recognize, that they appear larger and incomplete on Android. On PC it works fine. My code does not has to be changed to compile for Android. I use Ubuntu 14.10 and test it on it as well as on my Nexus 5 with Android 5.0.1.

I set up an orthographic projection matrix, that gives me a "surface" with an aspect ration of 16:9 in the area x 0.0 to 1.0 and y 0.0 to 0.5625. In this area I draw a rectangle to check that "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);

//.....

The results are following:

1620

Then I draw a square and map a texture to it. Here is the code:


//.....

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

But this gives me different results:

1621

Then I have tried to modify and test around with the texture coordinates. If I half them, so all 1.0s to 0.5, only the "1 field" of my texture should be displayed? On Linux it is that way, but on Android is just some random area of the texture displayed.

So can anyone give me a hint what I do wrong?

symla_
02-05-2015, 12:39 PM
I figured out the problem. I bind my attributes a_Vertex to 0 and a_TexCoordinate to 1. On PC it happens, but on Android it is reversed. So I took a look in the OpenGL ES 2.0 Reference concerning glBindAttribLocation. You have to bind the locations before linking the program. Now it works perfectly the same on Android as on PC.