For this first release, I am showing how I go about making a generic particle emitter. I used Processing v.135 and Karsten Schmidt’s Vec3D library. It was developed on a MacBook Pro laptop, but this shouldn’t be a problem.

There is an emitter on screen which will follow your cursor position. When you click and hold the left mouse button, it will create particles with a randomized velocity and send them out into the world.
‘P’ will toggle a Perlin Noise influence to the Particle velocity.
‘G’ will add a gravity influence to the Particle velocity.
‘F’ will turn on an invisible floor allowing the Particles to bounce.
‘T’ will toggle the rendering of a Particle trail.
Some features of this piece of source code:
• Some example usage of Karsten’s Vec3D library.
• ArrayList to handle the variable number of particles.
• Location history used as a ribbon trail.
• OpenGL display lists and additive blending.
Click here to download the source code.
UPDATES———————————————————————
• February 11th, 2008: Implemented an OpenGL reorganization as per Simon Gelfius’ recommendation. Should improve performance a bit.
• December 8th, 2008: This code doesn’t quite work with Processing 1.0. There will be some bindTexture errors. This post should help you resolve those errors until I can find the time to redo this source code properly.

Thankyou for sharing this with us, I’ve downloaded the sourcecode and I look forward to browsing through it over a coffee after work when I get home!
Wow! Tons more code than I was expecting – this covers pretty much every technique I’m interested in.
Thank you, Robert!
Thanks!
great!
thanks, really!
Marcello
Thanks for sharing your code! I haven’t tried out Processing yet (I use primarily C++ and OpenGL for my artwork and software) but I am somewhat familiar with noise. If Processing uses Perlin’s original implementation of noise, you might want to consider coding your own noise routine, for instance Perlin’s Simplex noise (http://en.wikipedia.org/wiki/Simplex_noise). There’s a link to his SIGGRAPH 2001 pdf on the Wikipedia page, and his revised algorithm is faster, smoother, and doesn’t have discontinuities.
Yeah! Thanks Robert! Nice stuffs to share

I think I’ve red something like post your suggestions/improvements,…
So here is my little 10cents optimisation. As you know the less gl call you do the better it is. Another thing is that bindingTextures is very expensive, and it is why Processing don’t manage that well too much textures, good point to use opengl texturing instead of the processing image() method, but if you do the same way with the binding you won’t get that much performance increase.
So what I would change in your code is to do the most actions possible inside the main loop. and not repeat useless call for each particles.. so I just modified a bit the emiter exist method:
void exist(){
setVelToMouse();
findVelocity();
setPosition();
pgl.beginGL(); // only one call for all the particles
gl.glEnable( GL.GL_TEXTURE_2D ); // only one call for all the particles
pgl.bindTexture( particleImg ); // only one call for all the particles
iterateList();
pgl.bindTexture( emitterImg );
render();
gl.glDisable( GL.GL_TEXTURE_2D );
if( ALLOWTRAILS ){ /* render the trails separatly so opengl don’t have to mix different type of stuffs wich is really really faster, there must be another improvement that can be done here like doing the quads call here and not for all particles, but then you will need to change your trail rendering */
renderTrails();
}
pgl.endGL();
}
Anyway that’s very nice too see how you code! Thanks for that!
(Also I have a version of particle system using very simple shaders that REALLY explode the framerate (I can put almost 50 000 particles on my old computer and keep a good fps), if you are interested I can share it somewhere)
Oh! I forgot to tell!
“2D trails” = greeeaaat idea
This is great Robert, particularly the raw OpenGL commands to create a textured polygon. Your comments are very helpful. I’m glad you chose to release some code, this is pretty much exactly what many of us were looking forward to.
This is really really helpful, every bit of source code like this helps loads more developers so many thanks Robert!
Sweet, totally interested in how you get the glow color blending and high speed open GL working. Thanks a ton for sharing this!
In the past I have been using Shiffman’s Vector3D class for animating. What are the main differences and advantages of Karsten Schmidts 3D library? It seems to have a lot more than just a vector 3d class…
Thx,
-Karl
Hey Robert, I didn’t realize your tutorial was *that* imminent!
So I just managed to clean up after myself in the SVN in time before the storm broke loose… Anyhow, merci pour le plug and for kinda launching this little library to the public. As to the differences/benefits over Daniel’s Vector3D class: I’ve started this library when working on the Audi TT windtunnel last year and needed a lot more features. The main pluses (for me) are that there’re 2 versions for most vector operations to work either in-place or return the result as new vector. In-place operations can help reducing the overall number of vector objects created and so help performance. The class is also implemented as “fluid interface” and supports/encourages method chaining, making it easier to write complex vector maths. Generally it has a lot more features for handling common geometry problems (collision, intersection, reflection, interpolation, culling etc.)… enough said.
Alternatively, you might also want to try out the new OpenMath lib project @ java.net – it’s 10x more comprehensive and has dedicated types and utils for 2D-4D… https://openmali.dev.java.net
Rock on!
Thanks for taking the time to do this.
Thanks for the suggestions, Simon. I have worked your solution into the source and reuploaded. And yes, Id love to see your particles via shaders. I played a bit with shaders but cant quite wrap my head around how to send data to and from the shaders. I managed to get a utah teapot to heat shimmer, but thats about it.
Heh, Toxi, did I catch you unprepared? I gave you 20 minutes warning, wasnt that enough? SO yeah, thanks for your fantastic library. I use it daily.
totally awesome. and couldn’t be timed more perfectly as my class this week is covering particle systems. Anyone reading who wants a gentle introduction to help with this post: http://www.shiffman.net/teaching/nature/particles.
Toxi, you seem to have a habit of remaking my libraries into much much much better libraries! I’m embarrassed I hadn’t looked closely at your Vec3D class before, awesome. I think I will have to update and adjust mine, I like the add() addSelf() syntax, etc. . . . or better yet, maybe I will revise my examples to use yours. . . ?
I’ve often longed to take a look at the code behind your magic; I’m excited to dig in to it. Thanks for making it available!
To send values to the shader use uniform variables in the shader and this class: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1199877349;start=2#2
Add this function to the class and and it will be work:
public void setUniform(String name, float value){
int myId=getUniformLocation(name);
gl.glUniform1f(myId, value);
}
public void setUniform(String name, float value1, float value2, float value3){
int myId=getUniformLocation(name);
gl.glUniform3f(myId, value1, value1,value1);
}
I probably should start a discussion about shaders soon. Better yet, perhaps Simon and Andreas can post some nice shader examples.
Hey hey!
Here’s my 5 minutes implementation of what i was saying…. It must have something better to do but it’s a good start. Here’s what I change: Only one call to gl_quads for each particles and for each trails no more translate and scale for the particles (and no more rotate if you want to move the cam and keep the particles aligned), the shader do almost all this stuffs just for you. No more call to enable/disable 2d_texture anymore.
I’m also using JohnG class for importing the shaders wich is very usefull.
Press “s” to switch to the shaders mode…. unfortunatly the trails need more optimisations than that…. because I put 250 particles by click wich is very expensive if you render the trails also.
if(ALLOWSHADERS) emitter.addParticles( 250 ); // hahaha
else emitter.addParticles( 10 );
I coded this directly after seeing your answer so there’s certainly some mistakes and some stuff to optimise more but it work here… on my old, almost dying computer.
The code is quite simple, and I leave your code almost like it was and just enclose it in a if statement… so you can see the difference and try to understand how it work… if you want more explanations I’m affraid I’m not the right guy. Anyway I’m sure you will do crazy stuffs with that.
Oh! and I had a little question… Could you explain the advantage of using this syntax for arraylist:
Iterator it = particles.iterator(); it.hasNext(); ….. I don’t even knew such things exists…
Anyway good idea to start sharing stuffs… I will definitively join the move
http://kinesis.be/projects/PROCESSING/source_particles_optimised2.rar
I stay tuned for the next episode
Oups! I forgot to keep your displayList when using the shaders….
if you want to use it also (wich increase performance a lot especially on old computers) here it is:
void initGL(){
pgl.beginGL();
squareList = gl.glGenLists(1);
gl.glNewList(squareList, GL.GL_COMPILE);
gl.glVertex2f( -1.0f, -1.0f ); // I removed the gl.glBegin and the texturing stuffs
gl.glVertex2f( +1.0f, -1.0f );
gl.glVertex2f( +1.0f, +1.0f );
gl.glVertex2f( -1.0f, +1.0f );
gl.glEndList();
pgl.endGL();
}
and then this for rendering the particles:
void render(){
color c = color( agePer, agePer*.75, 1.0 – agePer );
gl.glColor4f(red(c), green(c), blue(c),1.0f);
gl.glTexCoord4f( loc[0].x, loc[0].y, loc[0].z, radius * agePer *.5 );
gl.glCallList( squareList );
}
sorry for this :p
That is too funny. Such a dramatic speed difference! Thanks for sharing that. So much to wrap my head around. Sure, its faster and probably ‘better’ to do it with shaders, but man… shaders are one of those things that is deceptively difficult. Looks easy enough. 5 lines in a couple .glsl files. Piece of cake. But if you mess something up, generally you get a blank screen and no error messages (as is my understanding of working with shaders within the Processing environment). Addictive though, and hopefully this year will be the year that I start to get a handle on shaders. Its what all the cool kids are doing.
As for the iterator, lets see. That was was the way I was taught. I was also told I could do it this way:
int mylength = myList.size();
Particle p = (Particle) myList.get(i);
for (int i = 0; i
p.exist();
}
But the problem I found with that method is I didn’t know how to implement the remove() part. I tried, but only for about 10 minutes. ArrayLists are still a bit hazy for me. Its one of those things that I know how to use, but I don’t really know much about why it works.
Sorry for the spam… here’s the updated version (no more no shaders/ shaders switching option in this one)… it’s easier like that.
http://kinesis.be/projects/PROCESSING/source_particles_ShadersAndDl.rar
Its not spam if its welcome. And believe me, its welcome!
Thanks for this, Robert, and thanks to Simon for providing alternate methods which, one day, I will know enough to make use of!
I’m thinking it’s time to learn OpenGL.
BTW, Simon, both of your last two uploaded sketches throw this error when I attempt to load them:
apple.awt.EventQueueExceptionHandler Caught Throwable : java.lang.NullPointerException
java.lang.NullPointerException
at processing.app.Base.handleOpen(Base.java:660)
at processing.app.Base.handleOpen(Base.java:650)
at processing.app.BaseMacOS.handleOpenFile(BaseMacOS.java:80)
at com.apple.eawt.Application$4.run(Application.java:353)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:189)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:478)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:170)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
(I’m on a MacBook Pro.)
Never mind that last error — it didn’t seem to happen when I quit Processing and restarted it. Sorry!
@ simon:
Thanks for sharing your Shader knowledge! What do I have to do if I want the particle Shaders not to align/face the camera when I rotate it?
Oh I almost forgot to thank robert for sharing some of his magic! Is there a documentation for toxis vec3D library?
Cool and more than an interactive. I hope you add a vector stuff, it cool like a turbulence. Awesome coding!
Moka, the library download contains a doc folder with complete Javadocs. I also recommend checking out the SVN version (see current branch used in the included README file) since this lib is under constant development
thank you! I found it!
thanks a lot for the codes
they include so much things i wish to learn and a good reference to let me learn to code in a better way
Thanks Robert! It’s great to know how you create your work.
This will be very usefull for me and my students.
I’m a huge fan of computer based art, usually more on the interactive side of things, but the work here is stunning.
Great choice for the release; small projects consisting of a few components that demonstrate one or two concepts are ultimately much easier to dissect and understand than a whole codebase of different projects.
I’m looking forward to diving into this and making some tweaks to see what I can come up with, particularly on the texturing side… I’m also looking forward to the next code release, and discovering some new additions to the basics introduced here.
blessings
even
ps great to see the other posts tweaking the code!
this is fantastic! It motivated me to try processing for the first time. One note for other noobs: when you download the vec3d library – download the first zip ‘geomutils-0008.zip’ not the ‘-src’ file. otherwise you will get something like: “You need to modify your classpath, sourcepath, bootclasspath, and/or extdirs setup. Jikes could not find package…”. Unzip the file and copy the geomutils folder into the processing libraries folder.
I would like to say a really big thank you for taking the time to share with us. This sample code is fantastic and should make for some really interesting reading while I learn Processing…. Thanks again.
Sorry Robert for misusing your blog to answer questions about the vec3d library..
@felix – personally am trying to build libraries which don’t have any direct relationship with Processing, but can be used just like any other Java building block in the bigger world. That means I’m usually working in Eclipse with the latest JDK and want to use all the syntax features Java has been offering for the past 2+ years. Processing on the other hand is still using an old version of the IBM Jikes compiler, which only supports the old Java syntax (up to version 1.4). This is pretty rubbish if you’re not 100% focused on Processing, but am still making an effort to avoid any newer syntax for classes which have to be Processing compatible. The Vec3D class is just a small piece I’ve started splitting out from the bigger collection of classes in the project. If you guys have any further comments or issues feel free to file an issue over here: http://code.google.com/p/toxiclibs/issues/list
Thanks for this post Robert!
I thought i knew a few things about particles but really learned a few new tricks here.
Especially thanks to Simon for sharing his knowledge about all the GL stuff!
Apologies for being a complete simpleton, but I get the following error when I try to run the source code:
Type ‘PGraphicsOpenGL’ was not found.
Any idea what I’m missing here?
cheers,
Windy
Hi Robert!!! I make a port to AS3 and Papervision3D.
http://www.vasava.es/lab/pv3DContest/
It’s not so beatifual as your example but a like it
.
I love Karsten Schmidt’s Vec3D too!!!
Well thanks for sharing your code
Bye!!!
[...] some of this is adapted from a Robert Hodgin source which can be found here [...]
Thank you so much for making this available. I’ve been a huge fan of your work ever since attending your session at Flashbelt this past year.
I’ve managed to get this working to a degree but it took some fiddling around. Initially Processing throws this error:
“the method bindTexture(PImage) from the type PGraphicsOpenGL is not visible”
I got around this by using the image method in render() in place of renderImage():
image(emitterImg, loc.x, loc.y, 450,450);
I get the feeling though that I’m probably not getting the full effect here.. any idea what I’m doing wrong?
Once again, I can’t thank you enough for this contribution. Commented code like this is an invaluable resource for beginners like myself. Keep up the good work!