Site icon Learn C++

Learn To Create A Torus As A Custom 3D Mesh Object in C++ Builder

Learn To Create A Torus As A Custom 3D Mesh Object in C++ Builder

In a 3D Application Development, 3D objects can be displayed with 2D projection methods by using 2D / 3D mathematical calculations drawings. That may be hard to code and needs much coding skills to display them. We can use OpenGL and DirectX with their 3D functions/commands to display them. We can also use and port a 3D Engine SDK. In C++ Builder, 3D objects can be easily displayed by using a Viewport3D component. We can change their shape, position, color, materials, etc. on design time or runtime. We can add Texture Materials or Light Materials to them. We can group them by using Dummy objects. In this post, we explain what a Viewport3D is and how to draw a very parametric 3D Torus in C++ Builder.

What is Viewport3D?

Viewport3D (TViewport3D) is a 3D component to display 3D objects in its space. TViewport3D implements IViewport3D methods to describe how a 3D object is seen. It is really simple and nice to develop some small 3D games or add some 3D features to our applications. Viewport3D is a component that shows the viewport of the camera or default view. We can arrange its position, Width, and Height on any place of our form or we can use it in client view to do full screen. We can use viewport3D without a 3D Camera, it uses its design camera view. If we want to use a camera and want to see the view from the view of that camera, we must set its Use Design Camera property to false from its properties. For more details about how to use Viewport3D, 3D objects, and lights, materials, and camera, here are some example posts that we released before,

How To Draw A Torus In 3D?

To create a torus in 3D, first, we should calculate the position of 2 points (P0, P1) in the inner circle (with R2 radius and Alpha angle), then we can rotate these points in the outer circle (with R1 and Beta angle) to find P2, P3 points. Then, we can create a plane with these 4 points, which means there will be two triangle faces on this plane. Here is a schematic of how we can draw torus.

How To Create A Custom Torus In 3D Mesh?

Let’s create a simple application that draws a custom torus as below,

We should add these components below from the Palette window on the right side;

We can create a new TMesh in C++ as shown below.

[crayon-674f484553ccb228920672/]

We can set some properties of TMesh object like so:

[crayon-674f484553cd4358532376/]

Here we use 4 and 6 magic numbers to create VertexBuffer and IndexBuffer.

[crayon-674f484553cd5973594743/]

Mesh Data has 4 parameters, these are P0, P1, P2, P3 nodes;

[crayon-674f484553cd6283513186/]

and Index Buffer has 6 params, because 4 nodes creates 2 faces and we can define each of them by 3 points.

[crayon-674f484553cd7992071495/]

This article explains more: https://vulkan-tutorial.com/Vertex_buffers/Index_buffer

How To Create A Custom Torus Class with TCustomMesh?

First, let’s create a new C++ Builder Multi-Device (FireMonkey) Application, and add let’S add some global variables as below.

[crayon-674f484553cd8867813663/]

In modern C++, we can create a custom torus by using a TCustomMesh class as its parent class.

[crayon-674f484553cd9077972166/]

A Torus can be drawn with rotating points in two radius (R1 and R2). R1 radius is the radius of the torus and R2 is the radius of its pipe. So we will have 2 loops for the beta angles in R1 and alpha angles in R2;

Here above, the last TTorus(…) method is the constructor of this class. Now, we can define this constructor at the outside of this class. We can create a Torus object in its parametric constructor as below.

[crayon-674f484553cdc051013683/]

Finally, we can create a custom torus object with a one click as below.

[crayon-674f484553cde077571097/]

We can use OnMouseMove, OnMouseUp, OnMouseDown events of Viewport3D to rotate this 3D object by mouse. Just select your ViewPort3D, go to the Events tab of the Object Inspector, and double click each of them to create their definitions. You can add your codes inside them. Here is a simple version.

[crayon-674f484553ce0813209902/]

As a result, if you run your code and click to button, it will create your own ViewPort3D object on your Dummy object as below.


Note that this is an educational example, you can improve and optimize this code to make it faster and better.

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.


Exit mobile version