Hello, I am writing a particle system, and I am trying to load particle objects into a vector.
Everytime I create a particle, it starts with a lifetime of 50.0f. The particle also starts at 0,0,0.
Every frame the particle increases the y value by 0.01f.
Also, all the particle are iterated through, and if the lifetime is <= 0.0f, then I swap and pop it from the list.
At first it seems like it should work without any problems at all, but...
for some strange reason, the y values seem to be doubled practicly as soon as the 50th particle is created and the particles are updated. It's like every particle that gets created asap, gets pushed above the 0,0,0 point, and new ones get pushed even farther. It's soooo weird D:!
Here is the code for the particles:
while(!glfwWindowShouldClose(window))
{
particle particle1;
particle1.alive = true;
particle1.lifeTime = 50.0f;
particle1.x = 0.0f;
particle1.y = 0.0f;
particle1.z = 0.0f;
particleList.push_back(particle1);
MessageBox(0, "m", "m", MB_OK);
unsigned int currentSize = particleList.size();
for(unsigned int iterator = 0;
iterator < currentSize;
iterator++)
{
particleList[iterator].lifeTime -= 1.0f;
if(particleList[iterator].lifeTime <= 0.0f)
{
std::swap(particleList[iterator], particleList[particleList.size() - 1]);
particleList.pop_back();
iterator--;
}
else
{
}
particleList[iterator].y += 0.01f;
currentSize = particleList.size();
}
I don't know why this is happening . It doesn't make any sense at all and I can't think of anything.
Here is the rendering code, although this is more of a general programming question I believe, but just in case:
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 streamedPositions[1000];
glm::vec4 streamedColors[1000];
//MessageBox(0, "l", "l", MB_OK);
for(unsigned int pIndex = 0; pIndex < particleList.size(); pIndex++)
{
translationMatrix = glm::translate(glm::mat4(1.0f),
glm::vec3(particleList[pIndex].x, particleList[pIndex].y, -10.0f));
worldMatrix = translationMatrix;
streamedColors[pIndex] = glm::vec4(1.0f, 1.0f, (float)particleList[pIndex].lifeTime / 50.0f, 1.0f);
streamedPositions[pIndex] = worldMatrix;
}
glBindBuffer(GL_ARRAY_BUFFER, streamedColorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 1000, streamedColors, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 4 * particleList.size() , streamedColors);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, streamedPositionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16 * 1000, (GLfloat*)&streamedPositions, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 16 * particleList.size(), (GLfloat*)&streamedPositions);
for(unsigned int i = 0; i < 4; i++)
{
glVertexAttribPointer(3+i, 4, GL_FLOAT, GL_FALSE, sizeof(float)*16.0f, (void*)0 + ((sizeof(float) * 4 * i)));
}
glUseProgram(testProgram);
location = glGetUniformLocation(testProgram, "worldMatrix");
glUniformMatrix4fv(location, 1, GL_FALSE, (GLfloat*)&worldMatrix);
location = glGetUniformLocation(testProgram, "viewMatrix");
glUniformMatrix4fv(location, 1, GL_FALSE, (GLfloat*)&viewMatrix);
location = glGetUniformLocation(testProgram, "projectionMatrix");
glUniformMatrix4fv(location, 1, GL_FALSE, (GLfloat*)&projectionMatrix);
location = glGetUniformLocation(testProgram, "diffuse");
glUniform1i(location, 0);
glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 0);
glVertexAttribDivisor(2, 1);
glVertexAttribDivisor(3, 1);
glVertexAttribDivisor(4, 1);
glVertexAttribDivisor(5, 1);
glVertexAttribDivisor(6, 1);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, particleList.size());
glfwSwapBuffers(window);
glfwPollEvents();
The rendering code works fine as far as I can tell, and I am using instanced rendering.