Loading...
Logo
Processing Forum
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...

Copy code
  1. import javax.media.opengl.GL2;
  2. import java.nio.*;
  3. import java.nio.ByteBuffer;
  4. import java.nio.MappedByteBuffer;

  5. PGraphicsOpenGL pgl;
  6. GL2 gl;
  7. ByteBuffer bb;
  8. int[] pboIds;
  9. boolean buffReady;

  10. void setup() {
  11.   size(300,300,OPENGL);
  12.   pgl = (PGraphicsOpenGL) g;
  13.   pboIds = new int[2];
  14.   buffReady = false;
  15. }

  16. void draw() {
  17.   println(frameCount);
  18.   background(0);
  19.   
  20.   if (!buffReady) {
  21.     println("buffer not ready, building it now...");
  22.     gl = pgl.beginPGL().gl.getGL2();
  23.     
  24.     //set the way the data is packed in and unpacked from memory - 1=byte alignment
  25.     //see http://samplecodebank.blogspot.co.uk/2011/06/glpixelstorei-example-c-c-java-objc.html
  26.     gl.glPixelStorei(GL2.GL_PACK_ALIGNMENT, 1);
  27.     gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);
  28.     
  29.     // generate two buffer handles and store them int he pboIds array - what the third argument 
  30.     // is is anyones guess. http://samplecodebank.blogspot.co.uk/2011/05/glgenbuffers-example-c-c.html
  31.     gl.glGenBuffers(2, pboIds, 0);
  32.     
  33.     // bind a PIXEL_UNPACK_BUFFER to one of the two ids and then populate it with data. In this case
  34.     // the buffer size is 4*width*height) for storing 4-byte integers for each pixel in the sketch
  35.     // window. The array is populated with nulls. The last arg of glBufferData is the draw mode
  36.     // this can be on of a few things depending on whats being done with the data.
  37.     // http://samplecodebank.blogspot.co.uk/2011/05/glbindbuffer-example-c-c.html
  38.     // http://samplecodebank.blogspot.co.uk/2011/05/glbufferdata-example-c-c.html
  39.     gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
  40.     gl.glBufferData(GL2.GL_PIXEL_UNPACK_BUFFER, width*height*4, null, GL2.GL_STREAM_DRAW);
  41.     
  42.     // do the same with the second buffer
  43.     gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[1]);
  44.     gl.glBufferData(GL2.GL_PIXEL_UNPACK_BUFFER, width*height*4, null, GL2.GL_STREAM_DRAW);
  45.     
  46.     // get the fist of the buffers, map it to a byte array so we can edit it directly and the
  47.     // add some ints to it.
  48.     gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
  49.     bb = gl.glMapBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, GL2.GL_WRITE_ONLY);
  50.     //FloatBuffer floatbuffer = bb.order( ByteOrder.nativeOrder() ).asFloatBuffer();
  51.     
  52.     println("buffer created and mapped to a ByteBuffer. Time to add data! ...");
  53.     
  54.     int thisint = 1;
  55.     for (int i=0; i<width*height; i++) {
  56.       if (i>300) {
  57.         thisint = 300;
  58.       }
  59.       bb.putInt(thisint);
  60.     }
  61.             
  62.     // unmap the buffer and bind it back to an empty value (which apparently resets stuff!)
  63.     println("done.");
  64.     gl.glUnmapBuffer(GL2.GL_PIXEL_UNPACK_BUFFER);
  65.     gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, 0);
  66.     
  67.     println("buffer ready!");
  68.     buffReady = true;
  69.   }
  70.   
  71.   gl.glBindBuffer(GL2.GL_PIXEL_UNPACK_BUFFER, pboIds[0]);
  72.   gl.glDrawPixels(width, height, GL2.GL_COLOR_INDEX, GL2.GL_INT, pboIds[0]);
  73.   gl.glBindBuffer( GL2.GL_PIXEL_UNPACK_BUFFER, 0 );

Replies(1)

aha! I have solved the weird error. Changing line 76 to 
Copy code
  1. gl.glDrawPixels(width, height, GL2.GL_RED, GL2.GL_UNSIGNED_BYTE, pboIds[0]);
resolves the error and I get a successful render... almost. What I get is verticle red stripes... not exactly what I expected. Clearly there is something else going on here I don't understand...