开发者

How to draw a bitmap on Android using OpenGL

I have a byte array of RGB values, just like the contents(without header) of a *.bmp file. What I want to do is, draw the corresponding bitmap on Android, using OpenGL.

It seems that OpenGL ES doesn't have a single API that will do, it true?

If true开发者_如何学Go, how can I do this?

PS: Actually, I can draw it in JAVA code, but it's too slow and costs too much CPU time. So I want to try drawing it with OpenGL. Is there any other advises? Or maybe OpenGL can't be the right answer?

Thank you all!


bmp is a really bad format to start off with, you should use png files. They are smaller and offer the same quality. They're also easy to work with using openGL.

You can easily do it in Android. You'll want to look into texture loading. Here's a link: http://qdevarena.blogspot.com/2009/02/how-to-load-texture-in-android-opengl.html


That's a very general question, you can find lots of Android OpenGL ES code samples on the internet.

A good start however would be the Google IO talks on writing real time games for Android:

http://code.google.com/events/io/2009/sessions/WritingRealTimeGamesAndroid.html

http://www.androidphonethemes.com/google-io-2010-writing-real-time-games-for-android-redux

See page 23 to 25 of the first presentation I just linked for a comparison of the existing different Canvas and OpenGL ES drawing methods in Android.


I'm not sure the answers you got were related to the question you asked. I believe I had the same question, however I was not interested in drawing a bitmap on the screen, I was rather interested in creating (drawing on) a bitmap fast, using GL instead of java computations. Since I could find no reply to my question anywhere on the web I just started reading the GL API's and the way I solved my problem was by reading the pixels drawn by my onDrawFrame method with glReadPixels after turning on a 'grabdata' flag, somewhere else in the program, something like this:

// imports
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Bitmap;
import android.opengl.GLES20;


//somewhere inside the class that implements GLSurfaceView.Renderer:
    
ByteBuffer data;
private int height;
private int width;
boolean grabdata=false;
    
public void onSurfaceChanged(GL10 glUnused, int width, int height)  {
this.width=width;
this.height=height;
data=ByteBuffer.allocateDirect(width*height*4).order(ByteOrder.nativeOrder());
/*
......
prepare the surface
.......                  
*/
}

public void onDrawFrame(GL10 glUnused) {

/*
actually draw whatever you want, set matrices, rotate, draw etc... 
drawTriangle(mTriangleVertices);
.
. 
.
*/
if (grabdata) {
 GLES20.glReadPixels(0,0,width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,data);
 grabdata=false; //until we want to read again
 //create a bitmap now if you havent prepared it somewhere else.
 Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
 Bitmap bitmap = Bitmap.createBitmap(width, height, conf);
 bitmap.copyPixelsFromBuffer(data);
 //do something with it....
 // storeImage(bitmap);
 data.clear();
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜