https://www.jpct.net/wiki/index.php?title=Particle_effect_example&feed=atom&action=history
Particle effect example - Revision history
2024-03-29T08:57:19Z
Revision history for this page on the wiki
MediaWiki 1.23.15
https://www.jpct.net/wiki/index.php?title=Particle_effect_example&diff=731&oldid=prev
Corey: Created page with "=== Particle effect example === ==== Details ==== A straightforward particle effect demonstration that uses the opengl renderer. ==== Screen shot ==== File:Firepit_particl..."
2017-09-21T05:13:48Z
<p>Created page with "=== Particle effect example === ==== Details ==== A straightforward particle effect demonstration that uses the opengl renderer. ==== Screen shot ==== File:Firepit_particl..."</p>
<p><b>New page</b></p><div>=== Particle effect example ===<br />
<br />
==== Details ====<br />
A straightforward particle effect demonstration that uses the opengl renderer.<br />
<br />
==== Screen shot ====<br />
[[File:Firepit_particle.png]]<br />
<br />
==== Source code ====<br />
<pre><br />
<br />
import java.awt.Color;<br />
import java.io.File;<br />
<br />
import org.lwjgl.LWJGLException;<br />
import org.lwjgl.input.Keyboard;<br />
import org.lwjgl.input.Mouse;<br />
import org.lwjgl.opengl.Display;<br />
<br />
import com.threed.jpct.*;<br />
<br />
/**<br />
* A simple particle demonstration.<br />
* In the demo we use a fire model, with 2 types of particles: flames and smoke.<br />
*<br />
* The code is not the best, but it should give you a general idea<br />
* on how to create particle effects for your own project.<br />
* <br />
* @author 'words' - jPCT.net<br />
*/<br />
public class ParticleDemo {<br />
<br />
private World world;<br />
private FrameBuffer frameBuffer;<br />
<br />
private Object3D fireModel;<br />
private GenericParticle particleTest;<br />
<br />
public ParticleDemo() {<br />
try {<br />
initGL();<br />
} catch (LWJGLException e) {<br />
e.printStackTrace();<br />
System.exit(1);<br />
}<br />
<br />
/*<br />
* Initialize the world<br />
*/<br />
this.world = new World();<br />
world.setAmbientLight(60, 60, 60);<br />
<br />
/*<br />
* Load the model<br />
*/<br />
TextureManager.getInstance().addTexture("flame", new Texture("res/flame.jpg"));<br />
TextureManager.getInstance().addTexture("smoke", new Texture("res/smoke.jpg"));<br />
fireModel = Object3D.mergeAll(Loader.load3DS("res/firepit.3ds", 4f));<br />
TextureManager.getInstance().addTexture("firepit", new Texture("res/firepit.jpg"));<br />
fireModel.setTexture("firepit");<br />
fireModel.rotateX((float) (-.5 * Math.PI)); // correct the rotation<br />
fireModel.rotateMesh();<br />
fireModel.setRotationMatrix(new Matrix());<br />
<br />
/*<br />
* Setup the camera<br />
*/<br />
Camera camera = world.getCamera();<br />
camera.moveCamera(Camera.CAMERA_MOVEOUT, 45); // zoom<br />
camera.moveCamera(Camera.CAMERA_MOVEUP, 45); // height<br />
camera.lookAt(new SimpleVector(10, 0, 10));<br />
camera.setFOV(1.5f);<br />
<br />
// Example!<br />
addFire(10, 0, 10);<br />
<br />
/*<br />
* Start the main loop:<br />
*/<br />
boolean running = true;<br />
while (running) {<br />
if (Display.isCloseRequested()) {<br />
running = false;<br />
return;<br />
}<br />
render();<br />
if (particleTest != null) {<br />
particleTest.update(world);<br />
}<br />
}<br />
shutdown();<br />
}<br />
<br />
private void addFire(float x, float y, float z) {<br />
Object3D fire = fireModel.cloneObject();<br />
fire.translate(x, y ,z);<br />
world.addObject(fire);<br />
this.particleTest = new GenericParticle(world, fire.getTransformedCenter());<br />
<br />
}<br />
<br />
private void initGL() throws LWJGLException {<br />
Logger.setLogLevel(Logger.LL_ONLY_ERRORS);<br />
Logger.setOnError(Logger.ON_ERROR_THROW_EXCEPTION);<br />
<br />
Config.glWindowName = "Particle Demo - jPCT " + Config.getVersion();<br />
Config.maxPolysVisible = 5000; // The maximum size of the VisList.<br />
<br />
this.frameBuffer = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY);<br />
frameBuffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);<br />
frameBuffer.enableRenderer(IRenderer.RENDERER_OPENGL);<br />
Display.setResizable(true);<br />
Mouse.create();<br />
}<br />
<br />
private void render() {<br />
frameBuffer.clear(Color.BLACK);<br />
world.renderScene(frameBuffer);<br />
world.draw(frameBuffer);<br />
frameBuffer.update();<br />
frameBuffer.displayGLOnly();<br />
<br />
pollKeyboard();<br />
pollMouse();<br />
}<br />
<br />
private void pollKeyboard() {<br />
while (Keyboard.next()) {<br />
final int keyCode = Keyboard.getEventKey();<br />
@SuppressWarnings("unused")<br />
final boolean pressed = Keyboard.getEventKeyState();<br />
if (keyCode == Keyboard.KEY_ESCAPE) {<br />
frameBuffer.dispose();<br />
System.exit(0);<br />
}<br />
}<br />
}<br />
<br />
private void pollMouse() {<br />
while (Mouse.next()) {<br />
if (Mouse.getEventButtonState()) {<br />
switch (Mouse.getEventButton()) {<br />
case 0: // left mouse button was clicked<br />
break;<br />
case 1: // right mouse button was clicked<br />
break;<br />
}<br />
}<br />
}<br />
}<br />
<br />
private void shutdown() {<br />
frameBuffer.dispose();<br />
Display.destroy();<br />
System.exit(0);<br />
}<br />
<br />
public static void main(String[] args) {<br />
String platform = System.getProperty("os.name").toLowerCase();<br />
String path = "./lib/native/";<br />
if (platform.indexOf("win") >= 0) {<br />
System.setProperty("org.lwjgl.librarypath", new File(path + "windows/").getAbsolutePath());<br />
} else if (platform.indexOf("mac") >= 0) {<br />
System.setProperty("org.lwjgl.librarypath", new File(path + "macosx/").getAbsolutePath());<br />
} else if ((platform.indexOf("nix") >= 0 || platform.indexOf("nux") >= 0 || platform.indexOf("aix") > 0)) {<br />
System.setProperty("org.lwjgl.librarypath", new File(path + "linux/").getAbsolutePath());<br />
} else if (platform.indexOf("sunos") >= 0) {<br />
System.setProperty("org.lwjgl.librarypath", new File(path + "solaris/").getAbsolutePath());<br />
}<br />
new ParticleDemo();<br />
}<br />
<br />
}<br />
</pre><br />
<pre><br />
<br />
import java.awt.Color;<br />
<br />
import com.threed.jpct.Matrix;<br />
import com.threed.jpct.Object3D;<br />
import com.threed.jpct.Primitives;<br />
import com.threed.jpct.SimpleVector;<br />
import com.threed.jpct.World;<br />
import com.threed.jpct.util.Light;<br />
<br />
/**<br />
* A generic particle handler.<br />
*<br />
*/<br />
public class GenericParticle {<br />
<br />
private final SimpleVector origin;<br />
private final Particle[] particles;<br />
private final int PARTICLE_COUNT = 1;<br />
private int count = 0;<br />
private long delay = System.currentTimeMillis();<br />
private int cycle = 0;<br />
private boolean firstTick = false;<br />
<br />
public GenericParticle(final World world, final SimpleVector origin) {<br />
this.origin = origin;<br />
this.particles = new Particle[25];<br />
}<br />
<br />
public void update(final World world) {<br />
if (!firstTick) {<br />
firstTick = true;<br />
Light light = new Light(world);<br />
light.setPosition(new SimpleVector(origin.x, origin.y - 40, origin.z));<br />
light.setAttenuation(-1);<br />
light.setIntensity(255, 255, 255);<br />
light.setDiscardDistance(500);<br />
light.enable();<br />
}<br />
if (System.currentTimeMillis() - delay > 120) {<br />
cycle++;<br />
if (cycle % 2 == 0) {<br />
for (int i = 0; i < PARTICLE_COUNT; i++) {<br />
// add particle<br />
Particle p = getParticle(world);<br />
p.setTranslationMatrix(new Matrix());<br />
p.setOrigin(origin);<br />
p.setVelocity(new SimpleVector(1 - Math.random() * 1, -1.4 - (Math.random() / 2f), 1 - Math.random() * 1));<br />
p.reset();<br />
<br />
}<br />
// move the visible particles<br />
for (int i = 0; i < count; i++) {<br />
Particle pp = particles[i];<br />
if (pp.getVisibility()) {<br />
pp.move(1);<br />
}<br />
}<br />
}<br />
delay = System.currentTimeMillis();<br />
}<br />
<br />
}<br />
<br />
private Particle getParticle(World w) {<br />
for (int i = 0; i < count; i++) {<br />
Particle pp = particles[i];<br />
if (!pp.getVisibility()) {<br />
pp.setVisibility(Object3D.OBJ_VISIBLE);<br />
return pp;<br />
}<br />
}<br />
Particle p = new Particle();<br />
w.addObject(p);<br />
particles[count] = p;<br />
count++;<br />
return p;<br />
}<br />
<br />
}<br />
<br />
@SuppressWarnings("serial")<br />
class Particle extends Object3D {<br />
<br />
private SimpleVector vel = new SimpleVector();<br />
private long time = 0;<br />
private long maxTime = 2000;<br />
private static final SimpleVector GRAV = new SimpleVector(0, -0.50f, 0);<br />
<br />
Particle() {<br />
super(Primitives.getPlane(2, 5));<br />
this.setBillboarding(true);<br />
this.setVisibility(true);<br />
this.setTransparency(12);<br />
this.setAdditionalColor(Color.WHITE);<br />
this.setLighting(Object3D.LIGHTING_NO_LIGHTS);<br />
this.enableLazyTransformations();<br />
this.build();<br />
reset();<br />
}<br />
<br />
void setVelocity(SimpleVector vel) {<br />
this.vel.set(vel);<br />
}<br />
<br />
void reset() {<br />
this.setTexture("flame");<br />
time = System.currentTimeMillis();<br />
getTranslationMatrix().setIdentity();<br />
}<br />
<br />
void move(int ticks) {<br />
if (getVisibility()) {<br />
if (System.currentTimeMillis() - time > 333) {<br />
this.setTexture("smoke");<br />
}<br />
for (int i = 0; i < ticks; i++) {<br />
vel.add(GRAV);<br />
this.translate(vel);<br />
}<br />
if (System.currentTimeMillis() - time > maxTime) {<br />
reset();<br />
this.setVisibility(Object3D.OBJ_INVISIBLE);<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre><br />
<br />
==== Download Link ====<br />
[http://www.mediafire.com/file/qzoo4arg67loa4z/fireparticle_res.zip http://www.mediafire.com/file/qzoo4arg67loa4z/fireparticle_res.zip]<br />
<br />
[[Category:jPCT]]</div>
Corey