Thread: Some help for openGL

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    Some help for openGL

    I know that if I wan to use print something in openGL, I would used this
    Code:
    glutDisplayFunc(renderScene);
    But now the problem is in my code, if I wan to display something in the screen, I use this.
    Code:
    if (myKeys['o'] == true)
    {
        glutDisplayFunc(renderScene);
    }
    It would not work because I am using pointer. I received this error.

    error C3867: 'myApplication::renderScene': function call missing argument list; use '&myApplication::renderScene' to create a pointer to member


    So I edit and change to this.
    Code:
    if (myKeys['o'] == true)
    {
        myApplication::renderScene();
    }
    It work but the problem is I need to include the glutDisplayFunc too. How should I do this to code that include the glutDisplayFunc in my application class with the function render scene.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by evildotaing View Post
    error C3867: 'myApplication::renderScene': function call missing argument list; use '&myApplication::renderScene' to create a pointer to member
    this error message is very clear. You have to use

    Code:
    if (myKeys['o'] == true)
    {
         glutDisplayFunc( &myApplication::renderScene );
    }
    myApplication::renderScene() has to be a static memberfunction of myApplication.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Quote Originally Posted by ZuK View Post
    this error message is very clear. You have to use

    Code:
    if (myKeys['o'] == true)
    {
         glutDisplayFunc( &myApplication::renderScene );
    }
    myApplication::renderScene() has to be a static memberfunction of myApplication.
    Kurt
    I code what you are saying but now I got new error again. error C2664: 'glutDisplayFunc' : cannot convert parameter 1 from 'void (__thiscall myApplication::* )(void)' to 'void (__cdecl *)(void)' Anyway I declared as this for pointer,
    Code:
    static myApplication* getInstance();
    And all my codes work expect when I dun know why got problem when I want to use glutDisplayFunc.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You simply get a calling convention conflict: the glut library is probably in C and expect a function using C calling convention, but you pass a class method that takes an implicit this pointer.
    This is what your compilers tries to tell you.
    You have to make the renderScene method a static method of the class as Kurt said it before.
    Also check that stackoverflow thread which concerns the same issue: c++ - Using OpenGL glutDisplayFunc within class - Stack Overflow

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    __thiscall usually only applies to non-static member functions. you need to make myApplication::renderScene static.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    How to do it. Help? Example of some code, I am very confused.
    Last edited by evildotaing; 08-20-2012 at 04:12 AM.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Quote Originally Posted by Elkvis View Post
    __thiscall usually only applies to non-static member functions. you need to make myApplication::renderScene static.
    I make the function to static void(renderScene); but still not working.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by evildotaing View Post
    I make the function to static void(renderScene); but still not working.
    it should be
    Code:
    static void renderScene();
    If it still won't work then post your code and full error messages.
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    I got this error doing making renderScene static. 1>h:\game protype project\game_v4\dm2231_lab_v4\dm2231_lab\myapplica tion.cpp(238) : error C2724: 'myApplication::renderScene' : 'static' should not be used on member functions defined at file scope

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    // in header
    class myApplication {
        public:
        static void renderScene();
        // .....
    };
    
    // in myapp.cpp
    
    void myApplication::renderScene() {  // no static needed here
        // do whatever
    }
    
    // in main.cpp
    
    int main() {
        // update display
        glutDisplayFunc( &myApplication::renderScene );
    }
    Kurt

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Glut is expecting a function pointer to a global C-style function. You can try to use a static member function, but beware:
    static member functions can be used in the contexts where a C callback is expected, if they are extern "C". Although on most compilers it would probably work without extern "C", the standard says it doesn't have to work.
    From C++ FQA Lite: Pointers to member functions

    Of course it would be a pain to use extern "C" member functions... Here's what I normally do if I can't get this kind of thing to work. Declare a global function, and just to be safe make it extern "C", and then call whatever function you really want to handle things.
    Code:
    // in .h
    class SomeClass {
    public:
        static void handleDisplay();
    };
    
    // in .cpp
    // note: in the .cpp file, you should leave out keywords like "static" and "virtual"; that was the cause of your second error message
    void SomeClass::handleDisplay() {
        // ...
    }
    
    // You can prototype this function if you really want to, but it's not really necessary
    extern "C" {
        void handler() {  // if you put "static" here, the function is only visible from within this file
            // can use C++ code here even though it's extern "C"
            SomeClass::handleDisplay();
        }
    }
    
    // okay, now we have a real C-style function we can pass to glut
    glutDisplayFunc(handler);  // & is optional on handler
    Sometimes you don't have to be that extreme but that's something you can try.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-02-2010, 01:26 PM
  2. OpenGL Quesiton: Does Windows XP, Vista still at OpenGL 1.1?
    By indigo0086 in forum Game Programming
    Replies: 8
    Last Post: 05-21-2007, 11:18 AM
  3. Replies: 5
    Last Post: 02-12-2006, 08:42 PM
  4. OpenGL, texturing...and...more OpenGL
    By Sunny in forum Game Programming
    Replies: 2
    Last Post: 07-08-2002, 02:34 PM
  5. opengl
    By Sunny in forum Game Programming
    Replies: 6
    Last Post: 05-07-2002, 11:22 PM