Tuesday, March 31, 2026

Vertex Shader vs Fragment Shader Explained (With Simple Example)

 Introduction

Vertex and fragment shaders are essential, programmable GPU stages in modern 3D graphics pipelines. Vertex shaders manipulate geometry (position, deformation), while fragment shaders determine pixel color, lighting, and textures. They are necessary for creating custom visual effects, dynamic lighting, and efficient, GPU-accelerated rendering.

Vertex Shaders (Geometry Manipulation)
  • Necessity: Transform 3D model vertices from object space to screen space (clip space) for rendering.
  • Applications: Modifying geometry, such as vertex animation (e.g., cloth simulation, waving water), transforming models (rotation, scaling), and passing data to the fragment shader.
  • Example: A shader that makes a mesh wave by altering vertex positions based on a sine function
Fragment Shaders (Per-Pixel Coloring)
  • Necessity: Determine the final color (pixel value) of every rendered object pixel, handling lighting and textures.
  • Applications: Lighting calculations (Phong/Blinn-Phong), applying textures (UV mapping), transparency, and special effects like bloom or sepia tones.
  • Example: Calculating per-pixel lighting to make a surface appear smooth rather than faceted.
Why Both Are Necessary
  • Pipeline Flow: The vertex shader prepares raw shape geometry, while the fragment shader colors those shapes.
  • Efficiency: Vertex shaders run per-vertex (few), while fragment shaders run per-pixel (many), allowing for complex visual effects without stalling the GPU.
  • Data Passing: The vertex shader can calculate data (like vertex colors or normals) and pass them to the fragment shader, which interpolates this data for smooth gradients or precise lighting.

Shaders are where most beginners get stuck in OpenGL.

You see words like:

  • Vertex Shader
  • Fragment Shader

…and it feels confusing.

Let’s break it down in the simplest way possible.

Core Idea

Vertex Shader = decides POSITION
Fragment Shader = decides COLOR

That’s it.

Visual Explanation

Think of a triangle:

  • Vertex Shader → places the 3 corners
  • Fragment Shader → fills the color inside

Vertex Shader Example

attribute vec4 vPosition;

void main() {
gl_Position = vPosition;
}

👉 It controls where things appear on screen


Fragment Shader Example

precision mediump float;

void main() {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

👉 It controls what color they are


Real Analogy

Think of painting:

  • Vertex Shader = drawing outline
  • Fragment Shader = filling color

Common Mistakes

1. Mixing Responsibilities

Trying to set color in vertex shader → confusion

2. Precision Missing

precision mediump float;

👉 Required in fragment shader


Next Step

Try:

  • Changing color dynamically
  • Passing color from vertex → fragment
  • Creating gradients

Final Insight

Shaders are not complex—you just need to see them as:
👉 Position + Color pipeline

Once that clicks, graphics becomes much easier.

If you want to explore further I have another post where I draw a triangle.

https://cglabprojects.blogspot.com/2018/07/drawing-triangle-in-android-app-using.html

Saturday, November 15, 2025

Connect Four

Connect Four (You = Red)

Your turn

Othello(Reversi)

Reversi / Othello (Player = Black)

Your turn

Macala(Kalah) played against the computer

Dots and Boxes - Learn by playing

Dots & Boxes

Wednesday, November 12, 2025

Math table practice

 

🎲 Fun Multiplication Tables!

Choose a number to start practicing!

Wednesday, September 17, 2025

BrainVita / Marbles Solitaire Game

  BrainVita / Marbles Solitaire Game

Brainvita (Peg Solitaire)

Brainvita / Peg Solitaire

Click a marble, then click an empty hole two steps away in a straight line to jump.