Android OpenGL纹理未呈现
作者:互联网
这是我的GL初始化代码:
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0, 0, width, height);
float left = 0;
float right = width;
float top = height;
float bottom = 0;
gl.glOrthof(left, right, top, bottom, -1, 1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
textures.put("ball", new Texture(gl, BitmapFactory.decodeResource(context.getResources(), R.drawable.ball)));
这是我的Texture类
public class Texture
{
int[] texture = new int[1];
static int real_width = 0;
static int real_height = 0;
Texture(GL10 gl, Bitmap bmp)
{
real_width = bmp.getWidth();
real_height = bmp.getHeight();
gl.glGenTextures(1, texture, 0);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
bmp.recycle();
int error = gl.glGetError();
if (error != GL10.GL_NO_ERROR)
{
Log.e("ROLL", "GL Texture Load Error: " + GLU.gluErrorString(error));
}
}
public void draw(GL10 gl, float x, float y)
{
draw(gl, x, y, real_width, real_height);
}
public void draw(GL10 gl, float x, float y, float w, float h)
{
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
float[] vertices = { x + w, y + h, x, y + h, x, y, x + w, y };
float[] textureCoords = { 1, 1, 0, 1, 0, 0, 1, 0 };
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
FloatBuffer vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(textureCoords.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(textureCoords);
textureBuffer.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
我得到的问题是,除了正确形状和位置的白色框外,什么都没画.
图像的大小为50×50,我认为这是由于2的功能所致,但是在模拟器上也会出现此问题.
另外,我的确在日志中得到一个错误,它说:“ GL Texture Load Error:无效值”,但是我不知道那是什么意思.
最后,我像这样绘制纹理:
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
textures.get("ball").draw(gl, 10, 10);
解决方法:
从glTexImage2D开始:
>如果internalFormat不是1、2、3、4或3,则生成GL_INVALID_VALUE
公认的分辨率和格式符号常量之一.
>如果宽度或高度小于0或则生成GL_INVALID_VALUE
大于2 GL_MAX_TEXTURE_SIZE.
>如果非二次幂纹理不是生成的,则生成GL_INVALID_VALUE
支持,宽度或高度不能表示为2k 2
k的某个整数值的边界.
>如果border不为0或1,则生成GL_INVALID_VALUE.
我认为内部格式是错误的.尝试自己指定该怎么办.有关GLUtils的更多文档,您可以找到there.
但首先,我看到在将纹理加载到GL之前尚未绑定纹理.
gl.glGenTextures(1, texture, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
bmp.recycle();
标签:opengl-es,textures,android 来源: https://codeday.me/bug/20191208/2089200.html