Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays
in
Core Library Questions
•
1 year ago
I am attempting to implement the following
example on the Processing wiki in the newest version of Processing 2.0b3 running on Ubuntu Linux 32-bit.
I am trying to run a 'stress test' in Processing 2.0 to see how many data points we can use in a 3D data visualization. Using just a PShape or a textured image has still been too slow. If this cannot work fast enough in Processing, we will likely have to turn to C and OpenGL.
The example here should simply draw a million randomly colored points on the screen using vertex arrays. All of the
OpenGL examples on this site are for older versions of Processing, which is where I am struggling.
This post guided me how to properly set up the JOGL interface to call OpenGL2 commands. This correctly uses OpenGL2 commands to draw on the screen.
After making those adjustments in the code and removed unnecessary imports, I ran into an issue with the BufferUtil class not being found (or else invalid imports to it). I do not know enough about the changes to JOGL to ascertain a solution to this issue.
I tried removing the reference to the BufferUtil class, replacing it with what I thought was a proper implementation to create the FloatBuffer, which you can see in the code below, lines 35 - 45. While running, I was getting the following error in the Processing IDE:
Exception in thread "Animation Thread" java.lang.UnsupportedOperationExceptionat java.nio.FloatBuffer.array(FloatBuffer.java:940)at java.nio.FloatBuffer.array(FloatBuffer.java:240)at com.jogamp.common.nio.Buffers.getArray(Buffers.java:351)at jogamp.opengl.gl4.GL4bcImpl.glVertexPointer(GL4bcImpl.java:33134)at temp.setup(temp.java:91)at processing.core.PApplet.handleDraw(PApplet.java:2103)at processing.opengl.PGL$PGLListener.display(PGL.java:2593)at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:189)at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:177)at javax.media.opengl.awt.GLCanvas$DisplayAction.run(GLCanvas.java:928)at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:425)at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:364)at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:945)at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646)at java.awt.EventQueue.access$000(EventQueue.java:84)at java.awt.EventQueue$1.run(EventQueue.java:607)at java.awt.EventQueue$1.run(EventQueue.java:605)at java.security.AccessController.doPrivileged(Native Method)at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)at java.awt.EventQueue.dispatchEvent(EventQueue.java:616)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import java.nio.*;
- // let's try 1,000,000 points
- int numPoints = 1000000;
- // pan, zoom and rotate
- float tx = 0, ty = 0;
- float sc = 1;
- float a = 0.0;
- // you'd better leave these here
- // otherwise they get garbage collected after setup
- // and everything will crash
- FloatBuffer vbuffer;
- FloatBuffer cbuffer;
- void setup() {
- size(displayWidth/2, displayHeight/2, OPENGL);
- smooth();
- // JOGL for Processing 2.0
- PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
- PGL pgl = pg.beginPGL();
- GL gl = pgl.gl;
- GL2 gl2 = pgl.gl.getGL2();
- // JOGL for Processing 1.5
- //PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
- //GL gl = pgl.beginGL(); // always use the GL object returned by beginGL
- // Float Buffers for Processing 1.5
- //vbuffer = BufferUtil.newFloatBuffer(numPoints * 2);
- //cbuffer = BufferUtil.newFloatBuffer(numPoints * 3);
- // Float Buffers for Processing 2.0
- int vSize = (numPoints * 2);
- int cSize = (numPoints * 3);
- vSize = vSize << 2;
- cSize = cSize << 2;
- vbuffer = ByteBuffer.allocate(vSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
- cbuffer = ByteBuffer.allocate(cSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
- for (int i = 0; i < numPoints; i++) {
- // random x,y
- vbuffer.put(random(width));
- vbuffer.put(random(height));
- // random r,g,b
- cbuffer.put(random(1.0));
- cbuffer.put(random(1.0));
- cbuffer.put(random(1.0));
- }
- vbuffer.rewind();
- cbuffer.rewind();
- gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
- gl2.glVertexPointer(2, GL2.GL_FLOAT, 0, vbuffer);
- gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
- gl2.glColorPointer(3, GL2.GL_FLOAT, 0, cbuffer);
- // Close JOGL for Processing 1.5
- //pgl.endGL();
- // Close JGOGL for Processing 2.0
- pg.endPGL();
- }
- void draw() {
- // if (currentTime == startTime)
- background(0);
- // JOGL for Processing 2.0
- PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
- PGL pgl = pg.beginPGL();
- GL gl = pgl.gl;
- GL2 gl2 = pgl.gl.getGL2();
- /// JOGL for Processing 1.5
- //PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change
- //GL gl = pgl.beginGL(); // always use the GL object returned by beginGL
- gl2.glPushMatrix();
- gl2.glTranslatef(width/2, height/2, 0);
- gl2.glScalef(sc,sc,sc);
- gl2.glRotatef(a, 0.0, 0.0, 1.0);
- gl2.glTranslatef(-width/2, -height/2, 0);
- gl2.glTranslatef(tx,ty, 0);
- gl2.glPointSize(2.0);
- gl2.glDrawArrays(GL2.GL_POINTS, 0, numPoints);
- gl2.glPopMatrix();
- // Close JOGL for Processing 1.5
- //pgl.endGL();
- // Close JOGL for Processing 2.0
- pg.endPGL();
- if (keyPressed) {
- if (key == CODED) {
- if (keyCode == LEFT) {
- a -= 1;
- }
- else if (keyCode == RIGHT) {
- a += 1;
- }
- }
- else if (key == '+' || key == '=') {
- sc *= 1.05;
- }
- else if (key == '_' || key == '-' && sc > 0.1) {
- sc *= 1.0/1.05;
- }
- else if (key == ' ') {
- sc = 1.0;
- tx = 0;
- ty = 0;
- a = 0;
- }
- }
- }
- void mouseDragged() {
- float dx = (mouseX - pmouseX) / sc;
- float dy = (mouseY - pmouseY) / sc;
- float angle = radians(-a);
- float rx = cos(angle)*dx - sin(angle)*dy;
- float ry = sin(angle)*dx + cos(angle)*dy;
- tx += rx;
- ty += ry;
- }
Any help or thoughts on this would be much appreciated. Thank you!
Peace.
Peace.
1