Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 焰火opengl_C++_Opengl - Fatal编程技术网

C++ 焰火opengl

C++ 焰火opengl,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]); } } 粒

我正在尝试用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]);

    }
}
粒子*p[100];
void Build()
{
对于(int i=1;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__
类粒子
{
公众:
矢量3位置;//当前位置
Vector3 vel;//速度
Vector3 restPos;//静止(初始)位置
向量3 oldPos;//上一个位置
矢量3 acc;//加速度
粒子()
{
oldPos=restPos=pos=Vector3(0,0,0);
Init();
}
粒子(浮动x、浮动y、浮动z)
{
oldPos=restPos=pos=Vector3(x,y,z);
Init();
}
粒子(常数向量3和p)
{
oldPos=restPos=pos=\u p;
Init();
}
void Init()
{
acc=矢量3(0,0,0);
vel=矢量3(0,0,0);
}
无效更新(常量浮动和时间步长)
{
Verlet(时间步);
}
//与Verlet的集成步骤
void Verlet(常量浮动和时间步)
{
矢量3温度=位置;
pos+=vel*时间步长+acc*时间步长*时间步长;
水平=(温度-旧位置)/时间步长;
oldPos=温度;
}       
};
#endif/\u粒子__
使用名称空间std;
类分词系统
{
向量_粒子;//粒子
Vector3 m_vGravity;//施加在粒子系统上的重力
float m_fTimeStep;//时间步长
矢量3吸引子;
公众:
粒子系统()
{
m_vGravity=Vector3(0,-9.81f,0);
m_fTimeStep=时间步长;
吸引子=向量3(0,0,0);
}
无效重置()
{
_粒子。清除();
}
//访问字段
void SetGravity(vector3g){m_vGravity=g;}
void SetTimeStep(float ts){m_fTimeStep=ts;}
//添加粒子
粒子*添加粒子(粒子_p)
{
_粒子。推回(p);
返回(_particles.back());
}
void Build()
{
对于(int i=1;i pos;
glVertex3f(位置x、位置y、位置z);
}
格伦德();
glEnable(德国劳埃德大学照明);
}
#endif/\粒子系统__

您声明了一个指向粒子的指针数组,但实际上没有分配任何指针

(正如其他人指出的那样,数组的索引是0,而不是1——因此,您的循环以1为单位)

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

查看您的代码,您可能只需要以下内容:

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

对于(inti=1;i我认为这是因为数组从0到99…而不是从1到100

将for语句更改为
for(int i=0;i<100;i++)
,记住数组以0开头

另外,我想我知道你想做什么..试试下面的代码:

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

 }
}
void Build()
{
粒子p[100];
对于(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]);
}
}

向我们展示更多您的代码。您在哪里分配数组元素?这很难说,因为我们不知道什么是粒子,也不知道AddParticle实际做了什么。我有一个猜测,但如果没有帮助,您需要向我们提供更多信息。