如何通过C ++ OpenGLES显示Android的位图? [英] How to display a bitmap in android through OpenGLES in C++?

查看:222
本文介绍了如何通过C ++ OpenGLES显示Android的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类中实现渲染我的Java层。这个类中有我,我想传递给JNI层位图。 JNI层将负责通过使用OpenGL显示位图。谁能告诉我这可怎么办呢?

更新:
我知道有一个开放的GL方法 glTexImage2D 可用于显示简单的2D图像。谁能告诉我,我该如何使用呢?什么设置,我需要前后调用此方法后做什么?我应该如何通过我的位图到这种方法(从Java到JNI)?

更新2
这是我目前的code:

Java类

 公共类HelloRenderer实现渲染器
{
    上下文_context;
    位图木;
    公共无效SetContext(上下文的背景下)
    {
        _context =背景;
        木= BitmapFactory.de codeResource(_context.getResources(),R.drawable.hello);
    }    @覆盖
    公共无效onDrawFrame(GL10 GL)
    {
        RenderGLStuff(wood.getWidth(),wood.getHeight(),木材);
    }    @覆盖
    公共无效onSurfaceChanged(GL10 GL,诠释的宽度,高度INT)
    {
        InitGLStuff(wood.getWidth(),wood.getHeight(),木材);    }    @覆盖
    公共无效onSurfaceCreated(GL10 GL,EGLConfig配置)
    {    }    私有静态本地无效InitGLStuff(INT宽度,高度INT,位图PTR);
    私有静态本地无效RenderGLStuff(INT宽度,高度INT,位图PTR);    静止
    {
        的System.loadLibrary(myglstuff);
    }
}

JNI方法

  JNIEXPORT无效JNICALL Java_mine_NativeGL_HelloRenderer_InitGLStuff(JNIEnv的* ENV,jobject OBJ,诠释的宽度,高度INT,无效* PTR)
{
   glViewport(0,0,宽度,高度);
   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无效JNICALL Java_mine_NativeGL_HelloRenderer_RenderGLStuff(JNIEnv的* ENV,jobject OBJ,诠释的宽度,高度INT,无效* PTR)
{
    GLuint质感;
    glGenTextures(1,&安培;纹理);
    glBindTexture(GL_TEXTURE_2D,纹理);
    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,宽度,高度,0,GL_RGBA,GL_UNSIGNED_BYTE,&放大器; PTR);
}

更新3
我已经更新了我的家乡code这个样子,现在我能看到的地方形象的白色方块,但没有图像的内容。任何想法?

  JNIEXPORT无效JNICALL Java_mine_NativeGL_HelloRenderer_RenderGLStuff(JNIEnv的* ENV,jobject OBJ,诠释的宽度,高度INT,无效* PTR)
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);    GLfloat顶点[] = {-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,};
    GLfloat法线[] = {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质感;
    glGenTextures(1,&安培;纹理);
    glBindTexture(GL_TEXTURE_2D,纹理);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,宽度,高度,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,纹理);
    glVertexPointer(2,GL_FLOAT,0,顶点);
    glNormalPointer(GL_FLOAT,0,法线);
    glTexCoordPointer(2,GL_FLOAT,0,textureCoords);
    (4,8 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);
}


解决方案

您不能直接传递一个java Bitmap类的指针,并期望它与glTexImage2D工作。

glTexImage2D期望原始像素数据中的指针进行传递。 (其实你JNI本机C函数声明如下错误,最后一个参数应该是类型jobject,而不是无效*。使用JAVAH来生成正确的JNI函数签名)

您需要从位图使用bitmap.getPixels(..)或bitmap.copyPixelsToBuffer(..)召唤出获得这些数据,这取决于你想要的像素格式转换与否。

要看到它在Java中怎么做的,看code在这里有个例子:
<一href=\"http://stackoverflow.com/questions/6234642/solvedproblem-applying-texture-to-cube-different-texture-on-each-face-of-cube\">[SOLVED]problem运用纹理立方,不同的纹理立方体的每个面上

的ByteBuffer fcbuffer是保持原始像素数据的变量。你应该能够调用ByteBuffer.array()来获取实际的数据指针并把它传递到JNI。

编辑:要清楚,你的RenderGLStuff应该有类型为byte []的最后一个参数,并从JNI / C-端你应该呼吁通过jbyteArray类型变量GetByteArrayElements / ReleaseByteArrayElements。

EDIT2:要清透,林不知道你的限制是什么,但也有其他的解决方案,纹理创作的问题


  • 您可以做在Android上侧的纹理生成调用(GLUtils.texImage2D直接采用位图)


  • 您可以使用任何本机映像库在本机端文件加载纹理


  • 如果你必须通过位图从Java对象了,你所描述获得原始数据指针


有关的不同的方法了长时间的讨论,看到这个线程<一个href=\"http://groups.google.com/group/android-ndk/browse_thread/thread/eaf038cc50d5041e/c5874712be5382cf\" rel=\"nofollow\">http://groups.google.com/group/android-ndk/browse_thread/thread/eaf038cc50d5041e/c5874712be5382cf

EDIT3:我刚才了解到,还有另一种方法

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: [SOLVED]problem 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

这篇关于如何通过C ++ OpenGLES显示Android的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆