Skip to content Skip to sidebar Skip to footer

3d drawing of a space

This chapter is from the volume

Drawing Lines in 3D

The GL_POINTS archaic we have been using thus far is reasonably straightforward; for each vertex specified, it draws a point. The next logical step is to specify ii vertices and describe a line between them. This is exactly what the next archaic, GL_LINES, does. The following short department of lawmaking draws a unmarried line between 2 points (0,0,0) and (l,l,l):

glBegin(GL_LINES);   glVertex3f(0.0f, 0.0f, 0.0f);   glVertex3f(50.0f, 50.0f, 50.0f); glEnd();

Note here that two vertices specify a unmarried archaic. For every two vertices specified, a single line is fatigued. If you specify an odd number of vertices for GL_LINES, the concluding vertex is just ignored. Listing 3.4, from the LINES sample program on the CD, shows a more circuitous sample that draws a serial of lines fanned around in a circle. Each point specified in this sample is paired with a signal on the reverse side of a circle. The output from this program is shown in Figure 3.six.

Figure 3.6Effigy 3.half-dozen Output from the LINES sample program.

List three.4 Lawmaking from the Sample Plan LINES That Displays a Series of Lines Fanned in a Circle

// Call but one time for all remaining points glBegin(GL_LINES);   // All lines lie in the xy plane. z = 0.0f; for(angle = 0.0f; angle <= GL_PI; angle += (GL_PI/20.0f))   {   // Top half of the circumvolve   ten = fifty.0f*sin(bending);   y = l.0f*cos(angle);   glVertex3f(x, y, z);    // First endpoint of line    // Bottom one-half of the circle   10 = 50.0f*sin(bending + GL_PI);   y = l.0f*cos(angle + GL_PI);   glVertex3f(x, y, z);    // 2d endpoint of line   }  // Washed drawing points glEnd();

Line Strips and Loops

The side by side ii OpenGL primitives build on GL_LINES by allowing you to specify a list of vertices through which a line is drawn. When y'all specify GL_LINE_STRIP, a line is fatigued from one vertex to the side by side in a continuous segment. The post-obit code draws two lines in the xy airplane that are specified past iii vertices. Effigy 3.7 shows an case.

glBegin(GL_LINE_STRIP);   glVertex3f(0.0f, 0.0f, 0.0f);  // V0   glVertex3f(50.0f, 50.0f, 0.0f);  // V1   glVertex3f(50.0f, 100.0f, 0.0f);  // V2 glEnd();

Figure 3.7Effigy 3.seven An example of a GL_LINE_STRIP specified by iii vertices.

The final line-based primitive is GL_LINE_LOOP. This primitive behaves just like GL_LINE_STRIP, but ane last line is drawn between the terminal vertex specified and the get-go one specified. This is an like shooting fish in a barrel way to describe a closed-line figure. Effigy three.viii shows a GL_LINE_LOOP drawn using the same vertices every bit for the GL_LINE_STRIP in Figure 3.7.

Approximating Curves with Directly Lines

The POINTS sample program, shown earlier in Figure 3.3, showed you how to plot points along a spring-shaped path. You might have been tempted to push the points closer and closer together (by setting smaller values for the angle increase) to create a smooth jump-shaped curve instead of the broken points that only approximated the shape. This perfectly valid operation can motility quite slowly for larger and more circuitous curves with thousands of points.

Figure 3.8Figure 3.8 The aforementioned vertices from Figure three.vii used by a GL_LINE_LOOP archaic.

A better style of approximating a curve is to use GL_LINE_STRIP to play connect-the-dots. Every bit the dots move closer together, a smoother curve materializes without your having to specify all those points. Listing iii.5 shows the code from Listing iii.ii, with GL_POINTS replaced by GL_LINE_STRIP. The output from this new program, LSTRIPS, is shown in Effigy 3.9. As yous tin can see, the approximation of the curve is quite skilful. Y'all will find this handy technique near ubiquitous among OpenGL programs.

Figure 3.9Figure iii.9 Output from the LSTRIPS program approximating a smooth curve.

List 3.v Lawmaking from the Sample Program LSTRIPS, Demonstrating Line Strips

// Phone call only once for all remaining points glBegin(GL_LINE_STRIP);   z = -50.0f; for(angle = 0.0f; angle <= (two.0f*GL_PI)*3.0f; angle += 0.1f)   {   10 = 50.0f*sin(angle);   y = 50.0f*cos(angle);    // Specify the point and move the z value upwards a little    glVertex3f(10, y, z);   z += 0.5f;   }  // Done drawing points glEnd();

Setting the Line Width

Just every bit you tin ready different signal sizes, yous can as well specify various line widths when drawing lines by using the glLineWidth function:

void glLineWidth(GLfloat          width);

The glLineWidth part takes a single parameter that specifies the approximate width, in pixels, of the line fatigued. But like betoken sizes, not all line widths are supported, and you lot should make sure the line width you want to specify is available. Use the following lawmaking to get the range of line widths and the smallest interval between them:

GLfloat sizes[2];  // Store supported line width range GLfloat step;     // Store supported line width increments   // Get supported line width range and pace size glGetFloatv(GL_LINE_WIDTH_RANGE,sizes); glGetFloatv(GL_LINE_WIDTH_GRANULARITY,&step);

Here, the sizes array will incorporate two elements that contain the smallest and largest valid value for glLineWidth. In addition, the variable step will hold the smallest step size allowable between the line widths. The OpenGL specification requires only that one line width, i.0, be supported. The Microsoft implementation of OpenGL allows for line widths from 0.5 to x.0, with 0.125 the smallest step size.

Listing 3.six shows code for a more substantial example of glLineWidth. It's from the program LINESW and draws 10 lines of varying widths. It starts at the bottom of the window at –xc on the y-axis and climbs the y-axis 20 units for each new line. Every time it draws a new line, it increases the line width by one. Figure iii.ten shows the output for this program.

Figure 3.10Figure 3.ten Demonstration of glLineWidth from the LINESW program.

List 3.6 Drawing Lines of Various Widths

// Called to describe scene void RenderScene(void)   {   GLfloat y;        // Storage for varying Y coordinate   GLfloat fSizes[2];      // Line width range metrics   GLfloat fCurrSize;      // Save current size    ...   ...   ...    // Go line size metrics and save the smallest value   glGetFloatv(GL_LINE_WIDTH_RANGE,fSizes);   fCurrSize = fSizes[0];    // Step up y axis twenty units at a time   for(y = -90.0f; y < 90.0f; y += 20.0f)     {     // Set the line width     glLineWidth(fCurrSize);      // Describe the line     glBegin(GL_LINES);       glVertex2f(-fourscore.0f, y);       glVertex2f(eighty.0f, y);     glEnd();      // Increase the line width     fCurrSize += 1.0f;     }    ...   ...   }

Notice that we used glVertex2f this time instead of glVertex3f to specify the coordinates for the lines. As mentioned, using this technique is only a convenience considering we are drawing in the xy aeroplane, with a z value of 0. To see that you lot are still drawing lines in three dimensions, merely apply the arrow keys to spin your lines around. You easily run into that all the lines prevarication on a unmarried plane.

Line Stippling

In improver to changing line widths, y'all can create lines with a dotted or dashed pattern, chosen stippling. To use line stippling, you must first enable stippling with a call to

glEnable(GL_LINE_STIPPLE);

And then the function glLineStipple establishes the pattern that the lines employ for drawing:

void glLineStipple(GLint          factor, GLushort          pattern);

Reminder

Any feature or ability that is enabled by a phone call to glEnable tin can be disabled by a telephone call to glDisable.

The design parameter is a 16-flake value that specifies a pattern to apply when drawing the lines. Each scrap represents a section of the line segment that is either on or off. By default, each fleck corresponds to a unmarried pixel, but the factor parameter serves equally a multiplier to increase the width of the pattern. For case, setting factor to v causes each scrap in the blueprint to correspond v pixels in a row that are either on or off. Furthermore, bit 0 (the least significant bit) of the blueprint is used first to specify the line. Figure 3.11 illustrates a sample scrap blueprint applied to a line segment.

Why Are These Patterns Backward?

You might wonder why the bit pattern for stippling is used in opposite when drawing the line. Internally, information technology's much faster for OpenGL to shift this blueprint to the left one identify each time it needs to get the next mask value. OpenGL was designed for loftier-performance graphics and frequently employs similar tricks elsewhere.

Figure 3.11Figure 3.11 A stipple pattern is used to construct a line segment.

List 3.7 shows a sample of using a stippling pattern that is just a series of alternating on and off $.25 (0101010101010101). This lawmaking is taken from the LSTIPPLE program, which draws x lines from the bottom of the window upwardly the y-axis to the elevation. Each line is stippled with the pattern 0x5555, but for each new line, the pattern multiplier is increased by 1. Y'all can clearly see the effects of the widened stipple design in Figure 3.12.

Figure 3.12Figure 3.12 Output from the LSTIPPLE plan.

Listing 3.seven Code from LSTIPPLE That Demonstrates the Effect of factor on the Bit Pattern

// Called to draw scene void RenderScene(void)   {   GLfloat y;      // Storage for varying y coordinate   GLint gene = 1;    // Stippling cistron   GLushort pattern = 0x5555;  // Stipple design  ... ...    // Enable Stippling   glEnable(GL_LINE_STIPPLE);    // Step up Y axis 20 units at a fourth dimension   for(y = -90.0f; y < 90.0f; y += twenty.0f)     {     // Reset the repeat factor and pattern     glLineStipple(cistron,pattern);      // Depict the line     glBegin(GL_LINES);       glVertex2f(-80.0f, y);       glVertex2f(80.0f, y);     glEnd();      factor++;     }   ...   ...   }

Just the power to describe points and lines in 3D gives you a significant set up of tools for creating your own 3D masterpiece. I wrote the commercial application shown in Figure 3.13. Note that the OpenGL-rendered map is rendered entirely of solid and stippled line strips.

Figure 3.13Figure iii.xiii A 3D map rendered with solid and stippled lines.

hillmanyousit.blogspot.com

Source: https://www.informit.com/articles/article.aspx?p=328646&seqNum=6

Post a Comment for "3d drawing of a space"