PDA

View Full Version : glutDisplayFunc...



helda
12-10-2002, 01:10 AM
Hello
I have a Problem with glutDisplayFunc, GlutReshapeFunc, ... My Problem is, i want to use it in c++. I have made a class Szene where my callback functions are element functions. Here the class Definition.
class Szene {
public:
Szene(int argc, char **argv);
~Szene();
void AktElementSuchen();
void InitSchatten();
void InitSzene();
void InitOpenGl();
void SpecialKey(int key, int x, int y);
void Display();
void Reshape(int,int);
void InitWin(int argc, char** argv);
void Key(unsigned char key, int x, int );
private:
Licht* lichtarray;
Schatten* schattenarray;
Kamera betrachter;
bool schattenaktiviert;
Objekt* objektarray[1];
};
Now i have the Problem: That i have a compiler error like this:
h:\Szene\Szene.cpp(50): error C2664: 'glutKeyboardFunc' : cannot convert parameter 1 from 'void (unsigned char,int,int)' to 'void (__cdecl *)(unsigned char,int,int)'

when i cast the funktion like glutDisplayFunc((void)Display) it does not operate.

what can i do?

Perhaps my compiler is a problem, i use visual studio .net

Bob
12-10-2002, 01:24 AM
You have to make your callbacks static functions, like this.



class Szene
{
public:
...
static void Key(unsigned char key, int x, int );
...
}

helda
12-10-2002, 01:27 AM
I have tested this, it operates ok, but why must i do this?

Bob
12-10-2002, 04:01 AM
Consider this example.



class foo
{
public:
int get_value()
{
return bar;
}

int bar
}

int main(int, char **)
{
foo a;
foo b;

a.bar = 10;
b.bar = 20;

int c = b.get_value();
}

What is the value of c? Obviously it should be 20. But how does get_value know to return 20? Easy, because b's member bar has the value 20. But a's value is 10, how does get_value know to return b's value and not a's value? This is where the poblem is.

For get_value to know which object was used to call get_value, there must be some kind of information that tells the function what object was used. It is called the this pointer that is passed to all member function when called. this is a pointer that points to the object that was used to call get_value. What really happes in the code is something along this line.



int get_value(foo *this)
{
return this->bar;
}

class foo
{
public:
int bar
}

int main(int, char **)
{
foo a;
foo b;

a.value = 10;
b.value = 20;

int c = get_value(&c);
}

This is a differet kind of function than a regular function, and cannot be used as a callback in GLUT, because GLUT does not know which object to use to call the function, because it doesn't have the this pointer.

A static function does not have this hidden pointer and acts like a regular function, which can therefore be passed to GLUT. The "drawback" is that you can't access any members or call any other functions in your class, other than static functions and members.

[This message has been edited by Bob (edited 12-10-2002).]

helda
12-10-2002, 04:18 AM
thank you bob for this helpful answer, now i think i understand why.

helda