"OpenGL Basics"--encapsulating OpenGL functions with object-oriented methods (three)--drawing rectangles

Source: Internet
Author: User
Tags border color

Today's implementation is to draw a rectangle on the Window object, and you can customize the coordinates of the rectangle, the length width, the color of the border's size, whether it fills, and the color when it fills.
The main idea is to draw a border with a line, then draw a rectangle inside, and then choose the transparency of the rectangle according to the pattern of whether it is filled or not, and if it shows, the opacity is 1, and the opacity is 0.

The following is the code for the rectangle class:

/************************************************** file Name: Rectangle.h function: Draw Rectangle ********************************** *****************/#ifndef _rectangle_h_#define _rectangle_h_#include"Object.h"#include"Color.h"#include"Opengl\glut.h"Class Rectangle: PublicObject { Public:Rectangle() {init ();//Default settings} Rectangle (intXintYintWidthintHeight) {init ();    Setrectangle (x, y, width, height); }//Set four edges of the rectangle    voidSetrectangle (intXintYintWidthintHeight) { This->x = x; This->y = y; This->width = width; This->height = height; }//Set Border    voidSetBorder (intW,color c) { This->borderwidth = W; This->bordercolor = C; }//Drawing functions    voidShow () {Glcolor3d (BORDERCOLOR.R, BORDERCOLOR.G, bordercolor.b);//Set Border colorGllinewidth (BorderWidth);//Set Border widthGlbegin (Gl_line_loop);//Start Draw RectangleGlvertex2i (x, y);        Glvertex2i (x + width, y);        Glvertex2i (x + width, y + height);        Glvertex2i (x, y + height); Glend ();//Draw completeGlenable (Gl_blend);//set to blend colorsGlblendfunc (Gl_src_alpha, Gl_one);//Specify pixel algorithmglcolor4f (FILLCOLOR.R, FILLCOLOR.G, Fillcolor.b,//Specify rectangle color and transparency(mode==filling?)1.0:0.0)); GLRECTF (x + borderwidth/2, Y + borderwidth/2,//Draw RectangleX + width-borderwidth/2, Y + height-borderwidth/2); } Public:intx, y, width, height;//Rectangle coordinates and length-widthColor BorderColor, FillColor;//Border color and fill color    intBorderWidth;//Border width    intMode//Drawing modePrivate://initialization functionvoid Init() {x = y =0; width = height = -; BorderColor = Color (0,0,0);//Border color defaults to whiteFillColor = Color (255,255,255);//Fill color defaults to whiteBorderWidth =1;//Border width defaults to 1mode = notfilling; } Public://Rectangle fill modeStatic Const intFILLING =0;//Fill Rectangle    Static Const intNotfilling =1;//Do not fill rectangle}; #endif

Here is a test code:

#include "Window.h"#include "Application.h"#include "Line.h"#include "Rectangle.h"//Hide Console window#pragma COMMENT (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")intMainintARGC,Char* argv[]) {intW = -, h = -; Window window (string("Hello"), -, -, W, h);    Window.create ();                             Rectangle rect; Rect.x =Ten;//Set coordinates xRect.y =Ten;//Set coordinates yRect.width = $;//Set widthRect.height = $;//Set heightRect.borderwidth =5;//Set Border widthRect.mode = rect. FILLING;//Set to fillRect.bordercolor = Color (255,0,0);//Set Fill colorWindow.add (&rect);//Add a rectangle component to the Window objectapplication* app =NewApplication ();    App->init (argc, argv);    App->add (window);    App->show (); Delete app;return 0;}//*/

As follows:

"OpenGL Basics"--encapsulating OpenGL functions with object-oriented methods (three)--drawing rectangles

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.