dcsimg
December 8, 2016
Hot Topics:

Creating an Interactive 3D world with C# and DirectX

  • February 3, 2006
  • By Fatima Ahmed
  • Send Email »
  • More Articles »

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.

//Declaring our 3D mesh objects here
TVMesh Obj1, Obj2, Obj3, Obj4, Obj5, Obj6, Obj7, Obj8,
       Obj9, Obj10;
TrueVision3D.TVMeshClass Room;

In the Form1_Load event, you add:

//Initialize all 3D objects as TVMesh class objects
Obj1 = new TVMesh();
//Create a 3D Room mesh
Room = (TVMeshClass)TVScene.CreateMeshBuilder("roommesh");

The engine can load these types of textures:

  • Standard window formats: BMP, JPG, PNG, DIB, TGA
  • DirectX texture format: DDS

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:

//Load all the textures
TVScene.LoadTexture("Media\Textures\9.bmp",-1,-1,"RoomTexture");
TexFactory.LoadTexture("Media\Textures\bottom.bmp", "Carpet", -1, -1,
                       CONST_TV_COLORKEY.TV_COLORKEY_NO, true, true);
TexFactory.LoadTexture("Media\Textures\floor1.bmp", "Wall", -1, -1,
                        CONST_TV_COLORKEY.TV_COLORKEY_NO, true, true);

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.

Here, I am using the AddWall3D function to create solid walls.

//Load the room coordinates and assigns a texture to it
Room.AddWall3D(global.GetTex("RoomTexture"), 350.0f, -350.0f, -350.0f,
               -350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 1.0f);
Room.AddWall3D(global.GetTex("RoomTexture"), -350.0f, -350.0f, -350.0f,
               350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 1.0f);
Room.AddWall3D(global.GetTex("RoomTexture"), -350.0f, 350.0f, 350.0f,
               350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 1.0f);
Room.AddWall3D(global.GetTex("RoomTexture"), 350.0f, 350.0f, 350.0f,
               -350.0f, 350.0f, 5.0f, true, false, -50.0f, 5.0f, 1.0f);

Now, add the floor to the Room mesh:

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

The above function adds a floor face to the current mesh. Floor face is a rectangular horizontal plane on the Y plane.

// Like the "add wall" method, we will add a floor to this room.
Room.AddFloor(global.GetTex("RoomTexture"), -350.0f, -350-0f, 350.0f, 350.0f,
              -50.0f, 10.0f, 10.0f, true, false);
Room.AddFloor(global.GetTex("RoomTexture"), -350.0f, -350-0f, 350.0f, 350.0f,
              300.0f, 10.0f, 10.0f, true, false);

Then, you load the Room mesh at the proper location in the Scene specifying the values of x, y, and z as follows:

//Set the position  of the room mesh
Room.SetPosition(x_move, y_move, z_move);

The 3D room with the "RoomTexture" applied to it looks like this:

Figure 5: Creating a Room mesh and loading it in the scene





Page 2 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

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

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel