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-6647442318963498581822/]

We can set some properties of TMesh object like so:

[crayon-664744231896b649763305/]

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

[crayon-664744231896d069963592/]

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

[crayon-664744231896f863326794/]

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

[crayon-6647442318971899542508/]

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-6647442318973453708104/]

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

[crayon-6647442318975794272263/]

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-6647442318979955154645/]

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

[crayon-664744231897b739376057/]

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-664744231897e313770725/]

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