in "display" have access to variables in main

Hi folks
I have just a very simple question. How has my code to be, so that i have access to XXX and YYY in my “display” function. I know the Code below doesn’t work, but it shows the type of my problem :wink:

Thanks for help
B


void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1.0, 1.0, 1.0);
  glBegin(GL_POLYGON);
  glVertex3f(XXX, XXX, 0.0);
  glVertex3f(YYY, XXX, 0.0);
  glVertex3f(YYY, YYY, 0.0);
  glVertex3f(XXX, YYY, 0.0);
  glEnd();
  glFlush();
}


main(int argc, char** argv)
{
  
  float XXX=3;
  float YYY=6;
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(250, 250);
  glutInitWindowPosition(100, 100);
  glutCreateWindow(“hello”);
  glutDisplayFunc(display);
  glutMainLoop();
  return 0;
} 


Your questions is more of a general C/C++ programming one than OpenGL related, you may get better answers if you ask in a general programming forum.

In C/C++ (and many other languages) variables only exist in certain scope (the enclosing { }, in your case this is the main function for the two floats). You can not access a variable from outside its scope. The easiest solution is to make the two variables you need to access in main and display global:


float XXX;
float YYY;

void display(void)
{
    // ...
    glVertex3f(XXX, XXX, 0.f);
    // ...
}

int main(int argc, char* argv[])
{
    XXX = 3.f;
    YYY = 6.f;

    // rest of main

    return 0;
}