Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
What was missing in the code I posted before is the copy of the Processing projection and modelview matrices to the corresponding matrices in OpenGL:
  1. import javax.media.opengl.GL;
  2. import javax.media.opengl.GL2;
  3. import java.nio.*;
  4.  
  5. // let's try 1,000,000 points
  6. int numPoints = 1000000;
  7.  
  8. // pan, zoom and rotate
  9. float tx = 0, ty = 0;
  10. float sc = 1;
  11. float a = 0.0;
  12.  
  13. FloatBuffer vbuffer;
  14. FloatBuffer cbuffer;
  15. float[] projMatrix;
  16. float[] mvMatrix;
  17.  
  18. void setup() {
  19.   size(displayWidth/2, displayHeight/2, OPENGL); 
  20.   smooth();
  21.   
  22.   PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  23.   PGL pgl = pg.beginPGL();
  24.   GL gl = pgl.gl;
  25.   GL2 gl2 = pgl.gl.getGL2();
  26.    
  27.   int vSize = (numPoints * 2);
  28.   int cSize = (numPoints * 3);
  29.   vSize = vSize << 2;
  30.   cSize = cSize << 2;

  31.   vbuffer = ByteBuffer.allocateDirect(vSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  32.   cbuffer = ByteBuffer.allocateDirect(cSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  33.   for (int i = 0; i < numPoints; i++) {
  34.     // random x,y
  35.     vbuffer.put(random(width));
  36.     vbuffer.put(random(height));
  37.     // random r,g,b
  38.     cbuffer.put(random(1.0));
  39.     cbuffer.put(random(1.0));
  40.     cbuffer.put(random(1.0));
  41.   }
  42.   vbuffer.rewind();
  43.   cbuffer.rewind();
  44.  
  45.   gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  46.   gl2.glVertexPointer(2, GL2.GL_FLOAT, 0, vbuffer);
  47.  
  48.   gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
  49.   gl2.glColorPointer(3, GL2.GL_FLOAT, 0, cbuffer);
  50.   
  51.   pg.endPGL();
  52.  
  53.   projMatrix = new float[16];
  54.   mvMatrix = new float[16]; 
  55. }
  56.  
  57. void draw() {
  58.   background(0);
  59.   
  60.   PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  61.   PGL pgl = pg.beginPGL();
  62.   GL gl = pgl.gl;
  63.   GL2 gl2 = pgl.gl.getGL2();
  64.   
  65.   
  66.   gl2.glMatrixMode(GL2.GL_PROJECTION);
  67.   projMatrix[0] = pg.projection.m00;
  68.   projMatrix[1] = pg.projection.m10;
  69.   projMatrix[2] = pg.projection.m20;
  70.   projMatrix[3] = pg.projection.m30;

  71.   projMatrix[4] = pg.projection.m01;
  72.   projMatrix[5] = pg.projection.m11;
  73.   projMatrix[6] = pg.projection.m21;
  74.   projMatrix[7] = pg.projection.m31;

  75.   projMatrix[8] = pg.projection.m02;
  76.   projMatrix[9] = pg.projection.m12;
  77.   projMatrix[10] = pg.projection.m22;
  78.   projMatrix[11] = pg.projection.m32;

  79.   projMatrix[12] = pg.projection.m03;
  80.   projMatrix[13] = pg.projection.m13;
  81.   projMatrix[14] = pg.projection.m23;
  82.   projMatrix[15] = pg.projection.m33;
  83.   
  84.   gl2.glLoadMatrixf(projMatrix, 0);
  85.   
  86.   gl2.glMatrixMode(GL2.GL_MODELVIEW);
  87.   mvMatrix[0] = pg.modelview.m00;
  88.   mvMatrix[1] = pg.modelview.m10;
  89.   mvMatrix[2] = pg.modelview.m20;
  90.   mvMatrix[3] = pg.modelview.m30;

  91.   mvMatrix[4] = pg.modelview.m01;
  92.   mvMatrix[5] = pg.modelview.m11;
  93.   mvMatrix[6] = pg.modelview.m21;
  94.   mvMatrix[7] = pg.modelview.m31;

  95.   mvMatrix[8] = pg.modelview.m02;
  96.   mvMatrix[9] = pg.modelview.m12;
  97.   mvMatrix[10] = pg.modelview.m22;
  98.   mvMatrix[11] = pg.modelview.m32;

  99.   mvMatrix[12] = pg.modelview.m03;
  100.   mvMatrix[13] = pg.modelview.m13;
  101.   mvMatrix[14] = pg.modelview.m23;
  102.   mvMatrix[15] = pg.modelview.m33;
  103.   gl2.glLoadMatrixf(mvMatrix, 0);
  104.     
  105.   
  106.   gl2.glPushMatrix();
  107.   gl2.glTranslatef(width/2, height/2, 0);
  108.   gl2.glScalef(sc,sc,sc);
  109.   gl2.glRotatef(a, 0.0, 0.0, 1.0);
  110.   gl2.glTranslatef(-width/2, -height/2, 0);
  111.   gl2.glTranslatef(tx,ty, 0);
  112.  
  113.   gl2.glPointSize(2.0);
  114.  
  115.   gl2.glDrawArrays(GL2.GL_POINTS, 0, numPoints);
  116.  
  117.   gl2.glPopMatrix();

  118.   pg.endPGL();
  119. }
  120.  
  121. void mouseDragged() {
  122.   float dx = (mouseX - pmouseX) / sc;
  123.   float dy = (mouseY - pmouseY) / sc;
  124.   float angle = radians(-a);
  125.   float rx = cos(angle)*dx - sin(angle)*dy;
  126.   float ry = sin(angle)*dx + cos(angle)*dy;
  127.   tx += rx;
  128.   ty += ry;
  129. }
You have to do the glLoadMatrixf() call manually because the OpenGL renderer only uses functions available in the GLES2 API, and all the matrix-related functions are gone in GLES2 (and GL 3.2+). 


Viewing all articles
Browse latest Browse all 1768

Trending Articles