我的粒子系统中的ssbo未更新

时间:2018-12-08 13:06:43

标签: glsl

我在粒子系统上工作,我想使用SSBO通过计算着色器更新粒子上的速度和位置。但是我看到对于每个更新调用,计算使用相同的位置值,但是计算更新位置,因为在绘制调用中粒子被移动。

将粒子加载到SSBO中

 $.ajax({
    'async': false,
    'type': "POST",
    'global': false,
    'dataType': 'html',
    'url': "barRules.bc",
    'data': { 'id': JSON.stringify(id)  },
    'success':(response)=>{
        tmp = response;
    }
});

更新

// Load Positions
glGenBuffers(1, &m_SSBOpos);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_SSBOpos);


// Allocation de la mémoire vidéo

glBufferData(GL_SHADER_STORAGE_BUFFER, pb.size() * 4 * sizeof(float), NULL, GL_STATIC_DRAW);


GLint bufMask = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT; // the invalidate makes a big difference when re-writing
float *points = (float *) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, pb.size() * 4 * sizeof(float), bufMask);
for (int i = 0; i < pb.size(); i++)
{
    points[i * 4] = pb.at(i).m_Position.x;
    points[i * 4 + 1] = pb.at(i).m_Position.y;
    points[i * 4 + 2] = pb.at(i).m_Position.z;
    points[i * 4 + 3] = 0;
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

// Load vélocité

glGenBuffers(1, &m_SSBOvel);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_SSBOvel);

// Allocation de la mémoire vidéo

glBufferData(GL_SHADER_STORAGE_BUFFER, pb.size() * 4 * sizeof(float), NULL, GL_STATIC_DRAW);

float *vels = (float *)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, pb.size() * 4 * sizeof(float), bufMask);
for (int i = 0; i < pb.size(); i++)
{
    vels[i * 4] = pb.at(i).m_Velocity.x;
    vels[i * 4 + 1] = pb.at(i).m_Velocity.y;
    vels[i * 4 + 2] = pb.at(i).m_Velocity.z;
    vels[i * 4 + 3] = 0;
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

绘制

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, shaderUtil.getSSBOpos());
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, shaderUtil.getSSBOvel());

    // UPDATE DES PARTICULES
    shaderUtil.UseCompute();
    glUniform1i(shaderUtil.getDT(), fDeltaTime);
    glDispatchCompute(NUM_PARTICLES / WORK_GROUP_SIZE, 1, 1);
    glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
    shaderUtil.DeleteCompute();

计算机着色器

shaderUtil.Use();

    glUniformMatrix4fv(glGetUniformLocation(shaderUtil.getProgramID(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    glUniformMatrix4fv(glGetUniformLocation(shaderUtil.getProgramID(), "modelview"), 1, GL_FALSE, glm::value_ptr(View * Model));

    glPointSize(10);
    // Rendu
    glBindBuffer(GL_ARRAY_BUFFER, shaderUtil.getSSBOpos());
    glVertexPointer(4, GL_FLOAT, 0, (void *)0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_POINTS, 0, NUM_PARTICLES);
    glDisableClientState(GL_VERTEX_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
shaderUtil.Delete();

你知道为什么会发生吗?

0 个答案:

没有答案