使用帧缓冲区渲染到立方体贴图纹理

Rendering to a cubemap texture with a framebuffer

我正在尝试使用帧缓冲区渲染到立方体贴图,但出现 "FRAMEBUFFER_INCOMPLETE_ATTACHMENT" 错误。我可以使用带有 2d 纹理的代码,类型设置为 FLOAT 或 UNSIGNED_BYTE。我设置纹理立方体参数的方式或在这段代码中附加的方式是否有错误:

this.inscatterTexture_ = gl.createTexture();
gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.inscatterTexture_);

gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);       

for (let i = 0; i < 6; i++) {
    // Create framebuffer
    this.inscatterFrameBuffers_[i] = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, this.inscatterFrameBuffers_[i]);

    // Create and attach depth buffer
    this.inscatterDepthBuffers_[i] = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, this.inscatterDepthBuffers_[i]);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, INSCATTER_RESOLUTION, INSCATTER_RESOLUTION);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.inscatterDepthBuffers_[i]);
    gl.bindRenderbuffer(gl.RENDERBUFFER, null);

    // Attach one face of cube map
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.RGBA, INSCATTER_RESOLUTION, INSCATTER_RESOLUTION, 0, gl.RGBA, gl.FLOAT, null);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, this.inscatterTexture_, 0);

    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
      let status_code = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
      console.log("Inscatter frame buffer, " + i + ", is not complete: " + FramebufferStatus[status_code]);
    }

    this.CreateInscatterTexture(gl, i);
}

显然您需要先创建立方体贴图的所有面。

"use strict";

function log() {
  var pre = document.createElement("pre");
  pre.appendChild(document.createTextNode(Array.prototype.join.call(arguments, " ")));
  document.body.appendChild(pre);
}

function glEnumToString(gl, value) {
  for(var key in gl) {
    if (gl[key] === value) {
      return key;
    }
  }
  return "0x" + value.toString(16);
}

var INSCATTER_RESOLUTION = 64;

var gl = document.createElement("canvas").getContext("webgl");
var ext = gl.getExtension("OES_texture_float");
if (!ext) { log("need OES_texture_float"); }
ext = gl.getExtension("OES_texture_float_linear");
if (!ext) { log("need OES_texture_float_linear"); }
           
var o = {};
(function() {
  this.inscatterFrameBuffers_ = [];
  this.inscatterDepthBuffers_ = [];
  this.inscatterTexture_ = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.inscatterTexture_);

  gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);       
  for (let i = 0; i < 6; i++) {
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.RGBA, INSCATTER_RESOLUTION, INSCATTER_RESOLUTION, 0, gl.RGBA, gl.FLOAT, null);
  }

  for (let i = 0; i < 6; i++) {
    // Create framebuffer
    this.inscatterFrameBuffers_[i] = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, this.inscatterFrameBuffers_[i]);

    // Create and attach depth buffer
    this.inscatterDepthBuffers_[i] = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, this.inscatterDepthBuffers_[i]);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, INSCATTER_RESOLUTION, INSCATTER_RESOLUTION);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.inscatterDepthBuffers_[i]);
    gl.bindRenderbuffer(gl.RENDERBUFFER, null);

    // Attach one face of cube map
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, this.inscatterTexture_, 0);

    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
      let status_code = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
      log("Inscatter frame buffer, " + i + ", is not complete: " + glEnumToString(gl, status_code));
    } else {
      log("success");
    }

  }
}).call(o);

这似乎是驱动程序错误。我知道至少在过去,Nvidia 要求纹理在提供 FRAMEBUFFER_COMPLETE 之前可渲染,即使您可能尚未使用该纹理进行渲染。例如,如果您制作没有 mips 的纹理并且不设置其过滤,它将失败。