我在计算着色器方面遇到了一个奇怪的问题,因为我更改了传递给着色器的缓冲区的结构大小。
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);
***********************已编辑安东的评论*********************** ******* 这是 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)
};