我正在尝试借助 drawBuffers 编写简单的 webgl 多通道程序。我创建了 2 个 drawBuffer 纹理并在 fragmentShaderPass1 中为它们分配颜色。Texture1 应该是绿色的,Texture2 应该是棕色的。然后我将这些纹理作为制服传递给 fragmentShaderPass2,它使用 Texture1 的颜色来渲染矩形。问题是矩形总是黑色而不是绿色!我在这里做错了什么?
// STAGE 1
let fragmentShaderPass1 = `
#extension GL_EXT_draw_buffers : require
precision highp float;
precision highp int;
precision highp sampler2D;
void main( void )
{
gl_FragData[0] = vec4(.2, .8, .0, 1); // green
gl_FragData[1] = vec4(.6, .5, .4, .3); // brown
}
`;
let pass1_prog = createProgram(gl, globals.vertexShader, fragmentShaderPass1);
let firstStageFrameBuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, firstStageFrameBuffer);
gl.useProgram(pass1_prog);
for (let i = 0; i < 2; i++)
{
let tex = gl.createTexture();
textures.push(tex);
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, canvas.width, canvas.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
// attach texture to framebuffer
gl.framebufferTexture2D(gl.FRAMEBUFFER, ext.COLOR_ATTACHMENT0_WEBGL + i, gl.TEXTURE_2D, tex, 0);
}
gl.disable(gl.DEPTH_TEST); // framebuffer doesn't even have a depth buffer!
gl.viewport(0, 0, _this.canvas.width, _this.canvas.height);
ext.drawBuffersWEBGL([
ext.COLOR_ATTACHMENT0_WEBGL,
ext.COLOR_ATTACHMENT1_WEBGL
]);
gl.drawArrays(gl.POINTS, 0, 1);
// STAGE 2
let fragmentShaderPass2 = `
precision highp float;
precision highp int;
precision highp sampler2D;
uniform sampler2D inColor0;
uniform sampler2D inColor1;
void main( void )
{
gl_FragColor = texture2D(inColor0, vec2(0.5));
}
`;
let pass2_prog = createProgram(gl, globals.vertexShader, fragmentShaderPass2);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.useProgram(pass2_prog);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[0]);
gl.uniform1i(gl.getUniformLocation(pass2_prog, "inColor0"), 0);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, textures[1]);
gl.uniform1i(gl.getUniformLocation(pass2_prog, "inColor1"), 1);
gl.drawArrays(gl.POINTS, 0, 1);