我正在尝试几种方法来实现一个简单的粒子系统。在这里,我在几个纹理之间使用了乒乓技术,所有纹理都附加到一个独特的 fbo。
我认为所有绑定/设置都是正确的,因为我看到它使用来自 A 纹理的数据写入 B 纹理。
问题是 B 纹理中只有一个纹素被写入:
在这张图片中,我尝试将纹素从源纹理复制到目标纹理。
那么让我们来看看代码:
设置代码
#define NB_PARTICLE 5
GLuint fbo;
GLuint tex[6];
GLuint tex_a[3];
GLuint tex_b[3];
int i;
// 3 textures for position/vitesse/couleur
// we double them for ping-pong so 3 * 2 = 6 textures
glGenTextures(6, tex);
for (i = 0 ; i < 6 ; i++ )
{
glBindTexture(GL_TEXTURE_1D, tex[i]);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, NB_PARTICLE, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
for (i = 0; i < 3; i++)
{
tex_a[i] = tex[i];
tex_b[i] = tex[i + 3];
}
// Uploads particle data from "a" textures
glBindTexture(GL_TEXTURE_1D, tex_a[0]);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, NB_PARTICLE, 0, GL_RGBA, GL_FLOAT, seed_pos);
glBindTexture(GL_TEXTURE_1D, tex_a[1]);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, NB_PARTICLE, 0, GL_RGBA, GL_FLOAT, seed_vit);
glBindTexture(GL_TEXTURE_1D, tex_a[2]);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, NB_PARTICLE, 0, GL_RGBA, GL_FLOAT, seed_color);
// Create the fbo
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
// Attach the textures to the corresponding fbo color attachments
int i;
for (i = 0; i < 6; i++)
glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_1D, tex[i], 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
渲染代码
static int pingpong = 1;
glUseProgram(integratorShader);
glBindVertexArray(0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
// Set the input textures to be the "a" textures
int i;
for (i = 0 ; i < 3 ; i++ )
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_1D, tex_a[i]);
}
// Set the draw buffers to be the "b" textures attachments
glDrawBuffers(3, GL_COLOR_ATTACHMENT0 + (pingpong * 3) );
glViewport(0, 0, NB_PARTICLE, 1);
glDrawArrays(GL_POINTS, 0, NB_PARTICLE);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glUseProgram(0);
// A became B and B became A
swapArray(tex_a, tex_b, 3);
pingpong++;
pingpong %= 2;
顶点着色器
#version 430
void main() {
gl_Position = vec4(gl_VertexID, 0, 0, 1);
}
片段着色器
#version 430
// binding = texture unit
layout (binding = 0) uniform sampler1D position_texture;
layout (binding = 1) uniform sampler1D vitesse_texture;
layout (binding = 2) uniform sampler1D couleur_texture;
// location = index in the "drawBuffers" array
layout (location = 0) out vec4 position_texel;
layout (location = 1) out vec4 vitesse_texel;
layout (location = 2) out vec4 couleur_texel;
void main() {
vec4 old_position_texel = texelFetch(position_texture, int(gl_FragCoord.x), 0);
vec4 old_vitesse_texel = texelFetch(vitesse_texture, int(gl_FragCoord.x), 0);
vec4 old_couleur_texel = texelFetch(couleur_texture, int(gl_FragCoord.x), 0);
position_texel = old_position_texel;
vitesse_texel = old_vitesse_texel;
couleur_texel = old_couleur_texel;
}
当我使用一维纹理时,我认为我需要发送的唯一数据是一个索引,我可以完美地使用 gl_VertexID 。这就是为什么我要发送 0 个属性数据。
我认为问题在于我设置 gl_FragCoord 的方式(遗憾的是它是我无法调试的唯一变量:()