Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create texture from byte array in OpenGL, Android

I am trying to display a byte array in an OpenGL GLSurfaceView.

So I have a GLRendererclass implementing Renderer and a method onSurfaceCreated

byte[] data = new byte[512*512];
            for (int i = 0; i < 512*512; i++) {
                data[i] = 100;
            }
            ByteBuffer buffer = ByteBuffer.allocateDirect(512*512);
            buffer.put(data);
            buffer.position(0);

            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 512, 512, 0,
                    GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);

But nothing is displayed on the screen. FYI, there is no special code in onSurfaceChanged and onDrawFrame method.

like image 839
epsilones Avatar asked Feb 20 '26 20:02

epsilones


1 Answers

The format and data size don't match. GL_RGBA takes up 4*sizeof(type) per texel so you need to scale the buffer accordingly. However, if you only want a single channel texture, use GL_RED instead.

like image 86
pleluron Avatar answered Feb 23 '26 10:02

pleluron