在VertexShader 中,和顶点相关的数据如Position、Normal、Color等等均称为Attribute,为了给VertexShader传入这些数据,必须首先知道相关Attribute在Shader中位置。在layout尚未出现之间有两种设定Attribute 位置的方法。 方法一OpenGL允许我们显示的设定Attribute的位置,方法二OpenGL帮我们设定好Attribute的位置,然后我们在程序中查询Attribute的位置。方法一要特别注意:设定Attribute的位置必须在Link Program 之前,如果在Link之后才设定Attribute的位置,需要重新Link program.
对于下面的VertexShader:
1 #version 330 2 3 in vec3 position; 4 in vec3 normal; 5 in vec2 texCoord; 6 ...
设定Attribute位置的函数glBindAttribLocation如下:
void glBindAttribLocation( GLuint program, GLuint index, const GLchar *name); Params: program: the program object handle index: the index to bind the attribute to name: the name of the vertex attribute
对于上面shader中的
Mismatched Attributes and Programs
You may be wondering what happens if there is a mis-match between the attributes provided by a VAO and the vertex shader inputs. For example, we could use the position-only vertex shader with a mesh that provides attributes 0 and 1, with 0 being the position and 1 being the color.
OpenGL is actually very lenient about this sort of thing. It also goes through some effort to fully define what information the vertex shader gets in the event of a mismatch.
A VAO can provide attributes that a vertex shader does not use without penalty. Well, there may be a performance penalty for reading unused information, but it will still render correctly.
If a vertex shader takes attributes that the VAO does not provide, then the value the vertex shader gets will be a vector of (0, 0, 0, 1). If the vertex shader input vector has fewer than 4 elements, then it fills them in in that order. A vec3 input that is not provided by the VAO will be (0, 0, 0).
Speaking of which, if a VAO provides more components of an attribute vector than the vertex shader expects (the VAO provides 4 elements, but the vertex shader input is a vec2), then the vertex shader input will be filled in as much as it can be. If the reverse is true, if the VAO does not provide enough components of the vector, then the unfilled values are always filled in from the (0, 0, 0, 1) vector.
reference:
[1] http://www.arcsynthesis.org/gltut/Positioning/Tut07%20Shared%20Uniforms.html