Android OpenGL 纹理未渲染

3

以下是我的OpenGL初始化代码:

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

这是我的纹理类

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

}
}

我遇到的问题是,正确的形状和位置上没有绘制任何东西,只有一个白色框,应该是纹理的问题。
这个图片大小为50x50,我以为是2的幂次方的问题,但这个问题在模拟器上也会出现。
此外,我的日志中出现了一个错误,它说:“GL纹理加载错误:无效值”,但我不知道这是什么意思。
最后,我像这样绘制纹理:
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

textures.get("ball").draw(gl, 10, 10);

你用于加载纹理的代码在哪里? - Nicol Bolas
2个回答

2

glTexImage2D

  • 如果internalFormat不是1、2、3、4或接受的分辨率和格式符号常量之一,则生成GL_INVALID_VALUE。
  • 如果宽度或高度小于0或大于2 + GL_MAX_TEXTURE_SIZE,则生成GL_INVALID_VALUE。
  • 如果不支持非二次幂纹理,并且宽度或高度不能表示为2k + 2边框,其中k是某个整数值,则生成GL_INVALID_VALUE。
  • 如果边框不为0或1,则生成GL_INVALID_VALUE。

我认为内部格式可能有误。您可以尝试自己指定它。有关GLUtils的更多文档,请参见此处

但首先我发现您在将纹理加载到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();

1
我做了以下事情:
  1. 将所有图像移动到res/drawable-nodpi中
  2. 按2的幂缩放,在Linux中,mogrify可以完成这项工作(find . -exec mogrify -scale '512x512!' {} \;)
如果您从外部源加载纹理,则此代码将将其转换为最接近的2的幂调整大小的位图。
public class ScalePowerof2 { // some devices require to scale texture bitmaps to power of 2 size 'ScalePower2.scalePowerof2(Bitmpap)' does the job
private static boolean isPowerof2(int v) { // is v a 2^n ?
    for (int i=1; i!=0; i<<=1) if (v==i) return true;
    return false;
}
private static int closestPowerof2(int v) { // n | 2^n <= v
    for (int i=1; i!=0; i<<=1) if (i>=v) return i;
    return 0;
}
public static Bitmap scalePowerof2(Bitmap bmp) {
    Bitmap b=null;
    if (bmp!=null) {
        int w=bmp.getWidth(), h=bmp.getHeight();
        if (isPowerof2(w) & isPowerof2(h)) return bmp; // ok, both 2^n nothing to do
        w=closestPowerof2(w); h=closestPowerof2(h); // new 2^n values
        b=Bitmap.createScaledBitmap(bmp, w, h, false);
    }
    return b;
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接