First off I know this isn’t the place to post this, but the opengl es forum seems a bit dead. I’m trying to port some C code to run on an android device, and what I need is to draw a 2-D array of RGBA integers to the screen. These integers obviously correspond to pixels, and in C I’d do this via glDrawPixels. However in OpenGL es that doesn’t seem to be a possibility, so I’m exploring other options. My current strategy is to draw a square and apply my int array as a texture map, and I can’t seem to figure out why the code below doesn’t work. I’m following a guide I found that I’d gladly post the link to if I could, and I’m using the ClearRenderer class as outlined in an android blog post (that I’ve tried and failed to link 3 times now.)
I hate to just throw my code at you and expect results, but I’m pretty stumped. I’m open to any suggestions re: getting these integers on the screen, but this texturing route is all I can think of. In the code below the array is 200x200, and rather than use an int array I must use Java’s ByteBuffer class. The image is completely red at the moment, though all I see on the screen is a black square.
class ClearRenderer implements GLSurfaceView.Renderer {
private FloatBuffer coords;
private FloatBuffer texCoords;
private ByteBuffer PXA;
private IntBuffer tex;
public ClearRenderer(){
float[] coords = {
0f, 1f, 0f,
1f, 1f, 0f,
0f, 0f, 0f,
1f, 0f, 0f,
};
float[] texC = {
0f, 0f,
1f, 0f,
0f, 1f,
1f, 1f
};
this.coords = makeFloatBuffer(coords);
this.texCoords = makeFloatBuffer(texC);
//Allocate enough space for a 200x200 array of ints
this.PXA = ByteBuffer.allocateDirect(200*200*4);
this.PXA.order(ByteOrder.nativeOrder()).position(0);
//Just fill it with red to test
while (this.PXA.hasRemaining())
this.PXA.putInt(0xFF0000FF);
//Return to start of buffer
this.PXA.position(0);
//We only need one int here
this.tex = IntBuffer.allocate(1);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLU.gluOrtho2D(gl, -5f, 5f, -5f, 5f);
gl.glGenTextures(1, this.tex);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void onDrawFrame(GL10 gl) {
gl.glClearColor(0f, 0f, 0f, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, this.tex.get(0));
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 200, 200, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PXA);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.coords);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, this.texCoords);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
private FloatBuffer makeFloatBuffer(float[] arr){
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(arr);
fb.position(0);
return fb;
}
}