How to Create a Line Draw Tool Opengl
This chapter is from the book
Drawing Lines in 3D
The GL_POINTS untrained we have been using as yet is sensibly straightforward; for each apex specified, information technology draws a point. The adjacent logical gradation is to specify two vertices and draw a subscriber line between them. This is exactly what the following primitive, GL_LINES, does. The succeeding short section of inscribe draws a single line 'tween two points (0,0,0) and (50,50,50):
glBegin(GL_LINES); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(50.0f, 50.0f, 50.0f); glEnd();
Note present that two vertices specify a single primitive. For every two vertices specified, a I line is haggard. If you specialise an odd number of vertices for GL_LINES, the death vertex is just ignored. Itemisation 3.4, from the LINES sample program on the CD, shows a more mazy sample that draws a series of lines fanned around in a R-2. Each tip nominal in this sample is paired with a point on the opposite side of a traffic circle. The output from this program is shown in Figure 3.6.
Figure 3.6 Output from the LINES sample program.
Listing 3.4 Code from the Sample Program LINES That Displays a Series of Lines Fanned in a Circle
// Call only once 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)) { // Overstep half of the rope x = 50.0f*hell(angle); y = 50.0f*cos(angle); glVertex3f(x, y, z); // First termination of line // As half of the circle x = 50.0f*sin(angle + GL_PI); y = 50.0f*cos(angle + GL_PI); glVertex3f(x, y, z); // Second termination of telephone circuit } // Done drawing points glEnd(); Line Strips and Loops
The following ii OpenGL primitives build upon GL_LINES by allowing you to set apart a list of vertices through which a job is drawn. When you specify GL_LINE_STRIP, a line is careworn from one vertex to the next in a continuous section. The chase code draws two lines in the XY shave that are specific by three vertices. Shape 3.7 shows an representative.
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();
Visualise 3.7 An example of a GL_LINE_STRIP specified by three vertices.
The last line-based primitive is GL_LINE_LOOP. This rude behaves impartial like GL_LINE_STRIP, but one final line is drawn between the penultimate vertex specified and the get-go one specified. This is an easy way to draw a blinking-line figure. Figure 3.8 shows a GL_LINE_LOOP drawn exploitation the same vertices as for the GL_LINE_STRIP in Figure of speech 3.7.
Approximating Curves with Straight Lines
The POINTS sample program, shown early in Fig 3.3, showed you how to plot points along a spring-molded course. You might have been tempted to push the points closer and closer together (by setting littler values for the angle growth) to make a smooth spring-attribute curve rather of the tame points that only approximated the SHAPE. This perfectly valid procedure can move quite slowly for larger and more complex curves with thousands of points.
Figure 3.8 The equivalent vertices from Figure 3.7 used by a GL_LINE_LOOP primitive.
A better way of approximating a curve is to utilise GL_LINE_STRIP to play unite-the-dots. As the dots move closer together, a smoother breaking ball materializes without your having to specify each those points. Listing 3.5 shows the code from Listing 3.2, with GL_POINTS replaced by GL_LINE_STRIP. The output from this new platform, LSTRIPS, is shown in See 3.9. Equally you can see, the bringing close together of the curve is quite sound. You will find this handy technique just about ubiquitous among OpenGL programs.
Figure 3.9 Output from the LSTRIPS program approximating a sleek curve.
List 3.5 Code from the Sample Program LSTRIPS, Demonstrating Line Strips
// Call lonesome formerly for all remaining points glBegin(GL_LINE_STRIP); z = -50.0f; for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; tip += 0.1f) { x = 50.0f*blunder(weight); y = 50.0f*cos(lean); // Specify the spot and move the z value up a little glVertex3f(x, y, z); z += 0.5f; } // Done drawing points glEnd(); Mount the Line Width
Just as you can set different point sizes, you can also specify various line widths when draftsmanship lines aside using the glLineWidth function:
void glLineWidth(GLfloat width);
The glLineWidth function takes a single parameter that specifies the underestimate width, in pixels, of the line drawn. Just like point sizes, not all line widths are supported, and you should make sure the line width you want to specify is available. Use the following code to get the range of line widths and the smallest interval 'tween them:
GLfloat sizes[2]; // Store supported bloodline width range GLfloat step; // Store supported line width increments // Mystify supported line breadth range and step size glGetFloatv(GL_LINE_WIDTH_RANGE,sizes); glGetFloatv(GL_LINE_WIDTH_GRANULARITY,&ill-use);
Here, the sizes array will contain 2 elements that contain the smallest and largest valid value for glLineWidth. In addition, the variable step will hold out the smallest step size allowable between the line widths. The OpenGL specification requires only that one line width, 1.0, represent supported. The Microsoft execution of OpenGL allows for occupation widths from 0.5 to 10.0, with 0.125 the smallest step size.
Itemisation 3.6 shows code for a more square 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 –90 on the y-axis of rotation and climbs the y-axis 20 units for each new air. Every time IT draws a new line, it increases the line width by 1. Figure 3.10 shows the output for this program.
Figure 3.10 Demonstration of glLineWidth from the LINESW program.
Listing 3.6 Drawing Lines of Various Widths
// Titled to draw shot vacuum RenderScene(void) { GLfloat y; // Reposition for varying Y coordinate GLfloat fSizes[2]; // Line width roll metrics GLfloat fCurrSize; // Redeem stream size ... ... ... // Get line of descent size metrics and economise the smallest value glGetFloatv(GL_LINE_WIDTH_RANGE,fSizes); fCurrSize = fSizes[0]; // Step up y axis 20 units at one time for(y = -90.0f; y < 90.0f; y += 20.0f) { // Set the line width glLineWidth(fCurrSize); // Draw the line glBegin(GL_LINES); glVertex2f(-80.0f, y); glVertex2f(80.0f, y); glEnd(); // Increase the melodic phras breadth fCurrSize += 1.0f; } ... ... } Observation that we in use glVertex2f this time instead of glVertex3f to set the coordinates for the lines. As mentioned, using this technique is only a convenience because we are drawing in the XY plane, with a z value of 0. To see that you are still draught lines in three dimensions, simply use the arrow keys to twist your lines around. You easily see that wholly the lines lie connected a single plane.
Line Stippling
In addition to changing line widths, you can create lines with a speckled or dashed practice, called stippling. To use telephone circuit stippling, you must first enable stippling with a call to
glEnable(GL_LINE_STIPPLE);
Then the function glLineStipple establishes the pattern that the lines use for drawing:
vacuum glLineStipple(Shine element, GLushort pattern);
Admonisher
Any feature or ability that is enabled away a call to glEnable can be disabled past a margin call to glDisable.
The normal parametric quantity is a 16-bit value that specifies a pattern to use when drawing the lines. Each bit represents a section of the line section that is either on or murder. By default, from each one bit corresponds to a unwed pixel, simply the factor parameter serves as a multiplier factor to increase the width of the pattern. For case, scene agent to 5 causes each bit in the pattern to represent five pixels in a row that are either on or soured. Furthermore, bit 0 (the least significant bit) of the pattern is used first to specify the business. Figure 3.11 illustrates a sampling bit pattern applied to a line section.
Why Are These Patterns Backward?
You mightiness wonder why the bit pattern for stippling is in use in reverse when drawing the line. Internally, it's much faster for OpenGL to shift this pattern to the left same place each time it of necessity to bewilder the next cloak value. OpenGL was designed for high-performance nontextual matter and frequently employs standardized tricks elsewhere.
Bod 3.11 A stipple radiation diagram is used to construct a job segment.
Listing 3.7 shows a sample of using a stippling radiation pattern that is honourable a series of alternating happening and off bits (0101010101010101). This computer code is taken from the LSTIPPLE program, which draws 10 lines from the worst of the window up the y-axis to the top. Each line is stippled with the figure 0x5555, but for for each one new line, the practice multiplier factor is increased by 1. You can clearly see the effects of the widened stipple pattern in Estimate 3.12.
Figure 3.12 Output from the LSTIPPLE program.
Listing 3.7 Code from LSTIPPLE That Demonstrates the Essence of divisor on the Act Pattern
// Called to draw scene void RenderScene(empty) { GLfloat y; // Storage for varying y coordinate GLint factor = 1; // Stippling cistron GLushort form = 0x5555; // Speckle pattern ... ... // Enable Stippling glEnable(GL_LINE_STIPPLE); // Step up Y axis 20 units at a time for(y = -90.0f; y < 90.0f; y += 20.0f) { // Reset the recapitulate divisor and pattern glLineStipple(factor,pattern); // Draw the line glBegin(GL_LINES); glVertex2f(-80.0f, y); glVertex2f(80.0f, y); glEnd(); factor++; } ... ... } Just the ability to draw points and lines in 3D gives you a key set of tools for creating your possess 3D masterpiece. I wrote the commercial coating shown in Figure 3.13. Note that the OpenGL-rendered mapping is rendered entirely of solid and stippled credit line strips.
Figure 3.13 A 3D mapping rendered with solid and flecked lines.
How to Create a Line Draw Tool Opengl
Source: https://www.informit.com/articles/article.aspx?p=328646&seqNum=6
0 Response to "How to Create a Line Draw Tool Opengl"
Post a Comment