Patch to add multi-samping (anti-aliasing) capabilities to TOpenGLControl
Original Reporter info from Mantis: Michalis @michaliskambi
-
Reporter name: Michalis Kamburelis
Original Reporter info from Mantis: Michalis @michaliskambi
- Reporter name: Michalis Kamburelis
Description:
Attaching a patch for Lazarus OpenGL context components/opengl/ to allow MultiSampling, which allows to do anti-aliasing in a fast way on modern GPUs. The API for users is trivial, just new TCustomOpenGLControl.MultiSampling property that can be set to values like 2 or 4 to request anti-aliasing. It's nicely documented in the interface.
Patch is tested and successfully initializes multi-sampling under both WinAPI and GLX. Other LCL widgetsets should compile, but nothing more (they will ignore MultiSampling value for now).
This patch is created by Jan Adamec (base + WinAPI multisampling), based a little on Michalis Kamburelis code in "Castle Game Engine", and then enhanced by Michalis to handle also multisampling under GLX and add small comments.
Done vs Lazarus SVN revision 37259 (from today, 2012-05-13).
Some implementation comments:
- For Windows, the non-trivial change is that you need to create a temporary GL window to query WGL extensions. Previous code using wglGetExtensionsStringARB wasn't really working, it wasn't returning extensions list at the first context creation. (This is known deficiency of WinAPI, and creating temporary window is the most sane solution.)
- For GLX, the non-trivial change is that CreateOpenGLContextAttrList now honors UseFPGLX define and looks at GLX_version_1_3, to construct good context attributes both for new and old GLX (previous code had hardcoded attributes for FBConfig version).
I also renamed GDK_GL_ constants to just GLX_, as that is their purpose: they are useful only when UseFPGLX is not defined, when the current implementation doesn't use GLX unit.
(Hopefully at some point we can drop this old stuff, and just use GLX unit always in GLGtkGlxContext, this would simplify code. Even FPC 2.4.x had working GLX unit, only with some features missing (like FBConfig functions). I can volunteer to do this cleanup (use always GLX unit in GLGtkGlxContext), if it's agreed.)
Everything else is pretty much straightforward: we pass MultiSampling property around and use it to request proper support from OpenGL context. Care is taken to fallback on no-multi-sampling context if no context satisfies the MultiSampling requirements.
To test: simply take any TOpenGLControl demo (like components/opengl/example/testopenglcontext1.lpr, you may want to stop rotations there to actually observe anti-aliasing) and change MultiSampling to a value > 1, like 2 or 4. On modern GPU, you will see nice anti-aliasing.
P.S. Once this is accepted, Michalis also plans to submit another patch to add StencilBufferBits property (to control stencil buffer, useful e.g. for shadow volumes). The patch here already fixes also issue #21881 (closed) for GLX, as both new and old GLX approaches (FBConfig or not) will now set stencil bits = 1. But the real solution is to give control over StencilBufferBits to the developer (currently, WinAPI doesn't request stencil at all, only GLX). I want to make simple TOpenGLControl.StencilBufferBits property to request stencil in a portable manner.
Mantis conversion info:
- Mantis ID: 22026
- Version: 1.1 (SVN)
- Monitored by: » @corpsman (Uwe Schächterle)