<commit_before>#include "rendering/particles/particlesystem.h"

float ParticleSystem::ticksPerSecond = 60.0f;
float ParticleSystem::secondsPerTick = 1.0f/60.0f;

ParticleSystem::ParticleSystem(unsigned int particleCount):particleCount(particleCount)
{
    particles.resize(particleCount);
}

void ParticleSystem::init(){


    for(unsigned int i=0;i<particleCount;++i){
        Particle p;
        p.position = glm::sphericalRand(15.0f);
        p.velocity = vec4(glm::sphericalRand(1.0f),1);
        addParticle(p);
    }

    particleBuffer.set(particles);
    particleBuffer.setDrawMode(GL_POINTS);

    initialized = true;
}

void ParticleSystem::reset()
{
    if (!initialized) return;

    for(unsigned int i=0;i<particleCount;++i){
        Particle& p = particles[i];
        p.lifetime = 0.f;
        p.position = vec3(0);
    }

    particleBuffer.updateVertexBuffer(&particles[0],particles.size(),0);
}

void ParticleSystem::update()
{
    tick++;

    if( uploadDataNextUpdate ){
        updateParticleBuffer();
        uploadDataNextUpdate = false;
    }
}


void ParticleSystem::render(Camera *cam, float interpolation){
    //    cout<<tick<<" ParticleSystem::renderr()"<<endl;

    if(blending){
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    particleShader->bind();

    particleShader->uploadAll(model,cam->view,cam->proj);
    particleShader->uploadTexture(arrayTexture);

    particleShader->uploadTiming(tick,interpolation);
    particleShader->uploadTimestep(secondsPerTick);

//    particleBuffer.bindAndDraw();
    //draw old particles first, so new ones are on top
    particleBuffer.bind();
    particleBuffer.draw(nextParticle,particleCount-nextParticle);
    particleBuffer.draw(0,nextParticle);
    particleBuffer.unbind();

    particleShader->unbind();

    if(blending){
        glDisable(GL_BLEND);
    }
}

void ParticleSystem::renderDeferred(Camera *cam, float interpolation, raw_Texture* d)
{

    deferredParticleShader->bind();

    deferredParticleShader->uploadAll(model,cam->view,cam->proj);
    deferredParticleShader->uploadTexture(arrayTexture);
    deferredParticleShader->uploadDepthTexture(d);
    deferredParticleShader->uploadTiming(tick,interpolation);
    deferredParticleShader->uploadTimestep(secondsPerTick);

//    particleBuffer.bindAndDraw();

    particleBuffer.bind();
    particleBuffer.draw(nextParticle,particleCount-nextParticle);
    particleBuffer.draw(0,nextParticle);
    particleBuffer.unbind();

    deferredParticleShader->unbind();

}


void ParticleSystem::addParticle(Particle &p){
    p.start = tick+1;


    particles[nextParticle] = p;
    nextParticle = (nextParticle+1)%particleCount;

}

Particle &ParticleSystem::getNextParticle()
{
    Particle &p = particles[nextParticle];
    nextParticle = (nextParticle+1)%particleCount;
    p.start = tick+1;

    return p;
}

void ParticleSystem::updateParticleBuffer(){
    //    cout<<tick<<" ParticleSystem::updateParticleBuffer()"<<endl;

    if(nextParticle>saveParticle){
        int size = (nextParticle-saveParticle);
        int offset = saveParticle;

        particleBuffer.updateVertexBuffer(&particles[saveParticle],size,offset);
    }

    if(nextParticle<saveParticle){
        int size = (particleCount-saveParticle);
        int offset = saveParticle;
        particleBuffer.updateVertexBuffer(&particles[saveParticle],size,offset);

        size = (nextParticle);
        offset = 0;<commit_msg>particle reset<commit_after>#include "rendering/particles/particlesystem.h"

float ParticleSystem::ticksPerSecond = 60.0f;
float ParticleSystem::secondsPerTick = 1.0f/60.0f;

ParticleSystem::ParticleSystem(unsigned int particleCount):particleCount(particleCount)
{
    particles.resize(particleCount);
}

void ParticleSystem::init(){


    for(unsigned int i=0;i<particleCount;++i){
        Particle p;
        p.position = glm::sphericalRand(15.0f);
        p.velocity = vec4(glm::sphericalRand(1.0f),1);
        addParticle(p);
    }

    particleBuffer.set(particles);
    particleBuffer.setDrawMode(GL_POINTS);

    initialized = true;
}

void ParticleSystem::reset()
{
    if (!initialized) return;

    for(unsigned int i=0;i<particleCount;++i){
        Particle& p = particles[i];
        p.lifetime = 0.f;
        p.position = vec3(0);
    }

    particleBuffer.updateVertexBuffer(&particles[0],particles.size(),0);
}

void ParticleSystem::update()
{
    tick++;

    if( uploadDataNextUpdate ){
        updateParticleBuffer();
        uploadDataNextUpdate = false;
    }
}


void ParticleSystem::render(Camera *cam, float interpolation){
    //    cout<<tick<<" ParticleSystem::renderr()"<<endl;

    if(blending){
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    particleShader->bind();

    particleShader->uploadAll(model,cam->view,cam->proj);
    particleShader->uploadTexture(arrayTexture);

    particleShader->uploadTiming(tick,interpolation);
    particleShader->uploadTimestep(secondsPerTick);

//    particleBuffer.bindAndDraw();
    //draw old particles first, so new ones are on top
    particleBuffer.bind();
    particleBuffer.draw(nextParticle,particleCount-nextParticle);
    particleBuffer.draw(0,nextParticle);
    particleBuffer.unbind();

    particleShader->unbind();

    if(blending){
        glDisable(GL_BLEND);
    }
}

void ParticleSystem::renderDeferred(Camera *cam, float interpolation, raw_Texture* d)
{

    deferredParticleShader->bind();

    deferredParticleShader->uploadAll(model,cam->view,cam->proj);
    deferredParticleShader->uploadTexture(arrayTexture);
    deferredParticleShader->uploadDepthTexture(d);
    deferredParticleShader->uploadTiming(tick,interpolation);
    deferredParticleShader->uploadTimestep(secondsPerTick);

//    particleBuffer.bindAndDraw();

    particleBuffer.bind();
    particleBuffer.draw(nextParticle,particleCount-nextParticle);
    particleBuffer.draw(0,nextParticle);
    particleBuffer.unbind();

    deferredParticleShader->unbind();

}


void ParticleSystem::addParticle(Particle &p){
    p.start = tick+1;


    particles[nextParticle] = p;
    nextParticle = (nextParticle+1)%particleCount;

}

Particle &ParticleSystem::getNextParticle()
{
    Particle &p = particles[nextParticle];
    p = Particle();
    nextParticle = (nextParticle+1)%particleCount;
    p.start = tick+1;

    return p;
}

void ParticleSystem::updateParticleBuffer(){
    //    cout<<tick<<" ParticleSystem::updateParticleBuffer()"<<endl;

    if(nextParticle>saveParticle){
        int size = (nextParticle-saveParticle);
        int offset = saveParticle;

        particleBuffer.updateVertexBuffer(&particles[saveParticle],size,offset);
    }

    if(nextParticle<saveParticle){
        int size = (particleCount-saveParticle);
        int offset = saveParticle;
        particleBuffer.updateVertexBuffer(&particles[saveParticle],size,offset);

        size = (nextParticle);
        offset = 0;
{'fraction_non_alphanumeric': 0.12677063957647733, 'fraction_numerical': 0.008298755186721992, 'mean_word_length': 4.158671586715867, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3214291', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2157, 'n_words': 300}