Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++Code

How To Create 3D Data Visualizations With Viewport3D in C++

visualize 3dcubic3

C++ Builder makes it easy to build engineering applications with custom data visualization methods. These kinds of applications are necessary to understand data results easily. You can use OpenGL or Direct3D libraries or similar 3rd party engines to create great-looking 3D visualizations which help users to make sense of complex datasets. With C++ Builder you can directly create your own 3D objects in your own C++ app by using TViewport3D. You can also animate them at runtime. In this post we will explain how to visualize 3D nodal data with colored cubes.

Why are 3D data visualizations used?

3D data visualization is important an important tool to help understand complex physical and statistical systems such as thermal distributions, fluid properties, pressure changes, and stress analysis of materials.

How To Create 3D Data Visualizations With Viewport3D in C++ viewport 3D

How to use Viewport3D for 3D data visualizations?

Viewport3D (TViewportd3D) component in C++ Builder FireMonkey projects is a great way to display many basic 3D Objects like Plane, Cube, Sphere, Cone, Plane, Ellipse3D etc. Please see this post about Working With 3D In Modern Windows C++ Development for examples on creating these 3D objects. You can also easily load your 3D objects into Viewport3D by using Model3D (TModel3D).

To create a 3D object to be used in Viewport3D we need to use TMesh classes.  TMesh is a custom 3D shape that can be customized by drawing 3D shapes.  It is a class which publishes a set of properties from its ancestor, TCustomMesh, in order to let you design new 3D shapes at design time from within the IDE, through the Object Inspector. Use the Data property to specify the points, normals and textures for each point, and the order in which the resulting triangles are drawn. The designed shape is filled with the material specified through the MaterialSource property. If no material is specified, then the shape is filled with a red color.

How to use TCube for 3D data visualizations?

How To Create 3D Data Visualizations With Viewport3D in C++ The TCube component

The TCube is used in the Viewport3D component of C++ Builder. It is a class that implements a 3D cube shape, built on a 3D wireframe, that can be placed with Viewport3D component or with a 3D FireMonkey form. We can use these cubes to visualize the magnitude of our data in XYZ Cartesian coordinates with colors.

TCube is a 3D visual object that can be added from the Tool Palette in C++ Builder. To change the color or add texture to the cube, use the MaterialSource property.

Set SubdivisionsDepthSubdivisionsHeight and SubdivisionsWidth to specify how smooth the cube’s surfaces are.

In this post we will use these cubes to display our 3D data (i.e. Temperatures in every nodes of a 3D Object). We can also use other 3D objects in C++ Builder. For example you can use TPlane (TPlane) forms in 3D to display surface distributions in a 3D view. For more details about 3D objects in C++ Builder, please read more about Learn To Quickly Create Specific 3D Objects In Modern C++ Applications For Windows

cube 8875832 8154904

How to code 3D data visualizations for a 3D grid object with cubes in C++?

Let’s assume we have 3D data grid, perhaps from some calculations or the obtained values of sensor about a 3D environment or about a 3D object. We can use cubes to visualize these kinds of 3D data. the idea here is to display magnitudes in every node. To do that we will use a color gradient (i.e gradient from yellow to red and to blue). We will display these colors without 3D cubes. Using cubes has some advantages: easy to visualize in nodal form, easy to color, easy to rotate and zoom all, one of the most important parts is we can easily slice this 3D form in X Y or Z directions so we can visualize every layer. The disadvantage are bigger grids may take a lot of memory and slower display time and, consequentially, slower animations.

Let’s start to create our 3D grid object with cubes.

1. Create a new C++ Builder Console FMX application, save all project and unit files to a folder. Modify the code lines as shown below.
2. Drag a Viewport3D from Tool Palette on to Form. We will use this to display our 3D map space. Add a Dummy Object, Small Cube and Camera, 2 Lights to our ViewPort3D by dragging from the Tools Palette. Let’s modify these from design.

3. Dummy1 will be our rigid object which will be composed with a lot of cubes; thus, we can easily move or rotate this Dummy object and all other 3D objects attached to this.
4. The small cube will be used to see center of this Dummy1 object in design. We will make it invisible at run time.
5. Set both Light Types to Point and, put Lights to upper left and right corners, one light could be gray.
6. Set Camera in a good position for example z=-20, position where the cube (origin of Dummy1) is centered.
7. Select Viewport3D, set Use Design Camera property to false, arrange your camera position.

Let’s start with some global definitions about our cubic grid, and let’s create TCube grid array as below.

Now, simple we can create a 3D cube as below:

We can set features of this cube as given example below:

We can generate 3D data as shown below, this procedure also prints results to the Memo1 component.

If we combine these two examples we can create a function which creates DX,DY and DZ sized cube in a given x, y, z with origin displacement

We can rotate all 3D cubes by rotating their parent Dummy1. Select ViewPort3D1 and double click its OnMouseDown(), OnMouseMove(), OnMouseUp() events and modify them as below,

When we close the form we should free all cube material sources and cubes as given example below,

Now lets change our Button1 click, we can create data cubes with Button1 click as below,

What does the C++ 3D visualization example look like?

When you run you application, if you click the button, it will generate data and display as below,

visualize 3dsubic 8109316

These kinds of graphics are good to help users understand thermal distributions, fluid properties, pressure changes, stress analysis of materials or any similar 3D data. They can be displayed as above to understand and check the whole data on that system.

Is there a full Example of 3D data visualizations of a 3D Grid object with cubes in C++?

Here is the full C++ Builder – FireMonkey example of creating 3D data visualizations in C++:

Where can I find other C++ examples of 3D data visualizations?

In our previous post we explained how we can solve heat transfer equations or other 3D equations on those kind of 3D problems. These kinds of methods can be combined together for more professional applications and analyses.

Do you want to learn how to visualize topographic 3D data? How can we view a 3D function? Can we display specific functions in 3D like activation functions in AI? How can we create a 3D map in C++? Topographic Data are data sets about the elevation of a surface, generally the surface of the Earth. C++ Builder makes it very easy to build these kinds of simple visualizations. Here is the post about it:

How To Create 3D Data Visualizations With Viewport3D in C++ the C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, and iOS operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.

IN THE ARTICLES