From: Ben Morrow Date: Wed, 5 Mar 2014 04:44:30 +0000 (-0800) Subject: Now with shader language and many fps X-Git-Url: https://git.piment-noir.org/?p=SugarCubes.git;a=commitdiff_plain;h=b5d6d7a0a56238e37d648489a1d95bb1dc351091 Now with shader language and many fps --- diff --git a/Internals.pde b/Internals.pde index b593f3e..5a74a37 100644 --- a/Internals.pde +++ b/Internals.pde @@ -134,7 +134,6 @@ void setup() { size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL); frameRate(targetFramerate); noSmooth(); - // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement? logTime("Created viewport"); // Create the model diff --git a/SugarCubes.pde b/SugarCubes.pde index 0afeefe..b941bdb 100644 --- a/SugarCubes.pde +++ b/SugarCubes.pde @@ -26,7 +26,7 @@ LXPattern[] patterns(LX lx) { return new LXPattern[] { - new SineSphere(lx), + //new SineSphere(lx), //new CubeCurl(lx), // Slee diff --git a/UIImplementation.pde b/UIImplementation.pde index 7750ab9..c3f0897 100644 --- a/UIImplementation.pde +++ b/UIImplementation.pde @@ -10,9 +10,69 @@ * * Custom UI components using the framework. */ + +import java.nio.*; import java.util.Arrays; class UICubesLayer extends UICameraComponent { + +int nvert = 200000; +int SIZEOF_INT = Integer.SIZE / 8; +int SIZEOF_FLOAT = Float.SIZE / 8; + +PGL pgl; +PShader sh; + +int vertLoc; +int colorLoc; + +IntBuffer vboName; +FloatBuffer vertData; + + boolean initted = false; + PShape sha1; + int numPoints = 10000; + + void initme() + { + nvert = model.points.size(); + sh = loadShader("frag.glsl", "vert.glsl"); + /*color[] simulationColors = lx.getColors(); + sha1= createShape(); + sha1.beginShape(POINTS); + for (LXPoint p : model.points) { + stroke(simulationColors[p.index]); + //pointColor(simulationColors[p.index]); + gl2.glPointColor=100; + sha1.vertex(p.x, p.y, p.z); + } + sha1.endShape();*/ + float[] temp = new float[nvert * 7]; + for (LXPoint p : model.points) { + // position + temp[p.index * 7 + 0] = p.x; + temp[p.index * 7 + 1] = p.y; + temp[p.index * 7 + 2] = p.z; + + // color + temp[p.index * 7 + 3] = 0.0; + temp[p.index * 7 + 4] = 0.0; + temp[p.index * 7 + 5] = 0.0; + temp[p.index * 7 + 6] = 1.0; + } + + vertData = allocateDirectFloatBuffer(nvert * 7); + vertData.rewind(); + vertData.put(temp); + vertData.position(0); + vboName = allocateDirectIntBuffer(1); + initVBO(); + } void onDraw(UI ui) { + if(!initted) + { + initted=true; + initme(); + } color[] simulationColors = lx.getColors(); String displayMode = uiCrossfader.getDisplayMode(); if (displayMode == "A") { @@ -26,18 +86,14 @@ class UICubesLayer extends UICameraComponent { drawSimulation(simulationColors); } simulationNanos = System.nanoTime() - simulationStart; - - camera(); - PGraphicsOpenGL gl = (PGraphicsOpenGL) g; + camera(); strokeWeight(1); } - void drawSimulation(color[] simulationColors) { translate(0, 30, 0); - - noStroke(); + fill(#141414); - drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.); + //drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.); fill(#070707); stroke(#222222); beginShape(); @@ -59,22 +115,71 @@ class UICubesLayer extends UICameraComponent { for (Cube c : model.cubes) { drawCube(c); } - + noFill(); - strokeWeight(2); - beginShape(POINTS); + //strokeWeight(2); + int count=0; + + ///magic: + + pgl = beginPGL(); + + + sh.bind(); + + vertLoc = pgl.getAttribLocation(sh.glProgram, "vertex"); + colorLoc = pgl.getAttribLocation(sh.glProgram, "color"); + + pgl.bindBuffer(PGL.ARRAY_BUFFER, vboName.get(0)); + pgl.enableVertexAttribArray(vertLoc); + pgl.enableVertexAttribArray(colorLoc); + + pgl.vertexAttribPointer(vertLoc, 3, PGL.FLOAT, false, 7 * SIZEOF_FLOAT, 0); + pgl.vertexAttribPointer(colorLoc, 4, PGL.FLOAT, false, 7 * SIZEOF_FLOAT, 3 * SIZEOF_FLOAT); + + pgl.drawArrays(PGL.POINTS, 0, nvert); + + pgl.disableVertexAttribArray(vertLoc); + pgl.disableVertexAttribArray(colorLoc); + pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); + + sh.unbind(); + endPGL(); + createGeometry(simulationColors); + pgl.bindBuffer(PGL.ARRAY_BUFFER, vboName.get(0)); + pgl.bufferData(PGL.ARRAY_BUFFER, nvert * 7 * SIZEOF_FLOAT, vertData, PGL.STATIC_DRAW); + } + + void createGeometry(color[] simulationColors) { for (LXPoint p : model.points) { - stroke(simulationColors[p.index]); - vertex(p.x, p.y, p.z); - } - endShape(); + vertData.put(p.index * 7 + 3, ((simulationColors[p.index] >> 16) & 0xFF)/255.0); + vertData.put(p.index * 7 + 4, ((simulationColors[p.index] >> 8) & 0xFF)/255.0); + vertData.put(p.index * 7 + 5, (simulationColors[p.index]& 0xFF)/255.0); + } } +void initVBO() { + + pgl = beginPGL(); + pgl.genBuffers(1, vboName); + pgl.bindBuffer(PGL.ARRAY_BUFFER, vboName.get(0)); + pgl.bufferData(PGL.ARRAY_BUFFER, nvert * 7 * SIZEOF_FLOAT, vertData, PGL.DYNAMIC_DRAW); + pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); + endPGL(); +} + +IntBuffer allocateDirectIntBuffer(int n) { + return ByteBuffer.allocateDirect(n * SIZEOF_INT).order(ByteOrder.nativeOrder()).asIntBuffer(); +} + +FloatBuffer allocateDirectFloatBuffer(int n) { + return ByteBuffer.allocateDirect(n * SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer(); +} void drawCube(Cube c) { float in = .15; noStroke(); fill(#393939); - drawBox(c.x+in, c.y+in, c.z+in, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH-in*2, Cube.EDGE_HEIGHT-in*2, Cube.EDGE_WIDTH-in*2, Cube.CHANNEL_WIDTH-in); + //drawBox(c.x+in, c.y+in, c.z+in, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH-in*2, Cube.EDGE_HEIGHT-in*2, Cube.EDGE_WIDTH-in*2, Cube.CHANNEL_WIDTH-in); } void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) { diff --git a/frag.glsl b/frag.glsl new file mode 100644 index 0000000..be233b9 --- /dev/null +++ b/frag.glsl @@ -0,0 +1,4 @@ +varying vec4 vertColor; +void main() { + gl_FragColor = vertColor; +} \ No newline at end of file diff --git a/sketch.properties b/sketch.properties index 8630fa2..6550104 100644 --- a/sketch.properties +++ b/sketch.properties @@ -1,2 +1,2 @@ -mode.id=processing.mode.java.JavaMode -mode=Java +mode.id=com.martinleopold.mode.debug.DebugMode +mode=Debug diff --git a/vert.glsl b/vert.glsl new file mode 100644 index 0000000..0b58f60 --- /dev/null +++ b/vert.glsl @@ -0,0 +1,11 @@ +uniform mat4 transform; +attribute vec4 vertex; +attribute vec4 color; +varying vec4 vertColor; +attribute float pointsize; +void main() { + gl_Position = transform * vertex; + vertColor = color; + gl_PointSize = 100.0; +} +