I'm trying to figure out how to write to and draw from opengl pixel buffer objects in a processing sketch, but 1) I'm a complete novice to opengl coding and 2) my code is not working, but not failing catastrophically and producing a weird error. I'd really appreciate it if anyone can throw some light on this....
The source code is below. When I run it I get the not-particularly-informative error "OpenGL error 1282 at top endDraw(): invalid operation", the screen open and the sketch keeps running, but doesn't draw anything. I was hoping for a screen with a back bit on top and a red bit below...
- import javax.media.opengl.GL2;
- import java.nio.*;
- import java.nio.ByteBuffer;
- import java.nio.MappedByteBuffer;
- PGraphicsOpenGL pgl;
- GL2 gl;
- ByteBuffer bb;
- int[] pboIds;
- boolean buffReady;
- void setup() {
- size(300,300,OPENGL);
- pgl = (PGraphicsOpenGL) g;
- pboIds = new int[2];
- buffReady = false;
- }
- void draw() {
- println(frameCount);
- background(0);
- if (!buffReady) {
- println("buffer not ready, building it now...");
- gl = pgl.beginPGL().gl.getGL2();
- //set the way the data is packed in and unpacked from memory - 1=byte alignment
- //see http://samplecodebank.blogspot.co.uk/2011/06/glpixelstorei-example-c-c-java-objc.html
- gl.glPixelStorei(GL2.GL_PACK_ALIGNMENT, 1);
- gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);
- // generate two buffer handles and store them int he pboIds array - what the third argument
- // is is anyones guess. http://samplecodebank.blogspot.co.uk/2011/05/glgenbuffers-example-c-c.html
- gl.glGenBuffers(2, pboIds, 0);
- // bind a PIXEL_UNPACK_BUFFER to one of the two ids and then populate it with data. In this case
- // the buffer size is 4*width*height) for storing 4-byte integers for each pixel in the sketch
- // window. The array is populated with nulls. The last arg of glBufferData is the draw mode
- // this can be on of a few things depending on whats being done with the data.
- // http://samplecodebank.blogspot.co.uk/2011/05/glbindbuffer-example-c-c.html
- // http://samplecodebank.blogspot.co.uk/2011/05/glbufferdata-example-c-c.html
- gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
- gl.glBufferData(GL2.GL_PIXEL_UNPACK_BUFFER, width*height*4, null, GL2.GL_STREAM_DRAW);
- // do the same with the second buffer
- gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[1]);
- gl.glBufferData(GL2.GL_PIXEL_UNPACK_BUFFER, width*height*4, null, GL2.GL_STREAM_DRAW);
- // get the fist of the buffers, map it to a byte array so we can edit it directly and the
- // add some ints to it.
- gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
- bb = gl.glMapBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, GL2.GL_WRITE_ONLY);
- //FloatBuffer floatbuffer = bb.order( ByteOrder.nativeOrder() ).asFloatBuffer();
- println("buffer created and mapped to a ByteBuffer. Time to add data! ...");
- int thisint = 1;
- for (int i=0; i<width*height; i++) {
- if (i>300) {
- thisint = 300;
- }
- bb.putInt(thisint);
- }
- // unmap the buffer and bind it back to an empty value (which apparently resets stuff!)
- println("done.");
- gl.glUnmapBuffer(GL2.GL_PIXEL_UNPACK_BUFFER);
- gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, 0);
- println("buffer ready!");
- buffReady = true;
- }
- gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
- gl.glDrawPixels(width, height, GL2.GL_COLOR_INDEX, GL2.GL_INT, pboIds[0]);
- gl.glBindBuffer( GL2.GL_PIXEL_UNPACK_BUFFER, 0 );
- }
1