我写了一个opengl android代码来显示一个正方形的位图。但是位图是相反的。当我将纹理数组组合更改为注释代码时,它被正确绘制。但我坚持我的纹理数组必须如下所示。我错了吗?
/** The initial vertex definition */ private float vertices[] = { -1.0f, 1.0f, 0.0f, //Top Left -1.0f, -1.0f, 0.0f, //Bottom Left 1.0f, -1.0f, 0.0f, //Bottom Right 1.0f, 1.0f, 0.0f //Top Right }; /** Our texture pointer */ private int[] textures = new int[1]; /** The initial texture coordinates (u, v) */ private float texture[] = { //Mapping coordinates for the vertices // 1.0f, 0.0f, // 1.0f, 1.0f, // 0.0f, 1.0f, // 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, }; /** The initial indices definition */ private byte indices[] = { //2 triangles 0,1,2, 2,3,0, };
答案 0 :(得分:5)
Android使用左上角作为坐标系的0,0,而OpenGL使用左下角为0,0这就是你的纹理被翻转的原因。
常见的解决方法是在加载时翻转纹理
Matrix flip = new Matrix();
flip.postScale(1f, -1f);
Bitmap bmp = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), flip, true);
答案 1 :(得分:0)
实际上,我认为Will Kru的解决方案应该在两个轴周围翻转背景
flip.postScale(-1f, -1f);
该解决方案对我有用!
答案 2 :(得分:0)
这对我有用。 (无需创建另一个位图和缩放)
private float vertices[] = {
1.0f, -1.0f, 0.0f, //Bottom Right
1.0f, 1.0f, 0.0f, //Top Right
-1.0f, 1.0f, 0.0f, //Top Left
-1.0f, -1.0f, 0.0f, //Bottom Left
};