我有一个简单的webGL程序,几乎可以从Mozilla开发人员网络复制粘贴内容。由于某种原因,我设法创建了一种具有单色的立方体侧面,并在顶部和底部创建了纹理,并且可以看到纹理,但是我不确定是否正确设置了闪电。我正在尝试修复以下错误,而我尝试使用aTextureCoord参数初始化着色器时发现该错误发生。以下是我的着色器和我要使用的javascript代码。有人能弄清楚为什么会发生这种情况吗?属性highp vec3 aVertexNormal; 属性highp vec3 aVertexPosition; 属性highp vec2 aTextureCoord;
uniform highp mat4 uNormalMatrix;
uniform highp mat4 uMVMatrix;
uniform highp mat4 uPMatrix;
varying highp vec3 vLighting;
varying highp vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
highp vec3 ambientLight = vec3(0.6, 0.6, 0.6);
highp vec3 directionalLightColor = vec3(0.5, 0.5, 0.75);
highp vec3 directionalVector = vec3(0.85, 0.8, 0.75);
highp vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);
highp float directional = max(dot(transformedNormal.xyz, directionalVector), 0.0);
vLighting = ambientLight + (directionalLightColor * directional);
}
</script>
以下是javascript代码。
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram4Tex.vertexPositionAttribute = gl.getAttribLocation(shaderProgram4Tex, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram4Tex.vertexPositionAttribute);
shaderProgram4Tex.vertexTextureAttribute = gl.getAttribLocation(shaderProgram4Tex, "aTextureCoord");
gl.enableVertexAttribArray(shaderProgram4Tex.vertexTextureAttribute);
shaderProgram4Tex.pMatrixUniform = gl.getUniformLocation(shaderProgram4Tex, "uPMatrix");
shaderProgram4Tex.mvMatrixUniform = gl.getUniformLocation(shaderProgram4Tex, "uMVMatrix");
shaderProgram4Tex.vertexNormalAttribute = gl.getAttribLocation(shaderProgram4Tex, "aVertexNormal");
gl.enableVertexAttribArray(shaderProgram4Tex.vertexNormalAttribute);
shaderProgram4Tex.samplerUniform = gl.getUniformLocation(shaderProgram4Tex, "uSampler");
shaderProgram4Tex.normalMatrix = gl.getUniformLocation(shaderProgram4Tex, "uNormalMatrix");
运行程序时出现以下错误。
WebGL: INVALID_OPERATION: drawElements: attribs not setup correctly
希望有人能回答这个问题,因为我已经花了10个小时来解决这个问题,却不知道为什么会这样。
“未正确设置文件”是指
您尚未设置任何程序。换句话说,您没有gl.useProgram
使用有效的程序进行调用。
您使用打开了一个属性,gl.enableVertexAttribArray
但是没有通过调用来为其分配缓冲区,而gl.bindBuffer(gl.ARRAY_BUFFER, someBuffer)
在某个时候通过调用gl.vertexAttribPointer
将当前绑定分配ARRAY_BUFFER
给指定的属性来对其进行分配。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句