下面列出了android.opengl.GLUtils#texSubImage2D ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) {
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if (recycle) {
img.recycle();
}
return textures[0];
}
public static int loadTexture(final Bitmap bitmap, final int usedTextureId) {
int[] textures = new int[1];
if (usedTextureId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTextureId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, bitmap);
textures[0] = usedTextureId;
}
return textures[0];
}
public static int loadTexture(final Bitmap img, final int usedTexId, boolean recyled) {
if(img == null)
return NO_TEXTURE;
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if(recyled)
img.recycle();
return textures[0];
}
public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) {
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if (recycle) {
img.recycle();
}
return textures[0];
}
public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) {
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if (recycle) {
img.recycle();
}
return textures[0];
}
public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) {
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if (recycle) {
img.recycle();
}
return textures[0];
}
@Override
public void texSubImage2D(BasicTexture texture, int xOffset, int yOffset, Bitmap bitmap,
int format, int type) {
int target = texture.getTarget();
mGL.glBindTexture(target, texture.getId());
GLUtils.texSubImage2D(target, 0, xOffset, yOffset, bitmap, format, type);
}
@Override
public void texSubImage2D(BasicTexture texture, int xOffset, int yOffset, Bitmap bitmap,
int format, int type) {
int target = texture.getTarget();
GLES20.glBindTexture(target, texture.getId());
checkError();
GLUtils.texSubImage2D(target, 0, xOffset, yOffset, bitmap, format, type);
}
public static int loadTexture(final Bitmap img, final int usedTexId, boolean recyled) {
if(img == null)
return NO_TEXTURE;
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
// Log.e("MagicCameraView","textureId:" + textures[0]);
// GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,img.getWidth(),img.getHeight(),0,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE,ByteBuffer.wrap(baos.toByteArray()));
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
// GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,img,0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if(recyled)
img.recycle();
return textures[0];
}
public void drawOverlay(float[] viewM) {
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
//Update every second frame (at 30fps)
if(i2==1){
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, UpdateOverdrawValues[0],0,bmp);
UpdateOverdrawLayer=true;
i2=4;
}else{i2-=1;}
GLES20.glUseProgram(mProgram2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]);
GLES20.glEnableVertexAttribArray(maPositionHandle2);
GLES20.glVertexAttribPointer(maPositionHandle2, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]);
GLES20.glEnableVertexAttribArray(maTextureHandle2);
GLES20.glVertexAttribPointer(maTextureHandle2, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, 3 * mBytesPerFloat);
GLES20.glUniform1i(mSamplerLoc, 0);
if(true){
/*
Matrix.multiplyMM(mMVPM, 0, viewM, 0, mOSDModelM, 0);
Matrix.multiplyMM(mMVPM, 0, mProjM, 0, mMVPM, 0);*/
Matrix.multiplyMM(mMVPM,0,mProjM,0,viewM,0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle2, 1, false, mMVPM, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6 * 14);
}
if(enable_height){
Matrix.multiplyMM(mMVPM, 0, viewM, 0, mHeightModelM, 0);
Matrix.multiplyMM(mMVPM, 0, mProjM, 0, mMVPM, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPM, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 6 * 14, 10*6);
}
GLES20.glDisableVertexAttribArray(maPositionHandle2);
GLES20.glDisableVertexAttribArray(maTextureHandle2);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glDisable(GLES20.GL_BLEND);
}
public static int loadTexture(Bitmap paramBitmap, int paramInt, boolean paramBoolean)
{
int[] arrayOfInt = new int[1];
if (paramInt == -1)
{
GLES20.glGenTextures(1, arrayOfInt, 0);
GLES20.glBindTexture(3553, arrayOfInt[0]);
GLES20.glTexParameterf(3553, 10240, 9729.0F);
GLES20.glTexParameterf(3553, 10241, 9729.0F);
GLES20.glTexParameterf(3553, 10242, 33071.0F);
GLES20.glTexParameterf(3553, 10243, 33071.0F);
GLUtils.texImage2D(3553, 0, paramBitmap, 0);
}
else
{
GLES20.glBindTexture(3553, paramInt);
GLUtils.texSubImage2D(3553, 0, 0, 0, paramBitmap);
arrayOfInt[0] = paramInt;
}
if (paramBoolean) {
paramBitmap.recycle();
}
return arrayOfInt[0];
}
public static int loadTextureWithOldTexId(final Bitmap img, final int usedTexId) {
int textures[] = new int[1];
if (usedTexId == GLEtc.NO_TEXTURE) {
return getTextureFromBitmap(img,null);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
return textures[0];
}
@Override
public void texSubImage2D(BasicTexture texture, int xOffset, int yOffset, Bitmap bitmap,
int format, int type) {
int target = texture.getTarget();
GLES20.glBindTexture(target, texture.getId());
checkError();
GLUtils.texSubImage2D(target, 0, xOffset, yOffset, bitmap, format, type);
}
public static int loadTexture2(final Bitmap bitmap) {
final int[] textureHandle = new int[1];
glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0) {
// Bind to the texture in OpenGL
glBindTexture(GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Load the bitmap into the bound texture.
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
// Buffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
// bitmap.copyPixelsToBuffer(byteBuffer);
// glTexImage2D(GL_TEXTURE_2D, 0, GLES20.GL_RGB, bitmap.getWidth(), bitmap.getHeight(),
// 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, byteBuffer);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
} else {
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}
private void setupTextures(TextureAtlas atlas) {
int[] names = new int[1];
glGenTextures(1, names, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, names[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL_TEXTURE_2D, 0, Bitmap.createBitmap(atlas.getWidth(), atlas.getHeight(),
Bitmap.Config.ARGB_8888), 0);
List<TextureAtlas.Region> regions = atlas.getRegions();
textureCoordsCacheArray = new float[regions.size() * 8];
final int k = 8;
float atlasWidth = atlas.getWidth();
float atlasHeight = atlas.getHeight();
for (int i = 0; i < regions.size(); i++) {
TextureAtlas.Region r = regions.get(i);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.bitmap);
float x0 = r.x / atlasWidth;
float y0 = r.y / atlasHeight;
float x1 = x0 + r.bitmap.getWidth() / atlasWidth;
float y1 = y0 + r.bitmap.getHeight() / atlasHeight;
List<Float> coords = Arrays.asList(x0, y0, x0, y1, x1, y1, x1, y0);
if (r.cwRotated) {
Collections.rotate(coords, 2);
}
for (int j = 0; j < coords.size(); j++) {
textureCoordsCacheArray[i * k + j] = coords.get(j);
}
}
}
@Override
public void texSubImage2D(BasicTexture texture, int xOffset, int yOffset, Bitmap bitmap,
int format, int type) {
int target = texture.getTarget();
GLES20.glBindTexture(target, texture.getId());
checkError();
GLUtils.texSubImage2D(target, 0, xOffset, yOffset, bitmap, format, type);
}
public void drawOverlay(float[] viewM) {
Matrix.multiplyMM(mOverdrawMVPM, 0, mProjM, 0, viewM, 0);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
if(UpdateOverdrawLayer){
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D,0,UpdateOverdrawValues[0],0,bmp);
UpdateOverdrawLayer=false;
}
GLES20.glUseProgram(mProgram2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
mTriangleVertices2.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glVertexAttribPointer(maPositionHandle2, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices2);
GLES20.glEnableVertexAttribArray(maPositionHandle2);
mTriangleVertices2.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glVertexAttribPointer(maTextureHandle2, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices2);
GLES20.glEnableVertexAttribArray(maTextureHandle2);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle2, 1, false, mOverdrawMVPM, 0);
int mSamplerLoc = GLES20.glGetUniformLocation (mProgram2,
"s_texture" );
GLES20.glUniform1i(mSamplerLoc, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,6*14);
GLES20.glDisableVertexAttribArray(maPositionHandle2);
GLES20.glDisableVertexAttribArray(maTextureHandle2);
GLES20.glDisable(GLES20.GL_BLEND);
}
@Override
protected void writeTextureToHardware(final GLState pGLState) {
final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat();
final int glInternalFormat = pixelFormat.getGLInternalFormat();
final int glFormat = pixelFormat.getGLFormat();
final int glType = pixelFormat.getGLType();
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null);
final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
/* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */
final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888;
final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
final int textureSourceCount = textureSources.size();
final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener();
for(int i = 0; i < textureSourceCount; i++) {
final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i);
try {
final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
if(bitmap == null) {
throw new NullBitmapException("Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
}
final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888;
if(!useDefaultAlignment) {
/* Adjust unpack alignment. */
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
}
if(preMultipyAlpha) {
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType);
} else {
pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat);
}
if(!useDefaultAlignment) {
/* Restore default unpack alignment. */
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
}
bitmap.recycle();
if(textureStateListener != null) {
textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource);
}
} catch (final NullBitmapException e) {
if(textureStateListener != null) {
textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e);
} else {
throw e;
}
}
}
}
private void uploadToCanvas(final GLCanvas canvas) {
final GL11 gl = canvas.getGLInstance();
final Bitmap bitmap = getBitmap();
if (bitmap != null) {
try {
final int bWidth = bitmap.getWidth();
final int bHeight = bitmap.getHeight();
final int texWidth = getTextureWidth();
final int texHeight = getTextureHeight();
// Upload the bitmap to a new texture.
GLId.glGenTextures(1, sTextureId, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, sTextureId[0]);
gl.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
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);
if (bWidth == texWidth && bHeight == texHeight) {
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
} else {
final int format = GLUtils.getInternalFormat(bitmap);
final int type = GLUtils.getType(bitmap);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, format, texWidth, texHeight, 0, format, type, null);
GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, bitmap, format, type);
}
} finally {
freeBitmap();
}
// Update texture state.
setAssociatedCanvas(canvas);
mId = sTextureId[0];
mState = STATE_LOADED;
mContentValid = true;
} else {
mState = STATE_ERROR;
throw new RuntimeException("Texture load fail, no bitmap");
}
}
public static Texture createTexture(Bitmap bitmap, FlipRenderer renderer, GL10 gl) {
Texture t = new Texture();
t.renderer = renderer;
Assert.assertTrue("bitmap should not be null or recycled",
bitmap != null && !bitmap.isRecycled());
int potW = Integer.highestOneBit(bitmap.getWidth() - 1) << 1;
int potH = Integer.highestOneBit(bitmap.getHeight() - 1) << 1;
t.contentWidth = bitmap.getWidth();
t.contentHeight = bitmap.getHeight();
t.width = potW;
t.height = potH;
if (AphidLog.ENABLE_DEBUG) {
AphidLog.d("createTexture: %d, %d; POT: %d, %d", bitmap.getWidth(), bitmap.getHeight(), potW,
potH);
}
gl.glGenTextures(1, t.id, 0);
gl.glBindTexture(GL_TEXTURE_2D, t.id[0]);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
switch (bitmap.getConfig()) {
case ARGB_8888:
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, potW, potH, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
break;
case ARGB_4444:
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, potW, potH, 0, GL_RGBA,
GL_UNSIGNED_SHORT_4_4_4_4, null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
break;
case RGB_565:
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, potW, potH, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
null);
GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap);
break;
case ALPHA_8:
default:
throw new RuntimeException(
"Unrecognized bitmap format for OpenGL texture: " + bitmap.getConfig());
}
FlipRenderer.checkError(gl);
return t;
}