(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 14020, 405]*) (*NotebookOutlinePosition[ 14734, 430]*) (* CellTagsIndexPosition[ 14690, 426]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Directx 9 tutorial 1 (Adapted from the Directx SDK)", "Title"], Cell[CellGroupData[{ Cell["\<\ Luc Barthelet Todd Gayley\ \>", "Section"], Cell["October 2005", "Subsection"] }, Open ]], Cell[CellGroupData[{ Cell["Load .NET", "Section"], Cell[BoxData[ \(<< NETLink`\)], "Input"], Cell[BoxData[ \(ReinstallNET[]; \)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Load the NET Assemblies", "Section"], Cell[BoxData[{ \(\(LoadNETAssembly["\"];\)\), "\n", \(\(LoadNETAssembly["\"];\)\), "\n", \(\(LoadNETAssembly["\"];\)\)}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Load the common NET types", "Section"], Cell["\<\ I don't think all the types here are needed for this example, but no harm \ done in loading them.\ \>", "Text"], Cell[BoxData[{ \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \), "\n", \(LoadNETType["\"]; \)}], "Input"], Cell[BoxData[ \(LoadNETType["\"]; \)], "Input"], Cell[BoxData[ \(LoadNETType["\"]; \)], \ "Input"], Cell[BoxData[ \(LoadNETType["\"]; \)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Initialize", "Section"], Cell["\<\ \t\tpublic bool InitializeGraphics() \t\t{ \t\t\ttry \t\t\t{ \t\t\t\t// Now let's setup our D3D stuff \t\t\t\tPresentParameters presentParams = new PresentParameters(); \t\t\t\tpresentParams.Windowed=true; \t\t\t\tpresentParams.SwapEffect = SwapEffect.Discard; \t\t\t\tdevice = new Device(0, DeviceType.Hardware, this, \ CreateFlags.SoftwareVertexProcessing, presentParams); \t\t\t\tthis.OnCreateDevice(device, null); \t\t\t\treturn true; \t\t\t} \t\t\tcatch (DirectXException) { return false; } \t\t} \t\tpublic void OnCreateDevice(object sender, EventArgs e) \t\t{ \t\t\tDevice dev = (Device)sender; \t\t\t// Now Create the VB \t\t\tvertexBuffer = new \ VertexBuffer(typeof(CustomVertex.TransformedColored), 3, dev, 0, \ CustomVertex.TransformedColored.Format, Pool.Default); \t\t\tvertexBuffer.Created += new \ System.EventHandler(this.OnCreateVertexBuffer); \t\t\tthis.OnCreateVertexBuffer(vertexBuffer, null); \t\t} \ \>", "Text", Background->GrayLevel[0.752941]], Cell["\<\ In the sample code they have a method called OnCreateDevice(). I don't like \ that name because OnXXX names ashould be event handler functions and this one \ is not. It is only called once (during InitializeGraphics()), so I just \ inlined its code into my InitializeGraphcs[] function.\ \>", "Text"], Cell[BoxData[ \(InitializeGraphics[] := \[IndentingNewLine]Module[{presentParams, \ presentParamsArray}, \[IndentingNewLine]presentParams\ = \ NETNew["\"]; \ \[IndentingNewLine]presentParams@Windowed = True; \[IndentingNewLine]presentParams@SwapEffect = SwapEffect`Discard; \[IndentingNewLine]presentParamsArray\ = \ MakeNETObject[{presentParams}, \ \ "\"]; \ \[IndentingNewLine]device\ = \ NETNew["\", \ 0, \ DeviceType`Hardware, \ form, \ \ CreateFlags`SoftwareVertexProcessing, \ presentParamsArray]; \[IndentingNewLine]vertexBuffer\ = \ NETNew["\", GetTypeObject[ LoadNETType["\"]], 3, device, 0, CustomVertex`TransformedColored`Format, Pool`Default]; \[IndentingNewLine]AddEventHandler[ vertexBuffer@Created, \ OnCreateVertexBuffer]; \[IndentingNewLine]OnCreateVertexBuffer[ vertexBuffer, \ Null]; \[IndentingNewLine]]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Create The vertex Buffer", "Section"], Cell["\<\ \t\tpublic void OnCreateVertexBuffer(object sender, EventArgs e) \t\t{ \t\t\tVertexBuffer vb = (VertexBuffer)sender; \t\t\tGraphicsStream stm = vb.Lock(0, 0, 0); \t\t\tCustomVertex.TransformedColored[] verts = new \ CustomVertex.TransformedColored[3]; \t\t\tverts[0].X=150;verts[0].Y=50;verts[0].Z=0.5f; verts[0].Rhw=1; \ verts[0].Color = System.Drawing.Color.Aqua.ToArgb(); \t\t\tverts[1].X=250;verts[1].Y=250;verts[1].Z=0.5f; verts[1].Rhw=1; \ verts[1].Color = System.Drawing.Color.Brown.ToArgb(); \t\t\tverts[2].X=50;verts[2].Y=250;verts[2].Z=0.5f; verts[2].Rhw=1; \ verts[2].Color = System.Drawing.Color.LightPink.ToArgb(); \t\t\tstm.Write(verts); \t\t\tvb.Unlock(); \t\t}\ \>", "Text", Background->GrayLevel[0.752941]], Cell[BoxData[ \(OnCreateVertexBuffer[sender_, \ eventArgs_]\ := \[IndentingNewLine]Module[{stm, \ verts}, \[IndentingNewLine]stm\ = \ sender@Lock[0, 0, 0]; \[IndentingNewLine]verts\ = \ Table[NETNew["\"], {3}]; \[IndentingNewLine]v\ = \ verts[\([\)\(1\)\(]\)]; \[IndentingNewLine]v@X = 150; \ v@Y = 50; v@Z = 0.5; \ v@Rhw = 1; \ v@Color = System`Drawing`Color`Aqua@ToArgb[]; \[IndentingNewLine]v\ = \ verts[\([\)\(2\)\(]\)]; \[IndentingNewLine]v@X = 250; \ v@Y = 250; v@Z = 0.5; \ v@Rhw = 1; \ v@Color = System`Drawing`Color`Brown@ToArgb[]; \[IndentingNewLine]v\ = \ verts[\([\)\(3\)\(]\)]; \[IndentingNewLine]v@X = 50; \ v@Y = 250; v@Z = 0.5; \ v@Rhw = 1; \ v@Color = System`Drawing`Color`LightPink@ToArgb[]; \[IndentingNewLine]stm@ Write[\ MakeNETObject[ verts, \ \ "\"]]; \ \[IndentingNewLine]sender@Unlock[]; \[IndentingNewLine]]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Rendering", "Section"], Cell["\<\ \tprivate void Render() \t\t{ \t\t\tif (device == null) \t\t\t\treturn; \t\t\t//Clear the backbuffer to a blue color (ARGB = 000000ff) \t\t\tdevice.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0); \t\t\t//Begin the scene \t\t\tdevice.BeginScene(); \t\t\t \t\t\tdevice.SetStreamSource( 0, vertexBuffer, 0); \t\t\tdevice.VertexFormat = CustomVertex.TransformedColored.Format; \t\t\tdevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); \t\t\t//End the scene \t\t\tdevice.EndScene(); \t\t\tdevice.Present(); \t\t} \t\t\ \>", "Text", Background->GrayLevel[0.752941]], Cell[BoxData[ \(Render[]\ := \[IndentingNewLine]\((\[IndentingNewLine]If[ device\ === \ Null, \[IndentingNewLine]Return[]\[IndentingNewLine]]; \ \[IndentingNewLine]device@ Clear[ClearFlags`Target, \ Color`Blue, \ 1.0, \ 0]; \[IndentingNewLine]device@ BeginScene[]; \[IndentingNewLine]device@ SetStreamSource[0, vertexBuffer, 0]; \n\t\tdevice@VertexFormat = CustomVertex`TransformedColored`Format; \n\t\tdevice@ DrawPrimitives[PrimitiveType`TriangleList, 0, 1]; \n\t\tdevice@ EndScene[]; \n\t\tdevice@ Present[]; \[IndentingNewLine])\)\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Handling Events", "Section"], Cell["\<\ \t\tprotected override void OnPaint(System.Windows.Forms.PaintEventArgs e) \t\t{ \t\t\tthis.Render(); // Render on painting \t\t} \t\tprotected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs \ e) \t\t{ \t\t\tif ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) \t\t\t\tthis.Close(); // Esc was pressed \t\t}\ \>", "Text", Background->GrayLevel[0.752941]], Cell[BoxData[ \(OnPaint[__]\ := \ Render[]\)], "Input"], Cell[BoxData[ \(OnKeyPress[sender_, \ eventArgs_]\ := \[IndentingNewLine]If[ eventArgs@KeyChar\ \[Equal] \ NETObjectToExpression[Keys`Escape], \ form@Close[]]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Main", "Section"], Cell[BoxData[ \(\(\(device\ = Null; \)\(\ \ \)\( (*\ Checked\ against\ Null, \ so\ initialize\ *) \)\)\)], "Input"], Cell[BoxData[{ \(form\ = \ NETNew["\"]; \), "\n", \(form@ClientSize = NETNew["\", \ 300, 300]; \), "\[IndentingNewLine]", \(form@Text\ = \ "\"; \)}], "Input"], Cell["\<\ The C# sample code doesn't add event handlers for the Paint and KeyPress \ events like we do in the next two lines. That is because the C# code creates \ its own subclass of Form and overrides the protected OnPaint and OnKeyPress \ methods. When you want to control how your class responds to events you have \ two alternative techniques: you can add event handlers for events, or \ override the default internal functions that handle those events. When you \ are writing your own subclass of a Form or Control you can use the latter \ technique, but when you are programming with existin g classes you have to \ add event handlers (I personally think adding event handlers is cleaner \ anyway).\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(AddEventHandler[form@KeyPress, \ OnKeyPress]\)], "Input"], Cell[BoxData[ InterpretationBox[\(\[LeftGuillemet] NETObject[ System . Windows . Forms . KeyPressEventHandler] \[RightGuillemet]\), NETLink`Objects`NETObject$1119807411847169]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(AddEventHandler[form@Paint, \ OnPaint]\)], "Input"], Cell[BoxData[ RowBox[{\(General::"spell"\), \(\(:\)\(\ \)\), "\<\"Possible spelling \ error: new symbol name \\\"\\!\\(Paint\\)\\\" is similar to existing symbols \ \\!\\({Point, Print}\\). \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", \ ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \ ButtonData:>\\\"General::spell\\\"]\\)\"\>"}]], "Message"], Cell[BoxData[ InterpretationBox[\(\[LeftGuillemet] NETObject[ System . Windows . Forms . PaintEventHandler] \[RightGuillemet]\), NETLink`Objects`NETObject$1119809425113089]], "Output"] }, Open ]], Cell[BoxData[ \(InitializeGraphics[]; \)], "Input"], Cell[BoxData[ \(ShowNETWindow[form]; \)], "Input"], Cell[TextData[{ "Most of the Direct3D samples, including this one, run a loop that calls \ Render(). That isn't necessary here because the Render() method just draws a \ static picture. The samples that need to call Render() repeatedly use it to \ animate an object. Because it is not necessary, I have left this loop out. \ Adding the form@Paint event handler above is the only step needed to make \ sure that the picture gets rendered when necessary.\n\nNote that I also don't \ have to call Application.DoEvents() like the sample code did. That is only \ necessary in a standalone application. When using .NET/Link from ", StyleBox["Mathematica", FontSlant->"Italic"], ", there is already an event loop running that handles dispatching events." }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(Random[]\)], "Input"], Cell[BoxData[ \(0.5887392980066567`\)], "Output"] }, Open ]], Cell[BoxData[ \(Render[]\)], "Input"] }, Open ]] }, Open ]] }, FrontEndVersion->"5.2 for Microsoft Windows", ScreenRectangle->{{0, 1600}, {0, 1113}}, AutoGeneratedPackage->None, WindowSize->{1107, 945}, WindowMargins->{{11, Automatic}, {Automatic, 45}}, ShowSelection->True, Magnification->1 ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1776, 53, 68, 0, 95, "Title"], Cell[CellGroupData[{ Cell[1869, 57, 52, 3, 99, "Section"], Cell[1924, 62, 34, 0, 38, "Subsection"] }, Open ]], Cell[CellGroupData[{ Cell[1995, 67, 28, 0, 73, "Section"], Cell[2026, 69, 44, 1, 30, "Input"], Cell[2073, 72, 49, 1, 30, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2159, 78, 42, 0, 73, "Section"], Cell[2204, 80, 223, 3, 70, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2464, 88, 44, 0, 73, "Section"], Cell[2511, 90, 121, 3, 33, "Text"], Cell[2635, 95, 1069, 15, 310, "Input"], Cell[3707, 112, 85, 1, 30, "Input"], Cell[3795, 115, 96, 2, 30, "Input"], Cell[3894, 119, 79, 1, 30, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[4010, 125, 29, 0, 73, "Section"], Cell[4042, 127, 1023, 33, 543, "Text"], Cell[5068, 162, 310, 5, 52, "Text"], Cell[5381, 169, 1281, 22, 290, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[6699, 196, 43, 0, 73, "Section"], Cell[6745, 198, 736, 18, 258, "Text"], Cell[7484, 218, 1168, 22, 270, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[8689, 245, 28, 0, 73, "Section"], Cell[8720, 247, 592, 20, 372, "Text"], Cell[9315, 269, 660, 12, 270, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10012, 286, 34, 0, 73, "Section"], Cell[10049, 288, 397, 12, 201, "Text"], Cell[10449, 302, 60, 1, 30, "Input"], Cell[10512, 305, 194, 3, 50, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10743, 313, 23, 0, 73, "Section"], Cell[10769, 315, 127, 2, 30, "Input"], Cell[10899, 319, 273, 5, 70, "Input"], Cell[11175, 326, 719, 11, 90, "Text"], Cell[CellGroupData[{ Cell[11919, 341, 77, 1, 30, "Input"], Cell[11999, 344, 230, 5, 29, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[12266, 354, 71, 1, 30, "Input"], Cell[12340, 357, 358, 5, 22, "Message"], Cell[12701, 364, 214, 4, 29, "Output"] }, Open ]], Cell[12930, 371, 55, 1, 30, "Input"], Cell[12988, 374, 54, 1, 30, "Input"], Cell[13045, 377, 769, 12, 128, "Text"], Cell[CellGroupData[{ Cell[13839, 393, 41, 1, 30, "Input"], Cell[13883, 396, 53, 1, 29, "Output"] }, Open ]], Cell[13951, 400, 41, 1, 30, "Input"] }, Open ]] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)