You are on page 1of 23

Introduction to

3D programming in Delphi
Yuriy Kotsarenko
R&D Consulting

ykot@afterwarp.io
http://afterwarp.io

Developer of Asphyre / Platform eXtended Library:


http://asphyre.net
Agenda
● Why 3D?
● The basics of 3D programming
● Using Direct3D and OpenGL directly
● Using FireMonkey
● Other libraries
● Advanced 3D concepts
● The future
So why going 3D?

● Natural spatial representation


○ Model industrial processes accurately
○ Visualize and manipulate real world data
● Visual realism
○ Easier to interpret for human eye
○ Professionally looking
● GPGPU
○ Harness the power of GPU for general-purpose work!
The basics of 3D programming

● 3D vectors: X, Y and Z
● 3D matrices (4x4 array)
○ Translation
○ Rotation
○ Scaling
○ A combination of above…
● 3D vector multiplied by matrix
○ Applying transformation
● 3D matrix multiplied by another matrix
○ Combining multiple transformations
○ Order is important!
The basics of 3D programming

● World matrix
○ Place object in 3D world.
■ Scale, move, rotate, etc.
● View matrix
○ How do we see the world?
■ Eye position, direction and orientation.
● Projection matrix
○ How the world is displayed.
■ Field of view, aspect ratio, dimensions…
The basics of 3D programming
V(2)
Indices
Vertices I(0) = 2

I(1) = 0

I(2) = 1
V(1)

V(0)
3D: What library to use?

● “Write games, not engines”


https://geometrian.com/programming/tutorials/write-games-not-engines

● Prefer framework rather than an engine


● Remember KISS and YAGNI principles!
○ Avoid unnecessary complexity
○ Avoid unnecessary features
● Too many options to choose from are not
necessarily a good thing.
○ Choose none! :)
Using Direct3D in Delphi
● Direct3D is a de-facto Windows standard!
○ Excellent driver and hardware support.
○ Good documentation on MSDN web site.
● Delphi Tokyo 10.2 comes with latest DirectX
headers.
● Delphi RTL provides all necessary 3D vector
types and functions.
● Tutorial application on GitHub:
○ https://github.com/yunkot/DelphiDirect3DTutorial
Using Direct3D in Delphi

Application Window

D3D Swap Chain D3D Device D3D Context DXGI Factory

D3D Shaders D3D Buffers D3D Views


Using Direct3D Directly
Let’s open Direct3DTutorial project!
Using OpenGL in Delphi
● Ne-He Tutorials:
○ http://www.sulaco.co.za
● Delphi OpenGL community:
○ http://delphigl.com (site in German)
● Embarcadero blogs:
○ http://edn.embarcadero.com/article/26401
● Many others (several pages on Google)
○ http://github.com/neslib/DelphiLearnOpenGL
○ http://www.noeska.net/ogl3xexamples.zip
Using OpenGL Directly
Let’s see some existing examples!
OpenGL: what to watch for

● Mixing Legacy (GL2) and Core (GL3+) features


○ Many online examples have legacy or hybrid code.
○ Difficult to know if certain function, enumeration or a
specific value is deprecated or not.
○ Hard to predict efficient code-path.
● Limited support on OS X (only 4.1 out of 4.6)
○ No Compute Shaders :(
● Windows drivers issues
○ Nvidia (excellent), AMD (good) and Intel (mediocre).
OpenGL: considerations
● Fun learning experience
○ Large community and collection of online materials.
● Get things done in few lines of code
● Legacy features? Use them! :)
○ Currently all vendors support them fully and well.
○ Beginner friendly: code is very short and elegant.
○ Try things before getting into “serious stuff”.
● Avoid using in a production environment
○ Instead focus on Vulkan, Metal and/or Direct3D APIs.
Using FireMonkey
Let’s try some basic example!
Using FireMonkey
● FireMonkey 3D design characteristics
○ Wrapping Direct3D and OpenGL APIs
■ DirectX on Windows, OpenGL on others
○ Mixed fixed-function and programmable concepts
○ Oriented towards UI applications
○ Basic 3D functionality
■ Simple shapes and models
■ Minimalistic lighting without shadows
■ Shader effects baked on top
○ Might be difficult to use latest GPU features
FireMonkey 3D strengths

● Strong 3D math and vector framework in RTL


○ Initially was part of FireMonkey, now resides in
System.Math.Vectors.pas
● GUI-oriented, facilitates showing UI on 3D
surfaces
○ See ControlsDemo (ControlsDesktop) example.
● Hardware-Accelerated Direct3D 11 Canvas
with Batching
○ GlobalUseGPUCanvas := True
Other libraries
● GLScene
○ Supports many different 3D graphics formats.
○ Great integration with third-party physics, scripting
and game engines.
○ Many service and helper functions: Cadencer,
Skyboxes, Perlin noise…
○ Getting outdated (pre-GL2 design?)
■ But seems to have some recent movements!
○ MPL, OpenGL-only :(
○ Installation issues in Delphi Tokyo 10.2.3
Other libraries
● Castle Game Engine
○ A great collection of features – shadows,
bump mapping, mirrors, etc.
○ Supports a large variety of 2D and 3D
formats.
○ Integrated with Kraft Physics Engine.
○ Interesting features such as sound and
editors.
○ (L)GPL, OpenGL-only :(
Other libraries
● Other “game engines” out there
○ Proprietary vs open-source
● C/C++ libraries and engines
○ Unity
○ Unreal
○ Ogre3D
● Afterwarp Framework (http://afterwarp.io)
○ We’ll see a quick preview shortly…
○ But we’ll talk more about this one tomorrow. :)
Advanced 3D
● Real-time Shadows
○ Shadow Volumes
○ Shadow Maps
■ Hard edges
■ Soft edges
● Order-Independent Transparency
● Instancing
● Generating Geometry directly on GPU
The Future
● Compute Shaders and GPGPU
○ More powerful AI
○ Physics
○ Parallel computing
● Ray-tracing
○ Nvidia Opti-X Ray Tracing Engine
○ Direct3D 12 Ray Tracing API
● Where we go from here?

You might also like