[OpenGL Basics] -- use the object-oriented method to encapsulate OpenGL functions (3) -- draw rectangles, basics opengl
What we implement today is to draw a rectangle on the Window object, and to customize the coordinates of the rectangle, the length and width, the color of the border size, whether to fill, and the color when filling.
The main idea is to first draw a border with a line, then draw a rectangle in it, and then select the transparency of the rectangle Based on the set mode of filling or not, the transparency is 1. If not displayed, the transparency is 0.
The code for the Rectangle class is as follows:
/*************************************** * ********** File Name: rectangle. h function: ************************************** * ************/# ifndef _ RECTANGLE_H _ # define _ RECTANGLE_H _ # include "Object. h "# include" Color. h "# include" OpenGL \ glut. h "class Rectangle: public Object {public: Rectangle () {init (); // default setting} Rectangle (int x, int y, int width, int height) {init (); setRectangle (x, y, width, height);} // you can specify the four sides of a rectangle. Void setRectangle (int x, int y, int width, int height) {this-> x = x; this-> y = y; this-> width = width; this-> height = height;} // set the border void setBorder (int w, Color c) {this-> borderWidth = w; this-> borderColor = c ;} // drawing function void show () {int increment = borderWidth/2; glColor3d (borderColor. r, borderColor. g, borderColor. b); // set the border color glLineWidth (borderWidth); // set the Border Width glBegin (GL_LINES); // start to draw a rectangle gl Vertex2i (x-increment, y); glVertex2i (x + width + increment, y); glVertex2i (x + width, y); glVertex2i (x + width, y + height ); glVertex2i (x + width + increment, y + height); glVertex2i (x-increment, y + height); glVertex2i (x, y + height); glVertex2i (x, y ); glEnd (); glable (GL_BLEND); // you can set glBlendFunc (GL_SRC_ALPHA, GL_ONE) in a mixed color. // you can specify the glColor4f (fillColor. r, fillColor. g, fillColor. b, // specify the color and transparency of the rectangle (mo De = FILLING? 1.0: 0.0); glRectf (x + borderWidth/2, y + borderWidth/2, // draw a rectangle x + width-borderWidth/2, y + height-borderWidth/2);} public: int x, y, width, height; // coordinate of the rectangle and length and width Color borderColor and fillColor; // border color and fill color int borderWidth; // Border Width int mode; // drawing mode private: // initialization function void init () {x = y = 0; width = height = 100; borderColor = Color (0, 0, 0); // The border Color defaults to white fillColor = Color (255,255,255 ); // The default fill color is white borderWidth = 1; // The default Border width is 1 mode = NOTFILLING;} public: // The fill mode of the rectangle is static const int FILLING = 0; // fill the rectangle static const int NOTFILLING = 1; // do not fill the rectangle}; # endif
The following is a test code:
# Include "Window. h "# include" Application. h "# include" Line. h "# include" Rectangle. h "// hide the console window # pragma comment (linker,"/subsystem: \ "windows \"/entry: \ "mainCRTStartup \" ") int main (int argc, char * argv []) {int w = 400, h = 300; Window window (string ("Hello"), 100,100, w, h); window. create (); Rectangle rect; rect. x = 10; // set the coordinate x rect. y = 10; // set y rect coordinates. width = 200; // set the rect width. height = 200; // set the height rect. borderWidth = 5; // set the Border Width rect. mode = rect. FILLING; // you can enter rect. borderColor = Color (255, 0, 0); // you can specify the fill Color window. add (& rect); // add the rectangular component Application * app = new Application (); app-> init (argc, argv) to the window object ); app-> add (window); app-> show (); delete app; return 0 ;}//*/
As follows:
The red border rectangle with a width of 5 is filled with white area