Per-Vertex attributes can be passed to a vertex shader using in variables
Distinctions can be made regarding special in variables, which are automatically generated, and user-defined in variables, which must be passed by the application
The vertex data is transferred to the graphics card via Vertex Buffer Objects (VBOs)
To use the data of VBOs for the user-defined in variables, the usual procedure will be as follows:
With glGetAttribLocation a location identifier for an in variable is queried. For example, when the variable in the shader code is called “inputColor”
int colorLoc = glGenAttribLocation(progID, "inputColor");
The function glVertexAttribPointer(colorLoc, size, type, normalized, stride, offset) then defines the mapping between an in variable and VBO attribute
Finally, the attribute needs to be activated by
glEnableVertexAttribArray(colorLoc);
In a subsequent call to glDrawArray or glDrawElements with activated VBO the in variable of the vertex shader is then filled with the attribute data for each drawn vertex
Data Input and Output in the Fragment Shader
User-defined out variables of the vertex shader are interpolated in the rasterizer and the interpolated values are supplied in variables to the fragment shader
The out variables of the fragment shader will be sent to the framebuffer
For example, if the out variable “outputColor” should be written into the first color buffer of the framebuffer, this can be achieved by
glBindFragDataLocation(progID, 0, "outputColor");
Uniform Variables
In addition to in variables, uniform variables are another way to pass data to a shader
In contrast to the in variables that contain the vertex attributes, the uniform variables include data that remains constant for all vertices during a call to glDrawArray or glDrawElements
The location identifier of a uniform variable can be queried by
Example: Passing the Transformation Matrices As Uniforms
In this example, the modelview matrix and the projection matrix are passed as uniform variables. The functionality of the GL_MODELVIEW and GL_PROJECTION matrices (which are known from the fixed-function pipeline) are simulated
댓글