Android 在GLSURFACHEVIEW上加载两个纹理一个作为背景,一个作为带有alpha的前景
我已经建立了一个GLSURFACHEVIEW,并希望执行以下操作: 加载纹理作为背景使其全屏显示而不拉伸图像 用alpha加载另一个纹理,使其成为前景。因此背景图片是可见的,比如应用雾效果或雨效果。也可全屏显示,无需拉伸图像 任何关于如何正确加载纹理并将其设置在GLSURFACHEVIEW上的信息都将不胜感激 我尝试了以下代码在我的GLSURFACHEVIEW上加载纹理,但不适用于我:Android 在GLSURFACHEVIEW上加载两个纹理一个作为背景,一个作为带有alpha的前景,android,opengl-es,textures,alpha,Android,Opengl Es,Textures,Alpha,我已经建立了一个GLSURFACHEVIEW,并希望执行以下操作: 加载纹理作为背景使其全屏显示而不拉伸图像 用alpha加载另一个纹理,使其成为前景。因此背景图片是可见的,比如应用雾效果或雨效果。也可全屏显示,无需拉伸图像 任何关于如何正确加载纹理并将其设置在GLSURFACHEVIEW上的信息都将不胜感激 我尝试了以下代码在我的GLSURFACHEVIEW上加载纹理,但不适用于我: import javax.microedition.khronos.opengles.GL10; impor
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的代码:
我想我可能在绘图部分漏掉了一些代码:我不知道如何使用alpha启用加载其他纹理…如果我错了,很抱歉,但我想你是OpenGL的初学者。所以,我将尽力帮助你学习 首先,我建议您只使用OpenGLES 2.0,因为它受Android 2.2 API level 8及更高版本的支持。阅读如今,所有android版本都高于API 8级 要使用OpenGl ES 2.0绘制任何内容,必须使用GLES20.glDrawArrays。。或GLES20.GldrawerElements。。。阅读我没看到你被称为这些方法之一 回答你的问题:
有很多方法可以控制一幅图像的alpha。最简单的方法是创建自己的片段着色器。阅读以了解创建着色器的基本步骤。您还必须发布GlSurfaceView.Renderer.onSurfaceCreated的实现。。。onSurfaceChanged…完成,添加了实现。
@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);
}