Creating an Interactive 3D world with C# and DirectX

2. Design and Load 3D Models (Virtual Actors)

3D models are the mesh objects that are to be placed in the 3D scene. A mesh is a series of polygons grouped to form a surface. They can include hall, terrain, roads, factory, machines, cars, people, and the like. Design the 3D mesh objects (animated or unanimated) in any 3D graphics software available (for example, 3D Studio Max, Maya, Light Wave, and so on) and covert them into a format that can be imported in .NET (as in the .x file format for DirectX). Other file formats are also supported by the engine. I have used 3D Studio Max to design the mesh objects and converted them to X files using Panda Exporter. You can find many other 3D graphics software available both for purchase and for free to download on the Web. The other easy way is to download pre-built 3D models uploaded at many different Web sites like www.amazing3d.com and www.3dcafe.com.

Figure 4: Designing 3D models in 3D Studio Max

You have created 10 objects here. They are to be placed at different positions in the room, as shown in the first picture above.

PNG, TGA, and DDS can have an alpha channel that controls the transparency of the texture.

The function for loading texture in this engine is "LoadTexture" that can be called by the TVScene object or the TextureFactory object. It returns the Texture index in the texture factory. The list of the parameters is as follows:

TVScene.LoadTexture(Filename As String, Width As Long,
Height As Long, TextureName As String)
TVTextureFactory. LoadTexture(Filename As String, Name As String,
Width As Long, Height As Long,
Colorkey As CONST_TV_COLORKEY,
FilterTexture As Boolean,
multialpha As Boolean)

I hope all the arguments of the "LoadTexture" are clear enough to understand except for two. The colorkey of the texture will not be rendered, resulting in transparency. The multialpha is used to mask; if multialpha is true, you have only two possible alpha values (0 and 1). If it's set to false, the alpha value has 64 possible values, or 256 if it's 32bits. Now, it's time to load all the textures in the code:

Now, move on to building a room having four walls and two floors (including a ceiling). The TrueVision engine contains three functions for creating the wall and adding it to a single mesh "Room" that you created. These are:

AddWall

Creates a wall (quad) that is perpendicular to the y plane along a line.

AddWall2

Creates a wall (quad) that is perpendicular to the y plane along a line with different y altitudes.

AddWall3D

Adds a 3D wall (solid wall made of 4 or 6 faces)

TVMesh.AddWall(Texture As Long, x1 As Single, Z1 As Single, x2 As Single,
Z2 As Single, Height As Single, Altitude As Single, tilew As Single,
tileh As Single, NewFace As Boolean, OtherFaceSet As Boolean)

This simple method allows you to create textured walls easily. The X and Z coordinates are the same that for a line on a plane.

TVMesh.AddWall2(Texture As Long, x1 As Single, Z1 As Single, x2 As Single,
Z2 As Single, height1 As Single, height2 As Single,
Altitude1 As Single, Altitude2 As Single, tilew As Single,
tileh As Single, NewFace As Boolean, OtherFaceSet As Boolean)

This simple method allows you to create textured walls easily. The X and Z coordinates are the same that for a line on a plane. This method allows the wall to start at a specified altitude and to stop at another, so it's a little more powerful than TVMesh.AddWall2.

TVMesh.AddWall3D(Texture As Long, x1 As Single, Z1 As Single, x2 As Single,
Z2 As Single, Height As Single, Size As Single,
DisableTop As Boolean, DisableBottom As Boolean,
Altitude As Single, tilew As Single, tileh As Single)

Adds a 3D wall (solid wall made of four or 6 faces) to the current mesh.

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.