Skip to content

One Triangle

You'll open a window, send three vertices to the GPU, write the vertex and fragment shaders yourself, and make the result spin.

About

The first triangle is the hardest one, because nothing shows up until everything is right. You'll write the two shaders by hand, feed the GPU three positions and three colors, and watch the fragment shader blend them across the face. WebGL in a browser, WebGPU, or OpenGL 3.3 from C, Python or Rust are the cheap routes and take an evening. Vulkan draws the same triangle in about a thousand lines and takes a weekend, so save it for later. A black window is not a failure, it's the normal state until the last bug is fixed, so keep the error log open.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
One evening
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Get a window open and cleared to a color you chose. Use GLFW, SDL, a canvas element or whatever your language gives you. Clearing to a strong color rather than black tells you the context works before you draw anything. Take a screenshot of the colored window. Photo
  2. 2
    Write a vertex shader and a fragment shader from a blank file. Don't copy a working pair from a tutorial and tweak it. Type them out, and when the compile fails, read the shader log, which tells you the line. Take a screenshot of both shaders in your editor, no more than twenty lines each. Photo
  3. 3
    Draw a triangle with a different color at each corner. Send positions and colors in one buffer and let the rasterizer interpolate across the face. If you see one flat color, the varying between the two shaders is not wired up. Take a screenshot of the triangle. Photo
  4. 4
    Rotate it with a matrix you built in your own code. Write a 2D rotation matrix from sine and cosine, pass it as a uniform, and multiply the position in the vertex shader. Don't reach for a math library yet, because the point is to see the four numbers. Film the triangle spinning for a few seconds. Video
  5. 5
    Break it on purpose and read the error. Delete a semicolon in the fragment shader, run it, and find the line from the log. Then fix it. Take a screenshot of the log message so you'll recognize it next time it happens for real. Photo