The realization of the rubber band technology in OpenGL

Source: Internet
Author: User

To modify add-ons:

__windows-C + + in

Opengl32.lib Glu32.lib GLAUX.LIB in linker

The point of experimental attention:

The key to the rubber band technique is that the control pattern is constantly changing as the user moves (mouse movement), and the original graphics need to be erased at the same time to generate new graphics. There are two ways to achieve rubber band technology:

One is to use the color of the different or operation, the original shape is not erased, but in the drawing of an identical line segment and the original graphic to be different or operation, the original figure will disappear from the screen, the other is the use of double-cache

Techniques, drawing graphics are plotted separately into two caches, alternating display. Here we use dual-cache technology to achieve rubber band technology.

Frequently used functions in the program:

1/mouse function, the mouse in the window range of key press or release time for processing: Glutmousefunc (Mouseplot); where Mousepot function is the mouse response function,

void Mouseplot (Glint button,glint action,glint xmouse,glint ymouse);

2/handling the registration function for mouse movement:

The registration function that moves within the window when one or more mouse buttons are pressed: Glutmotionfunc (MouseMove);

The registration function that moves within the window when the mouse button is not pressed: Glutpassivemotionfunc (passivemousemove);

void Mousemove (Glint xmouse,glint ymouse); void Passivemousemove (Glint xmouse,glint ymouse);

3/Specify window Double cache: Glutinitdisplaymode (glut_double| GLUT_RGB);

4/Determine the height of the window to solve the problem of coordinates, GLUTRESHAPEFUNC (changesize), when the size of the window changes is called function. The function is prototyped as void changesize (int w,int h);

W and H indicate the width and height of the window after the change. Glviewport (0,0,w,h); re-specify the display area of the window.

The procedures and comments are as follows:

#include"gl/glut.h"intipointnum=0;//the number of points that have been determinedintx1=0, x2=0, y1=0, y2=0;//determine the point coordinatesintWinwidth= -, winheight= -;//width and height of the windowvoidInitial (void) {Glclearcolor (1.0f,1.0f,1.0f,1.0f);//set the background color of a window}voidChangesize (intWinth) {Winwidth=w;winheight=h;//Save the size of the current windowGlviewport (0,0, w,h);//Specify window display areaGlmatrixmode (gl_projection);//Specify set projection parametersGlloadidentity ();//Call the unit matrix to remove the previous projection parameter settingsGluortho2d (0.0, Winwidth,0.0, winheight);//Set Projection parameters}voidDisplay (void) {glclear (gl_color_buffer_bit);//fills the window with the current background colorGLCOLOR3F (1.0f,0.0f,0.0f);//Specify the current drawing color    if(ipointnum>=1) {glbegin (gl_lines);//Draw Straight Segmentsglvertex2i (x1,y1);        Glvertex2i (X2,Y2);    Glend (); } glutswapbuffers ();//Swap buffers}voidMouseplot (Glint button,glint action,glint xmouse,glint ymouse) {if(button==glut_left_button&&action==Glut_down) {        if(ipointnum==0|| ipointnum==2) {Ipointnum=1; X1=xmouse;y1=winheight-ymouse;//determine the first endpoint of a straight line segment        }        Else{ipointnum=2; X2=xmouse;y2=winheight-ymouse;//determine the second endpoint of a straight line segmentGlutpostredisplay ();//Specify window Redraw        }    }    if(button==glut_right_button&&action==Glut_down) {Ipointnum=0;    Glutpostredisplay (); }}voidpassivemousemove (Glint xmouse,glint ymouse) {if(ipointnum==1) {X2=Xmouse; Y2=winheight-ymouse;//designates the current mouse position as an open endpoint of a lineGlutpostredisplay (); }}intMainintargcChar*argv[]) {Glutinit (&argc,argv); Glutinitdisplaymode (glut_double| GLUT_RGB);//using dual-cache and RGB modelsGlutinitwindowsize ( -, -);//Specify the size of the windowGlutinitwindowposition ( -, -);//specify the position of the window on the screenGlutcreatewindow ("Rubber Band Technology");    Glutdisplayfunc (Display); Glutreshapefunc (changesize);//Specify window reshape callback functionGlutmousefunc (Mouseplot);//Specify the mouse response functionGlutpassivemotionfunc (Passivemousemove);//Specify mouse movement response functionInitial (); Glutmainloop ();//start the main glut time processing cycle    return 0;}
View Code

Implementation of the rubber band technology in OpenGL

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.