[英]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.