由于我更改了传入着色器的缓冲区的结构大小,因此计算着色器存在一个怪异的问题。

struct Particle
{
    vec3 position;
    vec2 uv;
    vec3 accumulated_normal;
    int id;
    int flattened_id;
    int movable;
    // can the particle move or not ? used to pin parts of the cloth
    float mass;
    // the mass of the particle (is always 1 in this example)
    vec3 old_pos;
    // the position of the particle in the previous time step, used as part of the verlet numerical integration scheme
    vec3 acceleration;
    // a vector representing the current acceleration of the particle
};

这样定义。 我在尝试使用扁平化的id时遇到问题,因此我想将id的内容写回到传入的缓冲区中。着色器看起来像这样。

layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;

void main()
{
    unsigned int flattened_id = gl_LocalInvocationIndex;

    particleBuffer.particles[0].id = 16;
    particleBuffer.particles[1].id = 17;
    particleBuffer.particles[2].id = 18;
    particleBuffer.particles[3].id = 19;
    //particleBuffer.particles[4].id = 20;
}

所以直到这一点都没问题,但是当我取消注释最后一行,即particleBuffer.particles [4]时,网格从屏幕上消失了。 我之前已经设法从该网格更改了位置数据,但这似乎很奇怪。 我验证了我确实传入了缓冲区的16个元素,因此也不应该超出范围(例如,cloth1.particles.size()= 16)。 有任何想法吗 ?? 整个代码在这里。.https ://github.com/ssarangi/OpenGL_Cloth/tree/master/cloth_4_3_compute

glUseProgram(computeShader);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, cloth1.vertex_vbo_storage);
glBufferData(GL_SHADER_STORAGE_BUFFER, cloth1.particles.size() * sizeof(Particle), &(cloth1.particles[0]), GL_DYNAMIC_COPY);

glDispatchCompute(1, 1, 1);

{
    GLenum err = gl3wGetError();
}

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, cloth1.vertex_vbo_storage);
Particle * ptr = reinterpret_cast<Particle *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, cloth1.particles.size() * sizeof(Particle), GL_MAP_READ_BIT));

memcpy(&cloth1.particles[0], ptr, cloth1.particles.size()*sizeof(Particle));
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);

***********************编辑有Andon的评论*********************** *******这是C ++方面的新布局。

struct Particle
{
    vec4 position;
    vec2 uv;
    vec4 accumulated_normal;
    vec4 old_pos; // the position of the particle in the previous time step, used as part of the verlet numerical integration scheme
    vec4 acceleration; // a vector representing the current acceleration of the particle
    int id;
    int flattened_id;
    int movable; // can the particle move or not ? used to pin parts of the cloth
    float mass; // the mass of the particle (is always 1 in this example)

GLSL侧面定义。 我不确定的是,是否需要在glsl结构中包含填充元素。 它仍然不会更新ID。

struct Particle
{
    vec4 position;
    vec2 uv;
    vec4 accumulated_normal;
    vec4 old_pos; // the position of the particle in the previous time step, used as part of the verlet numerical integration scheme
    vec4 acceleration; // a vector representing the current acceleration of the particle
    int id;
    int flattened_id;
    int movable; // can the particle move or not ? used to pin parts of the cloth
    float mass; // the mass of the particle (is always 1 in this example)
};

  ask by ssarangi translate from so

本文未有回复,本站智能推荐: