Presentation about developing games and graphic visualizations in Pascal by Michalis Kamburelis, author of Castle Game Engine.
Presented in Salamanca at International Pascal Congress 2023 . See https://castle-engine.io/conferences .
6. Modern graphic APIs
● 3D (higher poly each day)
● textures to provide details (of various params)
● shaders (for both mundane stuff and cool vfx)
● hardware-accelerated (utilize GPU hardware)
● Examples:
○ OpenGL(ES), WebGL
○ Vulkan, WebGPU
○ Metal
○ Direct3D
7. 0. Input: 3D vertexes that form triangles (e.g. box is 8 vertexes, connected into 12 triangles)
1. Process each vertex to map it to screen (3D -> 2D) with extra info (vertex shaders)
2. Rasterization (having 3x 2D screen coordinates, determine pixels to draw)
(Img source: https://joshbeam.com/articles/triangle_rasterization/ )
3. Process each fragment (pixel) using per vertex data (typical place to apply texture) (fragment shaders)
(determine pixel color)
How does rendering on GPUs work
9. Parallelism
This is simplified view,
to get the point across. In
reality:
- OpenGL guarantees
rendering order in
primitive.
- OTOH there are
additional tricks like early
Z-test.
10. Shaders (1)
● Specialized language, compiled, and then executed on GPU
● Examples:
○ GLSL (OpenGL Shading Language)
○ HLSL (Direct3D)
● Has operations to work with vectors, matrices
● Doesn't have I/O, strings, any recursion allowed (so you can inline
everything!)
11. Shaders (2)
● Has uniforms: parameters defined for entire draw call
● Has inputs and outputs
● Vertex shader input:
○ position of each vertex in 3D (actually 4D, but nevermind)
○ any additional per-vertex data (like tex coords, per-vertex colors)
● Vertex shader output:
○ where the vertex is on screen
○ any additional data for fragment shader (like texture coords)
12. Shaders (3)
● Fragment shader input:
○ additional data from vertex shader (like texture coords)
● Fragment shader output:
○ color of the pixel
○ it can also "discard" pixel (e.g. to not draw pixels with alpha < 0.5)
16. More shaders
● Geometry shaders
○ between vertex and fragment, can generate primitives
○ e.g. vertexes could define NURBS control points, geometry shader
does actual triangles
● Compute shaders
○ perform arbitrary calculations on GPU
17. OpenGL, essentially
● Load VBO data (per-vertex data, like positions, normals, colors, texture coordinates)
● Load textures
● Compile and link shaders
● Each frame: Render each VBO using "draw call" (glDrawArrays)
18. Beyond OpenGL (1)
● OpenGLES - very like OpenGL, common on mobile
● WebGL - very like OpenGLES, in modern browsers from JS
19. Beyond OpenGL (2)
● Vulkan - new API from Khronos (authors of OpenGL and other standards),
○ low level
○ needs much more work to render anything
○ even more work to do it with reasonable performance
○ but if you want to go extra mile, you can get amazing performance
■ because low level, organize data closer to what you need
■ because low level, closer to how modern GPUs work
■ can split CPU work in threads, so utilize cores better for rendering
● WebGPU: like Vulkan, but in browser. A bit of WebGL counterpart.
24. Many OpenGL bindings
● FPC and Delphi: dglOpenGL
https://github.com/SaschaWillems/dglOpenGL
● FPC: built-in FPC GL, GLExt
● Delphi: built-in OpenGL / OpenGLExt (only for Windows)
● FPC and Delphi (excited about this)
○ GLAD
○ https://glad.dav1d.de/ - supports Pascal
○ generates the loading code with entry points you need
○ it is always up-to-date as it reads Khronos specs
○ Let's add Pascal to GLAD2 https://gen.glad.sh/ , and get also Vulkan!
26. Skia4Delphi (1)
● https://github.com/skia4delphi/skia4delphi
● 2D effects library, based on Google Skia https://skia.org/
● Pretty UI with fancy effects - got it easily
● Can be your rendering workhorse for rendering something more complex,
like a game
28. FireMonkey 3D
● Built-in Delphi
● 3D shapes, camera, materials
● Internet has lots of examples how to extend it -- e.g. to read some popular
3D file formats
● https://github.com/VincentGsell/FMeX (by Vincent Gsell)
32. Castle Game Engine (1)
● https://castle-engine.io/
● Open-source (OK to use it for proprietary apps too)
● 3D and 2D
● Editor to design 3D, 2D games, UI
● Powerful OOP API, using type-safe and fast code.
○ Following Delphi and Lazarus visual designing concept: everything you see
in editor is a class/property in Pascal. But applied to games.
33.
34. Castle Game Engine - Hello World
uses SysUtils,
CastleWindow, CastleSceneCore, CastleScene, CastleViewport, CastleCameras, CastleVectors;
var
Window: TCastleWindow;
Viewport: TCastleViewport;
Scene: TCastleScene;
begin
Window := TCastleWindow.Create(Application);
Window.Open;
Viewport := TCastleViewport.Create(Application);
Viewport.FullSize := true;
Viewport.Camera.SetView(
Vector3(0, 0, 10), // position
Vector3(0, 0, -1), // direction
Vector3(0, 1, 0) // up
);
Viewport.InsertBack(TCastleExamineNavigation.Create(Application));
Viewport.Camera.Add(TCastleDirectionalLight.Create(Application));
Window.Controls.InsertFront(Viewport);
Scene := TCastleScene.Create(Application);
Scene.Load('castle-data:/jetpack-cat/scene.gltf');
Scene.RenderOptions.Blending := false;
Viewport.Items.Add(Scene);
Application.Run;
end.
35. Castle Game Engine (2) - Features
● For Pascal (FPC and Delphi both fully supported), engine and games are both written
in Pascal.
● Assets
○ We like standards. glTF rocks, X3D rocks.
○ Great integration with e.g. Blender through glTF
○ Sketchfab importer through glTF
○ Sprite sheets, Spine, many other assets supported too
● Gfx effects - shaders, mirrors, shadows, PBR, bump mapping...
● Friendly to CI: command-line build tool, GitHub Actions, GitLab CI, Jenkins, Docker
image.
36. Castle Game Engine (3):
Cross-platform - many source platforms, and
even more target platforms
● Desktops (target+source) - Windows, Linux, macOS, FreeBSD, Raspberry
Pi
● Mobile (target) - Android, iOS
● Consoles (target) - Nintendo Switch
● Planned next - WebAssembly (target+source) and Oculus Quest (OpenXR;
target)
● Games released on Steam, Nintendo Switch, Android, iOS…
38. Castle Game Engine (4)
● No longer a solo project, so happy about it!
● We are looking for
○ community support https://www.patreon.com/castleengine
○ and cooperation with companies (consulting, dedicated features,
platforms…) - talk to me :)
39. BeRo (1) - Kraft (physics engine)
What is "physics engine"?
● you define a world of objects ("rigid bodies"),
● each with certain shape (simple shapes are supported and can be dynamic, complex
shapes like meshes are supported too albeit static)
● certain mass,
● you define some forces (e.g. to push some object)
● you let physics engine to calculate what happens - how objects move/rotate, when
collisions between objects occur.
40. BeRo (2) - Kraft (physics engine)
● Kraft is a physics engine implemented in Pascal.
● Open-source - https://github.com/BeRo1985/kraft/
● By Benjamin Rosseaux - https://www.patreon.com/bero/
● Used by Castle Game Engine as our default physics engine now.
○ It rocks.
41.
42. BeRo (3) - RNL (Realtime Network Library) using
reliable UDP
● Networking library to real-time games
● Made for multi-player games, using UDP but made to be reliable
● Quite easy API to use.
○ You can extend both client and server using Pascal code, and easily share
code between client/server, which is a big plus.
● Open-source https://github.com/BeRo1985/rnl/
● By Benjamin Rosseaux - https://www.patreon.com/bero/
43. BeRo (4) - RNL (Realtime Network Library)
with Castle Game Engine
● I did use it, with success!
○ https://github.com/castle-engine/not-quake , Online multi-player
first-person shooter using Castle Game Engine and RNL.
● Runs on actual server (michalis.xyz)
● Download from https://cat-astrophe-games.itch.io/not-quake and shoot me (and
send chat) within this talk, if you want:) You can get the client from (Windows
and Linux are stable, macOS is not for now) and shoot me now :)
44.
45. BeRo (5) - PasVulkan
● Framework using Vulkan, in Pascal.
● Prospective game engine.
● Open-source https://github.com/BeRo1985/pasvulkan/
● By Benjamin Rosseaux - https://www.patreon.com/bero/
46. BeRo (6) - PasVulkan
● Cross-platform, everywhere where Vulkan is
○ (or compat layer like MoltenVK on macOS)
● Sprites
● UI
● Integrated with other BeRo projects, in particular PasGLTF to read and render glTF,
here in Vulkan
● Custom project manager
47.
48. ● https://github.com/Cooler2/ApusGameEngine
● Open-source game engine, 3D and 2D, Delphi and FPC
● By Ivan Polyacov - https://www.patreon.com/ApusGameEngine/
● Unique features
○ Powerful 2D - effects (GPU particles), GUI system
○ Scripting
○ Can be used as a framework - utilities for various things, like 3D and 2D geometry
Apus Game Engine (1)
49.
50. Apus Game Engine (2)
It is actually used to make production games
● Spectromancer - http://spectromancer.com/
● Astral Heroes - http://astralheroes.com/
51. Apus Game Engine (3): Features in
development now
● GUI styling in CSS manner
○ "color=FFF; borderColor=444; borderWidth=1"
● 3D Import of more formats
○ Using the ASSIMP library.
● Cross-platform TCP server
○ For Windows and Linux
53. 3D model formats
Standards matter.
● Look what others support and what is designed in a way that welcomes more ecosystem.
○ If something doesn't pass 1 of these 2 tests, it will not exist in long-term.
○ And software adapts better to changes than a bunch of data files, by design.
○ So think what will happen with your data in long-term.
● Use glTF.
○ Observe what X3D and USD are doing too.
● Ecosystem of applications around you.
○ like exporter from Blender
○ like Sketchfab integration
54. This is the benefit of interoperability: ~3 days of
work and we're the first engine on Sketchfab :)
https://sketchfab.com/importers#game-engine
55. Graphic effects to improve realism
● Physically-Based Rendering is now standard, across tools and renderers
● Shadows, mirrors - look for techniques that make your world realistic automatically
● Look at shaders support - ability to write custom shaders opens new world of possibilities
○ CGE has composable shader effects
56. Physics Engine
Physics is always fun.
● Unexpected things happen naturally.
● Integrate with existing physics engines (Pascal or non-Pascal: Kraft, Bullet, PhysX…)
57. Platforms
Selling games today is hard - lots of competition.
Cross-platform game engines allow to counter this by releasing on N markets instead of 1.
Without having Nx cost of development.
● desktop
● mobile
● consoles
● web
● vr ar
Integrations, like Steam and Google Play, come here into play too.
58. You are doing this for long-term
● Active community and developers are the value of a project
● Documentation
● Continuous Integration and Delivery!
○ Your QA will love you for this.
○ Games need QA desperately, good games are naturally "generators of
unexpected experiences". And we know what "something unexpected may
happen" usually means for software.
59. Graphic tools
Native to the engine, and/or external but well integrated.
● CGE editor
● Blender
● Tiled
● Spine
● Ask your artists, not only devs.
● Again, love standards.