We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Can anyone fix this code
Page Index Toggle Pages: 1
Can anyone fix this code ? (Read 1380 times)
Can anyone fix this code ?
Mar 9th, 2010, 6:16pm
 
I have seen this video and the author posted the code, but it does not work. It's slightly broken at the  for loop

Here is the video


Here is the site
http://null-null.net/blog/2007/11/569.php

Here is the code
Code:

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

//JOGL's
PGraphicsOpenGL pgl;
GL gl;

//constant numbers for particle
float DAMPING_VELOCITY = 0.98;
float GRAVITY = 0;
int AGE_MAX = 30;
int NUM_GENERATION_AT_ONE_LOOP = 30;

//particle list
ArrayList particles;
ArrayList clearParticles;

//texture
PImage texImage;
int[] texID;

//screen capture
PImage cp;
boolean isCap = false;
int cpTimer = 0;

void setup(){
 int i;

 size(480, 360, OPENGL);
 background(0);
 frameRate(30);
 noSmooth();

 //initialize JOGL's
 pgl = (PGraphicsOpenGL) g;
 gl = pgl.gl;

 randomSeed(int(random(1,1000)));

 //initialize particle list
 particles = new ArrayList();
 clearParticles = new ArrayList();

 //load image for texture
 texImage = loadImage("p.png");

 //generate texture and bind texture
 pgl.beginGL();
 gl.glEnable(GL.GL_TEXTURE_2D);
 texID = new int[1];
 gl.glGenTextures(1,texID,0);
 gl.glBindTexture(GL.GL_TEXTURE_2D, texID[0]);
 gl.glTexImage2D(
   GL.GL_TEXTURE_2D, 0, 4, texImage.width, texImage.height,
   0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(texImage.pixels)
 );
 gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
 gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
 pgl.endGL();

 //initialize image for screen capture
 cp = createImage(width,height,RGB);
}

void draw(){
 int i;
 Particle p;
 Iterator it;

 background(0);

 //generate
 for(i=0; i

 //brend func
 pgl.beginGL();
 gl.glDisable(GL.GL_DEPTH_TEST);
 gl.glEnable(GL.GL_BLEND);
 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
 pgl.endGL();

 //update and draw
 clearParticles.clear();
 it = particles.iterator();
 while(it.hasNext()){
   p = (Particle)it.next();
   p.update();
   p.drawMe();
   if(p.age>AGE_MAX){
clearParticles.add(p);
   }
 }

 //clear
 if(clearParticles.size() != 0){
   it = clearParticles.iterator();
   while(it.hasNext()){
p = (Particle)it.next();
particles.remove(p);
   }
 }

 // screen capture
 if(isCap){
   cp = get();
   cp.save("cap/cp_"+cpTimer+".jpg");
   cpTimer++;
 }
}

void keyPressed(){
 if(key=='s'){
   isCap = true;
 }else if(key=='a'){
   isCap = false;
 }
}

///////////////////Color4f///////////////////
class Color4f {
 float r, g, b, a;
 public Color4f(){
   r = g = b = a = 0.0;
 }
 public Color4f(float inR, float inG, float inB, float inA){
   r = inR;
   g = inG;
   b = inB;
   a = inA;
 }
}

///////////////////Particle///////////////////
class Particle {
 float x, y;
 float vx, vy;
 float size = 100;
 int age = 0;
 Color4f c;

 public Particle(float inX, float inY, float inVx, float inVy){
   x = inX;
   y = inY;
   vx = inVx;
   vy = inVy;
   age = 0;
   c = new Color4f(random(0,1), random(0,1), random(0,1), 1);
 }

 void drawMe(){
   float helfSize = size/2;
   float d = 1.0-(float)age/AGE_MAX;
   c.a = d;

   pgl.beginGL();
   gl.glBindTexture(GL.GL_TEXTURE_2D, texID[0]);
   gl.glColor4f(c.r, c.g, c.b, c.a);
   gl.glBegin(GL.GL_QUADS);
   gl.glTexCoord2f(0,0);
   gl.glVertex2f(x-helfSize, y-helfSize);
   gl.glTexCoord2f(1,0);
   gl.glVertex2f(x+helfSize, y-helfSize);
   gl.glTexCoord2f(1,1);
   gl.glVertex2f(x+helfSize, y+helfSize);
   gl.glTexCoord2f(0,1);
   gl.glVertex2f(x-helfSize, y+helfSize);
   gl.glEnd();
   pgl.endGL();
 }

 void update(){
   vy += GRAVITY;
   x += vx;
   y += vy;
   vx *= DAMPING_VELOCITY;
   vy *= DAMPING_VELOCITY;
   age++;
 }
}


You can see the break in code is here

background(0);

 //generate
 for(i=0; i





The void loop() should be void draw()

-Thanks
Re: Can anyone fix this code ?
Reply #1 - Mar 9th, 2010, 8:08pm
 
Lips Sealed
Re: Can anyone fix this code ?
Reply #2 - Mar 9th, 2010, 8:32pm
 
the error  I am getting is

Code:

OPENGL error at 1281 at top ENDRAW(): Invalid value
Re: Can anyone fix this code ?
Reply #3 - Mar 10th, 2010, 1:44am
 
Quote:
for(i=0; i

Typically, there is a < character after i.
And typically, it have been eaten by the browser because it marks the start of a tag: it should have been escaped to &lt;
If you do a View source in the browser, you can see the missing code:
Code:
  //generate
for(i=0; i<NUM_GENERATION_AT_ONE_LOOP; i++){
if(!mousePressed){
p = new Particle(mouseX, mouseY, random(-5,5), random(-5,5));
} else{
p = new Particle(mouseX, mouseY, random(-15,15), random(-15,15));
}
particles.add(p);
}
//brend func
Re: Can anyone fix this code ?
Reply #4 - Mar 10th, 2010, 5:00am
 
Thanks Philho that worked.

It actually putting something onscreen but but it is giving an error as a result of this code
[code]
gl.glTexImage2D(    GL.GL_TEXTURE_2D, 0, 4, texImage.width, texImage.height,    0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(texImage.pixels)  );
[/b]

Does anyone know what is wrong with this ?
Re: Can anyone fix this code ?
Reply #5 - Mar 10th, 2010, 5:29am
 
> but it is giving an error

so what is the error it's giving, perhaps there's a clue in there?

(and why do people say 'there's an error' and nothing else?)
Re: Can anyone fix this code ?
Reply #6 - Mar 10th, 2010, 11:26am
 
I should update the original post

andrewowaun wrote on Mar 9th, 2010, 8:32pm:
the error  I am getting is

Code:

OPENGL error at 1281 at top ENDRAW(): Invalid value

Re: Can anyone fix this code ?
Reply #7 - Mar 10th, 2010, 11:59am
 
ah, ok, didn't see that as i browse by recent posts.

tried that here using a dummy p.png. runs fine and looks great. no errors.

(this being ubuntu 9.10 32bit and processing 1.0.9)
Page Index Toggle Pages: 1