6.0 Shaders
- What's shader?
- Shaders are little programs that rest on the GPU, run for each specific section of the graphics pipeline.
6.1 GLSL
- What's GLSL?
- Shaders are written in the C-like language GLSL.
- GLSL is tailored for use with graphics and contains useful features specifically targeted at vector and matrix manipulation.
- A shader typically has the following structure:
- Version declaration
- Input variables
- Output variables
- Uniform variables
- Main function
|
#version version_number
in type in_variable_name; in type in_variable_name;
out type out_variable_name;
uniform type uniform_name;
void main() { // process input(s) and do some weird graphics stuff ... // output processed stuff to output variable out_variable_name = weird_stuff_we_processed; } |
- Vertex attribute
- For vertex shader each input variable is also known as a vertex attribute.
- Maximum number of vertex attributes
- Limited by the hardware.
- OpenGL guarantees at least 16 4-component vertex attributes available.
- To retrieve by querying GL_MAX_VERTEX_ATTRIBS:
|
int nrAttributes; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes); std::cout << "Maximum nr of vertex attributes supported: " << nrAttributes << std::endl; |
6.2 Data Types
- Basic types like C: int, float, double, uint and bool.
- Two container types: vector and matrix.
6.2.1 Vectors
- A vector is a 1,2,3 or 4 component container for any of the basic types.
- vecn: the default vector of n floats.
- bvecn: a vector of n booleans.
- ivecn: a vector of n integers.
- uvecn: a vector of n unsigned integers.
- dvecn: a vector of n double components.
(n represents the number of components)
- Components of a vector
- To access via vec.x, vec.y, vec.z and vec.w.
- Use rgba for colors or stpq for texture coordinates to access the same components.
- Swizzling syntax:
Use any combination of up to 4 letters to create a new vector:
|
vec2 someVec; vec4 differentVec = someVec.xyxx; vec3 anotherVec = differentVec.zyw; vec4 otherVec = someVec.xxxx + anotherVec.yxzy; |
Pass vectors as arguments to different vector constructor calls:
|
vec2 vect = vec2(0.5, 0.7); vec4 result = vec4(vect, 0.0, 0.0); vec4 otherResult = vec4(result.xyz, 1.0); |
6.3 Ins and outs
- Use in and out keywords to declare input and output variables.
- The input of vertex shader
- Receive input straight from the vertex data.
- To organize the vertex data: specify the input variables with location, such as layout(location = 0).
- Can also omit the layout(location = 0) specifier and query for the attribute locations via glGetAttribLocation.
- Fragment shader requires a vec4 color output variable to generate a final output color.
- To send data from one shader to the other:
- Declare an output in the sending shader and a similar input in the receiving shader.
- When the types and the names are equal on both sides, OpenGL will link those variables together and then it is possible to send data between shaders (this is done when linking a program object).
6.4 Uniforms
- Uniforms are global variables and can be accessed from any shader.
- Use uniform keyword to declare a uniform variable.
- Set the uniform value in OpenGL code:
- Query for the index/location of the uniform attribute using glGetUniformLocation.
- Set the uniform value using the glUniform.
- glUniform requires a specific postfix for the type of the uniform. Such as:
- f: the function expects a float as its value.
- i: the function expects an int as its value.
- ui: the function expects an unsigned int as its value.
- 3f: the function expects 3 floats as its value.
- fv: the function expects a float vector/array as its value.
- Updating a uniform requires to first use the program (by calling glUseProgram), because it sets the uniform on the currently active shader program.
Notes: To retrieve the running time in seconds via glfwGetTime().
6.5 More attributes!
- Add color data to the vertex data:
|
float vertices[] = { // positions // colors 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top }; |
- Adjust the vertex shader to also receive color value as a vertex attribute input.
|
#version 330 core layout (location = 0) in vec3 aPos; // position has attribute position 0 layout (location = 1) in vec3 aColor; // color has attribute position 1
out vec3 ourColor; // output a color to the fragment shader
void main() { gl_Position = vec4(aPos, 1.0); ourColor = aColor; // set ourColor to input color from the vertex data } |
- Change the fragment shader as well.
|
#version 330 core out vec4 FragColor; in vec3 ourColor;
void main() { FragColor = vec4(ourColor, 1.0); } |
- The layout of VBO's memory:
- Re-configure the vertex attribute pointers.
|
// position attribute glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // color attribute glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); |
6.6 Our own shader class
- Create the shader class in a header file.
- Constructor needs the source file path of vertex shader and fragment shader as arguments.
- The use method activates the shader program.
- All set… methods query a uniform location and set its value.
6.7 Reading from file
- The constructor of shader class:
- Read the file content of shaders into string objects.
- Compile and link the shaders.
- The use method is simply a call to glUseProgram.
- Similarly for any of the uniform setter functions, just call glUniform.
- Create a shader object and use it.
Reference
- Learn OpenGL: Shaders
https://learnopengl.com/Getting-started/Shaders

沒有留言:
發佈留言