Android OpenGL ES纹理映射/绘图问题
我在使用OpenGL的Android中进行纹理映射时遇到了问题。 基本上问题是,大多数Squares(我的班级用于绘制2个三角形)绘制得当,即它们上的纹理完全按照它们的样子绘制,一切都很美妙。 但是有些方格有点奇怪。 基本上,从顶部开始的每一条下一条水平线都会画出左移1个像素。 这是一个示例图像:http://postimage.org/image/1la0mr99g/这是它如何绘制(上面的按钮是完全相同的,但不同的纹理):http://postimage.org /图像/ 1lafildpg /
这是负责绘制图像到屏幕的类。 我还必须补充一点,一旦创建了Square,我将继续使用并稍后重新映射一些纹理,但是对于绘制错误的图像来说并不是这样(事实上,其他一些图像我已经重新映射了纹理以便绘制得很好)。 所以基本上,通过查看代码,我不知道会出现什么问题,因为大多数方块都可以正确绘制,并且没有可见的错误。 此外,我实际上有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();
}
}
它看起来像一个对齐问题。 这有点令人惊讶,因为我预计GLUtils.texImage2D
会GLUtils.texImage2D
设置所有参数。
在C中你会添加
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // correction, should be 1
但是,测试是否真的遇到对齐问题很容易:将图像宽度设置为8的倍数:无论对齐设置如何,使用该尺寸时,图像应始终显示正确。
它也可能是ROW_LENGTH的错误设置。 您可以尝试添加上面描述的两个调用(当然,添加适当的名称空间,即gl和GL10)。
链接地址: http://www.djcxy.com/p/33971.html上一篇: Android OpenGL ES texture mapping/drawing problem
下一篇: Full screen background texture with OpenGL performance issue (iPad)