directx 9 с Управляемым Кодом. Программирование Игр и...

386
Том Миллер Managed DirectX*9 Программирование графики и игр ** омп э*> Предисловие Боба Гейнса Менеджера проекта DirectX SDK корпорации Microsoft SAMS [Pi] KICK START

Upload: andrey-tkachuk

Post on 20-Dec-2015

330 views

Category:

Documents


12 download

DESCRIPTION

book

TRANSCRIPT

  • Managed DirectX*9

    * *

    * >

    DirectX SDK Microsoft

    SAMS [Pi] K I C K S T A R T

  • Managed DirectX 9 DirectX9

    .

    DirectX .

    3D-, , ,

    mesh-,

    .

    ,

    .

    "Managed DirectX 9"

    ,

    .

    ,

    # ( Visual Basic .NET) .NET Runtime.

    DirectX

    .

    API Managed DirectX. DirectX API, SDK DirectX Visual Basic. Visual Basic Office Microsoft.

    KICK START

    C D : DirectX 9 SDK #

    Visual Basic.NET .NET Runtime 1.1.

    : . : DirectX 9. : .

  • DirectX 9

    KICK START

    , 2005

  • 14

    15

    16

    ! 17

    18

    I

    1. Direct3D 24

    2. 48

    3. 57

    4. 76

    5. sh- 91

    6. DirectX 106

    II

    7. Mesh- 144

    8. 162

  • 6

    9. Mesh 177

    10. 193

    III

    11. , 216

    12. HLSL '237

    13. 257

    IV

    14. 276

    15. 291

    V 2D

    16. Direct3D 2D- 308

    17. DirectDraw 2D- 320

    VI

    18. DirectPlay 332

    19. Client/Server 350

  • 7

    20. 366

    21. 379

    VII

    . 386

    . 389

    393

  • 14

    15

    16

    16

    ! 17

    18 DirectX 18 20 21

    I. .... 23

    1. Direct3D 24 24 Direct3D 25 32

    40 41 44 - 44 46

    2. 48 48 53 54 56

    3. 57 57 68 74

  • 9

    4. 76 76 82 Z- 87 90

    5. sh- 91 Mesh- 91 94 Mesh- 98 105

    6. DirectX 106

    106 107 .... 118 124 135 142

    .

    143

    7. Mesh- 144

    Mesh- 144 Mesh- 146 Mesh- 149 Mesh- 156 158 161

    8. 162 162 163 Locking, 164 167 169 171 176

  • 10

    9. Mesh 177 Mesh- 177 ,

    Meshes- 181 -. 185 190 192

    10. 193 193 196 201 Environment Maps 205 214

    III. 215

    11. , 216

    216 ,

    TECHNIQUE 225

    mesh- 228 HLSL 230 236

    12. HLSL 237 ... 237 239 244 247 256

    13. 257 257 262 267 274

  • 11

    IV. 275

    14. 276 SOUND 276 276 D- 281 285 287 290

    15. 291 291 294 299 301 303 306

    V. 2D 307

    16. Direct3D 2D- 308 308 310 315 319

    17. DirectDraw 2D- 320

    DirectDraw 320 327 330

    VI. 331

    18. DirectPlay 332

    DirectPlay 332 2- 334 337 341 344

  • 12

    347 349

    19. Client/Server 350 350 - 353 357 359 363 365 365

    20. .-366

    366 368 , Lobby 371 - 374 374 378

    21. 379

    379 380 383 384

    VII. 385

    . 386 386 388

    . 389 389 390 390

    393

  • DirectX API ,

    , .

    DirectX Windows 95 Games SDK ( SDK ). API, , ,

    C++, ,

    DirectX.

    DirectX DirectX 20 2002

    ( DirectX 9), . ,

    DirectX 8.1 DirectX Visual Basic, - Visual Studio.NET, .

    DirectX .NET, DirectX.

    , DirectX , API. DirectX Visual Basic , DirectX API. Visual Basic, DLL.

    DirectX 8 , API, Direct3D. proxi DLL , , Direct3D API (OLE library). , ,

    API, Visual Basic, .

  • 19

    DirectX 8 , API . ,

    Visual Basic. , Visual Basic, API

    , C++ API, . -

    , -

    .

    API , ,

    C++ , , #. ,

    DirectX. -

    2002 . - DirectX DirectX 8.1., # .

    DirectX, DirectMusic, DirectX. DirectX, -, DirectDraw DirectShow, , ,

    .

    , ,

    , .

    DirectX, , , ,

    .

    ,

    API,

    C++.

    - DirectX 9. , DirectDraw DirectMusic. , #.

    . DirectX -

  • 20

    API COM API. .NET Runtime , DirectX . : . , .

    API -.

    .NET runtime, -.

    .

    , .

    ,

    , DirectX ( ). , ,

    . -

    .

    , .

    , API .

    DirectX 9 - .

    SDK, DirectX API. ,

    - .

    . , , .

    Web- , , API.

    DirectX 9, , .

    DirectX API , namespaces, DirectX. Microsoft.DirectX, . , DirectX, 1.

  • 21

    1. DirectX

    Microsoft.DirectX

    Microsoft.DirectX.Direct3D

    Microsoft.DirectX.DirectDraw

    Microsoft.DirectX.DirectPlay

    Microsoft.DirectX.DirectSound

    Microsoft.DirectX.Directlnput

    Microsoft.DirectX. AudioVideoPlayback

    Microsoft.DirectX.Diagnostics

    Microsoft.DirectX.Security

    Microsoft.DirectX.Security.Permissions

    ,

    Direct3D API , D3DX

    DirectDraw API

    DirectPlay API.

    DirectSound API.

    Directlnput API

    API

    API

    DirectX

    DirectX

    ,

    , DirectX. , Direct3D, .

    DirectX, .

    , ,

    . Visual Studio.NET 2003, Microsoft. , 1.1 .NET runtime, CD .

  • 22

    DirectX 9.0 SDK Update. DirectX 9.0 Software Development Kit Update, CD . , DirectX.

    , ,

    .

    ,

    , ,

    . GeForce3 , , ,

    2.0 (, Radeon 9700 ). DirectX,

    , .

    , .

    ,

    DirectX,

    , DirectX. , , #, - , Visual Basic.NET. , #, Visual Basic.NET. DirectX 9 SDK Update http://msdn.microsoft.com.

    DirectX.

  • I

    1. Direct3D

    2.

    3.

    4.

    5. sh-

    6. DirectX

  • 24 I.

    1. Direct3D

    .

    GPU, .

    Direct3D

    . .

    Direct3D .

    ( ). .

    Managed Direct3D :

    1. Visual Studio.NET (new project).

    2. Visual C#, ( Windows).

    3. .

    , , ,

    DirectX, . ,

    , Add References MicrosoftDirectX Microsoft.DirectX.Direct3D. , .

    , DirectX, using, .

    , Windows ( forml.cs) using:

    using Microsoft.DirectX; using Microsoft.DirectX.Direct3D;

    .

    -

  • 1. Direct3D 25

    . ,

    DirectX.

    Direct3D Direct3D

    . ,

    , .

    .

    , , Direct3D, .

    ,

    .

    , .

    :

    public Device (System.Int32 adapter, Microsoft.DirectX.Direct3D.DeviceType deviceType, System.Windows.Forms.Control renderWindow, Microsoft.DirectX.Direct3D.CreateFlags behaviorFlags, Microsoft.DirectX.Direct3D.PresentParameters presentationParameters )

    adapter () , .

    ( , , ). adapter .

    ,

    renderWindow, IntPtr .

    IntPtr, IDIRECT3DDEVICE9.

    .

    DeviceType ( ) Direct3D, . , DeviceType.Hardware, . -

  • 26 I.

    DeviceType.Reference, (rasterizer), Direct3D Realtime, .

    , .

    , rasterizer DirectX SDK, DirectX Runtime . DeviceType.Software rasterizer.

    renderWindow ( ) .

    ,

    .

    , , .

    .

    behaviorFlags ( ) .

    CreateFlags , .

    , ,

    .

    SoftwareVertexProcessing, . ,

    , ,

    , ..

    , .

    presentationParameters .

    .

    , Windowed SwapEjfect. Windowed , ,

    (

  • 1. Direct3D 27

    1 . SwapEffect.Discard, , .

    , ,

    , .

    , ,

    (private variable) device. :

    private Device device = null;

    InitializeGraphics, . :

    /// /// We will initialize our graphics device here /// public void InitializeGraphics)) (

    // Set our presentation parameters PresentParameters presentParams = new PresentParameters() presentParams.Windowed = true; presentParams.SwapEffeet = SwapEffect.Discard; // Create our device device = new Device(0, DeviceType.Hardware, this,

    CreateFlags.SoftwareVertexProcessing, presentParams); 1

    , presentParams, (Windowed

    . SwapEffect), . adapter . ,

    , .

    ,

    this, . , ,

    , .

    ,

    .

    InitializeGraphics:

  • 28 I.

    static void Main() (

    using (Forml frm = new Forml()) (

    // Show our form and initialize our graphics engine frm.Show(); frm.InitializeGraphics(); Application.Run(frm);

    I i

    ? , . using . , .

    , Show, , ( ) , .

    .

    Direct3D . , .

    , ,

    , , -

    # . - .

    Windows-form , ,

    OnPaint ( Paint). . -

    ,

    . :

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {

    device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, l.Of, 0); device.Present();

    }

    ,

    Clear. , CornflowerBlue. Clear , , .

  • 1. Direct3D 29

    ,

    . ,

    .

    Present. , , , .

    .

    , ,

    ( ). , .

    , - ,

    .

    ,

    . -.

    , ,

    ,

    .

    , .

    ,

    . , ,

    . , -,

    , , -

    , ,

    , .

    , DirectX .

    Direct3D CustomVertex, Direct3D CustomVertex. Direct3D . , TransformedCo-lored . Direct3D runtime, - , ,

    ,

    .

    .

    OnPaint Clear:

    CustomVertex.TransformedColored[j verts = new CustomVertex.TransformedColored[3]; verts[OJ.SetPosition(new Vector4 (this. Width / 2. Of, 50. Of, 0.5f, 1.0 f).) ; verts[0].Color = System.Drawing.Color.Aqua.ToArgb(); verts[lJ.SetPosition(new Vector4(this.Width - (this.Width / 5.Of),

    this.Height -(this.Height / 5.Of), 0.5f, 1.Of));

  • 30 I.

    verts [I] .Color = System.Drawing.Color.Black.ToArgb(); verts[2].SetPosition(new Vector4(this.Width / 5.Of, this.Height -

    (this.Height / 5.Of) , 0.5f, l.Of));

    verts [2] .Color = System.Drawing.Color.Purple.ToArgb();

    , ,

    ,

    . , Vector4, SetPosition .

    X Y ( 0,0), Z rhw-( homogenous w). . Vector4 .

    .

    ,

    ToArgb. Direct3D , 32- , .

    ,

    .

    , ,

    Direct3D , , , .

    , OnPaint:

    device.BeginScene (); device.VertexFormat = CustomVertex.TransformedColored.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts); device.EndScenef);

    BeginScene Direct3D , - , . ,

    , .

    VertexFormat Direct3D runtime ( fixed function pipeline). .

    .

    DrawUserPrimitives , . ? , . -

  • 1. Direct3D 31

    , , ,

    , TriangleList. , ,

    , , .

    , 1. , Direct3D . .

    EndScene Direct3D runtime .

    , BeginScene. .

    , .

    ,

    .

    Direct3D . .

    , , ,

    .

    ,

    , . , Windows ,

    . ,

    , ,

    , .

    , .

    Windows, , . ,

    OnPaint.

    this.Invalidate();

    , !

    , .

    , . ? , Windows ,

    . , style .

    TODO :

    this.SetStyLe(ControlStyles.ALlPaintinglnWmPaint | ControlStyles.Opaque, true);

  • 32 I.

    , , .

    ,

    ( WmPaint Win32), . ,

    . ,

    , .

    . ,

    ,

    GDI. , ? .

    , ,

    , ,

    (transformed coordinates). , , .

    ? 3D-rpe.

    ,

    ,

    . ,

    ,

    .

    .

    ,

    .

    ,

    CustomVertex.PositionColored. :

    CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3]; verts[0].SetPosition(new Vector3(0.0f, l.Of, l.Of)); verts[0].Color = System.Drawing.Color.Aqua.ToArgb(); verts[l].SetPosition(new Vector3(-l.Of, -l.Of, l.Of)); verts[1].Color = System.Drawing.Color.Black.ToArgb(); verts[2].SetPosition(new Vector3(l.Of, -l.Of, l.Of)); verts [2] .Color = System.Drawing.Color.Purple.ToArgb();

    VertexFormat:

  • . 1. Direct3D 33

    device.VertexFormat = CustomVertex.PositionColored.Format;

    , , ,

    , . ,

    , , .

    ,

    PositionColored. , .

    ,

    Vector3 Vector4, .

    Vector3 ( X, Y, Z). , Direct3D , . ,

    ,

    (fixed function pipeline), VertexFormat.

    ,

    ? , , , Direct3D , . ,

    .

    ,

    .

    .

    4x4, Direct3D. , .

    PerspectiveFovLH Matrix. ,

    .

    , ? X , Y . Z. Z , , Z . , X, Y, . 1.1. , , Z, X Y -

    : . 604

  • 34 I.

    ,

    .

    Y

    *-

    . 1.1.

    Direct3D . , .

    ,

    ( ). Z. 31, 32, 34 . RH .

    , ,

    , Direct3D. :

    public static Microsoft.DirectX.Matrix PerspectiveFovLH ( System.Single fieldOfViewY,

    System.Single aspectRatio , System.Single znearPlane , System.Single zfarPlane )

    ,

    (frustum), .

    , (znearPlane zfarPlane) , ,

    , .1.2. , .1.3, , ,

    1.85. . ,

  • 1. Direct3D 35

    Direct3D , .

    . 1.2.

    > Z

    . 1.3.

    , ,

    :

    public static Microsoft.DirectX.Matrix LookAtLH(Microsoft.DirectX.Vector3 cameraPosition,

    Microsoft.DirectX.Vector3 cameraTarget , Microsoft.DirectX.Vector3 cameraUpVector )

  • 36 I.

    . ,

    .

    .

    ,

    . ,

    Up . ,

    Direct3D , . ,

    . SetupCamera ( ) OnPaint Clear. :

    private void SetupCamera()

    device.Transform.Projection = Matrix.PerspectiveFovLHl(float)Math.PI / 4, this.Width / this.Height, i.Of, 100.Of);

    device.Transform.View = Matrix.LookAtLH(new Vector3(0,3, 5.Of), new Vector3(),

    new Vector3(0,1,0)); i

    ,

    , Direct3D .

    .

    OnPaint Clear .

    ? , , ,

    . ,

    (pretransformed), ,

    . Direct3D ,

    .

    ,

    , .

    , ,

    , ,

    SetupCamera:

    device.RenderState.Lighting = false;

  • 1. Direct3D 37

    ,

    . .

    ,

    , .

    , , ,

    ,

    .

    ? , ,

    , .

    ,

    , .

    ? , .

    , ,

    ,

    .

    , .

    ,

    .

    .

    , .

    SetupCamera:

    device.Transform.World - Matrix.Rotations((float)Math.PI / 6.Of);

    Direct3D .

    Z. . Direct3DX, ,

    Geornetry.DegreeToRadians. ,

    . ,

    -

    Z. .

  • 38 I.

    :

    device.Transform.World = Matrix.RotationZ((System.Environment.TickCount / 450.Of) / (float)Math.PI);

    ,

    Z. ,

    TickCount.

    GetTickCount Win32 API, 15 . , , ,

    15 , .

    , , TickCount. .

    :

    device.Transform.World = Matrix.RotationZ(angle / (float)Math.PI); angle += O.lf;

    .

    ( ) , .

    ,

    , ,

    ,

    .

    .

    , ,

    .

    . , .

    :

    device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.Of), angle / ((float)Math.PI * 4.Of), angle / ((float)Math.PI * 6.Of)), angle / (float)Math.PI);

  • 1. Direct3D 39

    RotationAxis. ,

    , ,

    .

    .

    , ,

    , . ,

    ,

    .

    .

    back face culling, , ( ). runtime , .

    culling Direct3D . culling: none, clockwise counterclockwise (: , ). ,

    , ,

    .

    , ,

    , . 1.4. , DirectX .

    . 1.4.

    , ,

    , ,

    , .

    (render state), .

    SetupCamera:

  • 40 I.

    device.RenderState.CullMode = Cull.None;

    , , ,

    . .

    .

    , .

    , ,

    .

    , - Direct3D C++ DirectX Visual Basic, , ,

    Direct3D , ,

    ( , ) .

    DirectX

    . , DeviceResizing ( ), .

    true (Cancel) EventArgs, .

    :

    private void CancelResize(object sender, CancelEventArgs e) 1

    e. Cancel = true; }

    , .

    .

    :

    device.DeviceResizing += new CancelEventHandler(this.CancelResize);

    ,

    , . ,

    , ,

    .

  • 1. Direct3D 41

    .

    DirectX , .

    , ,

    . ,

    ,

    .

    . ,

    , ,

    true :

    device.RenderState.Lighting = true;

    ,

    lighting on , . ,

    , .

    .

    ,

    ,

    .

    , OnPaint :

    device.Lights[0]. = LightType.Point; device.Lights[0].Position = new Vector3(); ievice.Lights[0].Diffuse = System.Drawing.Color.White; device.Lights[0] .Attenuation() = 0.2f; device.Lights[0].Range = 10000.Of; device.Lights[0].Commit)); device.Lights[0].Enabled = true;

    , .

    ,

    , .

    . ,

    , ,

    , ( . ,

    ). - -

  • 42 I.

    , -: , .

    ( , ), , ,

    ,

    .

    (, , ), .

    . -,

    .

    (0, 0, 0), .

    parameterless Vector3. diffuse , .

    attenuation, . range , .

    .

    DirectX SDK ( CD )

    .

    , .

    , ,

    Deferred (). false, Comit, . true Comit. , ,

    .

    , ,

    . , Direct3D , .

    ,

    , . 1.5.

  • 1. Direct3D 43

    2-\ \

    1 -

    . 1.5.

    , .

    :

    CustomVertex.PositionNormalColored[] verts=new CustomVertex.PositionNormalColored[3]; verts[0].SetPosition(new Vector3(0.0f, l.Of, l.Of)); verts[0].SetNormal(new Vector3(O.Of, O.Of, -l.Of)); verts[0].Color = System.Drawing.Color.White.ToArgbf); verts [l].SetPosition(new Vector3(-1.0f, -l.Of, l.Of)); verts [1]. Set-Normal (new Vector3(0.0f, O.Of, -l.Of)); verts[1].Color = System.Drawing.Color. White.ToArgb(); verts[2].SetPosition(new Vector3(1.0f, -l.Of, l.Of)); verts[2].SetNormal(new Vector3(0.0f, O.Of, -l.Of)); verts[2] .Color = System.Drawing.Color. White.ToArgb();

    , ,

    :

    device.VertexFormat = CustomVertex.PositionNormalColored.Format;

    ,

    . ,

    ,

    .

    Z (l.Of), X Y, Z. , , -

    1 '

  • 44 I.

    .

    , , .

    , ,

    , .

    ,

    .

    :

    ,

    (, ), .

    , . -,

    .

    , ,

    -

    . : ( ), . ,

    , .

    , Direct3D .

    ,

    ,

    . ,

    , fill mode ( ) .

    .

    ,

    .

    : ( ), . , ,

    , 255 .

    -

    , , .

    -,

    .

  • 1. Direct3D 45

    - ,

    . , ,

    . -,

    SwapEffect.Flip , ,

    .

    ,

    , .1.6. flip ,

    ,

    . DirectX 9 ,

    .

    , ,

    , . ,

    . , SwapEffect.Flip . SwapEffect.Discard Swap-Effect.Copy .

    . 1.6.

    -, SwapEffect.Copy SwapEffect.Flip, , -.

    runtime , . ,

    SwapEffect.Discard. -

  • 46 I.

    . , SwapEffectDiscard

    . runtime ,

    .

    (-)

    -,

    ( -), SwapEffect.Discard. .

    -

    . ,

    -,

    .

    ,

    . -

    , ,

    .

    .

    ,

    , .

    , .

    Direct3D , . .

    .

    .

    .1.7.

  • 1. Direct3D 47

    . 1.7.

    ,

    , .

  • 48 I.

    2.

    .

    , .

    ,

    .

    , .

    .

    ,

    .

    .

    ,

    . ,

    ,

    mainstream,

    . ATI, nVidia, Matrox ,

    .

    , Direct3D, .

    ,

    . ATI Radeon 9700 , (DVI VGA), , Direct3D .

    ,

    .

    Direct3D , Manager, .

    Manager . -. , count, .

    (, Manager.Adapters[0]), .

    ,

    , ,

  • 2. 49

    . Visual Studio, :

    1. Windows Form Project #, , , Enumeration .

    2. Microsoft.DirectX.Direct3D Microsoft.DirectX, using .

    3. windows, TreeView, .

    4. TreeView , F4 ( , Properties ). TreeView Dock Fill. , .

    ,

    . -

    :

    / / We will fill our tree view here /

    public void LoadGraphics()

    foreach(Adapter-Information ai in Manager.Adapters)

    TreeNode root = new TreeNode(ai.Information.Description); TreeNode driverlnfo = new TreeNode(string.Format

    ("Driver information: {0} - {1}", ai.Information.DriverName, ai.Information.DriverVersion) );

    // Add the driver information to the root node root.Nodes.Add(driverlnfo) ; // Get each display mode supported TreeNode displayMode = new TreeNode(string.Format

    ("Current Display Mode: (0}x{i}x{2}\ ai.CurrentDisplayMode.Width, ai.CurrentDisplayMode.Height, ai.CurrentDispIayMode.Format));

    foreach(DisplayMode dm in ai.SupportedDisplayModes)

    TreeNode supportedNode = new TreeNode(string.Format ("Supported: (0} x (1 }x {2}",. dm.Width, dm.Height, dm.Format));

  • 50 I.

    displayMode.Nodes.Add(supportedNode); } // Add the display modes root.Nodes.Add(displayMode); // Add the root node treeViewl.Nodes.Add(root);

    .

    , , .

    . Foreach, #, .

    Adapterlnformation . Adapterlnformation :

    public struct Adapterlnformation ( int Adapter; DisplayMode CurrentDisplayMode; AdapterDetails Information; AdapterDetails GetWhqllnformationO; DisplayModeEnumerator SupportedDisplayModes; }

    Adapter , . -

    ordinals, . AdapterDetails , .

    GetWhqlrnformation, Information WHQL ( Windows). , .

    AdapterDetails ,

    .

    , .

    DisplayMode, ,

  • 2. 51

    , .

    CurrentDisplayMode , Supported-DisplayModes , .

    , , ,

    .

    ,

    ( ) ,

    ,

    .

    .

    , .

    :

    using (Forml frm = new Forml()) {

    frm.LoadGraphics lb-Application.Run(frm) ;

    }

    ,

    .2.1. ,

    , .

    .

    -

    . ; . :

    alpha, X unused, R red, G green, blue, L luminance, palette,

  • 52 I.

    . 2 . 1 .

    . , X8R8G8B8, .2.1, 32- 8 , (, ) 8 .

    ,

    .

    : A2R10G10B10 A1R5G5B5 A8R8G8B8 X1R5G5B5 X8R8G8B8 R5G6B5 ,

    , ,

    . ,

    , - A2R10G10B10, .

    , ,

    Direct3D , .

    , ,

    , X8R8G8B8 R5G6B5.

  • 2. 53

    , ,

    , ,

    , .

    Manager ,

    . ,

    ,

    , Manager. :

    public static System.Boolean CheckDeviceType ( System.Int32 adapter , Microsoft.DirectX.Direct3D.DeviceType checkType , Microsoft.DirectX.Direct3D.Format displayFormat , Microsoft.DirectX.Direct3D.Format baciBufferformat , System.Boolean windowed , System.Int32 result )

    ,

    . adapter, . checktype, ( , DeviceType.Hardware). displayFormat -BufferFormat, , ( (windowed) ). , (HRESULT ) . true, , false.

    ,

    ,

    . CheckDeviceType , , ,

    CheckDeviceFormat-Conversion Manager. Format.Unknown.

  • 54 I.

    ,

    .

    .

    capability . Direct3D runtime ,

    . ,

    capability . ,

    Manager. ,

    .

    ,

    - .

    .

    view Fill Left. . , Dock Fill. , Multiline true, Scrollbars Both .

    ,

    . AfterSelect (, , ). :

    private void treeViewl_AfterSelect(object sender,System.Windows.Forms.TreeViewEventArgs e) (

    if (e.Node.Parent == null) // root node (

    // Get the device caps for this node

  • 2. 55

    textBoxl.Text = .Node.Text + " Capabilities: \r\n\r\n" + Manager.GetDeviceCaps

    (e.Node.Index, DeviceType.Hardware).ToString().Replace("\n", "\r\n");

    , . ,

    , ,

    Manager.GetDeviceCaps . ToString .

    , . .2.2.

    CAPABILITY

    , capability (, MaxActiveLights). ,

    .

    .

    , ,

    . ,

    , (fogging), .

    Capability . , ,

    . , SupportsAlphaCom- true, . Capability - , MaxActiveLights, , .

    .

  • 56 I.

    . 2.2.

    .

    .

    .

    , .

    .

    ,

    .

  • 3. 57

    3.

    ,

    , .

    , ,

    .

    ,

    . ,

    ,

    ,

    .

    Direct3D . , ,

    .

    . ,

    , .

    .

    , .

    .

    public VertexBuffer ( Microsoft.DirectX.Direct3D.Device device , System.Int32 sizeOfBufferInByt.es , Microsoft.DirectX.Direct3D.Usage usage , Microsoft.DirectX.Direct3D.VertexFormats vertexFormat , Microsoft.DirectX.Direct3D.Pool pool )

    public VertexBuffer ( System.Type typeVertexType , System.Int32 numVerts , Microsoft.DirectX.Direct3D.Device device , Microsoft.DirectX.Direct3D.Usage usage , Microsoft.DirectX.Direct3D.VertexFormats vertexFormat , Microsoft.DirectX.Direct3D.Pool pool )

    .

    Device , Direct3D .

    .

    SizeOfBufferlnBytes . ,

    .

  • 58 I.

    TypeVertexType , , .

    CustomVertex, , .

    .

    NumVerts , .

    .

    Usage , .

    , .

    DoNotClip , clipping () . ,

    clipping false. Dynamic ,

    . ,

    .

    ,

    AGP. , , .

    Dynamic DirectX SDK.

    Npatches , N-Patches. Points , .

    RTPatches , .

    SoftwareProcessing , ,

    .

    WriteOnly , - ,

    .

    .

    VertexFormat , . VertexFormat.No-, .

    Pool , .

    .

  • 3. 59

    Default , .

    , AGP, . ,

    ,

    .

    SystemMemory ,

    .

    Scratch , .

    ,

    .

    ,

    , IntPtr.

    IDirect3DvertexBuffer9. , ,

    () . ,

    .

    , 1, .

    , ,

    :

    private Device device = null; private VertexBuffer vb = null;

    , .

    .

    :

    // Create our device device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing

    , presentParams);

  • 60 I.

    CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3]; verts[0].SetPosition(new Vector3(0.Of, I.Of, l.Of)); verts [0] .Color = System.Drawing.Color.Aqua.ToArgb(); verts[l].SetPosition(new Vector3(-1.0f, -l.Of, l.Of)); verts [1] .Color = System.Drawing.Color.Black.ToArgb() verts[2].SetPosition(new Vector3(l.Of, -l.Of, l.Of)); verts [2] .Color - System.Drawing.Color.Purple.ToArgbO ; vb = new VertexBuffer(typeof(CustomVertex.PositionCoiored), 3, device, Usage.Dynamic :

    Usage.WriteOnly, CustomVertex.PositionCoiored.Format, Pool.Default); vb.SetData(verts, 0, LockFlags.None);

    , ,

    .

    ,

    . , ,

    .

    ,

    SetData. ( DrawUserPrimitives) verts. offset . , , .

    LockFlags.None , , .

    .

    , DrawUserPrimitives OnPaint verts. Direct3D, , , .

    , .

    SetStreamSource , Direct3D .

    :

    public void SetStreamSource ( System.Int32 streamNumber , Microsoft.DirectX.Direct3D.VertexBuffer streamData , System.Int32 offsetlnBytes , System.Int32 stride )

    public void SetStreamSource ( System.Int32 streamNumber , Microsoft.DirectX.Direct3D.VertexBuffer streamData , System.Int32 offsetlnBytes )

  • 3. 61

    ,

    . streamNumber , .

    , , .

    ,

    . offsetlnBytes ( ) , , Direct3D . stride ( ) .

    ,

    .

    drawing :

    device.SetStreamSource(0, vb, 0); device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

    ,

    ,

    , . ,

    .

    ,

    DrawUserPrimitives ( ,

    ). DrawPrimitives . DrawPrimitives , , .

    .

    , .

    10%, . , ,

    , .

    .

    , ,

    . ,

    .

    , (, ) , . , -

  • 62 I.

    , .

    DirectX , ,

    , , , ,

    .

    created, .

    , :

    private void OnVertexBufferCreate(object sender, EventArgs e) (

    VertexBuffer buffer = (VertexBuffer)sender; CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3]; verts[0].SetPosition(new Vector3(O.Of, l.Of, l.Of )); verts[0].Color = System.Drawing.Color.Aqua.ToArgb(); verts[l].SetPosition(new Vector3(-1.0f, -l.Of, l.Of)); verts[1].Color = System. Drawing.Color.Black.ToArgb(); verts[2].SetPosition(new Vector3(1.0f, -l.Of, l.Of)); verts[2].Color = System.Drawing.Color.Purple.ToArgb(); buffer.SetData(verts, 0, LockFlags.None);

    1

    ,

    .

    ,

    . , ,

    ,

    . , ,

    SetData.

    InitializeGraphics, :

    vb.Created += new EventHandler(this.OnVertexBufferCreate); OnVertexBufferCreate(vb, null);

    OnVertexBufferCreate , . ,

    .

    .

  • 3. 63

    ,

    ; ,

    . ,

    , .

    , ,

    . , ,

    .

    , .

    ,

    , , ,

    , . ,

    ,

    . , . 3.1:

    3.1. .

    CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[36]; // Front face verts[0] = new CustomVertex.PositionColored(-1.0f, l.Of, l.Of, Color.Red.ToArgb()) ; verts[l] = new CustomVertex.PositionColored(-1.0f, -l.Of, l.Of, Color.Red.ToArgb()); verts[2] = new CustomVertex.PositionColored(1.0f, l.Of, l.Of, Color.Red.ToArgb()); verts[3] = new CustomVertex.PositionColored(-1.0f, -l.Of, l.Of, Color.Red.ToArgb()); verts[4] = new CustomVertex.PositionColored(1.0f, -l.Of, l.Of, Color.Red.ToArgb()); verts[5] = new CustomVertex.PositionColored(l.Of, l.Of, l.Of, Color.Red.ToArgb()); // Back face (remember this is facing *away* from the camera, so vertices

    should be clockwise order) verts[6] = new CustomVertex.PositionColored(-1.0f, l.Of, -l.Of, Color. Blue.ToArgb()); verts[7] = new CustomVertex.PositionColored(l.Of, l.Of, -l.Of, Color.Blue.ToArgb());

  • 64 I.

    verts[8] = new CustomVertex.PositionColored(-1.0f, -l.Of, -l.Of, Color.Blue.ToArgb()); verts[9J = new CustomVertex.PositionCoiored(-1.0f, -l.Of, -l.Of, Color. Blue. ToArgb()); verts[lO] = new CustomVertex.PositionCoiored(1.0f, l.Of, -l.Of, Color.Blue.ToArgb(l); verts[ll] = new CustomVertex.PositionColored(1,Of, -l.Of, -l.Of, Color. Blue. ToArgb()); // Top face verts[12] = new CustomVertex.PositionColored(-1.0f, l.Of, l.Of, Color.Yellow.ToArgb()); verts[13] = new CustomVertex.PositionColored(1.0f, l.Of, -l.Of, Color.Yellow.ToArgb()) ; verts[14! = new CustomVertex.PositionColored(-1.0f, l.Of, -l.Of, Color.Yellow.ToArgb()); verts[15J = new CustomVertex.PositionColored(-1.0f, l.Of, l.Of, Color.Yellow.ToArgb()); verts[16] = new CustomVertex.PositionColored(1.0f, l.Of, l.Of, Color.Yellow.ToArgb()); verts[17] = new CustomVertex.PositionCoiored(1.0f, l.Of, -l.Of, Color. Yellow. ToArgb()); // Bottom face (remember this is facing *away* from the camera, so vertices

    should be clockwise order) verts[18] = new CustomVertex.PositionColored(-1.0f, -l.Of, l.Of, Color.Black.ToArgb()); verts[19] = new CustomVertex.PositionColored(-1.0f, -l.Of, -l.Of, Color.Black.ToArgb()); verts[20] = new CustomVertex.PositionColored(l.Of, -l.Of, -l.Of, Color.Black.ToArgb()); verts[21j = new CustomVertex.PositionColored(-1.0f, -l.Of, l.Of, Color.Black.ToArgb()); verts[22] = new CustomVertex.PositionColored(1.0f, -l.Of, -l.Of, Color. Black.ToArgb()); verts[23] = new CustomVertex.PositionCoiored(1.0f, -l.Of, l.Of, Color.Black.ToArgb()); // Left face verts[24] = new CustomVertex.PositionColored(-1.0f, l.Of, l.Of, Color.Gray.ToArgb()); verts[25] = new CustomVertex.PositionColored(-1.0f, -l.Of, -l.Of, Color.Gray.ToArgb()); verts[26] = new CustomVertex.PositionColored(-1.0f, -l.Of, l.Of, Color. Gray. ToArgb()); verts[27] = new CustomVertex.PositionColored(-1.0f, l.Of, -l.Of, Color.Gray.ToArgb()); verts[28] = new CustomVertex.PositionColored(-1.0f, -l.Of, -l.Of, Color.Gray.ToArgb());

  • 3. 65

    verts[29] = new CustomVertex.PositionColored(-1.0f, l.Of, 1.Of, Color.Gray.ToArgbO); // Right face (remember this is facing *away* from the camera, so vertices

    should be clockwise order)

    verts[30] - new CustomVertex.PositionColored(1.0f, l.Of, l.Of, Color.Green.ToArgbO ) ; verts[31] = new CustomVertex.PositionCoiored(1.0f, -l.Of, l.Of, Color.Green.ToArgbO ); verts[32] = new CustomVertex.PositionColored(1.0f, -l.Of, -l.Of, Color.Green.ToArgbO); verts[33] = new CustomVertex.PositionColored(1.0f, l.Of, -l.Of, Color.Green.ToArgbf)) ; verts[34] = new CustomVertex.PositionColoredfl.Of, l.Of, l.Of, Color.Green.ToArgbO ) ; verts[35] = new CustomVertex.PositionColored(1.0f, -l.Of, -l.Of, Color.Green.ToArgbO ) ; buffer.SetData(verts, 0, LockFlags.None);

    , 36 . , CD . , 12 , 3 . SetData.

    ,

    . ,

    .

    :

    vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 36, device, Usage.Dynamic ! Osage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

    device.Transform.World = Matrix.RotationYawPitchRoll(angle / (float)Math.PI, angle / (float)Math.PI * 2.Of, angle / (float)Math.PI);

    device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);

    , ,

    ,

    . ,

    . ,

    12 , . ,

    D- , -

    .! 604

  • 66 I.

    .

    Direct3D ( ). , cullig, . .

    . .

    ,

    ,

    ( ). .

    ,

    .

    ,

    . Look :

    device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 18.Of), new Vector3(), new Vector3(0,1,0));

    , ,

    .

    , .

    , ,

    ,

    Direct3D . Draw-Primitives:

    device.Transform.World = Matrix.RotationYawPitchRoll(angle / (float)Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI * 4.Of) * Matrix.Translation(5.Of, O.Of, O.Of);

    device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); device.Transform.World = Matrix.RotationYawPitchRoll(angle /

    (float)Math.PI, angle / (float)Math.PI * 4.Of, angle / (float)Math.PI / 2.Of) * Matrix.Translation(-5.Of, O.Of, O.Of);

    device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);

    Direct3D , , VertexFormat . SetStreamSource ( ) Direct3D , -

  • 3. 67

    .

    Direct3D, ? , , .

    , ,

    . , ,

    , SetupCamera ( ,

    ). , .

    (Matrix.Translation) .

    . ,

    ,

    .

    ,

    : , . ( , -

    JCBO ), .

    CD- , , ,

    , .3.1.

    . 3 . 1 .

  • 68 I.

    ,

    , .

    (texture) .

    , , bitmap-,

    . , ,

    , , , ,

    . Direct3D ,

    .

    . 3.2.

    Direct3D bitmap , , ,

    .

    2D , ? , , ,

    ( ) . ,

    texel, , .

  • 3. 69

    , U V, . ,

    0,0 1,0. 0,0 , 1,1 .

    0,5, 0,5, .3.2.

    ,

    , , .

    color () . ,

    ,

    . ,

    3.2:

    3.2. .

    CustomV'ertex.PositionTextured[] verts = new CustomV'ertex.PositionTextured [36]; // Front face verts[0] = new CustomVertex.PositionTextured(-1.0f, l.Of, l.Of, O.Of, O.Of); verts[1] = new CustomVertex.PositionTextured(-1.0f, -l.Of, l.Of, O.Of, l.Of); verts[2] = new CustomVertex.PositionTextured(1.0f, l.Of, l.Of, l.Of, O.Of); verts[3] = new CustomVertex.PositionTextured(-l.Of, -l.Of, l.Of, O.Of, l.Of); verts[4] = new CustomVertex.PositionTextured(1.0f, -l.Of, l.Of, l.Of, l.Of); verts[5] = new CustomVertex.PositionTextured(1.0f, l.Of, l.Of, l.Of, O.Of); // Back face (remember this is facing *away* from the camera, so vertices

    should be clockwise order)

    verts[6] = new CastomVertex.PositionTextured(-1.0f, l.Of, -l.Of, O.Of, O.Of); verts[7] = new CustomVertex.PositionTextured(i.Of, l.Of, -l.Of, l.Of, O.Of); verts[8] = new CustomVertex.PositionTextured(-1.0f, -l.Of, -l.Of, O.Of, l.Of); verts[9j - new CustomVertex.PositionTextured(-1.0f, -l.Of, -l.Of, O.Of, l.Of); verts[10] = new CustomVertex.PositionTextured(l.Of, l.Of, -l.Of, l.Of, O.Of); verts[11] = new CustomVertex.PositionTextured(l.Of, -l.Of, -l.Of, l.Of, l.Of);

  • 70 I.

    // Top face verts[12] = new CustomVertex.PositionTextured(-1.0f, l.Of, l.Of, O.Of, O.Of); verts[13] = new CustomVertex.PositionTextured(l.Of, l.Of, -l.Of, l.Of, l.Of); verts[14] = new CustomVertex.PositionTextured(-1.0f, l.Of, -l.Of, O.Of, l.Of); verts[15] = new CustomVertex.PositionTextured(-1.0f, l.Of, l.Of, O.Of, O.Of); verts[16] = new CustomVertex.PositionTextured(1.0f, l.Of, l.Of, l.Of, O.Of); verts[17] = new CustomVertex.PositionTextured(1.0f, l.Of, -l.Of, l.Of, l.Of); // Bottom face (remember this is facing *away* from the camera, so vertices

    should be clockwise order)

    verts[18] = new CustomVertex.PositionTextured(-1.0f, -l.Of, l.Of, O.Of, O.Of); verts[19] = new CustomVertex.PositionTextured(-1.0f, -l.Of, -l.Of, O.Of, l.Of); verts[20] = new CustomVertex.PositionTextured(1.0f, -l.Of, -l.Of, l.Of, l.Of); verts[21] = new CustomVertex.PositionTextured(-1.0f, -l.Of, l.Of, O.Of, O.Of); verts[22] = new CustomVertex.PositionTextured(1.0f, -l.Of, -l.Of, l.Of, l.Of); verts[23] = new CustomVertex.PositionTextured(1.0f, -l.Of, l.Of, l.Of, O.Of); // Left face verts[24] = new CustomVertex.PositionTextured(-1.0f, l.Of, l.Of, O.Of, O.Of); verts[25] = new CustomVertex.PositionTextured(-1.0f, -l.Of, -l.Of, l.Of, l.Of); verts[26] = new CustomVertex.PositionTextured(-1.0f, -l.Of, l.Of, l.Of, O.Of); verts[27] = new CustomVertex.PositionTextured(-1.0f, l.Of, -l.Of, O.Of, l.Of); verts[28] = new CustomVertex.PositionTextured(-1.0f, -l.Of, -l.Of, l.Of, l.Of); verts[29] = new CustomVertex.PositionTextured(-1.0f, l.Of, l.Of, O.Of, O.Of); // Right face (remember this is facing *away* from the camera, so vertices

    should be clockwise order)

    verts[30] = new CustomVertex.PositionTextured(1.0f, l.Of, l.Of, O.Of, O.Of);

  • 3. 71

    verts[31] = new CustomVertex.PositionTextured(1.0f, -1.Of, 1.Of, l.Of, O.Of); verts[32] = new CustomVertex.PositionTextured(1.0f, -l.Of, -l.Of, l.Of, l.Of); verts[33] = new CustomVertex.PositionTextured(1.0f, l.Of, -l.Of, O.Of, l.Of); verts[34] = new CustomVertex.PositionTextured(1.0f, l.Of, l.Of, O.Of, O.Of); verts[35] = new CustomVertex.PositionTextured(1.0f, -l.Of, -l.Of, l.Of, l.Of); buffer.SetData(verts, 0, LockFlags.None);

    , ,

    .

    , , U V , .

    ,

    , ,

    . ,

    0,0, 1,1. .

    ,

    :

    vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 36, device, Usage.Dynamic j Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);

    ,

    :

    private void DrawBox(float yaw, float pitch, float roll, float x, float y, float z, Texture t)

    ( angle += O.Olf; device.Transform.World = Matrix.RotationYawPitchRol(yaw, pitch, roll) * Matrix.Translation(x, y, z); device.SetTexture(0, t); device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);

    ,

    . (yaw), (pitch)

  • 72 I.

    (roll) , X, Y, Z . texture t , .

    SetTexture , Direct3D, .

    ,

    . ,

    . ,

    .

    ,

    0. , ,

    ,

    SetupCamera.

    , ,

    . , CD , puck.bmp, ground.bmp, banana.bmp, .

    Add Existing Item . Build Action () Embedded Resource ( ). ,

    :

    private Texture tex = null; private Texture texl = null; private Texture tex2 = null;

    .

    , .

    :

    tex = new Texture(device, new Bitmap(this.GetIype(), "puck.bmp"), 0, Pool.Managed); texl = new Texture(device, new Bitmap(this.GetType(), "banana.bmp"), 0, Pool.Managed); tex2 = new Texture(device, new Bitmap(this.GetType(), "ground.bmp"), 0, Pool.Managed);

    .

    device, , -

  • 3. 73

    . (, , .) . System.Drawing.Bitmap . bitmap, .

    ,

    .

    , .

    .

    , :

    public Texture ( System.IntPtr Ip , Microsoft.DirectX.Direct3D.Device device , Microsoft.DirectX.Direct3D.Pool pool )

    public Texture ( Microsoft.DirectX.Direct3D.Device device , System.Int32 width , System.Int32 height , System.Int32 numLevels , Microsoft.DirectX.Direct3D.Usage usage , Microsoft.DirectX.Direct3D.Format format , Microsoft.DirectX.Direct3D.Pool pool )

    public Texture ( Microsoft.DirectX.Direct3D.Device device , System.IO.Stream data , Microsoft.DirectX.Direct3D.Usage usage , Microsoft.DirectX.Direct3D.Pool pool )

    IntPtr, IDIRECT3DTEXTURE9.

    .

    (blank texture), , . ,

    . ,

    ,

    , bitmap . System.Drawing.Bitmap .

    TextureLoader, .

    ,

    , ,

    ,

    .

    :

  • 74 I.

    // Draw our boxes DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.Of, angle /

    (float)Math.PI / 4.Of, O.Of, O.Of, O.Of, tex);

    'DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI * 4.Of, 5.Of, O.Of, O.Of, texl);

    DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.Of, angle / (float)Math.PI / 2.Of, -5.Of, O.Of, O.Of, tex2);

    , ,

    , .

    , CD , , .3.3.

    . 3.3.

    , .

    ,

    .

  • 3. 75

    .

    .

    , ,

    ( Z-).

  • 76 I.

    4.

    ,

    ,

    . .

    ,

    .

    .

    Z- .

    . ,

    .

    PointList , , .

    , ( ), .4.1 LineList , .

    ( , ), .4.2.

    LineStrip , .

    , ,

    .

    , .4.3 TriangleList ,

    .

    , .

    ,

    .4.4.

  • 4. 77

    . 4 . 1 .

    [~vo

    \

    V2

    \ V 4

    V1 \ V 5 V3

    . 4.2.

    . 4.3.

  • 78 I.

    . 4.4.

    TriangleStrip , ,

    .

    .

    ,

    .

    , .4.5. TriangleFan , TriangleStrip, , , .4.6.

    . 4.5.

    . Direct3D -,

    , .

    .

  • 4. 79

    V1

    V2

    V3

    V 4

    >V5

    . 4.6.

    , CD , , 3. . ,

    SetupCamera , angle. , :

    private const int NumberItems = 12;

    12 , . ,

    , - ,

    .

    LineList TriangleList. OnVertex-BufferCreate, 4.1:

    4.1. .

    vb = new VertexBuffer(typeof(CustomVertex.PositionColored), Numberltems, device, Usage.Dynamic \ Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

    // Create a list of vertices equal to the number of items CustomVertex.PositionColored[] verts = new

    CustomVertex.PositionColored[Numberltems]; 7 Randomly select the position of this vertex.. Keep it within a range of

    5. for(int i = 0; i < Numberltems; i++)

  • 80 I.

    float xPos = (float)(Rnd.NextDouble() * 5.Of) - (float)(Rnd.NextDouble() * 5.0f); float y?os = (float) (Rnd.NextDouble() * 5.Of) - (float) (Rnd.NextDouble() * 5.Of); .float zPos = (float) (Rnd.NextDouble() * 5.Of) - (float) (Rnd.NextDouble() * 5.Of); verts[i].SetPosition(new Vector3(xPos, yPos, zPos)); verts[i] .Color = RandomColor.ToArgb();

    1 buffer.SetData(verts, 0, LockFlags.None);

    . ,

    ( Numberltems). , .

    Rnd RandomColor CD

    . ,

    ,

    , ,

    . ,

    ( ) .

    :

    private bool needRecreate = false; // Timing information private static readonly int InitiallickCount = System.Environment.TickCount;

    ,

    .

    DrawPrimitives , 4.2:

    4.2. .

    // We will decide what to draw based on primitives int index = ((System.Environment.TickCount - InitialTickCount) / 2000) \ 6; switch (index) { case 0: // Points

    device.DrawPrimitives(Primitivelype.PointList, 0, Numberltems); if (needRecreate) }

  • 4. 81

    // After the primitives have been drawn, recreate a new set OnVertexBufferCreate(vb, null); needRecreate = false;

    } break;

    case 1: // LineList device.DrawPrimitives(PrimitiveType.LineList, 0, Numberltems / 2); needRecreate = true; break;

    case 2: // LineStrip device.DrawPrimitives(PrimitiveType.LineStrip, 0, Numberltems - 1); break;

    case 3: // TriangleList device.DrawPrimitives(PrimitiveType.TriangleList, 0, Numberltems / 3); break;

    case 4: // TriangleStrip device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, Numberltems - 2); break;

    case 5: // TriangleFan device.DrawPrimitives(PrimitiveType.TriangleFan, 0, Numberltems - 2); break;

    }

    . ,

    ,

    DrawPrimitives . ,

    . LineList TriangleList ,

    DrawPrimitives , ( , ). ,

    ,

    LineStrip , . TriangleStrip TriangleFan , ,

    , .

    , .

    ,

    . , ,

    , . ,

    ( scale ). SetupCamera , :

  • 82 I.

    device.RenderState.PointSize = 3.0 f ;

    , ,

    .

    , ,

    .

    ,

    36 , 2 ; , 12 . 3 , 36. , 8, .

    ,

    , ,

    ,

    . , Direct3D , .

    ,

    . , ,

    32- ( ) 16- ( ). 16- .

    . , 0, 1, 6 , .

    , , .

    , 4.3:

    4.3. .

    vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 8, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

    CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[8]; // Vertices verts[0] = new CustomVertex.PositionColored(-1.0f, l.Of, l.Of, Color. Purple. ToArgb());

  • 4. 83

    verts[1] = new CustomVertex.PositionColored(-1.0f, -l.Of, 1.Of, Color. Red. ToArgb()); verts[2] = new CustomVertex.PositionColored(1.0f, l.Of, l.Of, Color.Blue.ToArgb()); verts[3] = new CustomVertex.PositionColored(1.0f, -l.Of, l.Of, Color.Yellow.ToArgb()); verts[4] = new CustomVertex.PositionColored(-1.0f, l.Of, -l.Of, Color.Gold.ToArgb()); verts[5] = new CustomVertex.PositionColored(1.0f, l.Of, -l.Of, Color.Green.ToArgb()); verts[6] = new CustomVertex.PositionColored(-1.0f, -l.Of, -l.Of, Color.Black.ToArgb()); verts[7] = new CustomVertex.PositionColored(l.Of,-l.Of,-l.Of, Color.WhiteSmoke.ToArgb()); buffer.SetData(verts, 0, LockFlags.None);

    , ,

    8, . 36 , 8 . , ,

    36 . , 4.4, :

    4.4. .

    private static readonly short[] indices = { 0,1,2, // Front Face 1,3,2, // Front Face 4,5,6, // Back Face 6,5,7, // Back Face 0,5,4, // Top Face 0,2,5, // Top Face 1,6,7, // Bottom Face 1,7,3, // Bottom Face 0,6,1, // Left Face 4,6,0, // Left Face 2,3,7, // Right Face 5,2,7 // Right Face };

    , . ,

    .

    , 1, 2,

  • 84 I.

    1, 3, 2. 2, 3, 7, 5, 2, 7.

    .

    , , -

    , .

    .

    :

    private IndexBuffer ib = null;

    ,

    Direct3D . ,

    , IndexBuffer . ,

    . ,

    4.5:

    4.5. .

    ib = new IndexBuffer(typeof(short), indices.Length,device,Usage.WriteOnly,Pool.Default); ib.Created += new EventHandler(this.OnlndexBufferCreate); OnlndexBufferCreate (ib, null); private void OnlndexBufferCreate(object sender, EventArgs e)

    IndexBuffer buffer = (IndexBuffer) sender; buffer.SetData (indices, 0, LockFlags.None);

    }

    ,

    .

    . ,

    (System.Intl6), (System.Int32) .

    .

    .

    , ,

    . , ,

    SetStreamSource, Direct3D, .

    , , -

  • 4. 85

    ,

    .

    SetStreamSource:

    device.Indices = ib;

    , Direct3D , .

    12 (36 ) , ,

    .

    DrawBox:

    private void DrawBox(float yaw, float pitch, float roil, float x, float y, float z) (

    angle += O.Olf; device.Transform.World = Matrix.RotationYawPitchRolI(yaw, pitch, roll) *

    Matrix.Translation(x, y, z); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0,

    indices.Length / 3); 1

    , DrawPrimitives DrawIndexedPrimitives. :

    public void DrawIndexedPrimitives ( Microsoft.DirectX.Direct3D.PrimitiveType primitiveType , System.Int32 baseVertex , System.Int32 minVertexIndex , System.Int32 numVertices , System.Int32 startlndex , System.Int32 primCount )

    , ,

    , . BaseVertex .

    MinVertexIndex . NumVertices ( , ), baseVertex minVertexIndex. Startlndex .

    ( ).

    , , 8 , , 12 . DrawPrimitives

  • 86 I.

    DrawBox, 4.6:

    4.6. .

    // Draw our boxes DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI * 2.Of, angle /

    (float)Math.PI / 4.Of, O.Of, O.Of, O.Of);

    DrawBoxfangle / (float(Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI * 4.Of, 5.Of, O.Of, O.Of);

    DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI * 4.Of, angle / (float)Math.PI / 2.Of, -5.Of, O.Of, O.Of);

    DrawBoxfangle / (float(Math.PI, angle / (float)Math.PI * 2.Of, angle / (float)Math.PI / 4.Of, O.Of, -5.Of, O.Of);

    DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI * 4.Of, 5.Of, -5.Of, O.Of);

    DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI * 4.Of, angle / (float)Math.PI / 2.Of, -5.Of, -5.Of, O.Of);

    DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI * 2.Of, angle / (float)Math.PI / 4.Of, O.Of, 5.Of, O.Of);

    DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI * 4.Of, 5.Of, 5.Of, O.Of);

    DrawBoxfangle / (float)Math.PI, angle / (float)Math.PI * 4.Of, angle / (float)Math.PI / 2.Of, -5.Of, 5.Of, O.Of);

    .

    ,

    ,

    .

    , ,

    . ,

    ,

    , -

  • 4. 87

    ( ). , ,

    .

    Z- , depth buffer ( Z- W-

    ), Direct3D .

    , ,

    . ,

    ,

    . ,

    .

    DrawBox:

    DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.Of, angle / (float)Math.PI / 4.Of, O.Of, (float)Math.Cos(angle), (float)Math.Sin(angle));

    DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI * 4.Of, 5.Of, (float)Math.Sin(angle), (float)Math.Cos(angle));

    DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.Of, angle / (float)Math.PI / 2.Of, -5.Of, (float)Math.Cos(angle), (float)Math.Sin(angle) );

    ,

    , .

    , , ,

    .

    . .

    .

    ,

    . ,

    ,

    :

    public Microsoft.DirectX.Direct3D.DepthFormat AutoDepthStencilFormat [get, set] public bool EnableAutoDepthStencil [get, set]

  • 88 I.

    ,

    ,

    .

    , 32 CustomVertex.PositionColored. 16 (4 X, Y, Z ). , ,

    576 . ,

    . 8 ( ), 128 . , ,

    (2 ), 36 . , 72 , 128 , 200 . 576 . 65 %. , ,

    .

    EnableAutoDepthStencil true , ,

    AutoDepthStencilFormat. DepthFormat 4.1:

    4.1. Z

    D16

    D32

    D16Lockable

    D32FLockable

    D15S1

    16- Z-

    32- Z-

    16- Z-

    32- Z-. .

    16- Z-, 15 , , (stencil) ( )

  • 4. 89

    D24S8 32- Z-. 24 8

    D24X8 32- Z-. 24 , 8

    D24X4S4 32- Z-. 24 , 4 , . 4

    D24FS8 32- Z-. 24 ( ) 8

    Z-; , ,

    . Direct3D Z- ( O.Of 1 .Of), .

    Z-. ,

    1 .Of, 100.Of, 90 % 10 % . ,

    1000.Of, 98 % 2 % . .

    W- , .

    W- , . ,

    W-, Z-. ,

    ( Z) ( Z). Direct3D , ,

    . ,

    , .

  • 90 I.

    , .

    ,

    . 16- ,

    :

    presentParams.EnableAutoDepthStencil = true; presentParams.AutoDepthStencilFormat = DepthFormat.D16;

    , .

    .

    .

    , - ,

    , . ?

    ? : , ,

    . . clear :

    device.Clear(ClearFlags.Target [ ClearFlags.ZBuffer, Color.CornflowerBlue, l.Of, 0);

    . ,

    , .

    ,

    .

    ,

    .

    , .

    , .

    , . ,

    mesh.

  • 5. Mesh 91

    5. Mesh- .

    Mesh. , .

    Mesh-. Mesh .

    Mesh- ,

    , ,

    , .

    .X . , .

    .

    , ,

    36, , .

    , DirectX , ,

    . Mesh. Mesh ,

    . Mesh ,

    . Mesh- ( ), , .

    Mesh (Direct3D Extensions library (D3DX)). Direct3D, , Mesh- , Microsoft.DirectX.Direct3DX.dll. , Mesh, .

    ,

    Mesh. , :

    private Mesh mesh = null;

    Mesh; , . -

  • 92 I.

    Mesh, . ,

    , Box. , Mesh, .

    :

    mesh = Mesh.Box(device, 2.Of, 2.Of, 2.0f);

    Mesh-, , ,

    ( 2.Of). , , .

    , .

    , Mesh-, , , ? , SetStreamSource, Direct3D, ,

    . Mesh- .

    , MESH-

    Mesh-. Mesh , . Mesh- ,

    , .

    , Mesh- , . Mesh- ( , ), DrawSubset, . DrawBox :

    private void DrawBox(float yaw, float pitch, float roll, float x, float y, float z) {

    angle += O.Olf; device.Transform.World = Matrix.RotationYawPitchRoil(yaw, pitch, roll) *

    Matrix.Translation(x, y, z); mesh.DrawSubset(0);

    i

  • 5. Mesh 93

    , DrawIndexedPrimitives DrawSubset. , Mesh ( Mesh.Box), .

    ,

    . , ? . , , , ,

    . ,

    Mesh ( VertexFormat), , .

    Mesh , , .

    , 1 ( Direct3D) , , ,

    ,

    , .

    ,

    , false, true.

    , .

    , .

    .

    . ,

    (, , , .), .

    . ,

    , .

    , .

    ,

    :

    device.RenderState.Ambient = Color.Red;

    .

    , .

    , , , ,

    . ?

  • 94 I.

    , ,

    ? ( , Mesh) . .

    Direct3D ,

    , ,

    . ,

    ,

    . , ( , ) .

    Direct3D . ,

    ,

    ( ), . DrawBox ( DrawSubset):

    Material boxMaterial = new Material!); boxMaterial.Ambient = Color.White; boxMaterial.Diffuse = Color.White; device.Material = boxMaterial;

    , .

    , ,

    .

    , Direct3D , .

    ,

    .

    ,

    . ,

    ,

    , .

    ,

    .

    ,

    . -

  • 5. sh- 95

    ,

    . , ,

    . ,

    ,

    , .5.1.

    . 5 . 1 . () ,

    . ,

    , :

    device.Lights[0]. = LightType.Directional; device.Lights[0].Diffuse = Color.DarkBlue; device.Lights[0].Direction = new Vector3(0, -1, -1); device.Lights[0].Commit (); device.Lights[0].Enabled = true;

    - ,

    .

    -

    . ,

    ,

    ( ), .5.2.

  • 96 I.

    . 5.2.

    ,

    Mesh-. ( Mesh- ):

    mesh = Mesh.Box(device, 2.Of, 2.Of, 2. Of);

    , .

    Width - X Height - Y Depth - Z

    mesh = Mesh.Cylinder(device, 2.Of, 2.Of, 2.Of, 36, 36);

    ,

    .

    Radiusl Z. O.Of.

    Radius2 Z. O.Of.

  • 5. sh- 97

    Length Z. Slices (slices) (

    ). Stacks (

    ).

    mesh = Mesh.Polygon(device, 2.Of, 8);

    ,

    .

    Length . Sides .

    mesh = Mesh.Sphere(device, 2.Of, 36, 36);

    , .

    Radius . Slices (slices) (

    ). Stacks (

    ).

    mesh = Mesh.Torus(device, 0.5f, 2.Of, 36, 18);

    , .

    InnerRadius .

    OutterRadius .

    Sides . .

    Rings . .

    mesh = Mesh.Teapot(device) ;

    ,

    , .5.3.

    - . 604

  • 98 I.

    . 5.3. Mesh-

    ,

    ,

    Mesh-.

    Mesh-

    ,

    . Mesh- ,

    .

    .X, !

    .X DirectX SDK ( CD )

    .

    ,

    .

  • 5. Mesh"- 99

    , .

    , Mesh-. ,

    . Mesh- .

    .

    (- High Level Shader Language, HLSL), Mesh- . HLSL , .

    Mesh-, ,

    Mesh-, .

    Mesh.FromFile ( ) Mesh.FromStream ( ). ,

    . :

    public static Microsoft.DirectX.Direct3D.Mesh FromFile ( System.String filename , Microsoft.DirectX.Direct3D.MeshFlags options , Microsoft.DirectX.Direct3D.Device device , Microsoft.DirectX.Direct3D.GraphicsStream adjacency , out Microsoft.DirectX.Direct3D.ExtendedMaterial[] materials , Microsoft.DirectX.Direct3D.EffectInstance effects )

    public static Microsoft.DirectX.Direct3D.Mesh FromStream ( System.10.Stream stream , System.Int32 readBytes , Microsoft.DirectX.Direct3D.MeshFlags options , Microsoft.DirectX.Direct3D.Device device , Microsoft.DirectX.Direct3D.GraphicsStream adjacency , out Microsoft.DirectX.Direct3D.ExtendedMaterial[] materials , Microsoft.DirectX.Direct3D.EffectInstance effects )

    (filename stream), Mesh-. Mesh-.

    , .

    readBytes. MeshFlags , .

    , . 5.1.

  • 100 I.

    MeshFlags .DoNotClip

    MeshFlags.Dynamic

    MeshFlags.IbDynamic

    MeshFlags.IbManaged

    MeshFlags.IbSoftwareProcessing

    MeshFlags.IbSystemMem

    MeshFlags.lbWriteOnly

    MeshFlags.VbDynamic

    MeshFlags. VbManaged

    MeshFlags.VbSoftwareProcessing

    MeshFlags. VbSystemMem

    MeshFlags. VbWriteOnly

    MeshFlags.Managed

    MeshFlags .Npatches

    5.1. MeshFlags

    Usage.DoNotClip

    IbDynamic VbDynamic

    Usage.Dynamic

    Pool.Managed

    Usage.SoftwareProcessing

    Pool.SystemMemory

    Usage.WriteOnly

    Usage.Dynamic

    Pool.Managed

    Usage. SoftwareProcessing

    Pool.SystemMemory

    Usage.WriteOnly

    IbManaged VbManaged

    Usage.NPatches .

    Mesh N-Patch

    MeshFlags.Points Usage.Points

  • 5. Meshw- 101

    MeshFlags.RtPatches

    MeshFlags.SoftwareProcessing

    MeshFlags.SystemMemory

    MeshFlags.Use32Bit

    MeshFlags.UseHardwareOnly

    Usage.RtPatches

    IbSoftwareProcessing VbSoftwareProcessing

    IbSystemMem VbSystemMem

    32- . ,

    device , Mesh-. , .

    adjacency , ,

    .

    ,

    Mesh-.

    GRAPHICSSTREAM ,

    Mesh-, GraphicsStream.

    :

    int[] adjency = adjBuffer.Read(typeaf(int), mesh.NumberFaces * 3);

    ,

    , ,

    GraphicsStream.

    materials , ,

  • 102 I.

    Mesh. ExtendedMaterial Direct3D , string, .

    . ,

    ,

    .

    , Effectlnstance HLSL , Mesh-.

    , .

    ,

    .

    , ,

    , Mesh-. , ,

    . .

    ,

    ,

    Mesh. Mesh-:

    private Material[] meshMaterials; private Texture [] meshTextures;

    ,

    , ,

    . ,

    Mesh- ( LoadMesh), . 5.1:

    5.1. Mesh .

    private void LoadMesh(string file) {

    ExtendedMaterial[] mtrl; // Load our mesh mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl); // If we have any materials, store them if ((mtrl != null) && (mtrl.Length > 0)) {

    meshMaterials = new Material[mtrl.Length]; meshTextures = new Texture[mtrl.Length]; // Store each material and texture for (int i = 0; i < mtrl.Length; i++) {

  • 5. Mesh-o6beKTOB 103

    meshMaterials[i] = mtrl[i] .Material3D; if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename !=

    string.Empty)) {

    // We have a texture, try to load it meshTextures[i] = TextureLoader.FromFile(device, @"..\..\" +

    mtrl[i].TextureFilename); }

    } }

    ExtendedMaterial, Mesh-. FromFile . HLSL-, .

    Mesh- .

    , , ,

    ,

    .

    ExtendedMaterial . ,

    , TextureLoa-der.FromFile. , , ,

    System.Drawing.Bitmap, .

    Mesh- :

    private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z) {

    angle += O.Olf; device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z); for (int i = 0; i < meshMaterials.Length; i++) {

    device.Material = meshMaterials [i]; device.SetTexture(0, meshTextures[i]); mesh.DrawSubset(i);

  • 104 I.

    ,

    , DrawBox. , Mesh-, :

    1. . 2.

    . ,

    .

    3. DrawSubset .

    , , Mesh- . Mesh- tiny.x CD , DirectX SDK. :

    // Load our mesh LoadMesh(@"..\..\tiny.x");

    ,

    . ,

    .

    :

    device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, l.Of, 10000.Of);

    device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 580.Of), new Vector3(), new Vector3(0,1,0));

    ,

    . DrawMesh:

    DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.Of, angle / (float)Math.PI / 4.Of, O.Of, O.Of, O.Of);

    ,

    .5.4.

  • 5. Mesh- 1*5

    . 5.4 Mesh-,

    , ,

    .

    Mesh-, .

    Mesh .

    Mesh . Mesh .

    -

    Mesh- .

  • 106 I.

    6. DirectX

    ,

    ,

    , .

    , .

    , ,

    . ,

    , - .

    , .

    MS DOS , Donkey, , , , .

    ,

    .

    , .

    Dodger. ,

    . , ? , ,

    . , ,

    ,

    . ,

    ,

    .

    ,

    ,

    ,

    .

    , ,

    , ,

    .

    ,

    ( , ). , , .

    UML, .

  • 6. DirectX 107

    ,

    ,

    - .

    ,

    .

    .

    Visual Studio . # Windows Application Dodger. , Forml. Forml DodgerGame,

    . DirectX, ,

    using-. :

    public DodgerGame() (

    this.Size = new Size(800,600); this.Text = "Dodger Game"; this.SetStyle(ControlStyles.AllPaintinglnWmPaint | ControlStyles.Opaque, true);

    }

    800x600, .

    , Main 6.1.

    6.1. . static void Main() {

    using (DodgerGame frm = new DodgerGame!)) {

    // Show our form and initialize our graphics engine frm.Show(); frm.InitializeGraphics(); Application.Run(frm);

    } }

  • 108 I.

    , ,

    . Windows, , , .

    , InitializeGraphics, , .

    , 6.2.

    6.2. .

    /// /// We will initialize our graphics device here /// public void InitializeGraphics!) (

    // Set our presentation parameters PresentParameters presentParams = new PresentParametersO; presentParams.Windowed = true; presentParams.SwapEffeet = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; // Store the default adapter int adapterOrdinal = Manager.Adapters.Default.Adapter; CreateFlags flags = CreateFlags.SoftwareVertexProcessing; // Check to see if we can use a pure hardware device Caps caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware); // Do we support hardware vertex processing? if (caps.DeviceCaps.SupportsHardwareTransformAndLight)

    // Replace the software vertex processing flags = CreateFlags.HardwareVertexProcessing;

    // Do we support a pure device? if (caps.DeviceCaps.SupportsPureDevice)

    flags ] = CreateFlags.PureDevice; // Create our device device = new Device(adapterOrdinal, DeviceType.Hardware, this, flags, presentParams) ; // Hook the device reset event device.DeviceReset += new EventHandler(this.OnDeviceReset); this.OnDeviceReset(device, null);

    ,

    , , .

    ( ) ,

    .

  • 6. DirectX 109

    .

    ,

    ,

    ? , , . .

    , ,

    . 2, .

    , , DeviceCaps,

    .

    ,

    , ,

    : true, , , .

    ,

    . ,

    , hardware vertex processing . ,

    ( ); , OR (), .

    , ,

    , .

    , ,

    ,

    .

    ,

    ,

    . , ,

    .

    .

    (event handler method) 6.3, .

    6.3.

    private void OnDeviceReset(object sender, EventArgs e) (

    device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, l.Of, 1000.Of);

  • 110 I.

    device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 9.5f, 17.Of), new Vector3(), new Vector3 (0,1,0));

    // Do we have enough support for lights? if ((device.DeviceCaps.VertexProcessingCaps.SupportsDirectionalLights) &&

    ((unit)device.DeviceCaps.MaxActiveLights > 1)) (

    // First light device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White; device.Lights[0].Direction = new Vector3(l, -1, -1); device.Lights[0]. Commit (); device.Lights[0].Enabled = true; // Second light device.Lights[1].Type = LightType.Directional; device.Lights[l].Diffuse = Color.White; device.Lights[1].Direction = new Vector3(-l, 1, -1); device. Lights [ 1 ]. Commit () ; device.Lights[l].Enabled = true;

    1 else (

    // Hmm.. no light support, let's just use // ambient light device.RenderState.Ambient = Color.White;

    1

    .

    , .

    ,

    ( ).

    ,

    , ,

    . ,

    , ,

    Properties, , .

    ,

    ,

    . , , ,

    .

    OnPaint , . :

  • 6. DirectX 111

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {

    device.Clear(ClearFlags.Target \ ClearFlags.ZBuffer, Color.Black, l.Of, 0); device.BeginScene(); device.EndScene(); device.Present(); this.Invalidated ;

    }

    ,

    .

    ,

    , , .

    , .

    ( ) ,

    .

    ,

    .

    :

    // Do we have enough support for lights? if ((device.DeviceCaps.VertexProcessingCaps.SupportsDirectionalLights) &&

    ((unit)device.DeviceCaps.MaxActiveLights > 0)) (

    II First light device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White; device.Lights[0].Direction = new

    Vector3(l, -1, -1); device.Lights[0].Commit(); device.Lights[0].Enabled = true; if ((unit)device.DeviceCaps.MaxActiveLights > 1)) { // Second light

    device.Lights[l].Type = LightType. Directional; device.Lights[l].Diffuse = Color.White; device.Lights[1].Direction = new Vector3(-l, 1, -1); device.Lights[1].Commit () ; device.Lights[1].Enabled = true; } }

  • 112 I.

    OnPaint. , .

    road. CD , .X, road, road mesh:

    // Game board mesh information private Mesh roadMesh = null; private Material[] roadMaterials = null; private Texture[] roadTextures = null;

    Mesh-, .

    , ,

    ,

    level, . , . 6.4.

    6.4. Mesh-.

    public static Mesh LoadMesh(Device device, string file, ref Material!] meshMaterials, ref Texture[] meshTextures)

    { ExtendedMaterial[] mtrl; // Load our mesh Mesh tempMesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl); // If we have any materials, store them if ((mtrl != null) && (mtrl.Length > 0)) {

    meshMaterials = new Material[mtrl.Length]; meshTextures = new Texture[mtrl.Length]; // Store each material and texture for (int i = 0; i < mtrl.Length; i++) (

    meshMaterials [i] = mtrl[i].Material3D; if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename ! =

    string.Empty)) (

    // We have a texture, try to load it meshTextures[i] = TextureLoader.FromFile(device, @"..\..\" + mtrl[i].TextureFilename);

    } }

  • 6. DirectX 113

    return tempMesh; }

    ,

    . ,

    road, ,

    :

    // Create our road mesh roadMesh = LoadMesh(device, @"..\..\road.x", ref roadMaterials, ref roadTextures) ;

    ,

    . Mesh road, , , . ,

    , .

    :

    private void DrawRoad(float x, float y, float z) (

    device.Transform.World = Matrix.Translation(x, y, z); for (int i = 0; i < roadMaterials.Length; i++) (

    device.Material = roadMaterials[i]; device.SetTexture (0, roadTextures [i]) ; roadMesh.DrawSubset(i);

    ,

    Mesh-. , .

    ,

    ( : ). , ,

    .

    , , . ,

    ,

    , , .

    , .

    : , , ,

  • 114 I.

    ,

    .

    ,

    .

    , ,

    . level :

    // Constant values for the locations public const float RoadLocationLeft = 2.5f; public const float RoadLocationRight = -2.5f; private const float RoadSize = 100.Of; private const float MaximumRoadSpeed = 250.Of; private const float RoadSpeedlncrement = 0.5f; // Depth locations of the two 'road' meshes we will draw private float RoadDepthO = O.Of; private float RoadDepthl = -100.Of; private float RoadSpeed = 30.Of;

    Mesh- road, . 100 , 10 . ,

    .

    .

    250 , .

    ,

    . ,

    ( , ). , ,

    , .

    , DrawRoad BeginScene:

    // Draw the two cycling roads DrawRoad(O.Of, O.Of, RoadDepthO); DrawRoad(O.Of, O.Of, RoadDepthl);

    , , ,

    ,

    . ,

    Direct3D .

  • 6. DirectX 115

    , . -

    ,

    .

    , (Point Filter),

    . .

    , ,

    . ,

    , ,

    , . OnDeviceReset , 6.5.

    6.5. .

    // Try to set up a texture minify filter, pick anisotropic first if (device.DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic) {

    device.SamplerState[0].MinFilter = TextureFilter.Anisotropic; } else if (device.DeviceCaps.TextureFilterCaps.SupportsMinifyLinear) {

    device.SamplerState[0].MinFilter = TextureFilter.Linear; } // Do the same thing for magnify filter if (device.DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic) {

    device.SamplerState[0].MagFilter = TextureFilter.Anisotropic; } else if (device.DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear) {

    device.SamplerState[0].MagFilter = TextureFilter.Linear; }

    , ,

    . ,

    . , ,

    .

    , -

    .

    ,

    .

    , .

    ,

    ,

  • 116 I.

    .

    OnPaint ( Clear):

    // Before this render, we should update any state OnFrameUpdate ();

    ,

    6.6.

    6.6. .

    private void OnFrameUpdate () {

    // First, get the elapsed time elapsedTime = Utility.TimerjDirectXTimer.GetElapsedlime); RoadDepthO += (RoadSpeed * elapsedTime); RoadDepthl += (RoadSpeed * elapsedTime); // Check to see if we need to cycle the road if (RoadDepthO > 75.Of) {

    RoadDepthO = RoadDepthl - 100.Of;

    }

    if (RoadDepthl > 75.Of)

    {

    RoadDepthl = RoadDepthO - 100.Of; } ,

    , , ,

    , .

    elapsed time ,

    .

    , ( ) ( ), .

    elapsedTime : private float elapsedTime = O.Of;

  • 6. DirectX 117

    ? ,

    . ,

    ? , ,

    . ,

    , , .

    , ,

    . , ,

    60 , . ,

    40 , , , 80 ; . ,

    ,

    ,

    .

    ,

    . ,

    250 . , ,

    . .NET Runtime ( ),

    , ,

    ,

    , , 15 . , ( 60 ) .

    DirectX SDK , DirectXTimer, , ,

    ( 1 ). ,

    .

    (DirectXTimer) . ,

    .

  • 118 I.

    ,

    , ,

    ( ). ,

    Main class, , , .

    , ,

    .

    Add New Item , . ,

    DirectX , .

    ? ,

    , -,

    . ,

    :

    // Car constants public const float Height = 2.5f; public const float Depth = 3.0f; public const float Speedlncrement = 0.If; private const float Scale = 0.85f; // Car data information private float carLocation = DodgerGame.RoadLocationLeft; private float carDiameter; private float carSpeed = 10.Of; private bool movingLeft = false; private bool movingRight = false; // Our car mesh information private Mesh carMesh = null; private Material[] carMaterials = null; private Texture[] carlextures = null;

    ,

    . Height Depth ( -). Speedlncrement . Scale .

  • 6. DirectX 119

    r .

    ,

    . ,

    .

    ( , ). , , ,

    (-), .X Mesh. Mesh ( )

    r.

    , . 6.7.

    6.7. r

    /// /// Create a new car device, and load the mesh data for it /// /// D3D device to use public Car(Device device) {

    // Create our car mesh carMesh = DodgerGame.LoadMesh(device, @"..\..\car.x", ref carMaterials,

    ref carTextures); // We need to calculate a bounding sphere for our car VertexBuffer vb = carMesh.VertexBuffer; try {

    // We need to lock the entire buffer to calculate this GraphicsStream stm = vb.Lock(0, 0, LockFlags.None) ; Vector3 center; // We won't use the center, but it's required float radius = Geometry.ComputeBoundingSphere(stm,

    carMesh.NumberVertices, carMesh.VertexFormat, out center); // All we care about is the diameter. Store that carDiameter = (radius * 2) * Scale;

    } finally {

    // No matter what, make sure we unlock and dispose this vertex // buffer. vb.Unlock(); vb.Disposed;

    } }

  • 120 I.

    Mesh , , Mesh- road, . .

    (, Mesh ) . geometry , .

    , ,

    Mesh-. , , ,

    Mesh-. , Lock method, .

    , ,

    .

    ComputeBoundingSphere, Mesh- .

    , . ( finally), , , , .

    ,

    .

    , ,

    .

    DrawRoad, , .

    :

    /// /// Render the car given the current properties /// /// The device used to render the car public void Draw(Device device) {

    // The car is a little bit too big, scale it down device.Transform.World = Matrix.Scaling(Scale,

    Scale, Scale) * Matrix.Translation(carLocation, Height, Depth); for (int i = 0; i < carMaterials.Length; i++) {

    device.Material = carMaterials[i]; device.SetTexture(0, carTextures[i]); carMesh.DrawSubset(i);

    } }

  • 6. DirectX 121

    r,

    , .

    r:

    // Public properties for car data public float Location

    get ( return carLocation; } set ( carLocation = value; }

    public float Diameter

    get ( return carDiameter; )

    public float Speed

    get ( return carSpeed; ( set ( carSpeed = value; 1

    public bool IsMovingLeft

    get ( return movingLeft; } set ( movingLeft = value; I

    public bool IsMovingRight

    get { return movingRight; 1 set ( movingRight = value; }

    . -

    DodgerGame:

    // Car private Car car = null;

    , r

    .

    OnDeviceReset. Mesh- road , r :

    // Create our car car = new Car (device);

  • 122 I.

    r

    , .

    DrawRoad OnPaint r:

    // Draw the current location of the car car.Draw(device);

    , .

    . ,

    .

    .

    OnKeyDown DodgerGame, 6.8.-

    6.8. .

    /// /// Handle key strokes /// protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e) {

    // Handle the escape key for quiting if (e.KeyCode == Keys.Escape) {

    // Close the form and return this.Closed;

    return; } // Handle left and right keys if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.NumPad4)) {

    car.IsMovingLeft = true; car.IsMovingRight = false;

    } if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.NumPad6)) {

    car.IsMovingLeft = false; car.IsMovingRight = true;

    } II Stop moving if (e.KeyCode == Keys.NumPad5) {

    car.IsMovingLeft = false; car.IsMovingRight = false;

    } }

  • 6. DirectX 123

    .

    Escape . - true , ( IsMovingLeft IsMovingRight), false. , 5 . ,

    ,

    .

    . , 6.9, .

    6.9. .

    /// /// Update the cars state based on the elapsed time /// /// Amount of time that has elapsed public void Update(float elapsedTime) (

    if (movingLeft) {

    // Move the car carLocation += (carSpeed * elapsedTime); // Is the car all the way to the left? if (carLocation >= DodgerGame.RoadLocationLeft) {

    movingLeft = false; carLocation = DodgerGame.RoadLocationLeft;

    } } if (movingRight) {

    // Move the car carLocation -= (carSpeed * elapsedTime); // Is the car all the way to the right? if (carLocation

  • 124 I.

    .

    . true, ( elapsed time). , ( ), . ,

    , ,

    OnFrameUpdate DodgerGame. :

    // Now that the road has been 'moved', update our car if it's moving car.Update(elapsedTime);

    ! D--, .

    . ,

    ( ), . ,

    . ,

    . ,

    , Obstacle (). , using DirectX .

    Mesh- ,

    , , , .

    Mesh-, , . ,

    , obstacle, . 6.10.

    6.10. Obstacle.

    // Object constants private const int NumberMeshTypes = 5; private const float ObjectLength = 3.Of; private const float ObjectRadius = ObjectLength / 2.Of; private const int ObjectStacksSlices = 18; // obstacle colors private static readonly Color [] ObstacleColors = (

    Color.Red, Color.Blue, Color.Green, Color.Bisque, Color.Cyan, Color.DarkKhaki,

  • 6. DirectX 125

    Color.OldLace, Color.PowderBlue, Color.DarkTurquoise, Color.Azure, Color.Violet, Color.Tomato, Color.Yellow, Color.Purple, Color.AliceBlue, Color.Honeydew, Color.Crimson, Color.Firebrick };

    // Mesh information private