开发者

How to display a bitmap in android through OpenGLES in C++?

I have a class in my Java layer which implement Renderer. Inside this class I have a bitmap which I want to pass to JNI layer. JNI layer would be responsible for displaying bitmap by using OpenGL. Can anyone tell me how this can be done?

Update: I know there is an open gl method glTexImage2D which can be used to display a simple 2D image. Can anyone tell me how can I use it? what setup I need to do before and after calling this method? How should I pass my bitmap to this method (from java to JNI)?

Update 2 This is my current code:

Java class

public class HelloRenderer implements Renderer
{
    Context _context;
    Bitmap wood;
    public void SetContext(Context context)
    {
        _context = context;
        wood = BitmapFactory.decodeResource( _context.getResources(), R.drawable.hello);
    }

    @Override
    public void onDrawFrame(GL10 gl) 
    {
        RenderGLStuff(wood.getWidth(), wood.getHeight(), wood);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) 
    {
        InitGLStuff(wood.getWidth(), wood.getHeight(), wood);

    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {

    }

    private static native void InitGLStuff(int width, int height, Bitmap  ptr);
    private static native void RenderGLStuff(int width, int height, Bitmap  ptr);

    static
    {
        System.loadLibrary("myglstuff");
    }
}

JNI methods

JNIEXPORT void JNICALL Java_mine_NativeGL_HelloRenderer_InitGLStuff(JNIEnv * env, jobject obj, int width, int height, void * ptr )
{
   glViewport(0, 0, width, height);
   glClearColorx((GLfixed)(0.1f * 65536), (GLfixed)(0.2f * 65536), (GLfixed)(0.3f * 65536), 0x10000);
   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

}

 JNIEXPORT void JNICALL Java_mine_NativeGL_HelloRenderer_RenderGLStuff(JNIEnv * env, jobject obj, int width, int height, void * ptr)
{
    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &ptr);  
}

Update 3 I have updated my native code like this, now I can see white box in place of image but no content of image. Any ideas?

JNIEXPORT void JNICALL Java_mine_NativeGL_HelloRenderer_RenderGLStuff(JNIEnv * env, jobject obj, int width, int height, void * ptr)
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    GLfloat vertices[] = {  -1.0, 1.0,  1.0, 1.0,  -1.0, -1.0,   1.0, -1.0, }; 
    GLfloat normals[] =  {   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0,   0.0, 0.0, 1.0 }; 
    GLfloat textureCoords[] = {   0.0, 0.0,   1.0, 0.0,   0.0, 1.0,   1.0, 1.0 }; 

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 开发者_高级运维0, GL_RGBA, GL_UNSIGNED_BYTE, &ptr);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  

    glBindTexture(GL_TEXTURE_2D, texture);
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glNormalPointer(GL_FLOAT, 0, normals);
    glTexCoordPointer(2, GL_FLOAT, 0, textureCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-5.0, 5.0, -7.5, 7.5, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}


You cannot pass a java Bitmap class pointer directly and expect it to work with glTexImage2D.

glTexImage2D expects raw pixel data to be passed in the pointer. ( in fact your JNI native C function declaration looks wrong, last parameter should be of type jobject, not void * . use "javah" to generate correct JNI function signatures )

You need to get that data out from the bitmap using bitmap.getPixels(..) or bitmap.copyPixelsToBuffer(..) call, depending if you want the pixel format converted or not.

To see how its done in Java, look at the code in here for example: applying texture to Cube, different texture on each face of cube

ByteBuffer fcbuffer is the variable that holds the raw pixel data. You should be able to call ByteBuffer.array() to obtain the actual data pointer and pass it down to JNI.

EDIT : to be clear, your RenderGLStuff should have the last parameter of type byte[], and from JNI/C- side you are supposed to call GetByteArrayElements/ReleaseByteArrayElements on the passed jbyteArray type variable.

EDIT2: to be perfectly clear, im not sure what your constraints are, but there are other solutions to your texture creation problem

  • you can do the texture generation call on Android side ( GLUtils.texImage2D that takes a Bitmap directly )

  • you can load textures from files on native side using any native image library

  • if you MUST pass Bitmap objects down from Java, you have to get to the raw data pointer as described

For a lengthy discussion of the different approaches, see this thread http://groups.google.com/group/android-ndk/browse_thread/thread/eaf038cc50d5041e/c5874712be5382cf

EDIT3: i have just learned that there is yet another approach

  • pass down bitmap object as you did, and use jnigraphics library to obtain raw access to pixels as described here What is JNI Graphics or how to use it?
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜