c++ - Draw shape with GLUT_DOUBLE mode -


i have code:

#include <gl/glut.h> #include <stdio.h>  #define winw 1000 #define winh 500 /* ----------------------------------------------------------------------- */  bool mousedown = false;  void myinit(void)  {     glclearcolor(1.0, 1.0, 1.0, 0.0);     glcolor3f(1.0, 0.0, 0.0);     glpointsize(5.0);     glshademodel(gl_smooth);     glmatrixmode(gl_projection);     glloadidentity();     glortho(0.0, winw, winh, 0.0, -1.0, 1.0); }  void mydisplay(void)  {     glclear(gl_color_buffer_bit | gl_depth_buffer_bit);      glbegin(gl_points);     glcolor3f(1.0, 0.0, 0.0);     glvertex2i(50, 50);     glend();      //glflush();     glutswapbuffers(); } void mymouse(int button, int state, int x, int y){     if (button == glut_left_button && state == glut_down){         mousedown = true;     }     else         mousedown = false; } void mymovedmouse(int mousex, int mousey){     if (mousedown){         //printf("%d %d\n", mousex, mousey);         glbegin(gl_points);         glcolor3f(0.0, 1.0, 0.0);         glvertex2i(mousex, mousey);         glend();          //glflush();         glutswapbuffers();     } }  /* ----------------------------------------------------------------------- */  int main(int argc, char *argv[])  {     glutinit(&argc, argv);     glutinitdisplaymode(glut_double | glut_rgb);     glutinitwindowsize(winw, winh);     glutinitwindowposition(100, 150);     glutcreatewindow("computer graphic");      myinit();     glutdisplayfunc(mydisplay);     glutmousefunc(mymouse);     glutmotionfunc(mymovedmouse);      glutmainloop(); } 

i want draw free shape mouse dragging.
have try glutinitdisplaymode(glut_single | glut_rgb)and glflush(), work me.
when use glut_double , glutswapbuffers() in mymovedmouse(), screen blinking (black-white-black-white...)
new in opengl, have solution this.
help!

when using double buffering, have draw points on each redraw. have maintain list of points. steps then:

  1. on startup, create empty point list. if want nice code, define class/struct contains 2 values x , y position, , use c++ container std::vector list of points.

  2. when new point mouse input, add point point list, , call glutpostredisplay().

  3. in mydisplay() function, draw points in list.

having maintain list of points may seem add complexity compared using single buffering. go little farther, need anyway. example, if user resizes window, have able redraw points anyway. drawing points 1 one in single buffering mode not far.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -