PDA

View Full Version : Passing parameters to display function in GLUT



Orion801
10-16-2010, 09:12 AM
Hi,

I've recently started learning OpenGL and have been using freeglut. A problem I'm having at the moment is that I want to be able to pass a pointer to a vector into my display function but am unable to as I think it has to take the form "void glutDisplayFunc(SomeFunction(void))" and I get the error "invalid use of void expression" if I try to pass any parameters in to the function I'm using.

My program should eventually render a collection of particles that form larger objects that will move around. The approach I've taken is to have each particle as it's own object with information on its position that is then used to draw it. I need the display function to be able to access a vector holding all of these particles so that it knows where to draw them.

Thanks in advance for any help.

Jacek Nowak
10-17-2010, 02:21 AM
Have you tried something like:




void render()
{
glClearColor(...);

// draw stuff

glutSwapBuffers();

}

int main()
{
// initialize glut
glutDisplayFunc(render);
}

Orion801
10-17-2010, 05:15 AM
Thanks for the tip. Yeah I can get it to work like that but my problem is that I need the display function to know the address of a vector holding my particles so it knows where to draw them. At the moment the only way I can get it to work is to have it generate the particles within the display function but this both slows performance and makes it very difficult to make the particles interact with each other over time.

ZbuffeR
10-17-2010, 08:24 AM
This is a good use case for a global variable, pointing to an array initialized in main().



#include <GL/glut.h>

myVectorType myPointer;

void render()
{
glClearColor(...);

// draw stuff in myPointer

glutSwapBuffers();

}

int main()
{
// init array of particles
myPointer = ...
// initialize glut
glutDisplayFunc(render);
}

Orion801
10-17-2010, 11:41 AM
Thanks, I think that'll probably be the best way of doing it. I'd just been a bit reluctant to use global variables as when learning C++ I was told it's best to avoid using them too much. In this case it should do the job quite well though. Thanks for the help