In the OpenGL ES 2.0 introduction, found here: http://www.webreference.com/programming/opengl_es/2.html
a vertex shader is defined:
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"}; \n";
The vPosition attribute is a four component vector.
Later in the text the application will compile the vertex shader, together with the fragment shader.
With the glBindAttribLocation a handle to pass the applications vertex data to the shader is established:
// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "vPosition");
The 2 shaders are now linked and the program is ready to use.
The use case is this:
GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f};
...
// Load the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
Which means:
-
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices):
pass the vertex data to the program, use location 0, each vertex has 3 components, type is float, no normalisation shall be used. -
glEnableVertexAttribArray(0):
The shader will use the passed data. -
glDrawArrays(GL_TRIANGLES, 0, 3);:
The shader will execute the code to draw a single triangle, using the first (and only) three vertices found in the specified array.
My Questions are:
-
The vertex shader is expecting a four component vertex position (x,y,z,w), the program passes only three components (x,y,z) data
Why is this working? -
The shader needs a vec4, the w-component must be 1 to work as expected.
Which step is responsible for adding the w-component correctly?
See this answer: Can you use glVertexAttribPointer to assign a smaller vector to a larger one?
As mentioned in that post it appears in the gles specifications: http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.24.pdf
In section 2.8 of this document it says: