3 D Tutorial
3 D Tutorial
• Intro
• Manage Directx 9 THIS IS TO HELP YOU UNDERSTAND
• Why use C# MICROSOFT DIRECTX C# TUTORIAL. I TRIED
• What you will need TO EXPLAIN AS BEST AS I COULD. AND
COMPILDE THE CODE TO TEST IT. UNLIKE
• Getting Started MICROSOFT. I KNOW THERE ARE A LOT OF
• Device GRAMMAR ERROR THAT PROOF THE AM
HUMAN AND NOT A ROBOT. ANYHOW HOPE
• Vertices THIS HELP YOU
• Matrices AND VISIT THE LINKS @ THE BUTTON
• Lights
• Texture
• Meshes
• Good links
SDK
You need the DirectX 9 SDK . know if you don’t get this right
you should not be coding at all. Get DirectX 9.0 SDK for
Managed Code Please get this right cause noob always get this
wrong.
1. Note: Add the refference if you have too, please don’t noob out
2. Note: Yes code will run on Visual C#. thats what am using
3. Note: Yes new virsion of Sharp Develop has DirectX 9 Keyword support
Now compile the project by pressing F8 and Run it With F5. This is just a test to
see if the compiler and the DirectX are both happy. If it did not work and you don’t know
what the problem is ask at csharp-home.com or better yet go to Sharp Develop Forum
and search, then post. Now delete everything in the file not the header declarations at the
top and let’s get this party started. Your file should look like this.
1. using System;
2. using System.Collections;
3. using System.ComponentModel;
4. using System.Drawing;
5. using System.Windows.Forms;
6. using Microsoft.DirectX;
7. using Microsoft.DirectX.Direct3D;
Ok time to create our window and set up our main loop. its nothing like c++, less
work and very important, Less Typing.
1. namespace MYWIN
2. {
3. public class MYFORM : Form
4. {
5. public MYFORM ()
6. {
7. this.ClientSize = new System.Drawing.Size(200,200);
8. this.Text = "CSharp-Home Direct3D Tutorial";
9. }
10. static void Main()
11. {
12. using (MYFORM MYWIN = new MYFORM())
13. {
14. MYWIN.Show();
15. while(MYWIN.Created)
16. {
17. Application.DoEvents();
18. }
19. }
20. }
21. }
22. }
Line 3 public class MYFORM : Form is our class that inherit from the Form class
Line 5 public MYFORM () is the constructor
Line 12 using (MYFORM MYWIN = new MYFORM()) just create an object that is easy to find bugs in
Line 15 while (MYWIN.Created) is our main loop. In English it read. While the window is created, do events.
If everything works out fine, don’t get to happy, this is just the window with the title you
could do this with the design editor. In the next section you will learn how to create a
Device. But for now your program should look like this
Device:- Am In Control
Continuing with the above code, We are going to Add a function a to initialize a DirectX
9 Device to our Class. Right under the constructor create a public function call
InitializeDirectX(); Then go create a device object so that all function could see it. I
mean don’t put it in a method;
public Device device=null;
device is out Device object, the Device function looks like this
public Device(
int adapter,
DeviceType deviceType,
Control renderWindow,
CreateFlags behaviorFlags,
PresentParameters[] presentationParameters
);
All but the last parameters are built in. The PresentParameters struct have to be filed in
by you.
1. public bool InitializeDirectX()
2. {
3. try
{
4. PresentParameters presentParams = new PresentParameters();
5. presentParams.Windowed=true;
6. presentParams.SwapEffect = SwapEffect.Discard;
7. device = new Device(0, DeviceType.Hardware,this,
CreateFlags.SoftwareVertexProcessing,
presentParams);
return true;
}
8. catch (DirectXException) //sweet for error debuging
{
9. return false;
}
10. }
The next function is the Render function. This is not needed but it keeps everything
clean. It goes in the main loop and or in the OnPaint function.
Now Go back to the main function and add both functions to the execution. In the correct
place
1. using (MYFORM MYWIN = new MYFORM())
2. {
3. MYWIN.Show();
4. MYWIN.InitializeDirectX();
5. while(MYWIN.Created)
6. {
7. MYWIN.Render();
8. Application.DoEvents();
9. }
10. }
Before we continue lets add some use less function to our program. Well not useless but we not going
to use it a lot. I like to put it under the main function
What is VertexBuffer? A Buffer of Vertex. What is a Vertex? Why did you cut math
class ?. Look in the msdn for all noob answers. www.msdn.com. Ok VertexBuffer object
is used to store and render vertices.
Now we are going to add some more function to out program.
Next is our OnCreateVertexBuffer function. The code will tell our VertexBuffer
what to render and Color to render it in.
1. public void OnCreateVertexBuffer(object sender, EventArgs e)
2. {
3. VertexBuffer vb = (VertexBuffer)sender; // c#
4. GraphicsStream stm = vb.Lock(0, 0, 0);
5. CustomVertex.TransformedColored[] verts = new
CustomVertex.TransformedColored[3];
6. verts[0].X=150;verts[0].Y=50;verts[0].Z=0.5f; verts[0].Rhw=1;
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
7. verts[1].X=250;verts[1].Y=250;verts[1].Z=0.5f; verts[1].Rhw=1;
verts[1].Color = System.Drawing.Color.Brown.ToArgb();
8. verts[2].X=50;verts[2].Y=250;verts[2].Z=0.5f; verts[2].Rhw=1;
verts[2].Color = System.Drawing.Color.LightPink.ToArgb();
9. stm.Write(verts);
10. vb.Unlock();
11. }
Explain lines
• 9 look up parameters
• 10 set the vertex format to be the same as the vertex
• 11 draws. change parameter to get a good idea
Now compile and run the code and if the debugger don’t pop up BINGO
Your program should look like this
Thats not the hard work, we have to add 2 function and update our render function. I
want to stop typing and go chat in yahoo Programming:1 room. I will stop after Matrix
for (;;) a while. Let’s add our functions. The first function is our public void
OnResetDevice(object sender, EventArgs e); Guess what this function does ? Something
when the device is reset right?
Even thought the comments tell what it does. You should try different method to get a
good idea. Put lighting true and explore the Cull struct. Our next function is our private
void SetupMatrices(); I like to look at this as our cam you will see why.
Ok if this don’t work it might not be you bug it might be mine. So look in the sdk and
see what wrong or ask in csharp-home.com forum. However it did work I tried it;
Your Program should look like this, with a spinning triangle. I install style Xp so the title
bar looks different
Depth stencils enable applications to mask sections of the rendered image so that they are
not displayed.
We start by adding to our PresentParameters struct
Now its time to change our format in public void OnCreateDevice(object sender,
EventArgs e). so it should now look like this.
Ok so this always freak you out. Its just math ok. So if you don’t get it don’t cry. Math is
done in an xfile and imported. (Later)
I underline the most importing thing that you should know. Remember you don’t have to memorize
all theses stuff. That’s what the msdn is for; just make sure you get the concept down.
Lighting time. Now its time to play god and add light its very easy
1. private void SetupLights()
2. {
3. System.Drawing.Color col = System.Drawing.Color.White;
8. device.Lights[0].Type = LightType.Directional;
9. device.Lights[0].Diffuse = System.Drawing.Color.DarkTurquoise;
10. device.Lights[0].Direction = new
Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1.0f,
(float)Math.Sin(Environment.TickCount / 250.0f));
12. device.RenderState.Ambient =
System.Drawing.Color.FromArgb(0x202020);
13. }
The best way to under stand the above code to play with the struct . If you get an error for
this Direct3D.Material you are missing using Direct3D=Microsoft.DirectX.Direct3D;
Updating our render function
Our public void OnResetDevice(object sender, EventArgs e) function will stay the same.
We will just load our texture.
device.RenderState.Lighting = false;
This Explain the code under I did not want to put code on a page break
Lines
• 3 ExtendedMaterial object that is used to capture the mesh file data to a Material
structure.
• 12 Load our mesh into our system memory
• 8 and turns on the z-buffer
• 10 and turns on white ambient lighting.
• 22 meshMaterials are initialized to the dimension (Length) of the materials
structure from the file.
• 24 meshTextures are initialized to the dimension (Length) of the materials
structure from the file
1. public void OnResetDevice(object sender, EventArgs e)
2. {
3. ExtendedMaterial[] materials = null;
The material subset is drawn with the DrawSubset method, which inherits from the
BaseMesh class.
If you load a giant mesh and cant see the full mesh in the window
Good links:-
http://www.xbdev.net/xbdev.php
http://csharp-home.com/
http://www.c-sharpcorner.com/Directx.asp
http://www.microsoft.com/downloads/details.aspx?FamilyID=592393a7-
c677-4023-8b27-94e61141e9c6&displaylang=en
http://opencpp.tk
http://users.pandora.be/riemer/keyboard.html
http://www.icsharpcode.net/OpenSource/SD/
if you find any code bug or error (none grammar) email me @
[email protected]