Monday, May 4, 2020

May the Fourth (be with you) - and some FireMonkey feature requests




Happy Star Wars Day!

What? Delphi programming types are allowed to be silly sometimes. :)

This iconic 3D Model is used with permission from CoreyLoses. Corey created a full conversion mod of Sins of a Solar Empire called Star Wars: Thrawn's Revenge II: Ascendancy. If you like real time strategy games and have a soft spot for Star Wars, maybe take a look. Plus, Corey is a fellow Canadian, which is nice.


Working with 3D Models


Delphi has some interesting graphics functionality, including 3D features. It isn't a full blown game engine out of the box and is arguably better suited for enterprise applications (more on this later), but it does cover the basics, including viewing existing 3D models. FireMonkey applications also take advantage of hardware acceleration and are cross platform.

The TModel3D component supports three different 3D file formats:


These formats have the benefit of being free for anyone to use and are plain text, so they can be edited without specialised software if necessary. And sometimes it's necessary.

You can find any number of 3D models online. Some free and some commercial. If a model that you really like isn't available in one of these formats, you can use something like 3D Object Converter. According to the web site it supports 803 different file formats and can be used for batch conversions. This is what I used to convert the .mesh models from Sins. And, as an added bonus, it was written in Delphi.


Demo


The demo application (available here) is really easy to make, doesn't require a single line of code and is supported in the Delphi Comunnity Edition. Note: The project on GitHub may differ slightly from the described steps.

Create a new FireMonkey 3D application
Set the form's colour to black (like space)
Add a TLight component to the form
Add a TModel3D component to the form
Double-click on the TModel3d's MeshCollection property and select the 3D model file to use
You may need to tweak the TModel3d's Scale and RotationAngle properties to get things to look right
Add a TFloatAnimation component to the TModel3D (not to the form) and change the following properties:
  • Duration: 15
  • Enabled: True
  • Loop: True
  • PropertyName: RotationAngle.Z
  • StartValue: -180
  • StopValue: 180

That's all there is to it. A very simple application to view 3D models.


Feature Requests


3D models use image files for textures and two of the common file formats, Targa (.tga) and DirectDraw Surface (.dds) aren't supported in Delphi.

You can work around this by using your favourite image editing software to convert texture images (a single model might have several) into something that Delphi supports (like .bmp or .png) and then edit the 3D file and change the embedded texture file names to point to the new versions. But it's a bit tedious and you end up with two sets of image files; One for Delphi and one for everything else. 3D Object Creator has an option to save a model's textures as .bmp files, but you still need to edit the 3D file manually.

I think it would be better if these image formats were supported in Delphi. Please feel free to vote for these QP requests.

No comments: