Why can't my class find these callback functions?

Started by
3 comments, last by Concentrate 12 years, 11 months ago
Hi guys,

I've been working on OpenGL tutorials and now am moving what I'm learning into a class, but I ran into a problem. When I call glutDisplayFunc and glutReshapeFunc they can't find the callback functions (even though they exist). I'm really at a loss here and I either have a syntax error (even though everything works when I take it outside the class) or there is something going on under the hood that I am not aware of? Any help would be greatly appreciated :)

I'll admit I'm pretty rusty with classes but I can't seem to spot what I am doing wrong. Anyone have any idea?



//HEADER FILE
class OGLAppManager
{
public:
OGLAppManager();
void reshape(int width, int height);
void display(void);
private:
float rotationAngle;
};

//SOURCE FILE
#include "OGLAppManager.h"

OGLAppManager::OGLAppManager()
{
rotationAngle = 0;

//Set display properties
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //Set up a display buffer, enable double buffering, blending and depth testing
glutInitWindowSize(1000, 1000); //Set the width and height of the window
glutInitWindowPosition(100, 100); //Set window position
glutCreateWindow("GSP 420"); //Set window caption

//Window Control Functions
glutDisplayFunc(display); //Tell glut to draw window based on function
glutReshapeFunc(reshape); //Tell glut to reshape the window when dragged
};

//Function to reshape the window when dragged
void OGLAppManager::reshape(int width, int height)
{
//Set viewport to window size
glViewport(0, 0, (GLsizei)width, (GLsizei)height);

// Switch to projection matrix to manipulate how scene is viewed
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //Reset projection matrix to avoid artifacts

//Set FOV, + Near and Far clip planes
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);

//Switch back to modelview matrix
glMatrixMode(GL_MODELVIEW);
};

//Function to draw the scene
void OGLAppManager::display(void)
{
//Handle drawing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Clear background of window
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer
glLoadIdentity(); //Load identity matrix to reset drawing locations

//Translate scene back
glTranslatef(0.0f, 0.0f, -6.0f);

//Draw a pyramid
glPushMatrix();
glRotatef(rotationAngle, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);

//SNIP
glutSwapBuffers(); //Flush buffers to the window

rotationAngle++; //Rotation angle for pyramid
};

Advertisement
A class member function will receive the this pointer of the class as its first (hidden) argument, so you're not passing in a 'void func(void)' but a 'void func(OGLAppManager*)'.

The this pointer won't be passed in as first hidden argument when you declare the function as static.




Member functions are not normal functions, you cannot pass a pointer to a member function to something expecting a pointer to a function. To even refer to a member function, you must use &ClassName::functionName, unlike normal functions. There are many other caveats on member function pointers, but long story short glut cannot interact with c++ member functions. You'll need to write non-member functions. From these you may of course call member functions on any globally accessible object.

I'd recommend using SDL rather than GLUT if you can.
I tried this earlier before I posted:

[color=#008d00][color=#480083]glutDisplayFunc[color=#000000](*[color=#255b5f]display[color=#000000]); //Tell glut to draw window based on function

[color=#008d00][color=#480083]glutReshapeFunc[color=#000000](*[color=#255b5f]reshape[color=#000000]); //Tell glut to reshape the window when dragged

[color=#008d00]


[color=#008d00][color=#000000][font=arial, verdana, tahoma, sans-serif] But when I run the program it crashes (although it compiles and links just fine.) Am I doing something wrong? None of my C++ books cover using callback functions in a class :([/font]

Thanks so much for the responses guys!

I did notice the pointer thing when I double clicked the error message (and just to try I put a * in front of the name which compiled and linked just fine, but crashed the program on startup :P

I will look into using SDL since I am fairly familiar with it although I've never mixed it with SDL. Are you able to just use the functions of each? (It looked that way when I saw it on Lazyfoo's site).
You could do something like so :

class OGLAppManager{
public:
typedef void (*DisplayFunc)();
private:
DisplayFunc _display;
public:
OGLAppManager(): _display( _defaultDispFunc ) {}
OGLAppManager(const DiplayFunc& f) : _display(f) {}
void setDisplayFunc(const DisplayFunc& f){ _display = f; }
void initialize(){ /* all glut initialization here */ }
private:
static void _defaultDispFunc(){ /* default display function */ }
}

Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

This topic is closed to new replies.

Advertisement