从openGL类中调用外部方法

时间:2010-07-19 15:43:08

标签: android

我正在尝试从openGL线程调用一个我正在工作的类之外的方法但是我得到了一个Can't create handler inside thread that has not called Looper.prepare()运行时异常。有没有人知道如何解决这个问题,而不是把它                  在同一类中的方法?

程序中断@ Extract cam = new Extract();

        public void onDrawFrame(GL10 gl) {
    onDrawFrameCounter++;
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    bindCameraTexture(gl);
    System.out.println(Global.bearing);
    float bear = Global.bearing;
    gl.glLoadIdentity();
    gl.glNormal3f(0,0,1);
    System.out.println("ARRAY!: " + GLCamTest.array.length);
    p = 0;
    gl.glRotatef(bear, 1, 0, 0);
    int q = 0;
    int e = 0;  
    if(q < Global.cubes){

                Extract cam = new Extract();
                Bitmap image = cam.extractimage(q);
                final int TILE_WIDTH = 512;
                final int TILE_HEIGHT = 512;
                final int TILE_SIZE = TILE_WIDTH * TILE_HEIGHT;

                int[] pixels = new int[TILE_WIDTH];

                short[] rgb_565 = new short[TILE_SIZE];

                // Convert 8888 to 565, and swap red and green channel position:

                int i = 0;
                for (int y = 0; y < TILE_HEIGHT; y++) {
                    image.getPixels(pixels, 0, TILE_WIDTH, 0, y, TILE_WIDTH,
        1);
                    for (int x = 0; x < TILE_WIDTH; x++) {
                        int argb = pixels[x];
                        int r = 0x1f & (argb >> 19); // Take 5 bits from23..19
                        int g = 0x3f & (argb >> 10); // Take 6 bits from15..10
                        int b = 0x1f & (argb >> 3); // Take 5 bits from  7.. 3
                        int rgb = (r << 11) | (g << 5) | b;
                        rgb_565[i] = (short) rgb;
                        ++i;
                    }
                }
                ShortBuffer textureBuffer = ShortBuffer.wrap (rgb_565, 0,
        TILE_SIZE);
                gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, 48, 48, 0,
                        GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5,
        textureBuffer);

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e + 4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e + 4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e + 4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e + 4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e + 4;
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e + 4;
    q++;
    }

}

1 个答案:

答案 0 :(得分:2)

错误表明您正在调用一些期望从UI线程调用的代码,但事实并非如此。不会从UI线程调用gl onDrawFrame,因此会出错。

从代码中看起来这将在Extract类的构造函数中发生。班级全名是什么?

假设Extract方法从相机或类似物品加载图像。我会移动代码从gl线程中“提取”,你可能不想通过在gl线程上进行这种转换来限制你的fps :-)它更好地在一个单独的线程上“尽可能快地”做这个,并让gl绘制最新的。

如果您确实需要在此处开展此工作,请使用Handler