Using OpenGL in Lazarus

The use of OpenGL (in C++ programs) is carefully explained and demonstrated in the official OpenGL Programming Guide. In Pascal you can use OpenGL code in these contexts:

See also our demonstrations of WebGL in Smart Pascal.

OpenGL Routine Naming System

The way that suffixes are appended to names of routines can make the code look difficult to beginners, but soon after you start to write OpenGL code yourself it will seem more straightforward. Digit suffixes simply represent the number of parameters and the letters i, d and f stand for integer, double and real. The subscript v is used to denote that the parameter is a vector (array).

In the examples on the following two pages, glVertex2i takes as parameters the x and y coordinates as integers. (If the z coordinate is not supplied, it is given the value zero). The routine glColor3f takes for the three parameters the red, green and blue components of the colour as floats, with a maximum value of 1.0. It also sets the alpha (blending) value to 1.0 implicitly.

Shaders

You must write your vertex and fragment shaders in OpenGL Shading Language (GLSL) and can then import the code from file or include them as strings in your Pascal code. Our examples are short and we include them in the code. Max Foster imported from files the many shaders he used in MrSnugglekins. For your convenience we collect them here and display them syntax-highlighted as for the C language on which the syntax is based.

Numbers appended to data types indicate size, so mat4 represents a four dimensional matrix. A uniform is a variable that is the same for each pixel, whereas an attribute is a variable that may be different for each pixel. In order to pass data from the vertex shader to the fragment shader, declare the same varying variable in each shader.

Programming - a skill for life!

Vector, bitmap and motion graphics using Crt and WinGraph units or OpenGL