Wednesday 28 August 2013

renderDisplayFunc in opengl/glut is calling myfunc more than once

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 from last draw
Sets the current clearing color for 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 as 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 is "<<i<<endl;}
//Forces previously issued OpenGL commands to begin execution
glFlush();
}
// Driver program to 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 the position of Output window corresponding to Screen
glutInitWindowPosition(100,100);
//Create the Window
glutCreateWindow("OpenGL - Classify line among three classes");
//Set handler functions for drawing
glutDisplayFunc(renderFunction);
//Start the main loop
glutMainLoop();
return 0;
}
When I'm executing the above program, it's working fine. But the issue is
that when I'm printing the values of randomly generated variables r1 r2 r3
r4, they are being printed 8 or sometimes 12 times. It means
glutDisplayFunc(renderFunction); is calling renderFunction more than once
which is not required.
How to control this behavior. I want renderFunction to be called just once.
UPDATE: I want 4 lines to be created and exactly 4 Lines are being formed
but when I'm printing the coordinates, they are showing unexpected
behavior as I mentioned above.

No comments:

Post a Comment