Project triangle1

This chapter is a synoptic summary of three Direct3D-Tutorials from Microsoft: Tutorial1, Tutorial2 und Tutorial3. You find the tutorials here: C:\DXSDK\Samples\Managed\Direct3D\Tutorials.

Main Menu after starting VS 2008: File → New Project... → Visual Studio installed templates: Windows Forms Application
Name: triangle1 → Location: C:\temp → Create directory for solution: switch it off → OK
Delete the files Program.cs and Form1.Designer.cs and the content of Form1.cs, as described in the chapters 2DCisC1 to 2DCisC4.

If You find no Solution Explorer-window, open it via the main menu: View → Solution Explorer.
Inside the Solution Explorer-window click the plus-sign in front of triangle1. A tree opens. Look for the branch "References". Right-click References and left-click Add Reference.... An Add Reference dialog box opens. Scroll down to the component name: Microsoft.DirectX Version 1.0.2902.0.
Highlight this reference by a left-click and (holding the Strg-key pressed) the reference Microsoft.DirectX.Direct3D Version 1.0.2902.0 somewhere below. Quit the Add Reference dialog box with OK.
Check if both references Microsoft.DirectX and Microsoft.DirectX.Direct3D are now visible inside the Solution Explorer window underneath triangle1 → References.

If You use Visual Studio 2008 Professional You should switch off the vexatious automatic format- and indent- mechanism of the code editor before You copy the following code to Form1.cs (otherwise all the code will be reformatted into chaos):
1. Main menu of Visual Studio 2008 Professional: click menu "Tools".
2. A DropDown-menu appears. Click "Options...".
3. An Options dialog box appears.
4. Click the branch "Projects and Solutions". Click "General". Redirect all three pathes to C:\temp.
5. Click the branch "Text Editor", then click "C#".
6. A sub-tree appears with the branches "General, Tabs, Advanced, Formatting, IntelliSense".
7. Click "Tabs". Change "Indenting" to None, "Tab size" and "Indent size" to 1 and switch on the option "Insert spaces".
8. Inside the sub-tree "C#" click the plus-sign in front of "Formatting" and change all "Formatting"-branches as follows:
"General": switch off all CheckBoxes, "Indentation": switch off all CheckBoxes, "New Lines": switch off all CheckBoxes, "Spacing": switch off all CheckBoxes, "Wrapping": switch on both CheckBoxes.
9. Leave the dialog box with button "OK".

Using the VertexBuffer of the graphics card

Until now at any Timer-event triangle1 reads the triangle which has the form of the structure CustomVertex.PositionColored[3] from the main memory via the AGP- (or PCIe-) bus into the graphics card. This is no catastrophe in case of a single triangle but it is highly ineffective with komplex polygons. It is much faster to copy the polygons once into the
VertexBuffer-memory of the graphics card. Nearly all modern graphics cards have such an on board memory for polygons (dynamically allocated).
Write the following declaration into the head of public class Form1 : Form below the line static float fAngle;:

if ( vertexBuffer != null ) vertexBuffer.Dispose();//Free the old vertexBuffer if any.
//Create a new vertex buffer on the graphics card and connect it to the device.
vertexBuffer = new VertexBuffer( typeof(CustomVertex.PositionColored), 3,
device, Usage.WriteOnly, CustomVertex.PositionColored.Format,
Pool.Default );
vertexBuffer.SetData( v, 0, LockFlags.None );//Copy the vertices from main memory to graphics card memory.
device.SetStreamSource( 0, vertexBuffer, 0 );//Tell the device to use the vertexBuffer on the graphics card.

Chaos

Add the initializations of the rotating angles ax[i], ay[i], az[i] inside the loop for ( int i = 0; i < nTriangles; i++ ) at the end of the constructor public Form1() by rewriting the loop from scratch: