Sign in Join
Wiki copytemporary

OpenGL: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: OpenGL

OldNewLine
1+OpenGL (**Open** **G**raphics **L**ibrary) is a cross-platform API for producing 3D computer graphics. Most modern video cards provide hardware accelerated OpenGL support, wich makes OpenGL a good solution for writing advanced graphics software.
2+
3+## OpenGL units in Free Pascal
4+
5+The Free Pascal OpenGL interface consists of the following units:
6+
7+- **gl**: This unit contains the main functionality such as drawing polygons, applying transformations, setting colors and materials,... Procedures always start with the preposition "gl".
8+- **glu**: This unit contains OpenGL utils. Although it has some useful functions, this unit is not necessary as you can implement all glu Procedures with the functionality of the gl unit. Procedures always start with the preposition "glu".
9+- **glext**: Vendors can provide additional functionality through extensions. Use this unit to use these extensions.
10+ The functionality specific to higher OpenGL versions (1.2 and later) is available in this unit as well. Initializing this functionality is similar to initializing normal OpenGL extensions: just call Load_GL_version_X_Y function. If your OpenGL library version is older than X.Y, Load_GL_version_X_Y will return false.
11+- **glut**: This unit provides functionality for creating an OpenGL window. Although this is a cross-platform unit, most Windows operating systems don't have a glut dll by default.
12+- **glx**: glx provides functionality to set up an OpenGL window in an X Window system. Procedures always start with the preposition "glx". Obviously, you cannot use this unit on non-X Window systems such as Windows.
13+
14+## OpenGL units in Lazarus
15+
16+Lazarus includes a TOpenGLControl - a LCL control with an OpenGL context. The Lazarus package LazOpenGLContext can be found in lazarus/components/opengl/lazopenglcontext.lpk. An example can be found in lazarus/examples/openglcontrol/openglcontrol_demo.lpi.
17+
18+If you want to modify TOpenGLControl implementation, for example to add new properties like ColorBits and AuxBuffers, see [[Extending TOpenGLControl]].
19+
20+## Third party OpenGL units
21+
22+- [[GLScene]] is a Lazarus package with lots of extra features for OpenGL applications.
23+- [[Castle Game Engine]] allows you to navigate and render 3D worlds (in VRML, X3D and other 3D formats).
24+
25+## Tutorials
26+
27+- [[OpenGL Tutorial]]
28+- [OpenGL Core Tutorials](https://github.com/neurolabusc/OpenGLCoreTutorials)
29+
30+## Tutorial OpenGL 3.3 ( modern OpenGL )
31+
32+- [Lazarus OpenGL 3.3 - Tutorial](https://wiki.delphigl.com/index.php/Lazarus_-_OpenGL_3.3_Tutorial)- (deutsch) (extern)
33+- [Lazarus OpenGL 3.3 - Tutorial](https://github.com/sechshelme/Lazarus-OpenGL-3.3-Tutorial/blob/master/wiki.md)- (deutsch) (extern) (github)
34+
35+## Q/A using OpenGL binding
36+
37+**Q: I don't understand why the ARB version does not work. AFAIK it has the same entry points as the core functionality and OpenGL versions are required to support the ARB version of functions even if the non ARB version is in the core now.**
38+
39+A: No. OpenGL doesn't have to support the extension entry points, even when it supports the same functionality in core already. Vendor is free to remove the extensions, and support only the "core" functions.
40+
41+Also, sometimes when functionality is promoted from ARB extension into core it's slightly changed (although in 99% cases it stays the same). Example is the GLSL shaders, that expose (very slightly) different API through ARB extensions and through core 2.0 specification.
42+
43+Basically, what is exposed through extensions is completely orthogonal to what is exposed through core functionality, so they should be initialized separately. In particular cases, you can make tricks like
44+
45+``` pascal
46+if glIsBufferARB = nil then glIsBufferARB := @glIsBuffer;
47+```
48+
49+but this should be left to the final programmer that knows that "ARB_vertex_buffer_object" behavior is identical to how it works in core.
50+
51+**Q: When I try to use the gluTessCallback function from the glu unit, either the compiler complains or I get a segmentation fault when I try to run my program. How can I get it to work properly?**
52+
53+A: The gluTessCallback function is defined in the following way in the **glu** unit:
54+
55+``` pascal
56+{$IFDEF Windows}
57+ {$DEFINE extdecl := stdcall}
58+{$ELSE}
59+ {$DEFINE extdecl := cdecl}
60+{$ENDIF}
61+
62+gluTessCallback: procedure(tess: PGLUtesselator; which: GLenum; fn: TCallBack); extdecl;
63+```
64+
65+<b>Windows note:</b>A common problem is that the calling convention of the glu functions seems to vary in several different GLUT DLLs builds for windows. In some it is stdcall, in some cdecl. The most used one seems to be stdcall.
66+
67+You should define your [[callback]] functions like so:
68+
69+``` pascal
70+procedure MyTessBegin(which: GLEnum);
71+{$IFDEF Windows} stdcall; {$else} cdecl; {$endif}
72+begin
73+ glBegin(which);
74+end;
75+
76+procedure MyTessEnd;
77+{$IFDEF Windows} stdcall; {$else} cdecl; {$endif}
78+begin
79+ glEnd(); // The () is required
80+end;
81+
82+procedure MyTessVertex(data: Pointer);
83+{$IFDEF Windows} stdcall; {$else} cdecl; {$endif}
84+begin
85+ glVertex3dv(PGLDouble(data));
86+end;
87+```
88+
89+Note the cdecl/stdcall modifiers; they are required. You then call gluTessCallback in the following manner:
90+
91+``` pascal
92+gluTessCallback(tess, GLU_TESS_BEGIN, TCallBack(@MyTessBegin));
93+gluTessCallback(tess, GLU_TESS_END, TCallBack(@MyTessEnd));
94+gluTessCallback(tess, GLU_TESS_VERTEX, TCallBack(@MyTessVertex));
95+```
96+
97+Make sure you cast the pointer to your callback function to the TCallBack type, as shown above.
98+
99+Alternatively, if you want to refer to the OpenGL functions directly, use the following calling convention:
100+
101+``` pascal
102+gluTessCallback(tess, GLU_TESS_BEGIN, TCallBack(glBegin));
103+gluTessCallback(tess, GLU_TESS_END, TCallBack(glEnd));
104+gluTessCallback(tess, GLU_TESS_VERTEX, TCallBack(glVertex3dv));
105+```
106+
107+Finally, [here](http://www.ludicity.org/files/tess_demo.pp) is source code for a simple tessellation demo (uses glut).
108+
109+## See also
110+
111+- [[Vulkan]] is a new low-level Graphics API intended to replace OpenGL.
112+
113+---
114+*From the Free Pascal wiki, page "OpenGL", as it stood on 2022-08-09 after a change by Mathias, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history