Open source 2D graphics library ghost research (I)

Source: Internet
Author: User

Agg ry, a powerful 2D ry library. Due to some defects of GDI + (not cross-platform, poor accuracy in some application scenarios), I have been looking for a solution that can replace the GDI + drawing component, and finally I chose idea, it provides sub-pixel precision display, superb anti-sawtooth effect, cross-platform, and exquisite code for embedded development. There are many components related to the 2D graphics library on the Internet, such as Google Picasso, Cairo (Renderer used at the bottom layer of Firefox), QT, OpenGL, and so on. From the perspective of ease of use, tracing is relatively difficult, because only the C ++ source code is provided officially, and there is no component library or encapsulated package. It is impossible for other languages to use it directly. From the comments on the Internet about the use effect, the most efficient and cross-platform is the best.

At first, I started my learning from the fancy documents. I have some well-written articles on the Internet (currently the most comprehensive) on the subject: <using idea to achieve high-quality graphic output>. If you follow the article, you will have a preliminary understanding of NLP. Refer to the above article and do it yourself. I feel a little familiar with it, or I am tired of Directly Reading the source code or English documents. Now, you can view the source code while using it. By turning over the original documentation, you will feel that learning efficiency will be higher. Because GIS Data Format Parsing, which has been specially written for a period of time, has a certain foundation (if you can refer to the idea). In the future, the idea source code is quite enjoyable, in the meantime, many data structures can also be written in GIS (for example, how to express various graphical objects in a program ). In my opinion, the pipeline code structure is very clear and elegant, and the source code uses the C ++ template as a regular statement (I am not in favor of a clever C ++ abnormal Statement ).

What kind of people are suitable for using the idea class library? A little bit of computer graphics basics, but people familiar with C ++ are suitable for getting started. In fact, the generic development basically does not use the idea class library, because too many people are engaged in application development, few people will pay attention to the drawing quality, the underlying implementation of image transformation. The drawing quality of idea is really good. There is idea net in the user list. The company's SDK product is adopted by Autodesk, and the underlying layer of This SDK is the idea graphics library. The idea class library is very used in graphics practice and is familiar with the source code and can be expanded on the basis of existing classes.

After introducing so many tips, start to learn how to use idea. Step 1: Hello world? No, we need to download the source code. Download the latest version 2.5 at http://www.antigrain.com/download/index.html. After downloading the file, decompress it directly. A bunch of. cpp and. H files are listed in the folder. Of course, our development environment is vc6.0, So we downloaded the Windows version.

1. Create a Windows console project under vc6.0. The project can be named aggtest.

2. The key step is to set the callback class library reference. The most brutal and effective way is to include. copy the H files and their folders to the Microsoft Visual Studio/vc98/include folder under the vc6 installation directory, and add the CPP files under the SRC folder in the decompressed directory to the aggtest project.

3. Copy the following code to your aggtest. cpp file.


# Include "agg_basics.h"
# Include "agg_rendering_buffer.h"
# Include "agg_rasterizer_scanline_aa.h"
# Include "agg_scanline_u.h"
# Include "agg_renderer_scanline.h"
# Include "agg_pixfmt_rgb.h"
# Include "platform/agg_platform_support.h"
# Include "agg_ellipse.h"
# Include "agg_conv_0000.h"
# Include "agg_conv_stroke.h"

# Include "agg_conv_marker.h"
# Include "agg_arrowhead.h"
# Include "agg_path_storage.h"
# Include "agg_vcgen_markers_term.h"

# Include <agg_conv_stroke.h> // conv_stroke
# Include <agg_conv_dash.h> // conv_dash
# Include <agg_conv_marker.h> // conv_marker
# Include <agg_conv_curve.h> // conv_curve
# Include <agg_conv_0000.h> // conv_contour
# Include <agg_conv_smooth_poly1.h> // conv_smooth_poly1.h
# Include <agg_conv_bspline.h> // conv_bspline
# Include <agg_conv_transform.h> // conv_transform

 

Class the_application: public role: platform_support
{
Public:
The_application (metadata: pix_format_e format, bool flip_y ):
Protocol: platform_support (format, flip_y)
{
}

Virtual void on_draw ()
{
// Rendering Buffer // memory block used to store pixel dot matrix data, which is the final image data
Expiration: rendering_buffer & rbuf = rbuf_window ();
Authorization: pixfmt_bgr24 pixf (rbuf );

// Renderers
Typedef identifier: renderer_base <identifier: pixfmt_bgr24> renderer_base_type; // underlying Renderer
Renderer_base_type renb (pixf );

// Typedef handler: renderer_scanline_aa_solid <renderer_base_type> renderer_scanline_type; // high-level Renderer
Typedef handler: renderer_scanline_bin_solid <renderer_base_type> renderer_scanline_type; // high-level Renderer

Renderer_scanline_type rensl (renb );
/*
// Vertex Source
// Vertex: ellipse ell (100,100, 50, 50); // The vertex source contains a pile of 2D vertices and corresponding commands. The vertex source is a circle.
Vertex: triangle ell (100,100, 50 );

// Coordinate conversion pipeline // coordinates the pipeline. It can transform vertices in vertex source, such as matrix transformation, contour extraction, and line conversion.
// Typedef vertex: conv_contour <vertex: ellipse> ell_cc_type; // extension Contour
Typedef direction: conv_contour <direction: triangle> ell_cc_type;
Ell_cc_type ccell (ELL );

Typedef strike: conv_stroke <ell_cc_type> ell_cc_cs_type; // only display the contour line
Ell_cc_cs_type csccell (ccell );
*/

// Vertex Source
Vertex: ellipse ell (, 50, 50); // the center of the center is in the middle.
// Coordinate conversion Pipeline
Transform: trans_affine CTX; // trans_affine is not only used for conversion of Source Vertex, but can be seen in many places in the vertex library.
CTX. Scale (0.5, 1); // the X axis is halved.
CTX. Rotate (deg: 2rad (30); // rotate for 30 degrees
CTX. Translate (100,100); // Pan 100,100
Typedef Syntax: conv_transform <Syntax: ellipse> ell_ct_type;
Ell_ct_type ctell (Ell, CTX); // matrix transformation

Typedef Syntax: conv_contour <ell_ct_type> ell_cc_type;
Ell_cc_type ccell (ctell); // contour Transformation

Typedef Syntax: conv_dash <ell_cc_type> ell_cd_type;
Ell_cd_type cdccell (ccell );
Cdccell. add_dash (5, 5 );

Typedef Syntax: conv_stroke <ell_cd_type> ell_cc_cs_type;
// Ell_cc_cs_type csccell (ccell); // convert to a multidefinition line
Ell_cc_cs_type csccell (cdccell );

// Csccell. Width (3 );

 
// Scanline Rasterizer // converts vertex data (Vector Data) into a group of horizontal scanning lines. The scanning line consists of a group of line segments (spans) contains the start position, length, and coverage (which can be understood as transparency) information. Anti-aliasing is also introduced at this moment.
Authorization: rasterizer_scanline_aa <> RAS;
Authorization: scanline_u8 SL;

// Draw
Renb. Clear (partition: rgba8 (255,255,255 ));
// Renb. clip_box (30, 30, 160,160); // you can specify a writable area.

For (INT I = 0; I <5; I ++)
{
Ccell. Width (I * 20 );
RAS. add_path (csccell );
Rensl. color (gradient: rgba8 (, I * 50 ));
// Outputs: render_scanlines (Ras, SL, rensl );
Principal: render_scanlines_aa_solid (Ras, SL, renb, principal: rgba8 (, I * 50 ));

}

 

Usage: path_storage pS;

PS. start_new_path ();
PS. move_to (200,60 );
PS. line_to (400,100 );
PS. line_to (300,140 );
PS. end_poly ();

Usage: conv_stroke <usage: path_storage, usage: vcgen_markers_term> CSPs (PS );
RAS. add_path (CSPs );
Expiration: render_scanlines_aa_solid (Ras, SL, renb, placement: rgba8 (, 0 ));
 

 

/*
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 ));

Vertex: triangle T (100,100, 50); // custom vertex Source
Usage: conv_smooth_polyw.curve <usage: triangle> cspct (t );
RAS. add_path (cspct );
Expiration: render_scanlines_aa_solid (
Ras, SL, renb, serial: rgba8 (255, 0 ));

For (Int J = 0; j <20; j ++)
Pixf. blend_vline (50 + J, 20,100, numbers: rgba (J/20.0, 128 );



Rows: int8u * P = rbuf. row_ptr (20); // get the 20th rows pointer.
Memset (p, 0, rbuf. stride_abs (); // fill the whole row with 0
*/
}
};

Int agg_main (INT argc, char * argv [])
{

The_application app (usage: pix_format_bgr24, false );

App. Caption ("example. anti-aliasing Demo ");
 
If (App. INIT (600,400, rows: window_resize ))
{
Return app. Run ();
}
Return-1;

}

 

4. Compilation. Of course, it's okay, you know. Step 1: if a file (such as agg_platform_support.cpp) is not found, add it to the project. Step 2: After an error is reported, check whether there are two points that have not been modified, stdafx. H is it commented out (pure C ++); is it based on the vc6 settings. CPP settings.

/

5. Now we can finally compile the program. Congratulations, you can see the following content on the program:

The environment configuration is relatively simple. The program that can run successfully is a great encouragement to us. We will start to study the code later.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jx1228/archive/2011/05/31/6457076.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.