OpenGL Functions 4 — Questions and Answers
Question 1: What does glCompileShader() do?
- Links multiple shader objects into a program
- Compiles the GLSL source code attached to a shader object (Correct answer)
- Attaches source code to a shader object
- Validates the shader against the GPU
Correct answer: Compiles the GLSL source code attached to a shader object
glCompileShader() compiles the GLSL source strings previously provided to the shader object via glShaderSource().
Question 2: Which function is used to perform instanced rendering?
- glDrawArrays()
- glDrawElements()
- glDrawArraysInstanced() (Correct answer)
- glMultiDrawArrays()
Correct answer: glDrawArraysInstanced()
glDrawArraysInstanced() renders multiple instances of a primitive set in a single draw call, with each instance able to access gl_InstanceID in the shader.
Question 3: What is returned by glGetError()?
- A bitmask of all current errors
- The most recent error flag, which is then cleared (Correct answer)
- A string describing the last error
- The error count since last cleared
Correct answer: The most recent error flag, which is then cleared
glGetError() returns the value of the error flag and resets it to GL_NO_ERROR, so multiple errors require multiple calls.
Question 4: Which function enables a vertex attribute array so it can be used in vertex shaders?
- glVertexAttribPointer()
- glBindAttribLocation()
- glEnableVertexAttribArray() (Correct answer)
- glVertexAttribDivisor()
Correct answer: glEnableVertexAttribArray()
glEnableVertexAttribArray() activates the vertex attribute at the given index so the shader can receive per-vertex data from the buffer.
Question 5: What does glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) do?
- Repeats the texture when UV coordinates exceed 1.0 on the S axis
- Clamps UV coordinates to the edge texel color on the S axis (Correct answer)
- Mirrors the texture at each integer boundary on the S axis
- Sets the S coordinate wrap to repeat with mirroring
Correct answer: Clamps UV coordinates to the edge texel color on the S axis
GL_CLAMP_TO_EDGE ensures UV coordinates outside [0,1] on the S axis sample the nearest edge texel rather than repeating or wrapping.
Question 6: Which function transfers pixel data from client memory to a 2D texture already defined by glTexImage2D()?
- glTexImage2D()
- glTexSubImage2D() (Correct answer)
- glCopyTexImage2D()
- glCompressedTexImage2D()
Correct answer: glTexSubImage2D()
glTexSubImage2D() replaces a rectangular subregion of an existing 2D texture without reallocating GPU storage.
Question 7: What is the purpose of glFenceSync() in OpenGL?
- Locks a buffer object against CPU writes
- Inserts a sync object into the command stream to detect GPU completion (Correct answer)
- Flushes all pending commands immediately
- Waits for all GPU commands to finish before returning
Correct answer: Inserts a sync object into the command stream to detect GPU completion
glFenceSync() creates a sync object and inserts a fence command into the GPU pipeline, allowing the CPU to query or wait for that point to be reached.
What does glCompileShader() do?