0

我正在尝试转换一个起诉 emscripten 的小游戏。我设法编译它而没有任何错误,但是在尝试编译/使用着色器时出现以下错误 WebGL:INVALID_OPERATION:useProgram:程序无效 WebGL:INVALID_OPERATION:getAttribLocation:程序未链接

这是着色器代码

顶点:

attribute vec3 vertexPosition_modelspace;
attribute vec2 vertexUV;
varying vec2 UV;
varying float alfa;
uniform mat4 Proj;

void main()
{
    UV = vertexUV;
    alfa = vertexPosition_modelspace.z;
    gl_Position = Proj * vec4(vertexPosition_modelspace, 1.0);

}

分段

varying vec2 UV;
varying float alfa;
uniform sampler2D myTextureSampler;

    void main()
    {
        gl_FragColor = texture2D( myTextureSampler, UV );
        gl_FragColor.a =gl_FragColor.a* alfa;
    }

有什么帮助吗?谢谢你。

4

1 回答 1

2

我想你不见了

precision mediump float;

在片段着色器的顶部。OpenGL ES 2.0(以及 WebGL)要求您在片段着色器中指定精度。

理想情况下,您的原始 C/C++ 程序会检查错误并将错误打印出来,因为浏览器可能会告诉您问题。(可选)你可能会包装linkProgramcompileShader类似的东西

gl = canvas.getContext("experimental-webgl");
oldLinkProgram = gl.linkProgram;
gl.linkProgram = function() {

  // call the original linkProgram
  oldLinkProgram.apply(gl, arguments);

  var program = arguments[0];

  // Check the link status
  var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
  if (!linked) {
      console.error("ERROR !!!! linking program: " + gl.getProgramInfoLog(program));
  }
};

oldCompileShader = gl.compileShader;
gl.compileShader = function() {

  // call the original compileShader
  oldCompileShader.apply(gl, arguments);

  var shader = arguments[0];

  // Check the compile status
  var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  if (!compiled) {
      console.error("ERROR !!!! compiling program: " + gl.getShaderInfoLog(shader));
  }
};

然后检查浏览器的控制台是否有错误。

于 2013-11-20T04:53:05.367 回答