c++ - renderDisplayFunc in opengl/glut is calling myfunc more than once -


#include "gl/glut.h" #include "gl/gl.h" #include <iostream> #include <stdlib.h>  using namespace std;  #define xwidth 700  // clipping window size 700*700 #define yheight 700   void renderfunction() {  /*clear information last draw sets current clearing color use in clearing color buffers in rgba mode. */     glclearcolor(0.0, 0.0, 0.0, 0.0);     glclear(gl_color_buffer_bit);      //set line width     gllinewidth(1);      //(x,y) coordinates in pixels     glmatrixmode(gl_projection);     glloadidentity();     glortho(0, xwidth, 0, yheight, -1, 1);       //set line color     glcolor3f(1.0, 0.0, 0.0);      //random num generated     for(int i=0;i<4;i++){     int r1 = rand() % 1000;     int r2 = rand() % 1000;     int r3 = rand() % 1000;     int r4 = rand() % 1000;      //begin line coordinates     glbegin(gl_lines);         glvertex2d(r1, r2);         glvertex2d(r3,r4);     //end line coordinate     glend();     cout<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" "<<i<<endl;}      //forces issued opengl commands begin execution     glflush(); }  // driver program test above functions int main(int argc, char** argv) {      //initialize glut     glutinit(&argc, argv);     glutinitdisplaymode(glut_single);      //set output window size     glutinitwindowsize(xwidth,yheight);      //set position of output window corresponding screen     glutinitwindowposition(100,100);      //create window     glutcreatewindow("opengl - classify line among 3 classes");      //set handler functions drawing     glutdisplayfunc(renderfunction);      //start main loop     glutmainloop();     return 0; } 

when i'm executing above program, it's working fine. issue when i'm printing values of randomly generated variables r1 r2 r3 r4, being printed 8 or 12 times. means glutdisplayfunc(renderfunction); calling renderfunction more once not required.

how control behavior. want renderfunction called once.

update: want 4 lines created , 4 lines being formed when i'm printing coordinates, showing unexpected behavior mentioned above.

there couple of problems see here.

the smaller 1 indentation of loop within renderfunction bit misleading. there 4 iterations of loop, each printing random variables once.

the second 1 appear misunderstand meaning of glutdisplayfunc(renderfunction);. explained here, registers renderfunction default "display" callback glut, , also:

glut determines when display callback should triggered based on window's redisplay state.

glutmainloop call registered callbacks until program finishes. note glutmainloop never returns. program interrupted either unhandled signal or closing of main window. means if minimize , restore window, glut want repaint contents , call display callback (renderfunction) again.

since cannot control, or should not attempt control when glut calls display function, suggest make sure display function paint screen. can generate (and print) random values in main function , have random variables global accessible renderfunction. alternatively, guard execution of sampling , of printing conditional statement modifies flag upon first execution:

// @ global scope (before renderfunction) generated = false declare random variables ... // inside renderfunction  if (generated = false) {   generate rv   print rv   generated = true } display lines 

in either case, need store 16 random variables, since generate 4 random values 4 times (and use of them drawing/prnting). suggest storing them in int r[4][4];

here preferred version:

#include "gl/glut.h" #include "gl/gl.h" #include <iostream> #include <stdlib.h>  using namespace std;  #define xwidth 700  // clipping window size 700*700 #define yheight 700   int r[4][4];  void renderfunction() {    /*clear information last draw     sets current clearing color use in clearing     color buffers in rgba mode.    */   glclearcolor(0.0, 0.0, 0.0, 0.0);   glclear(gl_color_buffer_bit);    //set line width   gllinewidth(1);    //(x,y) coordinates in pixels   glmatrixmode(gl_projection);   glloadidentity();   glortho(0, xwidth, 0, yheight, -1, 1);    //set line color   glcolor3f(1.0, 0.0, 0.0);    //random num generated   for(int i=0;i<4;i++){     //begin line coordinates     glbegin(gl_lines);     glvertex2d(r[i][0], r[i][1]);     glvertex2d(r[i][2], r[i][3]);     //end line coordinate     glend();   }    //forces issued opengl commands begin execution   glflush(); }  // driver program test above functions int main(int argc, char** argv) {    //initialize glut   glutinit(&argc, argv);   glutinitdisplaymode(glut_single);    //set output window size   glutinitwindowsize(xwidth,yheight);    //set position of output window corresponding screen   glutinitwindowposition(100,100);    //create window   glutcreatewindow("opengl - classify line among 3 classes");    //set handler functions drawing   glutdisplayfunc(renderfunction);    //random num generated   for(int i=0;i<4;i++) {     r[i][0] = rand() % 1000;     r[i][1] = rand() % 1000;     r[i][2] = rand() % 1000;     r[i][3] = rand() % 1000;      cout<<r[i][0] << " " << r[i][1]<<" "<<r[i][2]<<" "<<r[i][3]<<" "<<i<<endl;   }    //start main loop   glutmainloop();   return 0; } 

edit interesting twist problem proposed in comments section original poster. paraphrase here:

if require large (10000+) number of lines , memory huge concern, can still desired behavior:random values printed once , lines not changing position every time screen gets repainted.

the answer is: yes! 1 way of keeping low memory footprint had using combination of "if" guard trick printing , generating (same) random values every time re-seeding random number generator.

#include "gl/glut.h" #include "gl/gl.h" #include <iostream> #include <stdlib.h>  using namespace std;  #define xwidth 700  // clipping window size 700*700 #define yheight 700  bool printed; int random_seed;  void renderfunction() {    /*clear information last draw     sets current clearing color use in clearing     color buffers in rgba mode.    */   glclearcolor(0.0, 0.0, 0.0, 0.0);   glclear(gl_color_buffer_bit);    //set line width   gllinewidth(1);    //(x,y) coordinates in pixels   glmatrixmode(gl_projection);   glloadidentity();   glortho(0, xwidth, 0, yheight, -1, 1);     //set line color   glcolor3f(1.0, 0.0, 0.0);     // =============================================   // set seed of random number generator   // ---------------------------------------------   srand(random_seed);   // =============================================    //random num generated   for(int i=0;i<4;i++) {     int r1 = rand() % 1000;     int r2 = rand() % 1000;     int r3 = rand() % 1000;     int r4 = rand() % 1000;      //begin line coordinates     glbegin(gl_lines);     glvertex2d(r1, r2);     glvertex2d(r3,r4);     //end line coordinate     glend();      // ***********************************************     // make sure values printed first     // time around     // ***********************************************     if (!printed) {       cout<<r1<<" "<<r2<<" "<<r3<<" "<<r4<<" "<<i<<endl;     }   }    printed = true;    //forces issued opengl commands begin execution   glflush(); }  // driver program test above functions int main(int argc, char** argv) {    //initialize glut   glutinit(&argc, argv);   glutinitdisplaymode(glut_single);    //set output window size   glutinitwindowsize(xwidth,yheight);    //set position of output window corresponding screen   glutinitwindowposition(100,100);    //create window   glutcreatewindow("opengl - classify line among 3 classes");    //set handler functions drawing   glutdisplayfunc(renderfunction);     // =======================================   // generate random seed lines   // ---------------------------------------   srand(time(0));   random_seed = rand();   // =======================================    // ==========================================   // initialize printing guard "false",   // i.e. "did not print random values yet"   // ------------------------------------------   printed = false;   // ==========================================     //start main loop   glutmainloop();   return 0; } 

note method very similar code in original question, added code delimited using comments. side uses constant amount of memory regardless of number of lines want draw. down side calling rand() bit expensive, usual trade-off between high speed (the former version) , low memory consumption (this latter version).


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -