c++ - 烟花opengl

标签 c++ opengl

我正在尝试使用 OpenGL 创建一个烟花(我必须在位置 (0,0,0) 中放置 100 个粒子)

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]);

    }
}

但我收到以下错误:

ass.exe 中 0x771b15de 处的未处理异常:0xC0000005:访问冲突写入位置 0x00000000。

这是剩下的代码:

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__

最佳答案

您已经声明了一个指向粒子的指针数组,但实际上并未分配其中任何一个。

(正如其他人指出的那样,数组是 0 索引的,而不是 1 - 所以你的循环无论如何都是 1)

目前还不完全清楚这是如何工作的,因为您似乎正在填充一个粒子结构,您将其传递给 AddParticle(),它返回一个指向粒子的指针,您将其放回数组中我已经尝试引用了。

看看你的代码,你可能只需要这样的东西:

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

不需要数组,因为粒子类负责管理粒子。

关于c++ - 烟花opengl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13869440/

相关文章:

具有隐式参数的 C++ 函数模板实例化

c++ - QPainter 和 QOpenGLWidget 类中的 OpenGL native 代码

c++ - mOffsetMatrix 在 Assimp 中实际上做了什么?

c++ - glFog导致透明png失去透明度

c++ - 无法将着色器发送到 GPU 内存

c++ - ostream::operator<< 不与 shared_ptr 中的 operator-> 一起工作

c++ - 函数模板中的类型参数

java - 如何阻止草地拖慢我的游戏速度?

c++ - 错误 : g++. exe : No such file or directory g++. exe : fatal error: no input files in Visual studio code?

c++ - 将巨大的 boost dynamic_bitset vector 写入文件并将其读回的有效方法