Processing Forum
- import processing.opengl.*; import codeanticode.glgraphics.*; import peasy.*; GLModel points; int numPoints = 10000; float a = 0; PeasyCam cam; //bodies and spots int spotsPerBody = 100; Body[] bodyArray = new Body[1]; void setup() { //size(screen.width, screen.height, GLConstants.GLGRAPHICS); size(600, 600, GLConstants.GLGRAPHICS); cam = new PeasyCam(this, 0, 0, 0, 2400); cam.setMinimumDistance(1); cam.setMaximumDistance(5000); for (int i = 0; i < bodyArray.length; i++) { bodyArray[i] = new Body(0,0,0); bodyArray[i].initializeVertex(this); } points = new GLModel(this, numPoints, POINTS, GLModel.DYNAMIC); points.initColors(); points.beginUpdateVertices(); for (int i = 0; i < numPoints; i++) points.updateVertex(i, 100 * random(-1, 1), 100 * random(-1, 1), 100 * random(-1, 1)); points.endUpdateVertices(); points.beginUpdateColors(); for (int i = 0; i < numPoints; i++) points.updateColor(i, random(0, 255), random(0, 255), random(0, 255), 225); points.endUpdateColors(); } void draw() { // When drawing GLModels, the drawing calls need to be encapsulated // between beginGL()/endGL() to ensure that the camera configuration // is properly set. //Do EVERYTHING in the glgraphics mode before anything else. God that was hard to figure out. GLGraphics renderer = (GLGraphics)g; renderer.beginGL(); background(0); // The vertices are displaced randomly (so each particle describes a random walk). points.beginUpdateVertices(); for (int i = 0; i < numPoints; i++) points.displaceVertex(i, random(-0.5, 0.5), random(-0.5, 0.5), random(-0.5, 0.5)); points.endUpdateVertices(); translate(width/2,height/2,0); rotateY(a); // A model can be drawn through the GLGraphics renderer: renderer.model(points); // ...or just by calling its render() method: //points.render(); for(Body body : bodyArray) { body.updateSpot(random(100),random(100),random(100)); body.points2.beginUpdateVertices(); body.points2.updateVertex(2,0,0,0); //body.points2.displaceVertex(0, random(-0.5, 0.5), random(-0.5, 0.5), random(-0.5, 0.5)); body.points2.endUpdateVertices(); body.displaySpots(); } renderer.endGL(); a += 0.005; println(frameRate); stroke(100); drawSquareGrid(1000.0,1000.0,20,20); } void drawSquareGrid(float horizontalSize, float verticalSize, int horizontalDivisions, int verticalDivisions){ float horMult = horizontalSize/horizontalDivisions; float vertMult = verticalSize/verticalDivisions; for(int i = 0;i<horizontalDivisions+1;i++){ line((-horizontalSize/2.0)+i*horMult,-verticalSize/2.0,(-horizontalSize/2.0)+i*horMult,verticalSize/2.0); } for(int i = 0;i<verticalDivisions+1;i++){ line(-horizontalSize/2.0,(-verticalSize/2.0)+i*vertMult,horizontalSize/2.0,(-verticalSize/2.0)+i*vertMult); } } ////////////////////// class Spot { PVector pos; color c; Spot() { this.pos = new PVector(0,0,0); } Spot(float x, float y, float z) { this.pos = new PVector(x,y,z); } void setColor(color qc) { c = qc; } } ////////////////////// class Body { PVector pos; color c; Spot[] spotArray = new Spot[spotsPerBody]; int spotIndex = 0; GLModel points2; Body(float x, float y, float z) { pos = new PVector(x,y,z); for(int i = 0;i<spotArray.length;i++) { spotArray[i] = new Spot(0,0,0); } } void initializeVertex(PApplet thing){ points2 = new GLModel(thing, spotArray.length, POINTS, GLModel.DYNAMIC); points2.initColors(); points2.beginUpdateVertices(); points2.beginUpdateColors(); for (int i = 0; i < spotArray.length; i++) { points2.updateVertex(i,0.0,0.0,0.0); points2.updateColor(i, 0); } points2.endUpdateVertices(); points2.endUpdateColors(); println("INITIALIZED"); } void setColor(color qc) { c = qc; } void updateSpots() { for(Spot s : spotArray) { s.pos.set(random(1000),random(1000), random(1000)); } } void updateSpot(float x, float y, float z) { spotArray[spotIndex].pos.set(x,y,z); //points2.beginUpdateVertices(); //points2.updateVertex(spotIndex,x,y,z); //points2.endUpdateVertices(); spotIndex = (spotIndex+1) % spotsPerBody; } void displaySpots() { beginShape(LINES); for(Spot s : spotArray) { vertex(s.pos.x,s.pos.y,s.pos.z); } endShape(); } }