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

Re : Drawing 120000 spheres each frame? (Introduction into OpenGL?)

$
0
0
I've seen that one before, but I have absolutely no idea what's happening there. The setup is still kind of readable, and I think in order to use that example with the database it should be something like the code under this paragraph. I just combined both very quickly, 119616 is the amount of stars in the csv. Down in the draw, it gets completely unreadable to me and of course, it results in an error (if it had worked I'd be very surprised).



import javax.media.opengl.GL2;
import java.nio.*;

Table starsInUniverse;
PVector location = new PVector();
 
// pan, zoom and rotate
float tx = 0, ty = 0;
float sc = 1;
float a = 0.0;
 
FloatBuffer vbuffer;
FloatBuffer cbuffer;
float[] projMatrix;
float[] mvMatrix;
 
void setup() {
  size(displayWidth/2, displayHeight/2, OPENGL); 
  smooth();
  starsInUniverse = loadTable("HYG.csv", "header");
  PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  PGL pgl = pg.beginPGL();
  GL2 gl2 = pgl.gl.getGL2();
 
  int vSize = (119616 * 3);
  int cSize = (119616 * 3);
  vSize = vSize << 2;
  cSize = cSize << 2;
 
  vbuffer = ByteBuffer.allocateDirect(vSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  cbuffer = ByteBuffer.allocateDirect(cSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  for (TableRow row: starsInUniverse.rows()) {
    // random x,y
    vbuffer.put(map(row.getFloat("X"), -10000000.0, 10000000.0, 500, 2300));
    vbuffer.put(map(row.getFloat("Y"), -10000000.0, 10000000.0, 0, 1800));
    vbuffer.put(map(row.getFloat("Z"), -10000000.0, 10000000.0, 900, -900));
    // 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(3, GL2.GL_FLOAT, 0, vbuffer);
 
  gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
  gl2.glColorPointer(3, GL2.GL_FLOAT, 0, cbuffer);
 
  pg.endPGL();
 
  projMatrix = new float[16];
  mvMatrix = new float[16]; 
}
 
void draw() {
  background(0);
 
  PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  PGL pgl = pg.beginPGL();
  GL2 gl2 = pgl.gl.getGL2();
 
 
  gl2.glMatrixMode(GL2.GL_PROJECTION);
  projMatrix[0] = pg.projection.m00;
  projMatrix[1] = pg.projection.m10;
  projMatrix[2] = pg.projection.m20;
  projMatrix[3] = pg.projection.m30;
 
  projMatrix[4] = pg.projection.m01;
  projMatrix[5] = pg.projection.m11;
  projMatrix[6] = pg.projection.m21;
  projMatrix[7] = pg.projection.m31;
 
  projMatrix[8] = pg.projection.m02;
  projMatrix[9] = pg.projection.m12;
  projMatrix[10] = pg.projection.m22;
  projMatrix[11] = pg.projection.m32;
 
  projMatrix[12] = pg.projection.m03;
  projMatrix[13] = pg.projection.m13;
  projMatrix[14] = pg.projection.m23;
  projMatrix[15] = pg.projection.m33;
 
  gl2.glLoadMatrixf(projMatrix, 0);
 
  gl2.glMatrixMode(GL2.GL_MODELVIEW);
  mvMatrix[0] = pg.modelview.m00;
  mvMatrix[1] = pg.modelview.m10;
  mvMatrix[2] = pg.modelview.m20;
  mvMatrix[3] = pg.modelview.m30;
 
  mvMatrix[4] = pg.modelview.m01;
  mvMatrix[5] = pg.modelview.m11;
  mvMatrix[6] = pg.modelview.m21;
  mvMatrix[7] = pg.modelview.m31;
 
  mvMatrix[8] = pg.modelview.m02;
  mvMatrix[9] = pg.modelview.m12;
  mvMatrix[10] = pg.modelview.m22;
  mvMatrix[11] = pg.modelview.m32;
 
  mvMatrix[12] = pg.modelview.m03;
  mvMatrix[13] = pg.modelview.m13;
  mvMatrix[14] = pg.modelview.m23;
  mvMatrix[15] = pg.modelview.m33;
  gl2.glLoadMatrixf(mvMatrix, 0);
 
 
  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, 119616);
 
  gl2.glPopMatrix();
 
  pg.endPGL();
}
 
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;
}

Viewing all articles
Browse latest Browse all 1768

Trending Articles