"OpenGL Basics"--encapsulating OpenGL functions using an object-oriented approach (II)

Source: Internet
Author: User
Tags polyline

Today, a line class is encapsulated. Responsible for drawing lines on the form that was written yesterday.

OpenGL drawing is achieved by setting a number of parameters to the Glbegin function, with three different parameters for drawing a line:

Gl_lines: Draw a segment that connects two points (the drawn endpoint is between the Glbegin function and the Glend function)

Gl_line_strip: Draw End-to-end polyline

Gl_line_loop: Draws the end-to-end polyline and connects the starting and ending points at the last. Closed path


The following is the code for the line class:

/*********************************************** file Name: Line.h function: Canvas. You can draw dots, lines and ellipses, rectangular ************************************************/#ifndef _line_h_#define _line_h_#include " Point.h "#include" Window.h "class Line:public Object {Public:line () {This->mode = this->line_mode_default;this- >status = This->line_init;} Starting point, set the starting point each time. All at the same time the record is at the start point State. If this is already the starting point//Then delete the previous starting point void MoveTo (point& p) {if (This->status = = This->line_start) {points.pop_back (); return;} Points.push_back (p); this->status = This->line_start;} Draw the end point of the line, if it starts at the end point. Forbid join void LineTo (point& p) {if (This->status = = This->line_start) {this->points.push_back (P);this-> status = This->line_end;return;}} Join the node array void addpoints (point* p,int size) {for (int i = 0; i < size; i++) {this->points.push_back (p[i]);}} Set Line color void SetColor (color& color) {this->color = color;} Set draw line mode void SetMode (int mode) {switch (mode) {case line_mode_default:mode = gl_lines; break;case Line_mode_loop:mode = GL _line_loop; Break;case Line_mode_notloop:mode = Gl_line_strip; break;} This->mode = mode;} public:static const int line_mode_loop = 0;//Set Line end-to-end static const int Line_mode_notloop = 1;//do not set line end-to-end static const int LI Ne_mode_default = 2;//Default Draw segment private://draw line State static const int line_init = 0;//initial state static const int Line_start = 1;//start Point State STA Tic const int line_end = 2;//end point State private:int mode;//Draw line mode, default feel does not connect vector<point> points;//point set int status;//draw line State color color;//Specifies the color public:void show () {//will be called by window to paint the virtual function glcolor3f (color. R, color. G, color. B); Glbegin (mode); for (int i = 0; i < points.size (); i++) {glvertex2i (Points[i]). X, Points[i]. Y);} Glend ();}}; #endif

Here is an example of a pentagram:

(Inside the window and application, point class in the blog (a))

#include "Window.h" #include "Application.h" #include "Line.h"//Hide Console form #pragma comment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "") int main (int argc, char* argv[]) {int w = N, h = 300; Window window (String ("Hello"), Window.create, W, h); Line line;//to draw the Pentagram Line.setmode (line. Line_mode_loop); Point P[5] = {Point (ten, a), point (+), point (+), point (+), point (+),};line.setcolor (Color (255, 0, 0)); line.addpoints (P, 5); Window.add (&line); application* app = new Application (), App->init (argc, argv), App->add (window), app->show ();d elete App;return 0;} //*/




This is the direct use of OpenGL's line-drawing function. The computer graphics algorithm used to draw straight line has DDA algorithm and Bresenham algorithm.


"OpenGL Basics"--encapsulating OpenGL functions using an object-oriented approach (II)

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.