Introduction to OpenGL: A 3D Text Rendering Tutorial
With the availability of tools like DirectX and OpenGL, writing a desktop application that renders 3D elements is not very difficult nowadays. However, like many technologies, there are sometimes obstacles making it difficult for developers trying to enter into this niche. Over time, the race between DirectX and OpenGL has caused these technologies to become more accessible to developers, along with better documentation and an easier process of becoming a skilled DirectX or OpenGL developer.
DirectX, introduced and maintained by Microsoft, is a technology specific to the Windows platform. On the other hand, OpenGL is a cross-platform API for the 3D graphics arena whose specification is maintained by the Khronos Group.
In this introduction to OpenGL from Toptal, I will explain how to write a very simple application to render 3D text models. We will be using Qt/Qt Creator to implement the UI, making it easy to compile and run this application on multiple platforms. The source code of the prototype built for this article is available on GitHub.
The goal of this simple application is to generate 3D models, save them to a file with a simple format, and to open and render them on screen. The 3D model in the rendered scene will be rotatable and zoomable, to give a better sense of depth and dimension.
Before getting started, we will need to prepare our development environment with some useful tools for this project. The very first thing we need is the Qt framework and relevant utilities, which can be downloaded from www.qt.io. It may also be available through your operating system's standard package manager; if that is the case, you might want to try with it first. This article requires some familiarity with the Qt framework. However, if you are not familiar with the framework, please do not feel discouraged to follow along, as the prototype relies on some fairly trivial features of the framework.
At this point, you might want to clone the repository from GitHub and follow it as you read through this article.
We will begin by creating a simple Qt application project with a single document widget. Since it is a bare-bones widget, compiling and running it will not produce anything useful. With Qt designer, we will add a "File" menu with four items: "Newâ€¦", "Openâ€¦", "Close", and "Exit". You can find the code that binds these menu items to their corresponding actions in the repository.
Clicking on "Newâ€¦" should popup a dialog that will look something like this:
Here, the user may enter some text, choose a font, tweak the resulting model height, and generate a 3D model. Clicking on "Create" should save the model, and should also open it if the user chooses the appropriate option from the lower-left corner. As you can tell, the goal here is to convert some user inputted text into a 3D model and render it on the display.
The project will have a simple structure, and the components will be broken down into a handful of C++ and header files:
Files contain QDialog derived object. This implements the dialog widget which allows the user to type text, select font, and choose whether to save the result into a file and/or display it in 3D.
Contains implementation of QOpenGLWidget derived object. This widget is used to render the 3D scene.
Contains implementation of the main application widget. These files were left unchanged since they were created by Qt Creator wizard.
Contains the main(â€¦) function, which creates the main application widget and shows it on screen.