OpenGL Shader Variable Types

From GPWiki
Jump to: navigation, search

Contents

OpenGL Shading Language Data Types

The OpenGL Shading Language (GLSL) defines many data types, 4 data type qualifiers, and 4 function parameter types. They are used to define data, how data can be accessed, and how a function can interact with data.

Data Types

GLSL provides many different data types to make mathematics and graphic processing easier. For basic numerical types such as float and int, the standard mathematical operators are implemented.

For vectors, addition, subtraction and scalar multiplication is defined. Also, multiplication with a matrix is also defined. These operations use the standard mathematical operators, +-*/.

Vectors

Vectors are mathematical vectors. They represent spacial coordinates, color, and texture coordinates. For this reason, there are multiple ways to reference the components of a vector.

Using the "swizzle" operator ., you can create vectors on the fly. You cannot mix component types in one call of the swizzle operator. void main() {

   vec4 someVector(1.0f, 2.0f, 3.0f, 4.0f);
   someVector.xyz; //Creates a vec3(1.0f, 2.0f, 3.0f);
   someVector.rgb; //Creates a vec3(1.0f, 2.0f, 3.0f);
   someVector.stp; //Creates a vec3(1.0f, 2.0f, 3.0f);
   someVector.xgp; //Not valid!
   someVector.stz; //Not valid!

}

Void Data Type

This type is used to designate the return type of a function only.

Floating Point Types

float myFloat = 1.0f; vec2 myVec2(1.0f, 2.0f); vec3 myVec3(1.0f, 2.0f, 3.0f); vec4 myVec4(1.0f, 2.0f, 3.0f, 4.0f);

Integer Types

int myInt = 1; ivec2 myVec2(1, 2); ivec3 myVec3(1, 2, 3); ivec4 myVec4(1, 2, 3, 4);

BooleanTypes

bool myBool = true; bvec2 myVec2(true, true) bvec3 myVec3(true, true, true); bvec4 myVec4(true, true, true, true);

The vector forms of a boolean have the functions any and all defined.

Matrix Types

These matrixies are constructed either with floats, or with vectors of the correct size.

Sampler Types

These types allow one to sample from a texture.

Data Type Qualifiers

Function Parameters

void doStuff(in float a, out float b, inout float c) {

   a = 0.0f;
   b = 0.0f;
   c = 0.0f;

}

void main() {

   float a = 1.0f;
   float b = 2.0f;
   float c = 3.0f;
   doStuff(a, b, c); //Inside doStuff, b can be any value, and c is 3.0f
   //Now, a == 1.0f; b == 0.0f; c == 0.0f

}

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox