From http://www.cppprog.com/2009/0821/150.html
The last time I talked about the display principle of tracing, I gave a simple example. This article begins with every step in the workflow. In order to facilitate the comparison, you can place a chart again to display the process diagram.
In addition, the examples in the previous article are also very important, and the subsequent examples will be based on this code.
Next, we will examine every step in the pipeline display process. The best way to understand each link is to compile the experiment code. We recommend that you first create a runtime experiment environment.
Vertex Source)
<! -- Basic Attributes -->
Vertex Source)
The vertex source is an object that can generate the "vertices with commands" required by a polygon. For example, the triangle vertex source generates a vertex with the "moveTo" command, and the other two vertices with the "lineto" command and the closed "closepoly" command.
Header file
#include <agg_path_storage.h> //path_storage#include <agg_ellipse.h> // ellipse#include <agg_arc.h> // arc#include <agg_arrowhead.h> // arrowhead#include <agg_curves.h> // curve3, curve4#include <agg_gsv_text.h> // gsv_text, gsv_text_outline#include <agg_rounded_rect.h> // rounded_rect...
Type
Custom class |
All classes that implement void rewind (unsigned path_id); and unsigned vertex (double * X, double * Y. |
Ellipse |
Circle. The input is the Center Coordinate and XY axis radius. This vertex source is used in this example. |
Arc |
Arc, the input is the Center Coordinate and XY axis radius, and the start and end angles (RAD), clockwise/counterclockwise |
Curve3 |
The Bessert curve. The input is the starting point coordinate, the first control point coordinate, and the end point coordinate. |
Curve4 |
The input is the starting point coordinate, the first control point coordinate, the second control point coordinate, and the end point coordinate. |
Gsv_text |
Use the literal output (only ASCII code is supported) of the internal model, use the start_point method to specify the text position, the text method to specify the text, flip to specify whether to reverse up or down, and size to specify the text size, it is suitable for combination with conv_stroke or gsv_text_outline. |
Gsv_text_outline <> |
Convertible text. The input is gsv_text and the transformation matrix (trans_affine by default, which will be mentioned later ). Width method to set the text width |
Rounded_rect |
Rounded corner square. Enter the coordinates and radius of the rounded corner in the lower right corner of the upper left corner. |
Path_storage |
Path storage. You can use the join_path method to add multiple vertex sources. In addition, path_storage supports move_to, line_to, curve, arc_to, and other online draw functions. |
Arrowhead |
Arrow used as a marking point |
The arrowhead is quite special. It is generally used as a marking point of a line segment. The specific usage is as follows:
Arrowhead Ah; Ah. head (D1, D2, D3, D4); // defines the arrow ah. tail (D1, D2, D3, D4); // defines the arrow vertexsource vs; // other vertex sources // use the vertex converter, specify the markers type as vcgen_markers_term // The vertex converter can be conv_dash, conv_stroke, or conv_marker_adaptor. For details, see "pipeline of coordinate transformation". // vcgen_markers_term: Mark the endpoint as conv_stroke <Vertex, vcgen_markers_term> csvs ();... draw_term // use conv_marker to specify ah as the mark conv_marker of the marker point of the Line Segment <vcgen_markers_term, Arrowhead> arrow (csvs. markers (), AH); Ras. add_path (csvs); Ras. add_path (arrow); // marker should be followed... render
For the meanings of the D1, D2, D3, and D4 parameters in the ah. Head () and ah. Tail () methods, see
For example, draw a simple arrow line (based on the code here)
Add the following code at the end of the on_draw () method:
- Direction: arrowhead Ah;
- Ah. Head );
- Ah. Tail (10, 10, 5 );
- // Generate a straight line using path_storage
- Usage: path_storage pS;
- PS. move_to (160,60 );
- PS. line_to (100,100 );
- // Conversion
- Usage: conv_stroke <usage: path_storage, usage: vcgen_markers_term> CSPs (PS );
- Usage: conv_marker <usage: vcgen_markers_term, usage: arrowhead>
- Arrow (CSPs. markers (), AH );
- // Draw a line
- RAS. add_path (CSPs );
- Expiration: render_scanlines_aa_solid (Ras, SL, renb, placement: rgba8 (, 0 ));
- // Draw arrows
- RAS. add_path (arrow );
- Principal: render_scanlines_aa_solid (Ras, SL, renb, principal: rgba8 (255, 0, 0 ));
The resulting figure is:
Note:
#include "agg_conv_marker.h"#include "agg_arrowhead.h"#include "agg_path_storage.h"#include "agg_vcgen_markers_term.h"
Test code to customize a vertex source (based on the code here)
To have a deeper understanding of the vertex source, we need to implement a vertex source by ourselves. This vertex source is just a simple triangle.
As mentioned above, as long as void rewind (unsigned path_id); and unsigned vertex (double * X, double * Y) are implemented, the method can be used as the vertex source.
The rewind method indicates that the vertex source returns to the first vertex. The vertex method extracts the current vertex and moves the current vertex down. The return value is the command carried by the current vertex. The so-called command is an Enum path_commands_e class type, which is defined as follows:
- EnumPath_commands_e
- {
- Path_1__stop = 0, // ---- path_1__stop
- Path_cmd_move_to = 1, // ---- path_cmd_move_to
- Path_cmd_line_to = 2, // ---- path_cmd_line_to
- Path_cmd_curve3 = 3, // ---- path_cmd_curve3
- Path_cmd_curve4 = 4, // ---- path_1__curve4
- Path_1__curven = 5, // ---- path_1__curven
- Path_cmd_catrom = 6, // ---- path_cmd_catrom
- Path_cmd_ubspline = 7, // ---- path_cmd_ubspline
- Path_cmd_end_poly = 0x0f, // ---- path_cmd_end_poly
- Path_cmd_mask = 0x0f // ---- path_cmd_mask
- };
Path_commands_e can also be combined with path_flags_e:
- EnumPath_flags_e
- {
- Path_flags_none = 0, // ---- path_flags_none
- Path_flags_ccw = 0x10, // ---- path_flags_ccw
- Path_flags_cw = 0x20, // ---- path_flags_cw
- Path_flags_close = 0x40, // ---- path_flags_close
- Path_flags_mask = 0xf0 // ---- path_flags_mask
- };
When the command returned by vertex () contains path_1__stop, it indicates the end.
- // Equilateral triangle
- ClassTriangle {
- Public:
- Triangle (double CX, double cy, double r) // center point. r is the length from the center point to the edge.
- {
- // Prepare three points directly.
- M_step = 0;
- M_pt [0]. x = Cx; m_pt [0]. Y = cy-R;
- M_pt [1]. x = cx + R * 0.866; m_pt [1]. Y = Cy + R * 0.5;
- M_pt [2]. x = Cx-R * 0.866; m_pt [2]. Y = Cy + R * 0.5;
- // Trim uses the direction as the basis for distinguishing the interior and exterior of a polygon. You can try m_pt [1] and m_pt [2 ].
- }
- VoidRewind (unsigned)
- {
- M_step = 0;
- }
- Unsigned vertex (double * X, double * Y)
- {
- Switch(M_step ++)
- {
- Case0:
- // Step 1: move_to
- * X = m_pt [0]. X;
- * Y = m_pt [0]. Y;
- ReturnLabels: path_cmd_move_to;
- Case1:
- Case2:
- // Step 2 and step 3, line_to
- * X = m_pt [m_step-1]. X;
- * Y = m_pt [m_step-1]. Y;
- ReturnUsage: path_cmd_line_to;
- Case3:
- // Step 4: Close the Polygon
- ReturnUsage: path_cmd_end_poly | usage: path_flags_close;
- Default:
- // Step 5, end
- ReturnUsage: path_cmd_stop;
- }
- }
- Private:
- Usage: point_d m_pt [3];
- Unsigned m_step;
- };
In the on_draw () method
Vertex: ellipse ell (100,100, 50, 50); changed to Triangle ell (100,100, 50 );
Typedef partition: conv_contour <partition: ellipse> ell_cc_type; changed to typedef partition: conv_contour <triangle> ell_cc_type;
The resulting figure is:
In addition to the text output function (gsv_text can only output ASCII text), the image richness provided by the above vertex sources has exceeded the system API. The text output function will be described in separate sections.