C++ComponentsDatabaseLanguage FeatureLearn C++

This Is The Smart Low Code Way To Do 3D Visualization In C++

How we can display two dimensional data? Can we display data in X and Y? How can we create rectangular prism in 3D in C++? How we can generate random two dimensional data? How can we display 2D data with rectangular bars? How can we rotate 3D visualization? How can we zoom in and zoom out to a 3D visualization?

C++ Builder helps you to use the minimum code possible

It’s true, sometimes C++ can be a little..verbose. It’s a great language but the power of things like classes can sometimes be wrapped up in quite a lot of ‘scaffold code’. The component-based idiom of C++ Builder means that a lot of that additional wordiness is avoided for you since it is encapsulated in components. This is one of the things developers love about Delphi, C++ Builder’s sibling. The fashion trend of the moment has a buzz word which the rest of the computer programming community is latching on to: low code.

C++ Builder and RAD Studio Delphi are the original low code kings

Those of us in the know find it not at all surprising that low code is a great advantage, After all, we’ve been doing it for decades now with C++ Builder and Delphi!

Data Visualization with C++ Builder is easy – and it’s super fast!

C++ Builder is a fast programming language that you can use to develop fully native games which operate at the full native speed of the device on which they are running. You can use OpenGL or Direct3D libraries or some other 3rd party 3D Engines. In C++ Builder you can directly create your own 3D objects, you can animate them on runtime. Viewport3D (TViewportd3D) component in C++ Builder FireMonkey projects is good 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 creating these 3D objects. You can also easily load your 3D objects into Viewport3D by using Model3D (TModel3D).

The TMesh class

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 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 MaterialSource property. If no material is specified, then the shape is filled with red color.

Using TCube in C++ Builder avoids lots of extra work for you

The TCube is used in 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.

cube-8875832-8154904

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

Setting TCube properties

We can set features of a dragged TCube from the Component Palette, as given example below,

We can create a 3D cube

Now let’s create a new project, generate a random 2D data and let’s visualize this data with rectangular bars in our Viewport3D.

1. Creating a New Project and Setting some Global Variables

Let’s create a new project, generate random 2D data and let’s visualize this data with rectangular bars in our Viewport3D. First,

1. Create a new C++ Builder Console FMX application, save all project and unit files to a folder.
2. Add some defines and some global variables for bars into our example as given below, add or modify code lines as below,

2. Generating Random 2D Data

Let’s assume that we have data in 2D form, that means we have values in each X and Y coordinates. So, as an example, we can generate a random 2D data as below,

Simply we can print all of these generated values of this 2D data to TMemo component as given example below,

3. Visualization of 2D Data with 3D Rectangular Prisms

We have 3D data grid from the calculations or from the obtained values of sensor about a 3D environment or about a 3D object. We can use cubes to visualize these kind of 3D data. Idea here, to display magnitudes in every node we will use color gradient (i.e gradient from yellow to red and to blue ) and we will display these colors with out 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 part is we can easily slice this 3D form in X Y or Z directions so we can visualize every layer. Disadvantages are bigger grids may take a lot of memory and slower display time and slower animations.

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

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 and

– 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.
– Small cube will be used to see center of this Dummy1 object in design, we will make it invisible on run time.
– Set both Light Types to Point and, put Lights to upper left and right corners, one light could be gray.
– Set Camera in a good position for example z=-20, position where the cube (origin of Dummy1) is centered.
– Select Viewport3D, set Use Design Camera property to false

Rotating a 3D prism in only 10 lines of code!

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

You can also do zoom in and zoom out by changing camera z position. To do this, select Viewport3D, double click to OnMouseWheel() event and change camera position to as below,

When done we must free all the bars which were allocated in the memory with new command. We can use this procedure when Form is closed or when 3D display is done, as given below,

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

Finally we should add all these above to our Button OnClick() events (Button1 , Button2)

We can update data and update the view by changing height of all bars as below,

Here is the finished 3D data visualization in C++ Builder

Final Words

From now, you can advance this example, by adding a plane object and a texture that shows directions and grids, or this texture can be a natural texture. You can light when a cube is selected or when the mouse is on it, you can also show its value with coordinates and percentage. Firemonkey has Opacity features in graphics so you can make cubes semitransparent to see their behind or to make them like glass. You can also apply different textures and colors to each of them. 

It is really easy and simple to do these kinds of graphics. 

In this method, we chose to use Viewport3D and 3D cubes. You can do these kinds of graphics with OpenGL or other 3D engines and components. You can also directly draw to the Canvas or to the Canvas of a Bitmap by calculating projections of objects with 3D Rotation Matrix as given example here.


RAD StudioC++ Builder is a great programming language to calculate these kinds of problems in C++ and is also powerful enough for all your development needs. If you are new to C++ Builder, why not download and try C++ builder today?

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Get A Substring of a Wide String in C++

C++C++11C++14C++17Introduction to C++Learn C++

How To Access Individual Character Elements Of A C++ String

en_USEnglish