Objects and draw instances

Hey there!
ive a question about instanced in openGL. i want to do instanced drawing in post to getting better perfomance. right now ive a system, a very tipical particle system:

    vector<Obj*>objects;
class Obj{
public:
    
    Obj(float x, float y, float z, int radio, int vertexCount);

     ofSpherePrimitive sphere;

    bool visible;
    void display();
};

as you can see, i keep track every obj position, size, radio, and vertexCount. also, i ve a boolean value to determine if the object are drawing or not.

how can i port this system into a “instanced style” ?
its possible to draw geometry instanced but also keep track every value individually?
what do you think?

Hi pxt,

well you should look into what your class should need :

// position : x y z, radio, vertexCount
float x,y,z;
int radio,
int vertexCount

bool visible;

Then keep track of it somehow in an update function for example, so you get something like this :

class Obj{
    public:
    
        Obj(float _x, float _y, float _z, int _radio, int _vertexCount){
            x = _x;
            y = _y;
            z = _z;
            radio = _radio;
            vertexCount = _vertexCount;

            visible = true;
            sphere.setPosition(x,y, z);
            sphere.setRadius(radio);
            sphere.setResolution(vertexCount);
        }
    
        ofSpherePrimitive sphere;
    
    float x,y,z;
    int radio;
    int vertexCount;
    
        bool visible;
        void display(){
            // you might a color for the Sphere here
            if(visible)sphere.draw();
        }
    
        void update(){
            // evolve params x,y,z,radio, vertexCount
            
        }
};

This would be an instance of your class, which you can after put in a vector:
in ofApp.h:

    vector < Obj* > objects;

in ofApp.cpp::setup():

 int amount = 10;
    for (int i=0;i < amount;i++){
        Obj* tempObj = new Obj(ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), ofRandom(40), 4, 30 );
        objects.push_back(tempObj);
    }

then in ofApp.cpp::update() go through all of them and update them :

 int amount = 10;
    for (int i=0;i < amount;i++){
        objects[i]->update();
    }

and in ofApp.cpp::draw() :

 int amount = 10;
    for (int i=0;i < amount;i++){
        objects[i]->display();
    }

Hey man, thanks. But if you take a look i want to do instanced drawing, in OpenGl! Actually the way you described is how i drawing my stuff now.

Ah sorry I got confused on the way you constructed your class.

You can use something like this instead of the sphere.draw() call :

// GL_TRIANGLE_FAN depends on what you want to use in this case : triangle fan
glBegin(GL_TRIANGLE_FAN);
//with positions from the data of the class
ofVertex(...);
glEnd();

you will easily find some other posts about it on the forum, like here with pretty good explanations (@zach).

Hope this helps,

++

P