OpenGL を使用した Android のテクスチャ マッピングに問題があります。基本的に問題は、正方形 (2 つの三角形を描画するために使用するクラス) のほとんどが適切に描画されていることです。しかし、いくつかの正方形は少し奇妙に描かれています。基本的に、上から始まるすべての次の水平線は、約 1 ピクセル左にシフトして描画されます。これは画像の例です: http://postimage.org/image/1la0mr99g/ で、これがどのように描画されるかです (上のボタンはまったく同じですが、テクスチャが異なります): http://postimage.org /image/1lafildpg/
画面に画像を描画するクラスです。また、正方形が作成されたら、使用し続け、後でいくつかのテクスチャを再マップする可能性があることも追加する必要がありますが、これは間違って描画される画像には当てはまりません (実際、うまく描画されるようにテクスチャを再マップした他の画像もあります) )。基本的に、コードを見ると、ほとんどの正方形が適切に描画され、目に見えるバグがないため、何が問題なのかわかりません。また、実際には2つのボタンがあり、それらはまったく同じサイズで、それらの画像も同じサイズですが、一方はうまく描画されますが、もう一方は「歪んで」います。これらのボタンを作成するためのコードは文字通り同じなので、何がうまくいかないのか完全に途方に暮れています。
public class GLSquare {
private float width, height;
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[]; /* = {
-1.0f, 1.0f, 0.0f, // V1 - bottom left
-1.0f, -1.0f, 0.0f, // V2 - top left
1.0f, 1.0f, 0.0f, // V3 - bottom right
1.0f, -1.0f, 0.0f // V4 - top right
};
*/
private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = {
// Mapping coordinates for the vertices
0.0f, 1.0f, // top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f // bottom right (V3)
};
/** The texture pointer */
private int[] textures = new int[1];
public GLSquare(float width, float height) {
this.width = width;
this.height = height;
vertices = new float[12];
vertices[0] = -(width/2); vertices[1] = (height/2); vertices[2] = 0.0f;// V1 - bottom left
vertices[3] = -(width/2); vertices[4] = -(height/2); vertices[5] = 0.0f;// V2 - top left
vertices[6] = width/2; vertices[7] = (height/2); vertices[8] = 0.0f;// V3 - bottom right
vertices[9] = width/2; vertices[10] = -(height/2); vertices[11] = 0.0f;// V4 - top right
// a float has 4 bytes so we allocate for each coordinate 4 bytes
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
// allocates the memory from the byte buffer
vertexBuffer = byteBuffer.asFloatBuffer();
// fill the vertexBuffer with the vertices
vertexBuffer.put(vertices);
// set the cursor position to the beginning of the buffer
vertexBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
public float getWidth(){
return width;
}
public float getHeight(){
return height;
}
public void loadGLTexture(GL10 gl, Bitmap bitmap) {
// Generate a texture pointer
gl.glGenTextures(1, textures, 0);
// Bind texture to array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Clean up
bitmap.recycle();
}
public void draw(GL10 gl, float x, float y, float scaleX, float scaleY, float angle) {
gl.glPushMatrix();
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
//translate to the wanted x,y coord
gl.glTranslatef(x, y, 0.0f);
if(angle != 0.0f){
gl.glRotatef(angle, 0.0f, 0.0f, 1.0f);
}
if(scaleX != 1.0f && scaleY != 1.0f){
gl.glScalef(scaleX, scaleY, 0.0f);
}
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glPopMatrix();
}
}