-1

I have function to draw eclipse :

void drawEclipse(GLfloat x, GLfloat y, GLfloat xrad,GLfloat yrad)

When I'm calling glutDisplayFunc(drawEclipse(10,10,3,3));

It has compilation error:

error: invalid use of void expression

How can I pass parameter to function inside glutDisplayFunc?

And this eclipse drawing function is being called dynamically, with a mouse click.

My updated code:


void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        printf("%s -- %d -- %d\n", "clicked",x,y);

        //Way 1
        glutDisplayFunc[&]() { drawEclipse(x,y,5,5); };

        //Way 2
        auto display = [x,y]() { return drawFilledelipse(x,y,3,3); };
        glutDisplayFunc(display);
   }
}
...
int main(){
...

    glutMouseFunc(mouseClicks);
...
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • 1
    So did you read [the docs](https://www.opengl.org/resources/libraries/glut/spec3/node46.html)? It wants a function pointer, So what you can do is pass a lambda function of the form `void(*func)(void)`. – πάντα ῥεῖ Sep 27 '20 at 17:30
  • @πάνταῥεῖ I have read the docs, and read as many answers as possible in this and some other site, I had read parameters can be passed as string or maybe other method.. I was looking for that kind of hack.. – Maifee Ul Asad Sep 27 '20 at 17:32

1 Answers1

0

I'll quote the answer to Passing capturing lambda as function pointer:

A lambda can only be converted to a function pointer if it does not capture, from the draft C++11 standard section 5.1.2 [...]


You have to options

The argument of glutDisplayFunc is a function pointer. You have to implement a function that draws the ellipse and pass a the function pointer to glutDisplayFunc:

void display()
{
    drawEclipse(10,10,3,3);

    // [...]
}

glutDisplayFunc(drawEclipse(10,10,3,3));

glutDisplayFunc(display);

Alternatively you can use a Lambda expression without any capture arguments:

glutDisplayFunc([]()
{
    drawEclipse(10,10,3,3);

    // [...]
});
Rabbid76
  • 202,892
  • 27
  • 131
  • 174