Android绘制纹理扩展绘制白色纹理

时间:2012-07-20 14:40:49

标签: android opengl-es textures

我开始使用OpenGL-ES为Android编写游戏,刚刚完成了使用glDrawTexfOES扩展的绘图代码。当我在模拟器上测试它时工作正常,但在我的三星Galaxy S2上测试它似乎所有的纹理都是白色的。

为了确保我没有犯任何错误,我从教程中复制了源代码并使用相同的结果运行它。我正在使用的教程代码可以看作here

我的纹理是.PNG格式和2的幂,我从R.drawable文件夹加载,虽然我已经尝试了一些其他位置,如drawable-nodpi,如我所见。

我还检查了glGenTextures的结果,我读过它可以为某些手机提供奇数值,但似乎给出了正确的值(1,2,3 ..)。

有人知道为什么会发生这种情况或建议我可以做些其他检查以找出问题所在吗?

这是我上面链接的示例代码的略微修改版本,以简化操作。

public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {

    gl10.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    // Set the background colour to black ( rgba ).
    gl10.glClearColor(0.0f, 0.0f, 0.0f, 1);
    // Enable Flat Shading.
    gl10.glShadeModel(GL10.GL_FLAT);
    // We don't need to worry about depth testing!
    gl10.glDisable(GL10.GL_DEPTH_TEST);
    // Set OpenGL to optimise for 2D Textures
    gl10.glEnable(GL10.GL_TEXTURE_2D);
    // Disable 3D specific features.
    gl10.glDisable(GL10.GL_DITHER);
    gl10.glDisable(GL10.GL_LIGHTING);

    gl10.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
    // Initial clear of the screen.
    gl10.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Test for draw texture

    // Test for device specific extensions
    String extensions = gl10.glGetString(GL10.GL_EXTENSIONS);
    boolean drawTexture = extensions.contains("draw_texture");
    Log.i("OpenGL Support - ver.:",
            gl10.glGetString(GL10.GL_VERSION) + " renderer:" +
            gl10.glGetString(GL10.GL_RENDERER) + " : " +
            (drawTexture ? "good to go!" : "forget it!!"));

    // LOAD TEXTURE

    mTextureName = new int[1];

    // Generate Texture ID
    gl10.glGenTextures(1, mTextureName, 0);
    assert gl10.glGetError() == GL10.GL_NO_ERROR;

    // Bind texture id / target (we want 2D of course)
    gl10.glBindTexture(GL10.GL_TEXTURE_2D,  mTextureName[0]);

     // Open and input stream and read the image
    InputStream is = mContext.getResources().openRawResource(R.drawable.asteroid);
    Bitmap bitmap;
    try {
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Build our crop region to be the size of the bitmap (ie full image)
    mCrop = new int[4];
    mCrop[0] = 0;
    mCrop[1] = imageHeight = bitmap.getHeight();
    mCrop[2] = imageWidth = bitmap.getWidth();
    mCrop[3] = -bitmap.getHeight();


    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    assert gl10.glGetError() == GL10.GL_NO_ERROR;

    bitmap.recycle();
}

public void onSurfaceChanged(GL10 gl10, int i, int i1) {

    gl10.glViewport(0, 0, i, i1);
    /*
     * Set our projection matrix. This doesn't have to be done each time we
     * draw, but usually a new projection needs to be set when the viewport
     * is resized.
     */
    float ratio = (float) i / i1;
    gl10.glMatrixMode(GL10.GL_PROJECTION);
    gl10.glLoadIdentity();
    gl10.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}


public void onDrawFrame(GL10 gl) {
    // Just clear the screen and depth buffer.
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Begin drawing
    //--------------
    // These function calls can be experimented with for various effects such as transparency
    // although certain functionality maybe device specific.
    gl.glShadeModel(GL10.GL_FLAT);
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);

    // Setup correct projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glPushMatrix();
    gl.glLoadIdentity();
    gl.glOrthof(0.0f, mWidth, 0.0f, mHeight, 0.0f, 1.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glPushMatrix();
    gl.glLoadIdentity();

    gl.glEnable(GL10.GL_TEXTURE_2D);

    // Draw all Textures
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName[0]);
    ((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCrop, 0);
    ((GL11Ext)gl).glDrawTexfOES(0, 0, 0, imageWidth, imageHeight);

    // Finish drawing
    gl.glDisable(GL10.GL_BLEND);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glPopMatrix();
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glPopMatrix();
}

1 个答案:

答案 0 :(得分:1)

你是否尝试过将所有图像都放在drawable-nodpi上而不是其他地方?

我不认为这可能很重要,但在附加渲染之前尝试这些行。

 glSurfaceView.setZOrderOnTop(true);
 glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
 glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);

错误可能与透明度和PNG格式有关。

如果它不起作用,您可以粘贴与GLSurfaceView和渲染器相关的代码吗?

谢谢!