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

Source: Internet
Author: User
Tags polyline

Today encapsulates a line class that is responsible for drawing lines on the window written yesterday.

OpenGL drawing is achieved by setting 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: Draw End-to-end polyline, and at last connect the starting point to the endpoint, close the path


Here is the code for the line class:

/*********************************************** file Name: Line.h function: Canvas, on which you can draw points, draw lines and ellipses, rectangles ***************************** /#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, each set start point, all need to record at this time is the starting point state, if this is already the starting point//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 line termination point, forbid add void LineTo (point& p) {if (This->status = = This->line_start) {This->points.push_ If first is the terminating point) Back (p); this->status = This->line_end;return;}} Add a 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 is not to 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 draw 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 window #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;} //*/




In this paper, OpenGL's line function is used directly, and the computer graphics algorithm for drawing 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.