开发者

Android problem drawing a bitmap with opengl es

I'm using https://github.com/markfguerra/GLWallpaperService/ to make an android live wallpaper. I'm trying to load a png file as fullscreen background, however currently all I get is a black screen.

I've searched for a few days now but still haven't found out the problem. I'm doing the following:

public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glTexParameterf(GL10.开发者_如何学GoGL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
}

and every frame:

public void onDrawFrame(GL10 gl)
{
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.target00074);
        int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 
}


I would really recommend to dive into the OpenGL basics as you don't seem to know what you are doing. The screen is black because you are just uploading a texture (something you should do at initialization instead of every frame) and not actually drawing anything. You will need to define some arrays defining vertex and texture positions for a start. Refer to http://blog.jayway.com/2009/12/04/opengl-es-tutorial-for-android-%E2%80%93-part-ii-building-a-polygon/

As for the fullscreen background, you would create a single polygon with your texture


I found my solution in: http://obviam.net/index.php/texture-mapping-opengl-android-displaying-images-using-opengl-and-squares/


Two basic mistakes for when textures don't work:

  • Texture width/height should be a power of two, unless the device supports some extension that alleviates this requirement.
  • Set the TEXTURE_MIN_FILTER to something like NEAREST or LINEAR, the default is mipmapped and this produces incomplete textures unless you provide mipmaps.

Also, the texture parameter state is not global, is per texture object, so set it after binding your texture.


I realize that this answer doesn't really address the question at hand. Downvote me if you must.

If all you want to do is draw a full-screen background image, you don't need OpenGL. You aren't working in 3D and a static background image doesn't need to be updated very often (especially not 30-60 times a second!) so using OpenGL will only kill the battery. A better solution is to have a look at the relevant code examples and documentation, where you will see that all you really need to do is copy-paste the CubeWallpaper example, but change drawFrame() to look like the following:

    void drawFrame() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                // draw something
                c.drawBitmap(myBitmap, myMatrix, myPaint);
            }
        } finally {
            if (c != null) holder.unlockCanvasAndPost(c);
        }

        // Reschedule the next redraw
        mHandler.removeCallbacks(mDrawCube);
        if (mVisible) {
            mHandler.postDelayed(mDrawCube, 1000 / 25);
        }
    }

where myBitmap, myMatrix, and myPaint are Bitmap, Matrix, and Paint objects that have been initialized previously.

Even if my initial assumption (your image is static) is false, this way can still be much more efficient if your image doesn't change terribly often, for example twice a second or slower.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜