Problem with a particle system tutorial


#1

Hi everyone.
Just to take my first steps with OF, I started following this-tutorial.

I’m currently stuck in the first part (when you sample the image’s pixels and then show the particles). I pretty much understand the logic of the code (and I was able to correct an error in the vector variable declaration), but still I have problems making it work. Basically all I get is a black screen.

I tried to put some debugs to see what’s happening. It seems the image is sampled correctly, but when it comes to get the colors from the particles stored in the vector, something wrong is going on: at this point all particles show a color of 0,0,0,0.

So the problem should lie in the way the vector is populated. Unfortunately I’m really a newbie with c++, so I cannot understand precisely what’s wrong. I post all the code here:

  
Particle.h  
#ifndef myParticles_Particle_h  
#define myParticles_Particle_h  
#include "ofMain.h"  
  
class Particle  
{  
public:  
    Particle();  
    Particle (ofPoint _position, ofColor _color)  
    {  
        position = _position;  
        color = _color;  
    }  
      
    ofPoint position, velocity;  
    ofColor color;  
};  
  
  
#endif  

testApp.h

  
#pragma once  
  
#include "ofMain.h"  
#include "Particle.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
      
        ofImage image;  
        int sampling; //pixels to skip;  
        vector<Particle> particles;  
        int numParticles;  
      
        //spring and sink factors  
    bool springEnabled;  
    float forceRadius;  
    float velocityDampingFactor;  
    float springFactor;  
    int cursorMode;  
      
		  

and finally the actual code, testApp.cpp:

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    ofBackground(0,0,0);  
    image.loadImage("devilSmall.jpg");  
    sampling=2;  
      
    unsigned char * pixels = image.getPixels();  
    int w = image.width;  
    int h = image.height;  
    int xOffset = (ofGetWidth()-w)/2;  
    int yOffset = (ofGetHeight()-w)/2;  
      
    for (int x=0; x<w; x+=sampling)  
    {  
        for (int y=0; y<h; y+=sampling)  
        {  
            int index = (y * w + x)*3;  
            ofColor color;  
            color.r=pixels[index];  
            color.g=pixels[index+1];  
            color.b=pixels[index+2];  
            particles.push_back(Particle(ofPoint(x+xOffset, y+yOffset),color));  
        }  
    }  
    ofSetFrameRate(30);  
    numParticles=(image.width*image.height)/sampling;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
    std::vector<Particle>::iterator p;  
    for (p=particles.begin(); p!=particles.end(); p++) {  
        p->position+=p->velocity;  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    std::vector<Particle>::iterator p;  
    for (p=particles.begin(); p!=particles.end(); p++);  
    {  
        ofColor color = p->color;  
        ofLog(OF_LOG_NOTICE, "the color is " + ofToString(color));  
        ofSetColor(color.r,color.g,color.b);  
        ofCircle(p->position.x, p->position.y, 1);  
          
    }  
}  

Where am I wrong?


#2

I just copied the example, fixing the vector declaration (vector) also, it worked ok :slight_smile:

Are you sure you have the image in the correct place?

example_root/bin/data/image.jpg
example_root/src/main.cpp <-- and all the code

I’m attaching the code :slight_smile:

particles_src.zip


#3

Found the error.
There was a STUPID semicolon after the For declaration in the draw method.
Thank you so much :slight_smile:


#4

Where is the tutorial?