如何在opengl android中绘制图像

发布于 2024-12-10 03:26:25 字数 5630 浏览 2 评论 0原文

我必须使用此代码将位图绘制到我的动态壁纸中,但图像不会加载其显示空指针异常。

public class Square {

    private FloatBuffer vertexBuffer; // buffer holding the vertices
    private float vertices[] = { -1.0f, -1.0f, 0.0f, // V1 - bottom left
            -1.0f, 1.0f, 0.0f, // V2 - top left
            1.0f, -1.0f, 0.0f, // V3 - bottom right
            1.0f, 1.0f, 0.0f // V4 - top right
    };

    private FloatBuffer textureBuffer; // buffer holding the texture coordinates
    private float texture[] = {
    // Mapping coordinates for the vertices
            0.0f, 1.0f, // top left (V2)
            0.0f, 0.0f, // bottom left (V1)
            1.0f, 1.0f, // top right (V4)
            1.0f, 0.0f // bottom right (V3)
    };

    /** The texture pointer */
    int[] textures = new int[1];

    public Square() {

        // a float has 4 bytes so we allocate for each coordinate 4 bytes
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());

        // allocates the memory from the byte buffer
        vertexBuffer = byteBuffer.asFloatBuffer();

        // fill the vertexBuffer with the vertices
        vertexBuffer.put(vertices);

        // set the cursor position to the beginning of the buffer
        vertexBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuffer.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);
    }

    /**
     * Load the texture for the square
     * 
     * @param gl
     * @param context
     */
    public void loadGLTexture(GL10 gl, Context context) {
        System.out.println("loaded");
        // // loading texture
         Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
         R.drawable.nehe);
//here is the problem when ever i command bitmap it will be working .with out command its //showing null pointer exception   
        // generate one texture pointer
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR);

        // Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
                GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                GL10.GL_REPEAT);

        // Use Android GLUtils to specify a two-dimensional texture image from
        // our bitmap
         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        // //
        // // // Clean up
         bitmap.recycle();
    }

    /** The draw method for the square with the GL context */
    public void draw(GL10 gl) {
        // System.out.println("draw");
        // bind the previously generated texture
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // Point to our buffers
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // Set the face rotation
        gl.glFrontFace(GL10.GL_CW);

        // Point to our vertex buffer
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        // Draw the vertices as triangle strip
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

        // Disable the client state before leaving
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }
}

这是我的 logcat 输出,

10-14 11:43:43.118: WARN/dalvikvm(3050): threadid=8: thread exiting with uncaught exception (group=0x4001d868)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): FATAL EXCEPTION: GLThread 9
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): java.lang.NullPointerException
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at frankandrobot.glwallpapervideodemo.com.Square.loadGLTexture(Square.java:73)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at frankandrobot.glwallpapervideodemo.com.VideoRenderer.onSurfaceCreated(VideoRenderer.java:77)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at net.rbgrn.android.glwallpaperservice.GLThread.guardedRun(GLWallpaperService.java:664)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at net.rbgrn.android.glwallpaperservice.GLThread.run(GLWallpaperService.java:538)
10-14 11:43:56.398: WARN/Email(1452): Exception detected: Read error: I/O error during system call, Connection reset by peer
10-14 11:43:56.398: WARN/Email(1452): Last network activities:
10-14 11:43:56.398: WARN/Email(1452): * OK Gimap ready for requests from 115.111.177.222 g2if3342858pbc.293
10-14 11:43:56.398: WARN/Email(1452): 1 CAPABILITY
10-14 11:43:56.398: WARN/Email(1452): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
10-14 11:43:56.398: WARN/Email(1452): 1 OK Thats all she wrote! g2if3342858pbc.293
10-14 11:43:56.398: WARN/Email(1452): 2 ID ("name" "com.android.email" "os" "android" "os-version" "2.2; FRF91" "vendor" "nvidia" "x-android-device-model" "INB-10/n" "AGUID" "+TIbwSeLDHum85UXux4T+QJD51g=")

需要一些帮助来完成这项工作。请指导我一个简单的方法,我是 android 和 opengl 的新手......

i have to draw bitmap in to the my live wallpaper using this code but image will not loaded its showing null pointer exception.

public class Square {

    private FloatBuffer vertexBuffer; // buffer holding the vertices
    private float vertices[] = { -1.0f, -1.0f, 0.0f, // V1 - bottom left
            -1.0f, 1.0f, 0.0f, // V2 - top left
            1.0f, -1.0f, 0.0f, // V3 - bottom right
            1.0f, 1.0f, 0.0f // V4 - top right
    };

    private FloatBuffer textureBuffer; // buffer holding the texture coordinates
    private float texture[] = {
    // Mapping coordinates for the vertices
            0.0f, 1.0f, // top left (V2)
            0.0f, 0.0f, // bottom left (V1)
            1.0f, 1.0f, // top right (V4)
            1.0f, 0.0f // bottom right (V3)
    };

    /** The texture pointer */
    int[] textures = new int[1];

    public Square() {

        // a float has 4 bytes so we allocate for each coordinate 4 bytes
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());

        // allocates the memory from the byte buffer
        vertexBuffer = byteBuffer.asFloatBuffer();

        // fill the vertexBuffer with the vertices
        vertexBuffer.put(vertices);

        // set the cursor position to the beginning of the buffer
        vertexBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuffer.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);
    }

    /**
     * Load the texture for the square
     * 
     * @param gl
     * @param context
     */
    public void loadGLTexture(GL10 gl, Context context) {
        System.out.println("loaded");
        // // loading texture
         Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
         R.drawable.nehe);
//here is the problem when ever i command bitmap it will be working .with out command its //showing null pointer exception   
        // generate one texture pointer
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR);

        // Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
                GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                GL10.GL_REPEAT);

        // Use Android GLUtils to specify a two-dimensional texture image from
        // our bitmap
         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        // //
        // // // Clean up
         bitmap.recycle();
    }

    /** The draw method for the square with the GL context */
    public void draw(GL10 gl) {
        // System.out.println("draw");
        // bind the previously generated texture
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // Point to our buffers
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // Set the face rotation
        gl.glFrontFace(GL10.GL_CW);

        // Point to our vertex buffer
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        // Draw the vertices as triangle strip
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

        // Disable the client state before leaving
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }
}

this is my logcat output

10-14 11:43:43.118: WARN/dalvikvm(3050): threadid=8: thread exiting with uncaught exception (group=0x4001d868)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): FATAL EXCEPTION: GLThread 9
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): java.lang.NullPointerException
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at frankandrobot.glwallpapervideodemo.com.Square.loadGLTexture(Square.java:73)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at frankandrobot.glwallpapervideodemo.com.VideoRenderer.onSurfaceCreated(VideoRenderer.java:77)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at net.rbgrn.android.glwallpaperservice.GLThread.guardedRun(GLWallpaperService.java:664)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050):     at net.rbgrn.android.glwallpaperservice.GLThread.run(GLWallpaperService.java:538)
10-14 11:43:56.398: WARN/Email(1452): Exception detected: Read error: I/O error during system call, Connection reset by peer
10-14 11:43:56.398: WARN/Email(1452): Last network activities:
10-14 11:43:56.398: WARN/Email(1452): * OK Gimap ready for requests from 115.111.177.222 g2if3342858pbc.293
10-14 11:43:56.398: WARN/Email(1452): 1 CAPABILITY
10-14 11:43:56.398: WARN/Email(1452): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
10-14 11:43:56.398: WARN/Email(1452): 1 OK Thats all she wrote! g2if3342858pbc.293
10-14 11:43:56.398: WARN/Email(1452): 2 ID ("name" "com.android.email" "os" "android" "os-version" "2.2; FRF91" "vendor" "nvidia" "x-android-device-model" "INB-10/n" "AGUID" "+TIbwSeLDHum85UXux4T+QJD51g=")

need some help to do this stuff.kindly guide me a easy way i'm new to android and opengl....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-12-17 03:26:25

尝试将 nehe.png (或 jpg 或其他)移动到 res/raw,然后使用以下内容:

InputStream inStream = context.getResources().openRawResource(R.raw.nehe);
Bitmap bitmap = BitmapFactory.decodeStream(inStream);

显然替换:

位图位图 =
BitmapFactory.decodeResource(context.getResources(),R.drawable.nehe);

Try moving the nehe.png (or jpg or whatever) to res/raw instead and then use the following:

InputStream inStream = context.getResources().openRawResource(R.raw.nehe);
Bitmap bitmap = BitmapFactory.decodeStream(inStream);

obviously replacing:

Bitmap bitmap =
BitmapFactory.decodeResource(context.getResources(),R.drawable.nehe);

北风几吹夏 2024-12-17 03:26:25

你能告诉我们哪一行的编号是 73 吗?

我猜您没有初始化 textures 变量(例如):

 int[] textures = new int[1];

Can you tell us which line is numbered 73?

I guess that you didn't initialized textures variable (for example):

 int[] textures = new int[1];
一笑百媚生 2024-12-17 03:26:25

有同样的问题,它是在渲染类中,而不是在 loadGLTexture 中。出现错误,因为动态壁纸中的其他位置未正确定义/调用“上下文上下文”。
验证渲染器类是否具有上下文,例如:

...
Private Square square;
Private Context context;

对于渲染器类,还需要放入渲染器方法,例如:

public Myrenderer(Context context){

   this.context=context;
   square = new Square();
}

对于壁纸服务,例如:

renderer = new render(getBaseContext());

您还可以定义 setContext 方法来定义壁纸服务的上下文

renderer = new MyRenderer();
renderer.setContext(getBaseContext());
setRenderer(renderer);

:工作代码: https://github.com/GLWallpaperService/GLWallpaperService/pull/7

Had the same problem and it was in the render class, not in the loadGLTexture. Was an error because the "Context context" is not properly defined/called everywhere else in the live wallpaper.
Verify that the renderer class has a context e.g:

...
Private Square square;
Private Context context;

for the renderer class also need to put in the renderer method something like:

public Myrenderer(Context context){

   this.context=context;
   square = new Square();
}

and for the wallpaper service something like:

renderer = new render(getBaseContext());

You can also define a setContext method to define the context from the wallpaper service:

renderer = new MyRenderer();
renderer.setContext(getBaseContext());
setRenderer(renderer);

a working code : https://github.com/GLWallpaperService/GLWallpaperService/pull/7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文