Computer Graphics (ii) Output elements _19_ display window reset function

Source: Internet
Author: User
Tags gety

OpenGL display window re-shaping function
In the introductory OpenGL program, we discussed the function of setting up the initial display window. However, after you create a drawing, you often need to drag the display window to another location on the screen or change its shape with the mouse. Changing the size of the display window may change its aspect ratio and cause the shape of the object to change.
to allow responses to changes in the size of the display window, the Glut library provides the following functions:
Glutreshapefunc (WINRESHAPEFCN);
This function can be placed with other glut functions in the main procedure of the program, which is activated immediately after the display window size is entered. The variable for the GLUT function is the name of the procedure that accepts the new window width and height. We can then use the new dimensions to reset the projection parameters and do any other things, including changing the display window color. In addition, we can save the width and height of the program to otherprocess to use.

As an example, the following procedure shows how to construct the WINRESHAPEFCN process. The command glloadidentity is contained in the Reset function, so that any preceding projection parameter values do not work for the new projection settings. The program shows the rules of the discussion hexagon. Although the Hexagon Center (at the center of the circle) in this example is described with the concept of a display window parameter, the position of the hexagon is not affected by any change in the size of the display window. This is because the hexagon is defined in the display table, and only the initial center coordinates are stored in the table. If you want to change the position of the hexagon when changing the display window size, you need to use a different method to define the hexagon or change the coordinate reference of the display window. Figure 3.64 shows the output of the program.


#include <GL/glut.h> #include <math.h> #include <stdlib.h>const double two_pi = 6.2831853;/* Intial Display-window Size.*/glsizei winwidth = n, winheight = 400;  Gluint reghex;class screenpt{private:glint x,y;public:/* Default constructor:initializes coordinate position to (0, 0). */SCREENPT () {x = y = 0;}   void Setcoords (Glint Xcoord, Glint ycoord) {x = Xcoord; y = Ycoord;} Glint Getx () const {return x;} Glint Gety () const {return y;}}; static void init (void) {SCREENPT Hexvertex, circctr; Gldouble Theta; Glint k;/* Set Circle Center coordinates.*/circctr.setcoords (WINWIDTH/2, WINHEIGHT/2); Glclearcolor (1.0, 1.0, 1.0, 0.0) ;//display-window color = white./* Set up a Display list for a red regular hexagon. * Vertices for the hexagon is Six equally spaced * points around the circumference of a circle. */Reghex = glgenlists (1); Get an identifier for the display list.    Glnewlist (Reghex, gl_compile); GLCOLOR3F (1.0, 0.0, 0.0); Set Fill Color for hexagon to Red.glbegin (Gl_polygon);        for (k = 0; k < 6; k++) {theta = Two_pi * k/6.0;            Hexvertex.setcoords (CIRCCTR.GETX () + * * cos (theta), circctr.gety () + * sin (theta));    Glvertex2i (Hexvertex.getx (), hexvertex.gety ()); } glend (); Glendlist ();} void Reghexagon (void) {glclear (gl_color_buffer_bit); glcalllist (Reghex); Glflush ();} void WINRESHAPEFCN (int newwidth, int newheight) {glmatrixmode (gl_projection); glloadidentity (); gluortho2d (0.0, (GLd ouble) Newwidth, 0.0, (gldouble) newheight); Glclear (gl_color_buffer_bit);} void Main (int argc, char** argv) {glutinit (&AMP;ARGC, argv); Glutinitdispaymode (Glut_single | Glutinitwindowposition (Glut_rgb), Glutinitwindowsize (Winwidth, winheight); Glutcreatewinow (" Reshape-function & Display-list Example "); init (); Glutdisplayfunc (Reghexagon); Glutreshapefunc (WINRESHAPEFCN); Glut Mainloop ();}


Computer Graphics (ii) Output elements _19_ display window reset function

Related Article

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.