Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing 1 argument (pointer) to glutDisplayFunc?

Tags:

c++

opengl

glut

I have created a virtual class with a basic draw() method which doesn't do anything. The purpose of this is that other classes, shapes and other stuff, which will be able to draw themselves in OpenGL, will inherit this virtual class, allowing me to create an array of pointers to many different classes. The idea behind this was that I was hoping I would be able to pass a pointer to this array into my glutDisplayFunc callback. (This happens to be named drawScene(). Unfortunately I don't seem to be able to pass anything to it, as glutDisplayFunc is designed to take a method which takes no parameters and return nothing.

Is there any way of passing arguments to callback functions, and then any way of passing a pointer into my drawScene function?

(TLDR? See below.)

Essentially I want to be able to do this:

class a{ ... };
void drawScene( a** a_array_pointer){ ... }
glutDisplayFunc(drawScene); // <-- How do I pass an argument into this?
like image 998
FreelanceConsultant Avatar asked Sep 06 '12 11:09

FreelanceConsultant


People also ask

What are the advantages of passing function arguments by reference or pointer over passing by value?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.

How are pointer arguments to functions passed in C by value by reference?

To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.

What is a glut display callback function?

The new display callback function. Description. glutDisplayFunc sets the display callback for the current window. When GLUT determines that the normal plane for the window needs to be redisplayed, the display callback for the window is called.

How do you pass a pointer to a function as a parameter?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.


1 Answers

Is there any way of passing arguments to callback functions, and then any way of passing a pointer into my drawScene function?

No. glut is really a C library, and the glutDisplayFunc expects a function pointer as it's parameter. The only way to use variables in that callback, is to make them global or static variables of a class.

like image 72
BЈовић Avatar answered Oct 14 '22 07:10

BЈовић