Graphics3D


In this article you will find a brief description of the 3D graphics module added to the framework in version 6.0.0.



About module

Graphics3D module is a set of objects and functions useful for building 3D applications. This is not a complete set of all entities you need to create 3D applications and games. This module contains only basic commonly needed entities like camera and model and some primitive 3D object generators. You may use such an entities for quick start.

Cameras

All 3D applications require a camera. Camera is a way to look into 3D world. The aim of a camera is to transfer objects from virtual 3D space into screen's/monitor's 2D flat space. This goal is achived by so called projection technique. In the module you can find template interface ICamera and it's implementation in template class TCamera which describes basic camera and implements its projection management part. The camera has 3 methods for setting projection: Frustum(), Perspective(), and Ortho(). The first two methods set the same type of projection which produce the illusion of real 3D graphics, they just use different arguments to describe such a projection. The last method sets orthographical projection which is commonly used in 3D editors to view/edit scenes from different sides(left, top, front).

Beside projection cameras could be placed in virtual 3D space in different positions and could look in different directions. These camera transormations are described by camera position(you may use getPosition() and setPosition() methods to get and change it) and directions. Directions are represented by 3D vectors and holds directions where camera look at, where its top and right directions are. This representation is more useful then angles of rotation around X, Y, Z axes.

Camera movement and rotation in virtual 3D space could be different depending on the purposes of your application. Lets imagine you are creating a 3D shooter game whether user walks through scene, kills monsters, and explore your game world. In such a game user can move in any direction by turning left and right and turn ones "head" up and down. The user's head is represented by a camera. In the module we have implemented such type of camera in a special template class called TCameraWalk.

Imaging another type of 3D game: flight simulator. In this king of game the vehicle like plane or spacecraft is no longer limited to turning only left, right, up, and down. In this kind of game camera associated with vehicle could usually be turned around vehicle axes. Such kind of camera is implemented in TCameraFly template class.

Both "walk" and "fly" cameras are derived from TCamera and could be used as final camera objects. The TCamera itself can not be used alone as it is only a partial implementation of ICamera interface.

Model

Another important part of all 3D applications is an 3D object located in space. Such objects also could be placed into different locations in space and could be rotated and scaled. To describe such transformations you can use TModel template class. This class is ready to use as is, you do not have to create derived class from it and implement any methods. This model class is responsible for two things. The first one is to describe and manage transformations and the second one is to describe and manage hierarchy of the models. This template class doesn't manage 3D geometry(triangles and polygons) itself.

Mesh and 3D Object

The Graphics3D module also has classes for representing geometry of 3D objects. There are two different implementations for it: TMesh and T3DObject. Although both of them can be used to store 3D geometry they are quite different. The TMesh template class stores all geometry in independent way while the T3DObject has ability to share positions, normals, and texture coordinates between different vertices and polygons. Both data representation methods has advantages and disadvantages. We won't describe all of them here. We only could say if you are building a 3D editor program then you would better use T3DObject because its architectural design is better suited for such purposes as geometry editing. You also may preffer this geometry management class for more optimal memory usage. On the other hand if you use geometry statically then you will probably use TMesh instead. Anyway, the choise is yours.

Besides the classes representing 3D geometry and its transformations the module contains functions for generation geometry primitives. Some of them are shown on the screenshot below.

3D primitives

Why templates?

As you might have noticed the module is created as a set of templates. This was done because in different applications 3D geometry and it's transformations can be described by different accuracy floating point values. For example, in games using simple precision floating point values is more than enought and one could want to use float which is also allows to save memory 2 times in comparison to double floating point type. On the other hand in scintific 3D modeling applications using single precision floating point data type could be not enough and double type is required to be used(or even better one). The aim of the framework is not limited to any kind of application and that is why we need templates here.