Ceil - Built-in functions - Shader Learning

Built-in functions

Ceil

Task

Create 10 equally sized vertical sections on the screen and label them with sequential numbers from 1 to 10. Adjust the intensity of the red color in each section based on its assigned number (0.1, 0.2 ... 1.0). This will result in a gradient effect where the red color becomes progressively stronger from left to right.


Requirements


The shader should avoid using branching or conditional statements in its code, and instead rely on the ceil function to determine the color of each pixel.

Theory

In GLSL/HLSL, the ceil function is used to calculate the smallest integer value that is greater than or equal to a given scalar or vector input. The ceil function rounds up the input value to the nearest integer value that is not less than the input value. This function is particularly useful when you need to round up a value to the next whole number.


Function Signature

float ceil(float x)

Where x is the input value for which the ceiling value is to be calculated. The output of the ceil function will be the ceiling value of x.


How It Works

  1. If the input value x is already an integer, the ceil function will return the same integer value without any change.

  2. If the input value x is a floating-point number, the ceil function will round up the value to the next whole number.

  3. The ceil function always returns a floating-point value, even if the input is an integer.

Examples

Basic usage with float numbers:

float number = 3.5;
float ceilVal = ceil(number); // will be 4.0

Handling vectors:

vec3 vector = vec3(2.3, 5.8, -4.7);
vec3 ceilVec = ceil(vector); // will be vec3(3.0, 6.0, -4.0)