Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a OpenGL texture from byte array in Android

I am new to OpenGL all together. I am making a color blind app on Android for academic purposes. I have all the code needed in the shader to do the manipulations I need, but I still have to get the shader to be able to see the data I create in my Java code. In my Java code, I ended up with a large int array of size 262144. I tried to pass this array into the shader any way I could, but it was too large. I have been informed that I can pass this much information easily into the shader if I turn my data into a texture.

I have converted my int array into a byte array as instructed by a colleague, but now I am lost as to how I create the opengl texture and input my byte data into it for the shader to see and use.

So, I need to create a opengl texture, fill it with my data (is byte array best?) and then be able to use that texture in my shader. If anyone can help me out, it would be much appreciated. I work well after seeing examples, not explanations because I am very opengl illiterate but I welcome any help.

I did try to code some. I ended up trying to make a GLES20.glTexImage2D. I figured I could just pass in my byte array, but it takes a buffer?? Please give me all the steps necessary to complete my task.

Thanks a lot!

Mike

like image 710
MikeShiny Avatar asked Jan 12 '13 04:01

MikeShiny


1 Answers

I think you need to do following;

byte data[4 * 262144];  // your byte array, 4 * int count
ByteBuffer buffer = ByteBuffer.allocateDirect(4 * 262144);
buffer.put(data);
buffer.position(0);

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
                    GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
like image 70
harism Avatar answered Oct 14 '22 03:10

harism