C++C++17ComponentsGame DevelopmentLanguage FeatureLearn 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.

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

How To Create A Custom Torus In 3D Mesh?

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

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

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

  • a Button (Button1)
  • a Viewport3D (Viewport3D1) component to our form,
  • a TDummy object (Dummy1),
  • a TLightMaterialSource (LightMaterialSource1),
  • and a TLight (Light1) and a TCamera (Camera1).

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

We can set some properties of TMesh object like so:

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

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

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

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.

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

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.

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

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.

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.

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

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

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

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.

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

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.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What is the negation (std::negation) metafunction in C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Multiset (std::multiset) In Modern C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Set (std::set) In Modern C++?

C++C++11C++14C++17C++20IteratorsLearn C++

What Is Deque (std::deque) In Modern C++?