Posts

A Beginner's Journey Through GLSL So Far

0 comments·0 reblogs
hey2d
25
·
0 views
·
min-read

When I started this series, the goal was never to memorize functions.

The goal was to understand how a shader thinks.

Every lesson so far has introduced a small piece of that puzzle. On their own, these concepts can feel simple. Together, they form the foundation of almost everything you will create in GLSL.

Before moving forward, it is worth taking a moment to look back at what we have covered and how these ideas connect.

Understanding the Screen

One of the first things we learned was that a shader runs once for every pixel on the screen.

Unlike traditional programming where code runs line by line and eventually stops, a fragment shader is executed thousands or even millions of times every frame.

Each pixel asks a simple question:

What color should I be?

Everything we do in GLSL is ultimately answering that question.


Image from thread

Color Fill

Our first shader was as simple as it gets.

gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 

Every pixel receives the exact same color.

The result is a solid screen.

At the time it may not have seemed exciting, but this introduced one of the most important ideas in shader programming:

Every pixel can be assigned a color directly.

Related Article

Your First Shader: Painting the Entire Screen One Color


UV Coordinates

The next breakthrough came when we stopped thinking in pixels and started thinking in percentages.

vec2 uv = gl_FragCoord.xy / u_resolution.xy; 

This transforms screen coordinates into values between 0.0 and 1.0.

Now the left edge is always:

0.0 

and the right edge is always:

1.0 

regardless of screen size.

This gives us a universal coordinate system.

Instead of saying:

Draw something at pixel 500.

we can say:

Draw something halfway across the screen.

That makes our shaders resolution independent.


Gradients

Once we had UV coordinates, gradients became possible.

vec3(uv.x) 

creates a horizontal gradient.

vec3(uv.y) 

creates a vertical gradient.

This taught us something important:

A color does not need to be fixed.

It can come from position.

Every pixel receives a slightly different value, creating a smooth transition across the screen.

Related Article

Gradients: When Color Learns About Position


Textures

Until now we had been generating colors mathematically.

Textures introduced a completely different approach.

Instead of creating color from equations, we read color information from an image.

texture2D(u_texture, uv) 

The UV coordinates tell GLSL where to look inside the texture.

Think of a texture as a map.

The UV coordinates are directions.

The texture stores color information.

The UV values decide which color gets sampled.

This is why UV coordinates are so important. They are not only used for gradients and shapes. They are also used to navigate images.


mix()

Then we learned how to blend values together.

mix(a, b, t) 

The first value is the starting point.

The second value is the ending point.

The third value controls the blend amount.

mix(colorA, colorB, 0.0) 

returns only the first color.

mix(colorA, colorB, 1.0) 

returns only the second color.

Values in between create smooth transitions.

This became our first real interpolation tool.

Related Article

GLSL Lesson 3: Understanding clamp() by Breaking and Fixing Colors


clamp()

Soon after, we learned about controlling values.

clamp(value, minimum, maximum) 

This function prevents values from going outside a specific range.

For example:

clamp(1.5, 0.0, 1.0) 

returns:

1.0 

because the value exceeded the maximum.

Similarly:

clamp(-0.3, 0.0, 1.0) 

returns:

0.0 

because it went below the minimum.

Clamp acts like a safety fence around a value.

Whenever a value becomes too large or too small, clamp keeps it under control.

Related Article

GLSL Lesson 3: Understanding clamp() by Breaking and Fixing Colors


Posted Using INLEO