Vector Types - Built-in functions - Shader Learning

Built-in functions

Vector Types

Task

Create a vec4 (GLSL) or float4 (HLSL) variable called color and use it as a pixel color.


Use the following components:

  • first element: 1.0

  • second element: 0.3

  • third element: 0.3

  • fourth element: 1.0

Theory

GLSL


In GLSL, vec is a data type used to define vectors. Vectors are groups of scalar values (like float, int, etc.) organized in two, three, or four dimensions. The vec type is followed by a number that tells how many components it has.


Common vec types in GLSL:

  • vec2: a 2D vector with two values.

  • vec3: a 3D vector with three values.

  • vec4: a 4D vector with four values.

You can use vectors to represent positions, directions, colors, texture coordinates, and more.


Defining a vector in GLSL:

vec3 position = vec3(1.0, 2.0, 3.0);



HLSL


In HLSL, the equivalent vector types are named float2, float3, and float4. They serve the same purpose: representing multi-component data such as positions, colors, normals, and texture coordinates.


Common float types in HLSL:

  • float2: two-component vector

  • float3: three-component vector

  • float4: four-component vector

Defining a vector in HLSL:

float3 position = float3(1.0, 2.0, 3.0);