2016-08-07 11 views
0

私はテクスチャに描画しようとしているので、レンダリングは互いに重ね合わされていきます。 2つのテクスチャと2つのフレームバッファを使用しています。 テクスチャ[0]はフレームバッファ[0]に添付され、テクスチャ[1]はフレームバッファ[1]に添付されます。2つのテクスチャを切り替えるにはどうすればいいですか?

問題は、以前のレンダリングがテクスチャに保存されていないことです。 私は、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); 
    } 
+0

:ここ

が更新され、頂点/フラグメントシェーダですか?マウスカーソルの周りに赤色(別のテクスチャの+色)の円を描きますか? –

+0

はい、マウスを動かすと、マウスカーソルの周りに赤い円が描かれます。私は前のドローを維持したいが、何らかの理由でそれをしていないので、マウスが移動した一連の円ではなく、赤い円しか得られない。 2つのテクスチャと2つのフレームバッファを使用して、円を描き、前の図面を保持するようにそれらの間を交互に使用できると思っていましたが、機能しません。なぜか分からない。 – user3591153

+0

私が心配しているのは、テクスチャがデータを保存していないかのように見えるということです。私がフレームバッファをバインドするときに私が描画している場合は、その後、色を取得するためにテクスチャをバインドすると、以前のレンダリングされたイメージを持つはずです。それでも私が色を読んだときには、以前に描画されたものは表示されません。ただ空白です。 – user3591153

答えて

0

私はあなたが何をしているかをエミュレートしようとした:

loop.flexStep = function(){ 
    gl.clear(gl.COLOR_BUFFER_BIT); 

    pingPong.pingPong().applyPass(//pingPong() just swaps the FBOs and return the current FBO being draw to 
     "outColor += src0 * 0.98;", 
     pingPong.otherTexture() // the src0 texture 
    ); 
    points.drawPoint([mouse.x, mouse.y, 0 ]); 
    points.renderAll(camera); 

    screenBuffer.applyPass(
     "outColor = src0;", 
     pingPong.resultFBO // src0 
    ); 
}; 

をここでは、(代わりに円のgl.POINTS)のようになります。 enter image description here

ここにglコマンドがあります。 enter image description here

フレームバッファが最初に正しく設定されているかどうかを確認する必要があります。その部分はそれほど難しいとは言えません。

また、シェーダを分離することを検討してください。以前のテクスチャの結果を現在のpingPong FBOテクスチャにコピー/変更するには、ピンポンステージに1つのシェーダが必要です。現在のpingPong FBOテクスチャに新しいものを描画するシェーダもあります。

+0

感謝 - 私は問題を見つけることができました。正しいテクスチャ座標を選択していないし、フリッピングの問題を並べ替える必要もありました。 – user3591153

+0

うれしいことに問題を解決しました。私はあなたが他の興味深い効果を実装するためにピンポンバッファを使いたいならば、上で概説した構造を実装することを検討すべきだと思います。 –

+0

私はあなたの構造を理解しようとします。私はwebglには比較的新しいので、あなたのフォームのパターンを認識するのが難しいです。私が現時点で扱うことができるものより少し進んだようです。たぶんあなたは、あなたが提案している構造化のタイプについての良いチュートリアルやウォークスルーを教えてくれるでしょうか? – user3591153

0

間違ったテクスチャ座標を取得したり、テクスチャを反転したりすることが問題でした。 これは期待どおりに機能しています。あなたは、シェーダが何を期待するかに説明でき

this.vertex = ` 
    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/u_resolution;        // 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); 
    } 

`; 

this.fragment = ` 

    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 float u_flip; 

    uniform sampler2D u_image;        // this isn't set, so it will default to 0 (the current active texture) 
    uniform sampler2D u_texture0; 
    uniform sampler2D u_texture1; 

    void main() {  

     vec4 texColor = texture2D(u_image, v_texCoord);   // Look up a color from the texture. 

     vec2 coord = vec2(gl_FragCoord.x, step(0.0,u_flip)*gl_FragCoord.y + step(u_flip,0.0)*(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 
    } 

`; 
関連する問題