[英]loading two textures on a GLSurfaceView one as background, one as foreground with alpha
我已經設置了GLSurfaceView
並想要這樣做:
加載紋理作為背景(使其全屏顯示而不拉伸圖像)
加載另一個帶有alpha的紋理,使其成為前景。 因此背景圖片是可見的,例如應用霧效果或雨效果。 (也可以全屏顯示而不拉伸圖像)
以及任何有關如何正確加載紋理並在GLSurfaceView
上進行設置的信息。
而且我已經嘗試過此代碼在我的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的情況下加載其他紋理...
抱歉,如果我錯了,但是我想你是OpenGL的初學者。 因此,我將嘗試幫助您的學習。
首先,我建議您僅使用OpenGLES 2.0,因為Android 2.2(API級別8)及更高版本支持它。 閱讀Android參考和教程 。 今天,所有android版本都大於API級別8。
要使用OpenGl ES 2.0繪制任何內容,必須使用GLES20.glDrawArrays(..)或GLES20.glDrawElements(..)。 閱讀《 OpenGl ES 2.0參考》 。 我沒看到您被稱為這些方法之一。
回答您的問題:有很多方法可以控制一個圖像的Alpha。 最簡單的方法是創建自己的“片段着色器”來執行此操作。 閱讀本教程,以了解創建着色器的基本步驟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.