Results 1 to 5 of 5

Thread: Callback functions in openGL

  1. #1
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    7

    Callback functions in openGL

    Hi
    I have the following problem.. I did a class called Keyboard
    having functions inputKey and myKeyboard.
    I made an object of that class (Keyboard k) in the main
    when I tried to do the following:
    glutSpecialFunc(k.inputKey);
    glutKeyboardFunc(k.myKeyboard);

    I get an error : left of '.inputKey' must have class/struct/union
    when I tried
    glutSpecialFunc(&Keyboard::inputKey);
    glutKeyboardFunc(&Keyboard::myKeyboard);

    I get another error: 'glutSpecialFunc' : cannot convert parameter 1 from 'void (__thiscall Keyboard::* )(int,int,int)' to 'void (__cdecl *)(int,int,int)'

    does anyone have a solution for this?
    I need to call the functions I did in the class keyboard into glutSpecialFunc and glutKeyboardFunc

    Thank you in advance

  2. #2
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Callback functions in openGL

    Try this in the Keyboard class declaration:

    Code :
     
    class Keyboard
    {
        void __cdecl inputKey( int, int, int );
        ...
    };

  3. #3
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    7

    Re: Callback functions in openGL

    No it didn't work :S

    for elaborating more
    here is part of the code

    //main.h
    class Keyboard
    { public:
    Keyboard();
    ~Keyboard();
    void inputKey(int key,int x,int y);
    void myKeyboard(unsigned char key, int x,int y);
    };
    [\code]
    and this is the Keyboard class
    [CODE]
    #include "main.h"

    Keyboard::Keyboard(){}
    Camera c(x,y,z,lx,ly,lz,angle);

    void Keyboard::inputKey(int key, int x, int y) {

    switch (key) {
    case GLUT_KEY_LEFT :
    angle -= 0.01f;
    c.orientMe(angle);break;
    case GLUT_KEY_RIGHT :
    angle +=0.01f;
    c.orientMe(angle);break;
    case GLUT_KEY_UP :
    c.moveMeFlat(1);break;
    case GLUT_KEY_DOWN :
    c.moveMeFlat(-1);break;

    case GLUT_KEY_PAGE_UP:
    c.moveme(1);break;
    case GLUT_KEY_PAGE_DOWN:
    c.moveme(-1);break;

    }
    }
    void Keyboard::myKeyboard(unsigned char key,int x, int y){

    switch(key){

    case 'q':
    drawSphere=false;break;
    case 's':
    drawSphere=true;break;

    }

    }
    [\code]
    for the main.cpp
    [CODE]
    #include "main.h"
    //some code here
    Keyboard k;
    void main(int argc, char **argv)
    {
    //---------
    glutSpecialFunc(k.inputKey);
    glutKeyboardFunc(k.myKeyboard);
    // ------

    }
    [\code]

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Callback functions in openGL

    Make your Keyboard callback methods static or if it is not possible write a static wrapping method in the class Keyboard that calls the proper keyboard callback of a Keyboard object.

    BTW, the end code tag is [/CODE] and not [\code].

  5. #5
    Senior Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    356

    Re: Callback functions in openGL

    class method use the _thiscall, the first parameter will always be the "this" pointer.
    With static as dletozeun suggest you have a standard call but then the Keyboard class will be useless cause you can modify only static attributes.

    You can do something like that:
    Code :
    Keyboard *k;
     
    void dummyKeyFunc(int k, int x, int y)
    {
       k->keyInput(k, x, y);
    }
     
    void main(int argc, char **argv)
    {
      k = Keyboard::getInstance(); //use singleton for manager
      glutSpecialFunc(dummyKeyFunc);
      glutKeyboardFunc(dummyKeyFunc);
    }
    By the way using a class method to modify a global variable is a very bad design issue. Check again your class relationship.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

Similar Threads

  1. Passing Parameters To Callback Functions?
    By larryl in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 01-29-2017, 09:20 AM
  2. Events and callback functions
    By zhugh in forum OpenCL
    Replies: 4
    Last Post: 12-12-2010, 06:55 PM
  3. Tessellation Callback Functions
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-20-2006, 01:51 PM
  4. Drawing in Callback-Functions not allowed ??
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-03-2004, 06:59 AM
  5. callback functions and C++
    By emshelly in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-17-2002, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean