1

A vertex array representing a triangle is never passed to the vertex shader with getAttribLocation / vertexAttribPointer. However, A Triangle will still draw on the screen. How is the triangle being drawn. In the shader I specify vec2 pos, but I never use getAttribLocation. Is vec2 position a reserved value in GLSL? :

function createShader( str, type ) {
  var shader = gl.createShader(type);
  gl.shaderSource(shader, str);
  gl.compileShader(shader);

  return shader;
}

function createProgram(vstr, fstr) {
  var program = gl.createProgram();
  var vshader = createShader(vstr, gl.VERTEX_SHADER);
  var fshader = createShader(fstr, gl.FRAGMENT_SHADER);
  gl.attachShader(program, vshader);
  gl.attachShader(program, fshader);
  gl.linkProgram(program);
  return program;
}
  
var c = document.getElementById('c');
var gl = c.getContext('experimental-webgl');
var vertexPosBuffer = gl.createBuffer();

gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
var vertices = [ -0.5, -0.5, 0.5, -0.5, 0, 0.5];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

var vs = 'attribute vec2 asdf;' +
         'attribute vec2 pos;' +
         'void main() { gl_Position = vec4( pos, 0, 1 ); }';

var fs = 'precision mediump float;' +
         'void main() { gl_FragColor = vec4(0,0.8,0,1); }';

var program = createProgram(vs,fs);
gl.useProgram(program);

//
function animate () {
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer( 0, 2, gl.FLOAT, false, 0, 0 );
  gl.drawArrays(gl.TRIANGLES, 0, 3);

  window.requestAnimationFrame(animate);
}

animate();
canvas { border: 1px solid black; }
<canvas id="c"></canvas>

2

The GL will assign locations for each attribute, no matter if you query them or not. However, you only have one active attribute, since asdf has no effect whatsoever and will be optimized away. So it is very likely (but not guaranteed) that pos ends up as attribute 0.

Even if asdf were used, there still would be the chance that it ends up as 0, as the mapping between attribute names and locations is totally implementation-specific.

The recommendation is of course: don't do that. Either, assign the locations yourself via glBindAttribLocation (before linking the program), or query what the GL has assigned, otherwise the behavior will vary with each implementation.

|improve this answer|||||
  • Where does it state this in the ES 2.0 Spec. – Steve Webster Dec 1 '14 at 16:19
  • @SteveWebster: ES spec 2.0.25, section 2.10.4 "Shader Variables", subsection "Vertex Attributes", pages 32-34. – derhass Dec 1 '14 at 19:22
  • The Spec does not mention the GLSL optimization or how the mapping is "implementation specific". – Steve Webster Dec 3 '14 at 4:10
  • @SteveWebster: The spec explicitely talks about active attributes and about the locations being assinged by the GL, if not explicitely assigned by the user. – derhass Dec 3 '14 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.