【发布时间】:2015-08-13 07:08:33
【问题描述】:
我正在尝试在纹理上渲染我的场景(立方体)。然后我想用这个纹理渲染同一个立方体。 (见最后的编辑) 我是 webgl 和 javascript 的新手。我用this webpage 作为开始。我还阅读并使用了this。
纹理似乎创建正确(没有错误或警告),但 the result 不是我所期望的。
第一张图片是具有“真实”纹理的立方体。第二张图片是使用创建的纹理渲染的立方体。 (两幅图像均未出现错误或警告)
由于某种原因,我无法在没有收到警告的情况下调用 gl.viewport(不显示任何内容):
RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete'
我的问题可能与此有关,但我不知道如何解决。
代码的重要部分:
function drawScene() {
gl.bindFramebuffer(gl.FRAMEBUFFER, rttFramebuffer);
drawOnCube();
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
[...]
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, rttTexture); //rttTexture is the created texure
gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);
[...]
}
rq:除了前 3 行和使用的纹理外,绘制场景与 drawOnCube 相同。
function drawOnCube(){
//gl.viewport(0, 0, rttFramebuffer.width, rttFramebuffer.height); //
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
perspectiveMatrix = makePerspective(45, rttFramebuffer.width/rttFramebuffer.height, 0.1, 300.0);
loadIdentity();
mvTranslate([-0.0, 0.0, -6.0]);
mvPushMatrix();
mvRotate(cubeRotation, [1, 0, 1]);
// Draw the cube by binding the array buffer to the cube's vertices
// array, setting attributes, and pushing it to GL.
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesBuffer);
gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesTextureCoordBuffer);
gl.vertexAttribPointer(textureCoordAttribute, 2, gl.FLOAT, false, 0, 0);
// Specify the texture to map onto the faces.
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, cubeTexture);
gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);
// Draw the cube.
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);
}
创建frameBuffer的函数,renderBuffer:
function initTextureFramebuffer(){
rttFramebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, rttFramebuffer);
rttFramebuffer.width = 256;
rttFramebuffer.height = 256;
rttTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, rttTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, rttFramebuffer.width, rttFramebuffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.generateMipmap(gl.TEXTURE_2D);
var renderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, rttFramebuffer.width, rttFramebuffer.height);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttTexture, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
编辑:我用硬编码值替换了 gl.viewportWidth 和 gl.viewportHeight。我仍然收到警告,但显示了多维数据集。纹理似乎一次只在一个面上使用,并且在大多数情况下(当立方体旋转时)是“不完整的”。 Pictures. 我检查了我的 TextCoordBuffer,它看起来没问题。 我试图只渲染立方体的一个面,但我仍然遇到了问题。
【问题讨论】:
标签: javascript opengl-es webgl