Let’s we help the world.

Don’t wait until the end, we must save ourselves now.
By turning off the light for 1 hour, http://www.voteearth2009.org.
See that site for more detail on how you can be one of the sounds and spreading this all over!!!
Let’s we help the world.

See that site for more detail on how you can be one of the sounds and spreading this all over!!!
Tags: earth, earthvote, global warming, help the earth, turn off, turn off the lights, vothearth
Posted in News | No Comments »
Okay, this is the step by step series of Tank.
As I promise to explain all of these stuffs, now this is the first of them.
We will create the world for our tank, allowing it to move (this included the generation of mountain, procedural method of terrain-generating).
Note: Please make sure that you also concurrently look at the sourcecode while going along with these explanation.
These explanation will not for line-by-line, I will grab for the overall concept so you can build it for your own.
All the project file can be downloaded from the Tank original post.
I create Terrain class (Terrain.cs) to handle and have all the responsibility for the terrain stuff.
Let’s take a look at the Terrain.cs file.
At first you will see these section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public struct MultitexturedVertex { public Vector3 Position; public Vector3 Normal; public Vector2 TextureCoords; public Vector4 TextureWeights; public static int SizeInBytes = sizeof(float) * (3 + 3 + 2 + 4); public static readonly VertexElement[] VertexElements = { new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0), new VertexElement(0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0), new VertexElement(0, sizeof(float) * (3+3), VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0), new VertexElement(0, sizeof(float) * (3+3+2), VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1) }; }; |
Vertice-declaration
This section of code is for the declaration of the individual terrain-vertice.
As you can see we want each vertice to have the following characteristics,
- define position (as can be seen from public Vector3 Position;)
- have normal, this is used for the calculation of ligting and others benefit when apply with object’s collision. (as can be seen from public Vector3 Normal;)
- define the texture coordinate, this is becase we want to map our texture on to them (as can be seen from public Vector2 TextureCoords;)
- define the texture-weight, as you will see soon that I have used 4 textures to map on to the terrain in which each texture will be used according to the height of each vertice, thus the weight comes to play becase it will smooth the adjacency texture on the terrain, so it will looks like the same large piece.
(as can be seen from public Vector4 TextureWeights;)
If you read down furthur, the question may arise, why do we have to do all like this?
The answer is ‘yes’. In order to define our customized-vertex declaration, we will strict with this declaration and must define like these all the time. Now I will show you what’s the less is about.
SizeInBytes is the total sizes of individual vertice, as you can see in the code.
The position takes 3 * sizeof(float), becase it is Vector3 which each component of vector is in float-type.
Normal is the same as position.
Also for TextureCoords and TextureWeights, which you must change the number to be multiplies with sizeof(float) to match that vector-type.
If it is Vector2, then the multiplier will be 2.
And now we are going to the last part of these declaration, it is VertexElements.
VertexElements have the information that is used to tell the GPU later that each vertice have what information, how many bytes it occupied, what does it used for? These such questions are very important, so that your graphics card will know how to extract bytes from your chunk of data sent to it.
The parameter to create VertexElements is as follow, (cut out from MSVS 2008 Documentation)
public VertexElement ( short stream, short offset, VertexElementFormat elementFormat, VertexElementMethod elementMethod, VertexElementUsage elementUsage, byte usageIndex )
Parameters
stream
Stream number (or index) to use.
offset
Offset (if any) from the beginning of the stream to the beginning of the vertex data.
elementFormat
One of several predefined types that define the vertex data size.
elementMethod
The tessellator processing method. These methods determine how the tessellator interprets/operates on the vertex data.
elementUsage
The intended use of the vertex data.
usageIndex
Modifies the usage data to allow the user to specify multiple usage types.
That’s it for the declaration.
In other cases that you use your own vertex-declaration, make sure to define it correctly according to your apps. Plan each vertice to have what characteristics is the crucial part and very important process before you begin any furthur actions. This can save time for you later on.
(for these reason I put this topic first before we start the less ;) )
Local Variables
Graphics device and effect
Now I will show you the variables need for this Terrain class.
What we need in the first place is these.
1 2 | GraphicsDevice device; Effect effect; |
Above for both device, and effect will be sent from the caller who create Terrain class. Normally they will be sent from Game1.cs file. (Initially they exist there at the very first time if you didn’t modify anything.)
Heightmap
Next we need heightmap (the map in which it contain the height data of each position in the terrain, so in this case we can let the artist do it for us or even tell them that we want the mountain!!!) to be used for our
terrain, note that it defines the environment appearance as the black area is lower, the white area is higher, this is becase white-color is 255 but black-color value is 0, so we use this data and tell the artist to create the mountain or even you can imagine for terrain-generation.
Sample heightmap is
This let us to define the following line,
1 | Texture2D heightMap; |
Vertices
See below,
1 2 3 | MultitexturedVertex[] vertices; short[] indices; float[,] terrainHeightData; |
Above is must like the vertices’ architecture.
We must keep all terrain’s vertices. Also we keep the information of each vertice’s height.
Note that we use the indice to hold which vertice is in what position, such as defining them in clockwise or counter-clockwise (according to what rule you use, left or right hand rule => to define such vertice for each polygon), indice also plays the important role here as it will minimize our vertice declaration to only one vertice for one declaration, thus we can reuse it for generate another polygon for our terrain.
If you confuse about the left, right hand rule or using the indice for defining the vertice for such polygon, you should read this for the real in-action of the purpose of indice.
Terrain width and height (non and scaled value)
See below,
1 2 3 4 5 6 7 8 9 | //Actual non-scaled terrain width int terrainWidth; //Actual non-scaled terrain height int terrainHeight; float minHeight; float maxHeight; float scaledWidth = 1.0f; //default float scaledHeight = 1.0f; //default |
As you see above, I have 2 version of width and height, that’re non-scale width and height, and another version is scaled width and height.
The reason for this is, I want to scale my terrain in the x-direction (its width, not its height) as far as I want, so my terrain will look large enough and suit my apps.
You can set the scale-multiplier for both x, and y-direction to suit your need.
Warning: Not recommend to change the scaledHeight, you should leave it as 1.0f unless you know what’re you doing.
The min and max hieght are used for information-purpose only. They are both automatically assigned the value when we create the terrain. So you will know your terrain’s min and max height.
4 Textures
Now it’s about the textures being used in the procedural method of generating terrain.
1 2 3 4 | Texture2D sandTexture; Texture2D grassTexture; Texture2D rockTexture; Texture2D snowTexture; |
I will use 4 different textures for the terrain, its used will depend on each vertice’s height.
As you may suggest, the lowest of the terrain will use sandTexture, above this level will use grassTexture, and so on. So at the very top you will see the snowTexture. It makes sense right?
The textures are below (included in the project file, show here for consistent)
- sand texture
- grass texture
- rock texture
- snow texture
Buffers
We use buffer to hold the vertices-data and send it to GPU and draw it.
It ranges from VertexBuffer which hold the vertices data.
IndexBuffer, as you know about indice, this’s for.
VertexDeclaration, it define the vertice, as you already know from the very top of this post about the vertex-delcaration.
See the declaration below,
1 2 3 | VertexBuffer vertexBuffer; IndexBuffer indexBuffer; VertexDeclaration vertexDeclaration; |
The less
The less is for the world matrix for the terrain, if you want to adjust its position and rotation (which is less lightly to do because it uses much more of cpu time and waste cycle) but for my apps I personally like to adjust it just for learning :) , and combined with the light and effect setting for this terrain.
You will know all about the LightSettings and EffectSettings class in the next appropriate subtopic. For the generating of terrain, they are not that important to know at this time.
Next post, I will show you the real implementation, that’s methods inside the Terrain class. I insist not to go furthur because the post will go too long, I think I should split it a little. See you next post about all of these Terrain stuff.
Tags: generating of terrain, procedural method, procedural texture, procedural texturing, terrain, terrain generation
Posted in 3D Grahphics | No Comments »
Okay, all the hard time has passed now.
I am back. And as I promise I will explain what I did on Tank.
You can see it below a couple posts.
As I came back, I have seen a little amount of comments posted on my blog, it’s some kind of relief
thanks to you all.
For me now, it’s summer time, I have plenty much time to study and read more on graphics programming book.
I hope I can post more article on these stuff.
Come back soon, Tank is waiting for you. :)
Posted in Uncategorized | No Comments »
You are currently browsing the Haxpor blog archives for March, 2009.
Powered by WordPress
Hosted by XtreemHost
This work is licensed under a Creative Commons Attribution 3.0 United States License.
View
My Stats
Entries (RSS)
and Comments (RSS).

