在GLSurfaceView上加载两个纹理作为背景,一个作为前景与alpha

时间:2014-11-18 06:16:15

标签: android opengl-es textures alpha

我已经设置了GLSurfaceView并想要这样做:

  • 将纹理加载为背景(使其全屏而不拉伸图像)

  • 使用alpha加载另一个纹理,使其成为前景。所以背景图片是可见的,如应用雾效果或雨效果。 (也是全屏而不拉伸图像)

  • 以及有关如何正确加载纹理并在GLSurfaceView上设置纹理的任何信息表示赞赏。

我尝试使用此代码在我的GLSurfaceView上加载纹理,但对我不起作用:

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.opengl.GLUtils;

public class TextureHelper {
/**
 * Helper method to load a GL texture from a bitmap
 *
 * Note that the caller should "recycle" the bitmap
 *
 * @return the ID of the texture returned from glGenTextures()
 */
public static int loadGLTextureFromBitmap( Bitmap bitmap, GL10 gl ) {

    // Generate one texture pointer
    int[] textureIds = new int[1];
    gl.glGenTextures( 1, textureIds, 0 );

    // bind this texture
    gl.glBindTexture( GL10.GL_TEXTURE_2D, textureIds[0] );

    // Create Nearest Filtered Texture
    gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR );
    gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR );

    gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT );
    gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT );

    // Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D( GL10.GL_TEXTURE_2D, 0, bitmap, 0 );

    return textureIds[0];
}

/**
 * Create a texture from a given resource
 * 
 * @param resourceID the ID of the resource to be loaded
 * @param scaleToPO2 determines whether the image should be scaled up to the next highest power
 * of two, or whether it should be "inset" into such an image. Having textures that are
 * dimensions of some power-of-two is critical for performance in opengl.
 *
 * @return the ID of the texture returned from glGenTextures()
 */
public static int loadGLTextureFromResource( int resourceID, Context context, boolean scaleToPO2 , GL10 gl ) {

    // pull in the resource
    Bitmap bitmap = null;
    Resources resources = context.getResources();

    Drawable image = resources.getDrawable( resourceID );
    float density = resources.getDisplayMetrics().density;

    int originalWidth = (int)(image.getIntrinsicWidth() / density);
    int originalHeight = (int)(image.getIntrinsicHeight() / density);

    int powWidth = getNextHighestPO2( originalWidth );
    int powHeight = getNextHighestPO2( originalHeight );

    if ( scaleToPO2 ) {
        image.setBounds( 0, 0, powWidth, powHeight );
    } else {
        image.setBounds( 0, 0, originalWidth, originalHeight );
    }

    // Create an empty, mutable bitmap
    bitmap = Bitmap.createBitmap( powWidth, powHeight, Bitmap.Config.ARGB_4444 );
    // get a canvas to paint over the bitmap
    Canvas canvas = new Canvas( bitmap );
    bitmap.eraseColor(0);

    image.draw( canvas ); // draw the image onto our bitmap

    int textureId = loadGLTextureFromBitmap( bitmap , gl );

    bitmap.recycle();

    return textureId;
}

/**
 * Calculates the next highest power of two for a given integer.
 *
 * @param n the number
 * @return a power of two equal to or higher than n
 */
public static int getNextHighestPO2( int n ) {
    n -= 1;
    n = n | (n >> 1);
    n = n | (n >> 2);
    n = n | (n >> 4);
    n = n | (n >> 8);
    n = n | (n >> 16);
    n = n | (n >> 32);
    return n + 1;
}
}

这是我活动中的onCreate代码:

    private GLSurfaceView mGLView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    GLES20Renderer.context = this;

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    if (hasGLES20()) {
        mGLView = new GLSurfaceView(this);
        mGLView.setEGLContextClientVersion(2);
        mGLView.setPreserveEGLContextOnPause(true);
        mGLView.setRenderer(new GLES20Renderer());
    } else {
        mGLView = new GLSurfaceView(this);
        mGLView.setEGLContextClientVersion(1);
        mGLView.setPreserveEGLContextOnPause(true);
        mGLView.setRenderer(new GLES20Renderer());
    Log.i("S"," does not support open gl 2");
        // you gotta get a phone that supports Open GL 2.0
    }

    setContentView(mGLView);
}

这是GLSurfaceView.Renderer的代码:

    @Override
    public void onDrawFrame(boolean firstDraw) {
        int textureid = TextureHelper.loadGLTextureFromResource(R.drawable.ic_launcher, context, true, gl);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.4f, 0.4f, 1.0f);
}

我认为我错过了绘制部分的一些代码可能:(我不知道如何在启用alpha的情况下加载其他纹理...

1 个答案:

答案 0 :(得分:0)

很抱歉,如果我错了,但我想你是OpenGL的初学者。所以,我会尽力帮助你学习。

首先,我建议您仅使用OpenGLES 2.0,因为Android 2.2(API级别8)及更高版本支持它。阅读Android reference and Tutorial。今天,所有Android版本都大于API级别8。

使用OpenGl ES 2.0绘制任何内容时,必须使用GLES20.glDrawArrays(..)或GLES20.glDrawElements(..)。阅读OpenGl ES 2.0 Reference。我没有看到你正在调用这些方法之一。

回答你的问题: 有很多方法可以控制一个图像的alpha。简单的方法是创建自己的片段着色器来完成它。阅读this great tutorial以了解创建着色器的基本步骤。