1 / 28

DirectX 3D Start

DirectX 3D Start. Chapter 1. Install Microsoft DirectX 9c. DirectX 9c can be downloaded from Microsoft web site for free. If using DirectX 9 for VC6 or VS NET, you must install them before installing DirectX 9. Click setup you can get window:.

yates
Télécharger la présentation

DirectX 3D Start

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DirectX 3D Start Chapter 1

  2. Install Microsoft DirectX 9c DirectX 9c can be downloaded from Microsoft web site for free. If using DirectX 9 for VC6 or VS NET, you must install them before installing DirectX 9. Click setup you can get window: We just accept license agreement and click next. Then the installation will be finished quickly.

  3. Check Dot NET configuration After installation, start a C# application program, right click Add Reference in solution explore, we should see the Microsoft DirectX references in .NET window as the following:

  4. Handily Configuration for VC++6 Microsoft DirectX9c can do configuration for Dot NET but not do it for VC++ 6. We must handily do that configurations such that VC++ 6 programs can get all the included files. Step 1: Go to Tolls ->Option

  5. Step 2: Go to Build Directories, choose Include files and click browse button

  6. Step 3: Go to C:\Program File ->Microsoft DirectX SDK (April 2006) ->Include and click OK button.

  7. Step 4: Click OK button. It’s done. Another alternative is to install Microsoft DirectX9 2004 version, which can automatically do configurations for VC++ 6 but not for DOT Net.

  8. Add DirectX references in C# Any C# program must add DirectX references like And usingMicrosoft.DirectX; using Microsoft.DirectX.Direct3D ;

  9. Deviceobject The most important DirectX object is Device, which is the start object of DirectX program. The whole D3D program only need one. Device object has a close connection with the video card. During the creation of Device object we also create a surface buffer in video card, which will used for drawing. Device object To create Device object, we must specify that how we will draw the images? For examples: Do we need full screen? Do we need set the Z buffer? Do we need Texture? Some drawing properties must be setup during the creation of Device object. Some drawing properties can be setup after the creation of Device object

  10. Device Capability First we have to know the capability of the video card device before any directX game programming. The minimum requirement for video card is Pixel Shader version2 and Vertex Shader version2. If they are version 1, some early 3D game are still can be running. If it is version 0, the video card is not GPU at all, which cannot run most 3D games. In the following, we use a console program to check the video card capability.

  11. Caps Structure Caps Structure represents the capabilities of the video hardware exposed through the Direct3D object. Namespace: Microsoft.WindowsMobile.DirectX.Direct3D Use property DeviceCaps to get device capabilities Manager class Manager class provides information about the environment, and enumerates and retrieves device capabilities. Namespace: Microsoft.WindowsMobile.DirectX.Direct3D public static Caps GetDeviceCaps( int adapter, // use 0 DeviceType deviceType // DeviceType.Hardware );

  12. static void Main(string[] args) { Caps DevCaps = Manager.GetDeviceCaps( 0, DeviceType.Hardware); Console.WriteLine("Vertex shader version = Version " + DevCaps.VertexShaderVersion); Console.WriteLine("Pixel shader version = Version " + DevCaps.PixelShaderVersion); DeviceCaps HardWare= DevCaps.DeviceCaps ; string str = (HardWare.SupportsHardwareTransformAndLight)?"YES":"No"; Console.WriteLine("SupportsHardwareTransformAndLight?" + str); str = (HardWare.SupportsPureDevice)?"YES":"No"; Console.WriteLine("SupportsPureDevice? " + str); str = (HardWare.SupportsStreamOffset)?"YES":"No"; Console.WriteLine("SupportsStreamOffset? " + str); str = (HardWare.VertexElementScanSharesStreamOffset)?"YES":"No"; Console.WriteLine("VertexElementScanSharesStreamOffset? " + str); Console.WriteLine("Max Stream number = " + DevCaps.MaxStreams); Console.Read(); }

  13. Test Output

  14. PresentParameters object PresentParameters object is used to setup drawing properties for Device object. PresentParameters object has 16 attributes properties.

  15. PresentParameters Properties One important property is Windowed. If it is false, then the program showing is full screen and we need to setup the resolution, which are BackBufferHeightandBackBufferWidth and setup the color depth for the screen, which is BackBufferFormat. However, many properties has default values, for our initial D3D program we only need to set up the following: PresentParameters pp = new PresentParameters(); pp.Windowed = true; // not full screen pp.SwapEffect = SwapEffect.Discard; // means throw away previous frame

  16. DeviceConstructor PresentParameters pp = new PresentParameters(); pp.Windowed = true; // not full screen pp.SwapEffect = SwapEffect.Discard; // discard the last frame data Device device = new Device( 0, // always 0 if only one video card DeviceType.Hardware, // very slow if Software this, // associate to this Frame window CreateFlags.SoftwareVertexProcessing, // no big difference if use HardwareVertexProcessing pp // present parameters );

  17. Class CustomVertex class CustomVertex defines many different type of vertices

  18. Some vertices 1) CustomVertex.PositionOnly defines position x, y, x only 2) CustomVertex.PositionColored defines position x, y, x and color Here Color is 32 bits integer, so we need to call ToArgb() to get integer value from any Color object.

  19. 3) CustomVertex.TransformedColored 4) CustomVertex. PositionColoredTextured

  20. Class VertexBuffer Object VertexBuffer defines memory data in video card to hold information for CustomVertex. public VertexBuffer ( Type typeVertexType, int numVerts, Device device, Usage usage, // use 0 VertexFormats vertexFormat, Pool pool //use Pool.Default )

  21. Create empty VertexBuffer 3 vertices of CustomVertex.TransformedColored. Now we create a VertexBuffer to hold them VertexBuffer VB = new VertexBuffer( typeof(CustomVertex.TransformedColored), 3, device, // Device object 0, CustomVertex.TransformedColored.Format, Pool.Default );

  22. Write data to VertexBuffer Must call Lock() and UnLock() GraphicsStream stream = VB.Lock(0, 0, 0); CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3]; verts[0].X=150; verts[0].Y=50; verts[0].Z=0.5f; verts[0].Rhw=1; verts[0].Color = Color.Aqua.ToArgb(); verts[1].X=250; verts[1].Y=250; verts[1].Z=0.5f; verts[1].Rhw=1; verts[1].Color = Color.Brown.ToArgb(); verts[2].X=50; verts[2].Y=250; verts[2].Z=0.5f; verts[2].Rhw=1; verts[2].Color = Color.LightPink.ToArgb(); stream.Write(verts); // copy data VB.Unlock();

  23. Resize the window When the window was resized, the vertex buffer will be recreated by the program automatically. However, the vertex buffer data won’t be automatically updated. That will cause the program crashed in C# (not in VC++). To avoid that, we can set form maximizebox =false. Another method is to add an event as the following: private void InitVB() { VB = new VertexBuffer(typeof(CustomVertex.TransformedColored), 3, m_device, 0, CustomVertex.TransformedColored.Format, Pool.Default); VB.Created += new System.EventHandler(this.WriteVertexData); // means automatically call function WriteVertexData( …..) WriteVertexData(null, null); }

  24. private void WriteVertexData(object sender, EventArgs e) { GraphicsStream stream = VB.Lock(0, 0, 0); CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3]; . . . . . . . . . .. . . stream.Write(verts); VB.Unlock(); } The define event handling function as: Now, when window is resized, the vertex buffer will be automatically updated too.

  25. Draw to Screen device.Clear(ClearFlags.Target, Color.Black , 1.0f, 0); // clear back surface to black color device.BeginScene(); // begin drawing device.SetStreamSource( 0, VB, 0); // set resource device.VertexFormat = CustomVertex.TransformedColored.Format; // get vertex information device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); // drawing device.EndScene(); // end drawing device.Present(); // flip back surface

  26. Application.DoEvents() To run a game program needs thread, which is in an infinity loop unless stopped by user. However, we must include the application events handling as a part of this loop, other wise the use cannot do any event input. For this purpose, we can call Application.DoEvents(). Form1 frm = new Form1(); frm.Show(); while(frm.GameActive) { frm.Render(); Application.DoEvents(); // window events }

  27. Output Note: This is actually not 3D drawing. Such 2D drawing always need CustomVertex.Transformed vertex

  28. Use Another Thread The other option is to use Another thread for the gamming. However, we must terminate this game thread when we close the window. First we must addusing System.Threading; Then we create a thread assign to StratGame() in Form_Load() And call thread.start();

More Related