OpenGL OpenGL Shaders 1 — Questions and Answers
Question 1: Which language is used to write shaders in OpenGL?
- HLSL
- GLSL (Correct answer)
- Cg
- Metal
Correct answer: GLSL
OpenGL uses GLSL (OpenGL Shading Language) to write vertex, fragment, and other shader programs.
Question 2: Which built-in GLSL variable holds the final position of a vertex in clip space?
- gl_Position (Correct answer)
- gl_FragCoord
- gl_VertexID
- gl_ClipDistance
Correct answer: gl_Position
The vertex shader must write the clip-space position to gl_Position for the rasterizer to use.
Question 3: What OpenGL function is used to compile a shader object?
- glLinkProgram
- glShaderSource
- glCompileShader (Correct answer)
- glAttachShader
Correct answer: glCompileShader
glCompileShader(shader) compiles the GLSL source code that was previously loaded with glShaderSource.
Question 4: What qualifier is used in GLSL to declare a variable that passes data from the vertex shader to the fragment shader?
- uniform
- attribute
- out/in (Correct answer)
- const
Correct answer: out/in
The 'out' qualifier in the vertex shader and matching 'in' qualifier in the fragment shader define interpolated varyings.
Question 5: What is a 'uniform' variable in GLSL?
- A variable that changes per vertex
- A constant set by the CPU that is the same across all shader invocations (Correct answer)
- A variable shared between two fragment shaders
- An output written to the framebuffer
Correct answer: A constant set by the CPU that is the same across all shader invocations
Uniform variables are set once by the application and remain constant for all vertices or fragments in a draw call.
Question 6: Which OpenGL function links compiled shader objects into a program object?
- glCompileShader
- glBindProgram
- glLinkProgram (Correct answer)
- glUseProgram
Correct answer: glLinkProgram
glLinkProgram(program) resolves references between attached shader stages and produces an executable program object.
Which language is used to write shaders in OpenGL?