Javascript WebGL:无效的_值:attachShader:没有对象或对象已删除。这秘密有用吗?
我开始进入WebGL,我想知道是否有一个好地方可以了解错误输出 我不断得到以下错误Javascript WebGL:无效的_值:attachShader:没有对象或对象已删除。这秘密有用吗?,javascript,opengl-es,webgl,shader,vertex-shader,Javascript,Opengl Es,Webgl,Shader,Vertex Shader,我开始进入WebGL,我想知道是否有一个好地方可以了解错误输出 我不断得到以下错误 WebGL: INVALID_VALUE: attachShader: no object or object deleted localhost:1 WebGL: INVALID_OPERATION: getAttribLocation: program not linked localhost:1 WebGL: INVALID_OPERATION: getUniformLocation: program
WebGL: INVALID_VALUE: attachShader: no object or object deleted localhost:1
WebGL: INVALID_OPERATION: getAttribLocation: program not linked localhost:1
WebGL: INVALID_OPERATION: getUniformLocation: program not linked localhost:1
WebGL: INVALID_OPERATION: useProgram: program not valid localhost:1
WebGL: INVALID_OPERATION: drawElements: attribs not setup correctly
因此,从这些错误中我可以看出,我的着色器无法正常工作
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
接下来的错误是没有程序
所以我的问题是:如何找出代码中的错误
程序没有以某种方式定义,我有var program=gl.createProgram()代码>。那么,为什么会发生这种情况?我在哪里看?我猜这是因为我的着色器无法编译,但据我所知,着色器中出现了0个错误或警告。上面提到的代码/警告使它变得模糊。。。此外,chrome提到了这些警告,firefox没有。两者都不能初始化着色器,尽管我建议编译着色器和链接程序
对于着色器
/**
* Creates and compiles a shader.
*
* @param {!WebGLRenderingContext} gl The WebGL Context.
* @param {string} shaderSource The GLSL source code for the shader.
* @param {number} shaderType The type of shader, VERTEX_SHADER or
* FRAGMENT_SHADER.
* @return {!WebGLShader} The shader.
*/
function compileShader(gl, shaderSource, shaderType) {
// Create the shader object
var shader = gl.createShader(shaderType);
// Set the shader source code.
gl.shaderSource(shader, shaderSource);
// Compile the shader
gl.compileShader(shader);
// Check if it compiled
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!success) {
// Something went wrong during compilation; get the error
throw "could not compile shader:" + gl.getShaderInfoLog(shader);
}
return shader;
}
节目
/**
* Creates a program from 2 shaders.
*
* @param {!WebGLRenderingContext) gl The WebGL context.
* @param {!WebGLShader} vertexShader A vertex shader.
* @param {!WebGLShader} fragmentShader A fragment shader.
* @return {!WebGLProgram} A program.
*/
function createProgram(gl, vertexShader, fragmentShader) {
// create a program.
var program = gl.createProgram();
// attach the shaders.
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
// link the program.
gl.linkProgram(program);
// Check if it linked.
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!success) {
// something went wrong with the link
throw ("program filed to link:" + gl.getProgramInfoLog (program));
}
return program;
}
你可以这样称呼它
var vertShader = compileShader(gl, vertShaderSource, gl.VERTEX_SHADER);
var fragShader = compileShader(gl, fragShaderSource, gl.FRAGMENT_SHADER);
var program = createProgram(gl, vertShader, fragShader);
如果着色器未编译或链接,则应将错误打印到javascript控制台。它还应该为您提供堆栈跟踪,以便您可以告诉代码中的问题所在。我建议您编译着色器和链接程序
对于着色器
/**
* Creates and compiles a shader.
*
* @param {!WebGLRenderingContext} gl The WebGL Context.
* @param {string} shaderSource The GLSL source code for the shader.
* @param {number} shaderType The type of shader, VERTEX_SHADER or
* FRAGMENT_SHADER.
* @return {!WebGLShader} The shader.
*/
function compileShader(gl, shaderSource, shaderType) {
// Create the shader object
var shader = gl.createShader(shaderType);
// Set the shader source code.
gl.shaderSource(shader, shaderSource);
// Compile the shader
gl.compileShader(shader);
// Check if it compiled
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!success) {
// Something went wrong during compilation; get the error
throw "could not compile shader:" + gl.getShaderInfoLog(shader);
}
return shader;
}
节目
/**
* Creates a program from 2 shaders.
*
* @param {!WebGLRenderingContext) gl The WebGL context.
* @param {!WebGLShader} vertexShader A vertex shader.
* @param {!WebGLShader} fragmentShader A fragment shader.
* @return {!WebGLProgram} A program.
*/
function createProgram(gl, vertexShader, fragmentShader) {
// create a program.
var program = gl.createProgram();
// attach the shaders.
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
// link the program.
gl.linkProgram(program);
// Check if it linked.
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!success) {
// something went wrong with the link
throw ("program filed to link:" + gl.getProgramInfoLog (program));
}
return program;
}
你可以这样称呼它
var vertShader = compileShader(gl, vertShaderSource, gl.VERTEX_SHADER);
var fragShader = compileShader(gl, fragShaderSource, gl.FRAGMENT_SHADER);
var program = createProgram(gl, vertShader, fragShader);
如果着色器未编译或链接,则应将错误打印到javascript控制台。它还应该为您提供堆栈跟踪,这样您就可以知道问题在代码中的什么位置。您是否检查过着色器是否已编译?调用gl.compileShader
后,调用gl.getShaderParameter(vertexShader,gl.COMPILE\u STATUS)
。如果返回false,则着色器中存在错误,这可能会阻止WebGL继续设置程序。是的,着色器正在编译。这不是链接,您是否检查过着色器是否已编译?调用gl.compileShader
后,调用gl.getShaderParameter(vertexShader,gl.COMPILE\u STATUS)
。如果返回false,则着色器中存在错误,这可能会阻止WebGL继续设置程序。是的,着色器正在编译。但它没有链接