OpenGL SuperBible中关于 gl_Position 的描述:
Fortunately, GLSL includes a special input to the vertex shader calledgl_VertexID, which is the index of the vertex that is being processed at the time. The gl_VertexID input starts counting from the value given by thefirst parameter of glDrawArrays()and counts upward one vertex at a time forcount vertices (the third parameter ofglDrawArrays()).
因为GLSL会将数据输入到 VAO中,也就是输入到了显卡缓存中。程序会将 gl_VertexID 传给顶点着色器,让它选择应该输入的顶点数据。
默认使用的VAO,如果调用的是EBO,那么 gl_VertexID 会根据 index 来变化,而不是默认的从传入参数的first递增。
也可以通过硬编码在顶点着色器里面定义,但是并不推荐这么做。
layout (location = 0) in vec3 position;
void main()
{
gl_Position = vec4(position,1.0f);
}
关于 gl_VertexID官方的解释:
gl_VertexID is a vertex language input variable that holds an integer index for the vertex. The index isimpliclty generated by glDrawArrays and other commands that do not reference the content of theGL_ELEMENT_ARRAY_BUFFER, or explicitly generated from the content of theGL_ELEMENT_ARRAY_BUFFER by commands such as glDrawElements.