我必须将位图图像转换为纹理格式。并需要显示它。为此,我编写了一个将位图转换为纹理的函数。但是在运行代码时,它会显示一个没有任何崩溃的空白图像。请帮助确定
在privew.java中,我在onCreate()函数中为图像(内部资源文件夹)创建了一个位图对象。
//Creating a sample image's resource id for testing in GlRenderer
drawableResourceId = this.getResources().getIdentifier("flower", "drawable", this.getPackageName());
System.out.println("FLOWER ID "+drawableResourceId);
InputStream is =this.getResources().openRawResource(drawableResourceId);
Bitmap tBitmap;
tBitmap = BitmapFactory.decodeStream(is, null, sBitmapOptions);
ThisAppGlobals.texture_bitmap=tBitmap;
在GlRenderer.java(OpenGl的Renderer类)中使用了一个名为" loadGLTextureFromBitmap()"的函数。为相应的位图对象创建纹理。 我称之为openGl类的onSurfaceCreated()函数。
// setup texture //
int mTextureID
System.out.println("Calling loadGLTextureFromBitmap");
mTextureID = loadGLTextureFromBitmap(ThisAppGlobals.texture_bitmap, gl );
System.out.println("Called loadGLTextureFromBitmap"+ mTextureID);
功能是,
private int loadGLTextureFromBitmap(Bitmap sideBysideImg, GL10 gl) {
// TODO Auto-generated method stub
// Generate one texture pointer
int[] textureIds = new int[1];
gl.glGenTextures( 1, textureIds, 0 );
// bind this texture
gl.glBindTexture( GLES20.GL_TEXTURE_2D, textureIds[0] );
// Create Nearest Filtered Texture
gl.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR );
gl.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR );
gl.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT );
gl.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT );
// Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D( GLES20.GL_TEXTURE_2D, 0,sideBysideImg, 0 );
return textureIds[0];
}
onSurfaceChanged()包含
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
checkGlError("glBindTexture mTextureID");
GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_NEAREST);
GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
/*
* Create the SurfaceTexture that will feed this textureID,
* and pass it to the MediaPlayer
*/
mSurface = new SurfaceTexture(mTextureID);
mSurface.setOnFrameAvailableListener(this);
Surface surface = new Surface(mSurface);
surface.release();