问题描述
我已设法隔离此代码中的问题:
I have managed to isolate the problem in this code:
var gl;
_main_web = function() {
gl = document.getElementById("canvas").getContext("experimental-webgl");
gl = WebGLDebugUtils.makeDebugContext(gl,
function (err, funcName, args) {
throw(WebGLDebugUtils.glEnumToString(err) + " was caused by call to " + funcName);
}
);
vert_shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vert_shader,"attribute vec4 vertex;attribute vec2 uv; void main(void) {gl_Position = vertex;}\n");
gl.compileShader(vert_shader);
if( !gl.getShaderParameter(vert_shader,gl.COMPILE_STATUS ) ) {
throw 0;
}
frag_shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(frag_shader,"void main(void) { gl_FragColor = vec4(1.0,1.0,1.0,1.0); } \n");
gl.compileShader(frag_shader);
if( !gl.getShaderParameter(frag_shader,gl.COMPILE_STATUS) ) {
throw 1;
}
program = gl.createProgram();
gl.attachShader(program,vert_shader);
gl.attachShader(program,frag_shader);
gl.linkProgram(program);
if( !gl.getProgramParameter(program,gl.LINK_STATUS) ) {
throw 2;
}
vertexLocation = gl.getAttribLocation(program,"vertex");
textureLocation = gl.getAttribLocation(program,"uv");
}
vertexLocation 没问题,它是 0.但是 textureLocation 是 -1,我错过了什么?
vertexLocation is alright, it is 0. But textureLocation is -1, what am I missing?
推荐答案
您正在尝试获取您声明但从未使用的属性的位置.您的顶点着色器代码是(为清晰起见进行了扩展):
You're trying to get the location for an attribute that you declare but never use. Your vertex shader code is (expanded for clarity):
attribute vec4 vertex;
attribute vec2 uv;
void main(void) {
gl_Position = vertex;
}
在您的着色器编译期间,uv"将被识别为未使用的参数并被剥离.即使您在此着色器中将其分配给一个变量,但从未在片段着色器中使用它,它仍可能被删除,因为它已被识别为对最终片段没有贡献.
During the compilation of your shader "uv" will be identified as an unused parameter and be stripped out. Even if you assign it to a varying in this shader but don't ever use it in the fragment shader, it may still be stripped out because it's been identified as not contributing to the final fragment.
这篇关于Webgl 的 getAttribLocation 奇怪地返回 -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!