Game scenario Rendering
The xNa shooter game scenario is just a simple 3D model with a texture of 1024x1024. However, Scene Rendering is not easy. In the next section, you will create scenes and tracks for the next racing game.
But I don't want to spend too much time on a simple shooting game because it only renders scenes on the background and does not require interaction. Therefore, there is no need to implement a scene rendering engine to render thousands of polygon. You only need to place the texture patch on the polygon and implement Alpha mixing for different texture types on the ground. I took a simple approach, that is, to use only one well-prepared 3D model for the entire scenario. This screen is enough, and it does not allow you to move around, because this game is not needed.
There are about 60 scenario models in a level. You just need to connect them to each other and always display the current and next scenarios. They are not perfectly connected because the light on the boundary is hard to process (textures, normal maps, and 3D models have no problem with normal), but it is good enough for the game.
In this section, I want to show you the necessary steps to create such a scenario model. I am not the only one who uses this simple technology (for example, canyon commander, an adapted version of a rocket Commander, uses similar technologies to display 3D Canyon ).
Basic texture and normal texture
Before you start to process the 3D data or elevation map of a scenario, you must know what to display on the background. Here I want to show the desert in the middle and the rocks on both sides to form a small Canyon. I made two textures and combined them to the backgroundrock. Dds texture (see Figure 11-8 ). To be honest, I didn't want to use sand textures at the beginning. I thought it would be enough to have a rock texture, but it looked too boring and without changes.
Figure 11-8
Normal textures are mixed in the same way, but for these two textures, I didn't have a normal texture at first. I used the NVIDIA Photoshop plug-in to generate a normal map from these two textures. You must adjust them repeatedly until they look good, and sometimes you have to redraw the texture to correct the error. The diffuse texture used here does not contain any height and normal information. All this plug-in can do is to convert the image into a grayscale image and generate a normal from a pseudo elevation map.
Sometimes it looks good, but sometimes this method also produces the wrong normal map. The best case is that the artist also provides a normal texture or at least an elevation map while creating these textures, but this is generally not a luxury. Sometimes there is only diffuse texture. When you take a photo with a camera, the normal and height information will not be recorded. We recommend that you keep it as simple as possible. You will create more complex scene textures and normal maps in the racing game in the next chapter. Figure 11-9 shows the normal textures mixed together. You will find that the normal map of the rock is stronger than that of the sand, because you want to make the rock look more uneven than that of the sand. But even the sand area has a little ups and downs (added to the relatively gentle sand texture) to achieve better illumination.
Figure 11-9
Elevation chart
With diffuse texture and normal texture, you can now display the scene on a simple polygon, but the effect is good enough. What you need is a real cliff on both sides and a canyon in the middle. I used 3D Studio MAX (you can also use any 3D creation tool, if you don't want to ask others to do it) to create this canyon, I created a simple plane object on the XY plane (Z axis facing up) with 64x64 intersections to Form 63x63x2 = 7938 polygon. 63 is used because each polygon requires a start point and an end point multiplied by 2 because each quadrilateral is composed of two triangles (see Figure 11-10 ).
Figure 11-10
To make each point have a Z value greater than the default value 0, you need to create a model in 3D.ProgramDrag them up and down, but I am not skilled, and have no patience to complete a scenario in this way. An easy way is to use an elevation chart and then display all vertices based on this elevation chart. Elevation maps are often used in geographical landscape maps, so it is not difficult to find some cool elevation maps on the Internet, or even elevation maps of other planets.
Fortunately, there is a simple modifier in 3D Studio Max called displace, which is exactly what you need (not easy to find, but once you know where it is, it becomes very useful ). Here you can add your new elevation chart (which I drew myself, which is not great but can work) to the 3D model (see Figure 11-11 ).
Figure 11-11
There is no response now, and you may ask why. Max has many settings, which are not always easy to find. You only need to explore the settings until they change. Here you need to adjust the top intensity settings, set it to 30 to 40, you can see the results of 11-12.
Figure 11-12
The last step is to assign the diffuse texture and normal texture to the shader material mapped to the normal. Then you can assign the material to the 3D model (see Figure 11-13) and export it. Now it can be used in the game.
Figure 11-13
The scene is now rendered from the top to the screen, and you can only see the boundaries between the interior of the valley and the cliff. Scene Rendering also supports a wide screen resolution of, which means that some parts may not always be visible. All the actions in the game are in the middle of the canyon. Everything is ready when you add 3D buildings and plants.
Take a look at unit tests in The misson class and learn how scene models are presented in the game. You can also take a look at the unit tests in the model class to show all the 3D models used in the game.
Add object
The scenario looks good (at least better than a simple xy plane), but it is a bit empty. Buildings and plants are added to make it look better. Check the last testrenderlandscapebackground unit test of The misson class. It only calls the renderlandscapebackground method. This method uses the current level position as a parameter and always displays only the current and next scenarios, so that you can still see the 3D model even when moving forward. Players are not aware of this because if you move forward enough distance, the current scene will be replaced by the next scene. New scenes will be generated at the top until the end.
Object generationCodeThe interesting thing is that a Model List is used to randomly add new objects. Plants are randomly placed and rotated, but the building only shows the left and right, that is, only Rotating 90 degrees.
// From the generatelandscapesegment method: List <matrixandnumber> ret = new list <matrixandnumber> (); int numofnewobjects = randomhelper. getrandomint (random); If (numofnewobjects <8) numofnewobjects = 8; For (INT num = 0; num <numofnewobjects; num ++) {int type = 1 + randomhelper. getrandomint (NumOfLandscapeModels-1); // create buildings only left and right IF (type <= 5) {Int rotsimple = randomhelper. getrandomint (4); float rot = rotsimple = 0? 0: rotsimple = 1? Mathhelper. piover2: rotsimple = 1? Mathhelper. pi: mathhelper. piover2 * 3; bool side = randomhelper. getrandomint (2) = 0; float ypos = segmentnumber * segmentlength + 0.94f * randomhelper. getrandomfloat (-segmentlength/2, segmentlength/2); vector3 Pos = new vector3 (side? -18: + 18, ypos,-16); // add very little height to each object to avoid same height // If buildings collide into each other. pos + = new vector3 (0, 0, 0.001f * num); ret. add (New matrixandnumber (matrix. createscale (landscapemodelsize [type]) * matrix. createrotationz (ROT) * matrix. createtranslation (POS), type);} // If else {ret. add (New matrixandnumber (matrix. createscale (landscapemodelsize [type]) * matrix. createrotationz (randomhelper. getrandomfloat (0, mathhelper. pI * 2) * matrix. createtranslation (New vector3 (randomhelper. getrandomfloat (-20, + 20), segmentnumber * segmentlength + randomhelper. getrandomfloat (-segmentlength/2, segmentlength/2),-15), type);} // else} //
Then, the RET list is returned to the calling function and saved to the scenario segment. The complete generatelandscapesegment Code also adds enemies, checks collision between objects, and prevents buildings or plants from getting too close.
If you perform the testrenderlandscapebackground unit test, you can see the scene and the objects on the ground, as shown in 11-14. Please note that this chapter does not discuss shadow ing. Please read the last part of this book to learn more about the shadow ing technology. If you are interested, take a look at the shadowmappingshader class.
Figure 11-14