opengl pointers to non-static member function

May 29, 2011 at 10:11am
Hello all,

I'm just starting with OpenGL and I'm trying to figure out how to stick all the functions like drawScene, handleKeyPress, etc, into a class. This works fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Viz {

  public:
	static void handleKeypress(unsigned char key, int x, int y) {
		...
	}

	static void initRendering() {
		...
	}

	static void handleResize(int w, int h) {
		...                  
	}

	static void drawScene() {
		// draw the objects here	
		...
	}

	static void Draw(int argc, char** argv)
	{
		glutInit(&argc, argv);
		glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
		glutInitWindowSize(400, 400);
	
		glutCreateWindow("Window Title");
		initRendering();
	
		glutDisplayFunc(drawScene);
		glutKeyboardFunc(handleKeypress);
		glutReshapeFunc(handleResize);
		
		glutMainLoop();
	}
};

int main(int argc, char** argv) 
{

	Viz::Draw(argc, argv);

	return 0;
}


But what I really need to do is to have drawScene() depend on some variable, say an integer. Writing

static void drawScene(int an_integer)

doesn't work. I could use a global variable, that works fine. But it really seems messy. I figured it would be much nicer to have the integer as a member variable of Viz. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Viz {

  public:

	int member_int;

	// static void handleKeypress, initRendering(), handleResize as before

	void drawScene() {
		// draw the objects here, code depends on member_int, e.g.
		if (member_int == 0) { ... }
	}

	void Draw(int argc, char** argv)
	{
		glutInit(&argc, argv);
		glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
		glutInitWindowSize(400, 400);
	
		glutCreateWindow("Window Title");
		initRendering();
	
		glutDisplayFunc(drawScene);
		glutKeyboardFunc(handleKeypress);
		glutReshapeFunc(handleResize);
		
		glutMainLoop();
	}
};

int main(int argc, char** argv) 
{
        Viz v;
        v.member_int = 10;

	v.Draw(argc, argv);

	return 0;
}


But the compiler doesn't like the pointer-to-a-(non static)-member-function.

In member function ‘void Viz::Draw(int, char**)’:
56: error: argument of type ‘void (Viz::)()’ does not match ‘void (*)()’


I tried making v a global object, as suggested in http://www.parashift.com/c++-faq-lite/pointers-to-members.html, but no luck.

Any help much appreciated!
May 29, 2011 at 1:13pm
glut functions will only accept static functions. Static functions can only use static variables. If you take the first snippet of code and add static int an_integer; in the class and int Viz::an_integer; outside it, things should work.
Topic archived. No new replies allowed.