[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Public WebGL] Vertex Attrib 0 in OpenGL vs OpenGL ES 2.0

So apparently it turns out that in OpenGL, you have to use enable vertex attrib 0 or nothing will render.

In other words:

---Vertex Shader---
attribute vec4 vPosition;
attribute vec4 vColor;
varying vec4 color;
void main()
    gl_Position = vPosition;
    color = vColor;

--Fragment shader--
precision mediump float;
varying vec4 color;
void main()
    gl_FragColor = color;


var kPositionLocation = 6;
var kColorLocation = 7;

vs = loadShader(ctx.VERTEX_SHADER, vertexShaderSource...);
fs = loadShader(ctx.FRAGMENT_SHADER, fragmentShaderSource, ...);
prg = ctx.createProgram();
ctx.attachShader(prg, vs);
ctx.attachShader(prg, fs);
ctx.bindAttribLoation(prg, kPositionLocation, "vPosition");
ctx.bindAttribLoation(prg, kColorLocation, "vColor");

...create buffers and fill with data..

// bind the buffers to the vertex attribs
ctx.bindBuffer(ctx.ARRAY_BUFFER, positionBuffer);
ctx.vertexAttribPointer(kPositionLocation, 3, ctx.FLOAT, false, 0, 0);
ctx.vertexAttribPointer(kColorLocation, 4, ctx.FLOAT, false, 0, 0);


The code above will render on OpenGL ES 2.0 but it will NOT render on OpenGL

If you assign anything to attrib 0 it will start rendering.

ctx.bindBuffer(ctx.ARRAY_BUFFER, bogusBuffer);
ctx.vertexAttribPointer(0, 1, ctx.FLOAT, false, 0, 0);

Now it will render.

Basically, vertex attrib 0 has to be enabled or else OpenGL will not render where as OpenGL ES 2.0 will.  This has another implication

If you take the above program but change kColorLocation = 0 and get rid of the bogusBuffer stuff it will render. But if you do this

var kColorLocation = 0;
ctx.vertexAttrib4f(kColorLocation, 1, 0, 0, 1);  // make it a constant red

On OpenGL ES 2.0 this will correctly render is in red. In OpenGL this will not render since location 0 has to be enabled.

There's no easy work around that comes to mind so the only suggestion I can think of is the WebGL spec needs to say that vertexAttribLocation 0 MUST be enabled. This means platforms running on real OpenGL ES 2.0 will need to enforce that in their WebGL implementations.  We'd need to decide if this should just not render or if it should generate an error.