Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:20:13+00:00 2026-05-16T14:20:13+00:00

I’ve created a C++ class ( myPixmap ) to encapsulate the work performed by

  • 0

I’ve created a C++ class (myPixmap) to encapsulate the work performed by the OpenGL GLUT toolkit. The display() member function of the class contains most of the code required to set up GLUT.


void myPixmap::display()
{
    // open an OpenGL window if it hasn't already been opened
    if (!openedWindow)
    {
        // command-line arguments to appease glut
        char *argv[] = {"myPixmap"};
        int argc = 1;
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(640, 480);
        glutInitWindowPosition(30, 30);
        glutCreateWindow("Experiment");
        glutDisplayFunc(draw);
        glClearColor(0.9f, 0.9f, 0.9f, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glutMainLoop();

        openedWindow = true;
    }  
}

The display function passed to glutDisplayFunc() is another member function of the class:


void myPixmap::draw(void)
{
    glDrawPixels( m,n,GL_RGB,GL_UNSIGNED_BYTE, pixel );
}

However, gcc 4.2.1 on Mac OS X 10.6.4 refuses to compile this code, claiming that:


argument of type 'void (myPixmap::)()' does not match 'void (*)()'

Is there a way to use the GLUT toolkit inside a member function of a class, or must I call all GLUT functions within the main function of the program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:20:13+00:00Added an answer on May 16, 2026 at 2:20 pm

    The problem is that a pointer to an instance bound member function has to include the this pointer. OpenGL is a C API, and knows nothing about this pointers. You’ll have to use a static member function (which doesn’t require an instance, and thus no this), and set some static data members (to access the instance) in order to use glutDisplayFunc.

    class myPixmap
    {
    private:
      static myPixmap* currentInstance;
    
      static void drawCallback()
      {
        currentInstance->draw();
      }
    
      void setupDrawCallback()
      {
        currentInstance = this;
        ::glutDisplayFunc(myPixmap::drawCallback);
      }
    };
    

    You may also have problems with C linkage vs C++ linkage, in which case you’ll have to play around with extern "C". If so, you might have to use a global function, rather than a static member function as your callback, and have that call myPixmap::draw. Something like:

    class myPixmap
    {
    public:
      void draw();
    
    private:
      void setupDrawCallback();
    };
    
    myPixmap* g_CurrentInstance;
    
    extern "C"
    void drawCallback()
    {
      g_CurrentInstance->draw();
    }
    
    void myPixmap::setupDrawCallback();
    {
      ::g_CurrentInstance = this;
      ::glutDisplayFunc(::drawCallback);
    }
    

    With all of this, try to make as few changes as possible, since this is really kind of a kludge to deal w/ OpenGL being a C API.

    If you want multiple instances (I don’t think most people using GLUT make multiple instances, but maybe you are), you’ll have to figure out a solution using a std::map to retrieve the instance:

    static std::map<int, myPixmap> instanceMap;
    

    Where you’d get the int to resolve which instance, I am not sure 🙂

    FYI, you should define functions that take no parameters this way:

    void some_function() { }
    

    not

    void some_function(void) { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.