Building 3D applications has always been a cumbersome task for programmers. Rendering of 3D objects is quite a time-consuming process and, if some code has to be debugged or rewritten, the programmers usually get exhausted in compiling the application again and again. Until now, game developers have mostly worked in Visual C++, which is quite difficult to use, and using a graphics API such as OpenGL or DirectX along with it, making it even more difficult. Managed programming languages like C#.NET have made programmers’ lives a bit easier and the advent of the new features in DirectX have given a boost to the way these 3D applications are coded. You cannot imagine how easy it is to create a 3D scene with DirectX 9.0 API and a 3D Engine.
DirectX is an advanced suite of multimedia application programming interfaces (APIs) built into Microsoft Windows’ operating systems. It provides a standard development platform for Windows-based PCs to access specialized hardware features with easy programming code. This technology was introduced in 1995 and is now widely used. The latest version of the DirectX family is DirectX 9.0 that has improved graphics, security, and performance in comparison to its previous versions. There are a number of 3D graphics engines available for .NET developers. These are usually built to enhance productivity in less time by creating a one-line method for tens of lines of code. A comprehensive list of these engines can be found at: http://www.devmaster.net/engines/.
In this tutorial, I am using 3D Engine (TrueVision TV3DSDK), downloadable from http://www.truevision3d.com/ and Managed DirectX 9.0 SDK, downloadable from http://www.microsoft.com/downloads/ to make this application. If you do not want to install the complete SDK, just install the DirectX runtime environment to run the code uploaded with this article.
I have defined the following steps in creating any 3D scene, based on simple objects and their movements and in this application; these will be carried out one by one:
- Imagine (What Is to be Built?)
- Design and Load 3D Models (Virtual Actors)
- Set Camera
- Apply Special Effects
- Define User Input Controls/li>
- Define Object Boundaries for Movement and Collision Detection
- Apply the Laws of Physics (if required)
- Render Everything!
1. Imagine (What Is to be Built?)
I have imagined making a 3D room having carpet, sofas, a glass table, dining table with six chairs, a hanging scenery, a lamp, chest of drawers, and a shelf table, as in a real home.
Figure 1: A 3D Room
Now, you start working on your application:
Make a new C# project as a Windows Application and name it DirectX Application1. Download the TrueVision SDK trial version from http://www.truevision3d.com/, install it, and then Import the TrueVision Engine and TrueVision Media libraries through Project–>Add Reference.
Figure 2: Adding the TrueVision Libraries in the Project
Place a PictureBox on the Form1 and name it pictureBox1 and a button to quit this application.
Figure 3: Placing a Picture Box and a Button on the Form
Include these libraries in your project:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using TrueVision3D; using DxVBLibA;
TV3DEngine is written using DirectX and provides powerful functions to accomplish amazing 3D abstraction. If you need to develop a powerful 3D Application/Game in a short time, TV3DEngine will help you with this. It wraps some of the more powerful features of DirectX into easily used functions and procedures. Accomplish the same results 10 times faster than with Core DirectX using your favorite language! It has some built-in classes that you have to load in the project; for example, TVEngine, TVInputEngine, TVLightEngine, TVTextureFactory, TVMaterialFactory, TVScene, TVLandscape, TVActor, TVCamera, and so forth. Some of the necessary classes used in this application will be discussed along with the code.
Declare and initialize TrueVision Variables in the Form1 class:
//Declaring and initializing the objects for the rendering Engine, //Input Engine, Light Engine, Texture Factory, Material Factory, //Scene, and Global classes of TrueVision (TV) library TVEngine TV = new TVEngine(); TVInputEngine Inp = new TVInputEngine(); TVLightEngine TVLightEngine = new TVLightEngine(); TVTextureFactoryClass TexFactory = new TVTextureFactoryClass(); TVMaterialFactory MatFactory = new TVMaterialFactory(); TVScene TVScene = new TVScene(); TVGlobals global = new TVGlobals();
TVEngine is the main TrueVision class used to create a TV object that is just similar to an Application Window on which the whole rendering is to be done. This object is used to set the application properties, such as Window Mode, Search Directory Path, Window Title, and so on. TVScene is the portion on the window or whole of the window where you have to render everything. TVInputEngine is for handling all the keystroke and mouse inputs. TVGlobals handles the global engine built-in variables. TVLightEngine handles the lights in the scene and the other two handle all texture bindings and material mapping on the mesh objects respectively.
In the Form1_Load event, add the following code:
private void Form1_Load(object sender, System.EventArgs e) { //Before trying to play with TrueVision, we have to create the TV8 object. TV = new TVEngine(); //Now create a windows handle for the pictureBox1 so that all the rendering //will be done on the picture box TV.Init3DWindowedMode(this.pictureBox1.Handle.ToInt32(), true); //This is the path where our media (texture and meshes) files are placed TV.SetSearchDirectory(System.Windows.Forms.Application.ExecutablePath); //This is to set the rotation angle to degrees (the other option is radians) TV.SetAngleSystem(CONST_TV_ANGLE.TV_ANGLE_DEGREE); }