Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firework opengl

Tags:

c++

opengl

I'm trying to create a firework with OpenGL (I must put 100 particles in the position (0,0,0)) with the function

Particle *p[100];

void Build()
{

    for (int i = 1; i <= 100; i++)
    {



    p[i]->pos.x = 0.0;
    p[i]->pos.y = 1.0;
    p[i]->pos.z = 5.0;

    p[i]=AddParticle(*p[i]);

    }
}

but I get the following error:

Unhandled exception at 0x771b15de in ass.exe: 0xC0000005: Access violation writing location 0x00000000.

this is the rest of the code:

class Particle
{
    public:

Vector3 pos;        // current position
Vector3 vel;        // velocity
Vector3 restPos;    // rest (initial) position
Vector3 oldPos;     // previous position

Vector3 acc;        // acceleration

Particle()
{
    oldPos = restPos = pos = Vector3(0, 0, 0);
    Init();
}

Particle(float x, float y, float z)
{
    oldPos = restPos = pos = Vector3(x, y, z);
    Init();
}

Particle(const Vector3 & _p)
{
    oldPos = restPos = pos = _p;
    Init();
}

void Init()
{
    acc = Vector3(0, 0, 0);
    vel = Vector3(0, 0, 0);
}

void Update(const float & time_step)
{
    Verlet(time_step);
}


// integration step with Verlet
void Verlet(const float & time_step)
{
    Vector3  temp = pos;

    pos += vel * time_step + acc * time_step * time_step ;
    vel = (temp - oldPos) / time_step;

    oldPos = temp;
}       
};

# endif // _PARTICLE__





using namespace std;

class ParticleSystem 
{
vector<Particle>   _particles;      // the particles

Vector3     m_vGravity;             // gravity force applied to the particles system
float       m_fTimeStep;            // time step

Vector3 attractor;

public:

ParticleSystem()
{
    m_vGravity = Vector3(0, -9.81f, 0);
    m_fTimeStep = TIME_STEP;    

    attractor = Vector3(0, 0, 0);
}

void Reset()
{
    _particles.clear();
}

// accessing the fields

void SetGravity(Vector3 g)  {       m_vGravity = g;}

void SetTimeStep(float ts)  {       m_fTimeStep = ts;}

// adding a particle
Particle* AddParticle(Particle _p)
{
    _particles.push_back(_p);

    return &(_particles.back());
}



void Build()
{

    for (int i = 1; i <= 100; i++)
    {


    Particle p;
    p.pos.x = 0.0;
    p.pos.y = 1.0;
    p.pos.z = 5.0;

    p[i]=AddParticle(p);

    }
}



void Draw()
{
    // draw round points
    glPointSize(4.f);
    glEnable(GL_POINT_SMOOTH);
    glAlphaFunc(GL_GREATER,0.5f); 
    glEnable(GL_ALPHA_TEST); 
    glEnable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);

    // draws the particles
    glBegin(GL_POINTS);

    glColor3f(1.f, 0.f, 0.f);
    vector<Particle>::iterator pIt;
    for(pIt = _particles.begin(); pIt != _particles.end(); pIt++) 
    {
        Vector3& pos = pIt->pos;
        glVertex3f(pos.x, pos.y, pos.z);
    }

    glEnd();    

    glEnable(GL_LIGHTING);


}


#endif // __PARTICLE_SYSTEM__
like image 549
Jen Avatar asked Feb 19 '23 04:02

Jen


2 Answers

You've declared an array of pointers to Particles, but not actually allocated any of them.

(and as someone else points out, arrays are 0 indexed, not 1 - so your loop is out by 1 anyway)

It's not entirely clear how this is supposed to work, as you seem to be filling in a particle structure, which you pass to AddParticle(), which returns a pointer to a particle, which you put back in the array you've already tried to reference.

Looking at your code, you probably just need something like:

void Build()
{
    for (int i = 1; i <= 100; i++)
    {
        AddParticle(Particle(0.f, 1.f, 5.f));
    }
}

No array needed as the particle class looks after the particles.

like image 176
JasonD Avatar answered Feb 27 '23 22:02

JasonD


i think it's because the array goes from 0 to 99...not 1 to 100 .

Change the for statement to for (int i = 0; i < 100; i++) and remember the array starts with a 0

Also i think i know what you're trying to do..try this code:

void Build()
{
 Particle p[100];
for (int i = 0; i < 100; i++)
 {
   p[i].pos.x = 0.0;
   p[i].pos.y = 1.0;
   p[i].pos.z = 5.0;

   AddParticle(p[i]);

 }
}
like image 25
skytz Avatar answered Feb 27 '23 22:02

skytz