2012-12-13 38 views
4

我试图与功能烟花的OpenGL

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的烟花(我必须把100个粒子的位置(0,0,0)),但我得到以下错误:

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__ 
+1

为什么不'性病:: VECTOR'? – genpfault

+0

向我们展示更多您的代码。你在哪里分配你的数组元素? –

回答

7

您已经声明指针数组的颗粒,但实际上没有分配任何人。

(和其他人所指出的,阵列0索引,而不是1 - 所以你的循环是从1反正)

这并不完全清楚这是如何工作的,你似乎被灌在您传递给AddParticle()的粒子结构中,它返回一个指向粒子的指针,该指针放回数组中,您已经尝试引用它。

看你的代码,你可能只需要类似:

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

需要作为颗粒类无阵列看起来颗粒之后。

+0

这很难说,因为我们不知道Particle是什么,也不知道AddParticle实际上做了什么。我有一个猜测,但如果它没有帮助,你需要给我们一些更多的信息。 – JasonD

+0

我添加了其余的代码 – Jen

+0

谢谢!现在它工作! – Jen

1

我认为这是因为数组从0到99 ...不是1到100。

更改为语句for (int i = 0; i < 100; i++)并记住阵列0

而且我想我知道你想do..try这段代码是什么开始:

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

} 
}