Drawing Lines

Drawing simple line types was actually covered back in Chapter 4, "More Rendering Techniques," when dealing with primitive types, as there is a primitive type for a line list or a line strip that can be easily used. However, when drawing these types of lines, you cannot have lines with varying widths, or lines that are anti-aliased (unless the whole scene is).

Depending on the type of application you are trying to write, drawing lines can be a very common occurrence, or something that you simply never have to do. However, with the ease of the Line class, it's a simple matter to draw them anytime they're necessary. To show the simplicity of drawing some lines, we will write a quick example to draw a random number of lines on the screen continuously.

Go ahead and create a new project, and get it ready for writing some Direct3D code. This includes adding the references to the assemblies, as well as including the using clauses for the namespaces. Go ahead and create a private variable for the device, and set the window style for our rendering as well. No need to put the code necessary for these items here, as we've done them quite a few times by now.

Lines can be drawn in either pre-transformed coordinates (that is, screen coordinates) or in world space. For this example, we will only care about the lines drawn in screen space, so our device will not need to have a depth buffer. Use the method found in Listing 10.1 to initialize the graphics.

Listing 10.1 Initializing Your Device
 public void InitializeGraphics() {     // Set our presentation parameters     PresentParameters presentParams = new PresentParameters();     presentParams.Windowed = true;     presentParams.SwapEffect = SwapEffect.Discard;     // Create our device     device = new Device(0, DeviceType.Hardware, this,          CreateFlags.SoftwareVertexProcessing, presentParams); } 

This code is just like we've seen before, only without the depth buffer. Now, our rendering function will be the painting override just like we've done for each example thus far:

 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {     device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);     device.BeginScene();     //Draw some lines     DrawRandomLines();     device.EndScene();     device.Present();     // Let them show for a few seconds     System.Threading.Thread.Sleep(250);     this.Invalidate(); } 

Once again, there is nothing overly spectacular here. We clear our device, begin the scene, make our call to draw some lines, end the scene, and present it. Finally, we sleep for a quarter of a second so that we can actually see the lines being drawn, and start the whole loop over again. Naturally, our draw lines method hasn't been defined yet. Use the one found in Listing 10.2.

Listing 10.2 Drawing Random Lines
 private void DrawRandomLines() {     Random r = new Random();     int numberLines = r.Next(50);     using (Line l = new Line(device))     {         for (int i = 0; i < numberLines; i++)         {             int numVectors = 0;             while (numVectors < 2)                 numVectors = r.Next(4);             Vector2[] vecs = new Vector2[numVectors];             for(int inner = 0; inner < vecs.Length; inner++)             {                 vecs[inner] = new Vector2(r.Next(this.Width),                     r.Next(this.Height));             }             // Color             Color c = Color.FromArgb(r.Next(byte.MaxValue),                 r.Next(byte.MaxValue), r.Next(byte.MaxValue));             int width = 0;             while (width == 0)                 width = r.Next(this.Width / 100);             // Set the width             l.Width = width;             // Should they be antialiased?             l.Antialias = r.Next(50) > 25 ? true : false;             // Draw the line             l.Begin();             l.Draw(vecs, c);             l.End();         }     } } 

Each time this method is called, you first decide a random number of lines to draw (up to a maximum of 50 of them). You then create a single line object to draw each line. You could create a new line object for each line, or you could just create one "global" one to use for all lines, but this way makes the code very easy to read.

Now, you randomly pick the number of points in this line, ensuring that there are at least two. After you've determined the total number of points this line will have, you can pick a random location for each point based on the start and end point based on the windows width and height. You also pick a random color for the line, and a random width. The width is also based on the window width. The anti-alias mode is then selected randomly as well. If this value ends up being false, the lines (particularly lines of greater-than-one width) will appear "jagged." If this value is true, the lines will appear much smoother.

Finally, you are actually ready to draw a line. Wrap the Draw call around the calls to Begin and End to let Direct3D know that we are drawing a line.

While you didn't actually use these options for this example, there are other properties you can use while drawing your lines. There is a GlLines Boolean property that determines whether the OpenGL-style lines should be used (the default value for this option is false). You can also use the DrawTransform method to draw lines in 3D space instead of the screen coordinates used here.

Now, you'll just need to update your main method as follows:

 static void Main() {     using (Form1 frm = new Form1())     {         // Show our form and initialize our graphics engine         frm.Show();         frm.InitializeGraphics();         Application.Run(frm);     } } 

Running this application should fill your screen with a wide array of constantly changing lines. Some will be thin, some will be thick, some will be jagged, and some will be smooth. Sometimes there will be many, other times just a few. See Figure 10.1.

Figure 10.1. Random lines across the screen.

graphics/10fig01.gif



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net