Using OpenGL in Lazarus
- within SDL (as demonstrated for example in MrSnugglekins by Max Foster, SpaceShooter by Christopher Winward and TesterGameEngine by Steven Binns);
- within PasSFML (as shown in Getting Started with PasSFML);
- on a form, as instructed in the first subsection linked to below;
- using the GLUT unit, as demonstrated in the last subsection.
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.
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.