2013-05-08 82 views
0

我寫了一個小函數在着色器之間切換,但不幸的是它不起作用。 我創建着色器並將其作爲着色器對象返回。WebGL:在着色器之間切換

  1. 程序= gl.createProgram(VS,FS, 「VertexPosition」, 「TextureCoord」, 「VertexNormal」);深度程序= gl.createProgram(vs,fs,「VertexPosition」,false,false);

我使用開關功能,其中包括我的更新功能:

function Update(){ 
      gl.switchProgram(program); 
      gl.Draw(Teapot, cam); 
    }; 

模型的所有緩衝器都在繪製函數綁定,(參數:模型,攝像機位置)

this.switchProgram = function(program){ 

    if (this.program !== program){ 
     this.lastprogram = this.program; 
     this.program = program; 
     gl.useProgram(this.program); 

    }; 

}; 

這裏是所產生的誤差:WebGL的:INVALID_OPERATION:drawElements:attribs沒有設置正確 如果我評論一切工作正常,但我無法切換:(

depthprogram = gl.createProgram(vs, fs, "VertexPosition", false, false); 
gl.switchProgram(program); 

回答

0

兩種着色器都使用相同的屬性嗎? 至於我可以從你的代碼的結論,第一着色器使用:

attribute vertexPos; 
attribute vertexNormal; 
attribute textureCoord; 

,而第二隻採用

attribute vertexPos; 

如果您嘗試發送未在頂點着色器中使用屬性程序,或者你不發送頂點着色器請求的屬性,那麼你會得到警告。 我會說,雖然着色器需要它們,但您不會爲第一個着色器發送vertexNormal和textureCoord。

希望這會有所幫助。

+0

thx,我發現我的問題:) – Pris0n 2013-05-19 15:02:52