Search code examples
javascriptwebgl2

gl.attachShader is not an object


I have added webGL2 as the context for the canvas but have an error saying:

Uncaught TypeError: Failed to execute 'attachShader' on 'WebGL2RenderingContext': parameter 2 is not of type 'WebGLShader'.
at CreateProgram (index.html:88)
at index.html:107

I also:

  • Tried using "webgl" instead of "webgl2" on the "canvas.getContext..."

I am following the tutorial here: https://webgl2fundamentals.org/webgl/lessons/webgl-fundamentals.html

Full code:

https://drive.google.com/file/d/1Nl10-ZEP8JujGXMwHHVGaDF6KdhvZcfg/view?usp=sharing

Problem found here:

function CreateProgram(gl,vertexShader,fragmentShader) {

var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);

var success = gl.getProgramParameter(program,gl.LINK_STATUS);

if(success) {

return program;

}

//Else delete program
console.log(gl.getProgramInfoLog(program));
gl.deleteProgram(program);

}


Solution

  • your GLSL fragment has a syntax error, which causes the type error.

    pay attention to the line 47

    you should use outColour = vec4(1,0,0.5,1); instead of outColour = vec4{1,0,0.5,1};