PDA

View Full Version : Global variables



Amikam
02-28-2009, 09:46 AM
Hi all,

I have this example code (I still didn't understand how to use generic vertex attributes but I will do).
My question is under the end of the code.

#include <GL/glut.h>
#define _USE_MATH_DEFINES
#include <math.h>

struct Vertex
{
float x,y;
float r,g,b;
} typedef Vertex;

Vertex polygon[6];

void SetPolygon()
{
float ang;
int index = 0;

float red[6] = {0.0,0.2,0.4,0.6,0.8,1.0};
float blue[6] = {0.1,0.3,0.5,0.7,0.9,1.0};
float green[6] = {0.0,0.5,0.0,0.5,0.0,0.5};

for (ang = 0; ang < 360; ang += 60)
{
polygon[index].x = 15*cos(ang*M_PI/180);
polygon[index].y = 15*sin(ang*M_PI/180);
polygon[index].r = red[index];
polygon[index].b = blue[index];
polygon[index].g = green[index];
index++;
}
}

void DrawPolygon()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3,GL_FLOAT,sizeof(Vertex),&polygon);
glColorPointer(3,GL_FLOAT,sizeof(Vertex),&polygon[0].r);
glDrawArrays(GL_POLYGON,0,6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

DrawPolygon();

glFlush();
glutSwapBuffers();
}

void glInit()
{
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutInitWindowPosition(200,200);
glutCreateWindow("polygon");

glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-20,20,-20,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,400,400);

glutDisplayFunc(display);

SetPolygon();
}

int main(int argc,char *argv[])
{
glutInit(&argc,argv);

glInit();
glutMainLoop();
return 0;
}

How do I pass the polygon to the display function so it is not a global array? what I can do in the case of Win32?

Thanks

akashiraffee
02-28-2009, 11:26 AM
A catch here: the display function cannot have any parameters, so you cannot pass it anything!

If you don't want to use a global, use a static, either in the display function itself or in a function used by the display function.

Amikam
02-28-2009, 01:50 PM
Thanks!

enunes
02-28-2009, 05:05 PM
Thats a GLUT limitation though.

Moving away from GLUT, you can choose other toolkit that doesnt use a callback function, and then being allowed to define any displaying function you want (with whatever parameters)!