gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:53:13 25 4
gpt4 key购买 nike

我正在尝试使用帧缓冲区渲染立方体贴图,但出现“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 的纹理并且不设置其过滤,它将失败。

关于javascript - 使用帧缓冲区渲染到立方体贴图纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38854832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com