Computer Science Grade 6 20 min

Shaders: Vertex Shaders and Fragment Shaders (GLSL/HLSL)

Study shaders, including vertex shaders and fragment shaders, and learn how to write them using GLSL or HLSL to control the rendering of objects.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Explain that a shader is a small program that runs on the graphics card. Describe the specific job of a vertex shader: to position the corners (vertices) of a shape. Describe the specific job of a fragment shader: to decide the color of each individual pixel. Identify the correct order in which vertex and fragment shaders run in the graphics pipeline. Trace the inputs and outputs of a simple vertex shader that moves a shape. Predict the final color of a shape based on a simple fragment shader's code. Define the terms 'vertex', 'fragment', and 'shader' using a simple analogy. Ever wonder how a video game character moves so smoothly or how a sunset in a game looks so beautiful? 🧙✨ You're about to learn the secret...
2

Key Concepts & Vocabulary

TermDefinitionExample ShaderA small program or set of instructions that you send to the computer's Graphics Processing Unit (GPU) to tell it how to draw something. Think of it as a recipe for visuals.A shader could have instructions to make a character's armor look shiny and metallic, or to make water ripple. Vertex (plural: Vertices)A single point in 3D space. Vertices are the corners or dots that connect to form shapes, like the three corners of a triangle.A square shape is made of four vertices, one for each corner. Vertex ShaderThe first type of shader. Its main job is to take each vertex (corner) of a shape and decide its final position on the screen. It's the 'shape mover' and 'placer'.If you want a character to jump, a vertex shader would take all...
3

Core Syntax & Patterns

The Graphics Pipeline Order 1. Vertex Shader -> 2. Fragment Shader This is the most important rule. The computer ALWAYS runs the Vertex Shader first to figure out WHERE the shape's corners go. Only AFTER the shape is positioned does it run the Fragment Shader to figure out WHAT COLOR the pixels inside the shape should be. The 'One Job' Rule Vertex Shader = Position. Fragment Shader = Color. To keep things simple, remember that each shader has one main job. The Vertex Shader only cares about the position of points (vertices). The Fragment Shader only cares about the color of pixels (fragments). They don't do each other's jobs. The 'Run for Everything' Rule Shaders run in parallel for every vertex or fragment. A shader program isn&...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A vertex shader is given a square with corners at (10, 10), (20, 10), (10, 20), and (20, 20). The shader's code subtracts 5 from the X-coordinate and adds 10 to the Y-coordinate of each vertex. What are the final positions of the square's corners?
A.(5, 20), (15, 20), (5, 30), (15, 30)
B.(15, 0), (25, 0), (15, 10), (25, 10)
C.(10, 10), (20, 10), (10, 20), (20, 20)
D.(5, 10), (15, 10), (5, 20), (15, 20)
Challenging
A game developer notices their 3D car model is the wrong size and in the wrong place on the screen, but its color is a perfect, shiny red, just as they designed. Which shader is the most likely source of the bug?
A.The Fragment Shader, because it controls the final look.
B.The Vertex Shader, because it controls the final position and shape of the model's vertices.
C.Both shaders are equally likely to be the problem.
D.Neither shader; the problem must be with the original 3D model file.
Challenging
In a game, a character gets hit with a magic spell and their whole body flashes white for a split second. Which shader is primarily responsible for creating this flashing effect?
A.The Vertex Shader, by quickly making the character model very large and then small again.
B.The Fragment Shader, by temporarily ignoring the model's normal colors and outputting white for every pixel.
C.The Vertex Shader, by moving the character to a white-colored area of the game world.
D.This is not possible with shaders and must be a pre-made video.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Game Development: Advanced Techniques and Engine Architecture

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.