0

I'm working on opengl c++ and tried to make a code for rendering random polygons on an terminal. I'm using CodeBlocks 13.12.

int width=800;
int height=600;

void RandomPolygons()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    GLint x[100],y[100],n,r,g,b;
    GLint i,j;
    cout<<"Enter the sides of the polygon to be displayed:"<<endl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        x[i]=rand()%800;
        cout<<"x["<<i<<"]=  "<<x[i]<<endl;
        y[i]=rand()%600;
        cout<<"y["<<i<<"]=  "<<y[i]<<endl;
    }
    x[i]=x[1];
    cout<<"x["<<i<<"]=  "<<x[i]<<endl;
    y[i]=y[1];
    cout<<"y["<<i<<"]=  "<<y[i]<<endl;

    r=rand()%2;
    g=rand()%2;
    b=rand()%2;

    glColor3f(r,g,b);
    glBegin(GL_POLYGON);
    for(j=1;j<=n;j++)
    {
        glVertex2i(x[j],y[j]);
    }
    glVertex2i(x[j],y[j]);
    glEnd();
    glFlush();
    glutSwapBuffers();
}


int main(int argc, char **argv)
{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA|GLUT_DOUBLE);
    glutInitWindowSize(width,height);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Random_Polygons!!!!");
    glClearColor(0,0,0,0);
    gluOrtho2D(0,800,0,600);
    glutDisplayFunc(RandomPolygons);
    glutIdleFunc(RandomPolygons);
    glutMainLoop();
}

output

It's just not responding(Rendering screen) and on the other hand terminal is working fine....

enter image description here

BDL
  • 21,052
  • 22
  • 49
  • 55
Fnu Avi
  • 13
  • 1
  • 6

2 Answers2

2

The std::cin blocks until the user has entered something. Since the application is blocked, the windows message loop is not executed and the window stops responding.

When you need input in a OpenGL application, you will have to listen to glutKeyboardFunc and glutSpecialFunc and construct the input from there.

BDL
  • 21,052
  • 22
  • 49
  • 55
0
  1. I agree with @BDL

    The cin blocks the processing... You should separate vertex generation and polygon rendering. Now you are calling Random polygons without predefined seed that will change the vertexes each frame making a blurry mess. Not to mention wanting keyboard input from user each frame

  2. on top of all that

    you are creating random points as vertexes which does not always create convex polygon which OpenGL can not handle properly. To avoid problems use GL_TRIANGLE_FAN instead of GL_POLYGON and sort the vertexes by angle (or generate them sorted already) for example something like:

    GLfloat x[100],y[100],a,r;
    GLint i,n=5;
    randseed(10); // init random generator with some value or do this not inside rendering instead
    for (i=0,a=0.0;i<n;a+=rand(6.28/n),i++)  
     {
     r=rand(0.5);
     x[i]=r*cos(a);
     y[i]=r*sin(a);
     }
    // render
    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(0.0,0.0);
    for (i=0;i<n;i++)  glVertex2f(x[i],y[i]);
    glVertex2f(x[0],y[0]);
    glEnd();
    

    or throw away vertexes turning your polygon into concave one.

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380