|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is the third in a series of tutorials which will allow you to create your own game engine (from initialization, to a fully rotatable, height mapped 3D world!). In this tutorial, we will use Direct3D to create and render a primitive, as well as learn some more DirectX concepts. BackgroundThe code from these tutorials is taken from my own game engine: MAGEngine.NET, at different stages in development. Using the codeYou may use all of the code I provide as how you see fit, except to create another tutorial. You can use it as a sturdy(ish ;)) framework for your own applications, or print loads of copies off so that when I'm rich and famous you can sell them for $100 each ;) PrerequisitesTo do this series of tutorials, you will need:
OK, in the last tutorial, we spent a long time discussing various aspects of the Direct3D component, we created a good, solid sample framework, but we never got to render anything – which is the whole concept of Direct3D. In this tutorial, we will:
TheoryAll 3D worlds - whether it is a character, terrain, or a box - are made out of triangles. And all these triangles are essentially three points, or vertices. So, to render our triangle, we will need to define three of these "points" and send them to be rendered in our From this you, can choose a "point" structure to suit your needs - if you want a textured box which you could rotate, then you would choose Transformed indicates that the coordinates we define have been declared as screen coordinates, and do not need to be scaled, rotated, or otherwise. Now, your initial reaction may be to jump straight into the code and render your triangle - but first, we should create a class to help us with this. We will be making two classes, one called In Direct3D, the best way to render is to use the The above classes will be declared in a new file called public class cObject
{
public CustomVertex.TransformedColored[] itsVertices;
}
public class cTriangle : cObject
{
bool isActive;
//Should this primitive be rendered?
public cTriangle(CustomVertex.TransformedColored[] itsVerts)
{
itsVertices = new CustomVertex.TransformedColored[3];
isActive = true; itsVertices[0] = itsVerts[0];
//Assign the parameter vertices to that of the object
itsVertices[1] = itsVerts[1];
itsVertices[2] = itsVerts[2];
}
public void Render(Device device)
{
device.DrawUserPrimitives(PrimitiveType.TriangleList,
1, itsVertices;);
// Generic code for the primitive to 'render itself'
}
As you can see, the The
The other parameters are fairly simple - how many primitives to render (=1) and then the source (or where the primitives to be rendered can be found). So, now that we have this class updated, we only have two more things to do to get our triangle rendered:
So, with this class created, it should be easy to create an instance of your class - make a public We need to create a CustomVertex.TransformedColored[] Verts =
new CustomVertex.TransformedColored[3];
Verts[0] = new CustomVertex.TransformedColored(new
Vector4(100.0f, 100.0f, 0.0f, 1.0f), Color.Red.ToArgb());
Verts[1] = new CustomVertex.TransformedColored(new
Vector4(200.0f, 100.0f, 0.0f, 1.0f), Color.Blue.ToArgb());
Verts[2] = new CustomVertex.TransformedColored(new
Vector4(150.0f, 200.0f, 0.0f, 1.0f), Color.Green.ToArgb());
OK, the only thing slightly strange here should be the Now, make a call to your triangle's constructor and pass in the vertices defined above as the parameter. Then, go to the From
The reason for locking and unlocking the GPU during rendering is to reduce errors at run time. When we render, we render to what is called a back buffer. This is an off-screen surface where any data can be stored until we call the To code, this translates as: device.Clear(ClearFlags.Target,
System.Drawing.Color.Black, 1.0f, 0);
//Clear display
device.VertexFormat =
CustomVertex.TransformedColored.Format;
//Notify GPU which vertices
device.BeginScene(); //Lock GPU
Triangle.Render(device); //Render primitives
device.EndScene(); //Unlock GPU
device.Present(); //Swap old with new
this.Invalidate(); //call invalidate
This should make up the entire body of the If you compile and run this code, you should see a blended-colour triangle on your screen. X-Challenge
ContactPlease send all emails to xpyder@magclan.cwhnetworks.com. I do have MSN Messenger, my email address for this is jamespraveen@aol.com. History
|
|||||||||||||||||||||||||||||||||||