【发布时间】:2016-08-07 20:54:01
【问题描述】:
我正在尝试绘制纹理,以便渲染不断叠加。 我正在使用两个纹理和两个帧缓冲区。 texture[0] 附加到 framebuffer[0],texture[1] 附加到 framebuffer[1]。
var gl = webgl.context;
gl.useProgram(this.program);
gl.bindTexture(gl.TEXTURE_2D, this.texture[this.pingpong]);
this.pingpong = (this.pingpong==0?1:0);
var pp = this.pingpong;
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer[pp]);
gl.drawArrays(gl.TRIANGLES, 0, 6); //primitiveType, offset, count
gl.bindTexture(gl.TEXTURE_2D, this.texture[pp]); //bind to the texture we just drew to
gl.bindFramebuffer(gl.FRAMEBUFFER, null); //render the above texture to the canvas
gl.drawArrays(gl.TRIANGLES, 0, 6);
问题是我没有看到之前的渲染被保存到纹理中。 我认为 bindframebuffer() 会使它渲染到纹理。 我的片段着色器:
precision mediump float; // fragment shaders don't have a default precision so we need to pick one. mediump is a good default
varying vec2 v_texCoord; // the texCoords passed in from the vertex shader.
uniform vec2 u_resolution; // a uniform
uniform vec2 u_mouse;
uniform sampler2D u_image; // this isn't set, so it will default to 0 (the current active texture)
void main() {
vec4 texColor = texture2D(u_image, v_texCoord); // Look up a color from the texture.
vec2 coord = vec2(gl_FragCoord.x,u_resolution.y-gl_FragCoord.y);
vec4 color = step(distance(coord,u_mouse),100.0)*vec4(1,0,0,1) + step(100.0,distance(coord,u_mouse))*texColor;
gl_FragColor = color; // gl_FragColor is a special variable a fragment shader is responsible for setting
}
u_image 设置为默认 0,即活动纹理。
有什么我忽略的吗?为什么以前的渲染不会相互叠加?它只是显示最新的渲染,就好像纹理没有被改变一样。
这是顶点着色器:
precision mediump float;
attribute vec2 a_position; // an attribute will receive data from a buffer
attribute vec2 a_texCoord;
varying vec2 v_texCoord; // a varying
uniform vec2 u_resolution; // a uniform
uniform vec2 u_mouse;
uniform float u_flip;
// all shaders have a main function
void main() {
v_texCoord = a_texCoord; // pass the texCoord to the fragment shader.The GPU will interpolate this value between points
vec2 zeroToOne = a_position / u_resolution; // convert the position from pixels to 0.0 to 1.0
vec2 zeroToTwo = zeroToOne * 2.0; // convert from 0->1 to 0->2
vec2 clipSpace = zeroToTwo - 1.0; // convert from 0->2 to -1->+1 (clipspace)
// gl_Position is a special variable a vertex shader is responsible for setting
gl_Position = vec4(clipSpace * vec2(1, u_flip), 0, 1);
}
【问题讨论】:
-
你能解释一下你期望着色器做什么吗?在鼠标光标周围画一个红色(+ 来自另一个纹理的颜色)圆圈?
-
是的,当我移动鼠标时,它会在鼠标光标周围画一个红色圆圈。我希望它保留以前的绘制,但由于某种原因它没有这样做,所以我只得到一个红色圆圈,而不是鼠标移动的一系列圆圈。我正在使用两个纹理和两个帧缓冲区,我认为它们可以用来绘制圆,然后在它们之间交替,以便保留以前的图形,但它不起作用。我不明白为什么。
-
我担心的是纹理似乎没有保留它们的数据。如果我在绑定帧缓冲区时绘制到它们,然后当我绑定纹理以获取颜色时,它应该具有先前渲染的图像,对吗?然而,当我阅读它们的颜色时,它似乎没有之前呈现的内容——它只是空白。
-
如果我尝试这样做,它看起来一样 - 只是鼠标周围有一个红色圆圈。
-
我用顶点着色器代码更新了帖子,以防有人看到错误?
标签: javascript textures webgl framebuffer