View Full Version : in "display" have access to variables in main

07-09-2010, 03:40 AM
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 ;)

Thanks for help

void display(void)
glColor3f(1.0, 1.0, 1.0);
glVertex3f(XXX, XXX, 0.0);
glVertex3f(YYY, XXX, 0.0);
glVertex3f(YYY, YYY, 0.0);
glVertex3f(XXX, YYY, 0.0);

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);
return 0;

carsten neumann
07-09-2010, 12:17 PM
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;