Trying hard to pass a pointer as argument to a function called by glutDisplayFunc,...

Hello,
My name is Jane Liu. I hope somebody can help me on my openGl project. In my program, I have a function void vis £¨void£©, and I call glutDisplayFunc(vis) and glutMainLoop() to render the objects. Everything works fine. But somehow, the project requires me to pass a pointer as argument to the vis funtion, which violate the prototype of glutDisplayFunc and glutMianLoop rotine. Is there any way to render the objects without calling glutDisplayFunc and glutMianLoop rotine? Or call them but with a parameter passing vis?
I don’t know how I can do this parameter passing issue, and already spend a lot time to try to solve the problem.

Can anyone help? Thanks.

I can see two solutions.

[ul][]Don’t use GLUT, since it doesn’t meet your requirements. Look into, for example, GLFW, SDL, or the native windowing API for your platform.[]Don’t pass the pointer as a parameter, but as a global variable.[/ul]

Hi Bob,

Thanks for your reply. Acturally, the application is working fine now with a goble variable. But I am not allowed to use global variable.

And I am required to working in Linux platform, so Windows API will be eliminated. So please tell me more about GLFW, SDL, or other solution you see is applicable. I am new, thanks.

Jane

Originally posted by Bob:
[b]I can see two solutions.

  • [li]Don’t use GLUT, since it doesn’t meet your requirements. Look into, for example, GLFW, SDL, or the native windowing API for your platform.[*]Don’t pass the pointer as a parameter, but as a global variable.

Well, I said windowing API, not Windows API. Not much experienced with development on Linux so don’t know what it’s windowing API is called. Is that X perhaps?

For information about GLFW and SDL, search Google. Searching for both SDL and GLFW and pressing “I’m feeling lucky” takes you directly to their respective homepage.

  1. Why are you “not allowed” to use a global?If it’s a restriction imposed by the assignment wording it seems a bit daft - it’s not as if an API can choose what application-specific pointer to pass to your displayFunc, so at best it’ll just pass back whatever you gave it.

Depending on how the restriction is worded you may be able to use something like a global to get the same effect (function scoped static, class static etc).

  1. What’s being represented by this extra pointer? Why do you need it in your displayFunc?

Create an object (in the Object Orientation sense) that receives and passes back this special pointer. Use the “receive the pointer” interface of this object to set your special pointer outside of the glut display function. Inside the glut display function, use the “pass the pointer back” interface to get your special pointer.

Originally posted by bsenftner:
Create an object (in the Object Orientation sense) that receives and passes back this special pointer. Use the “receive the pointer” interface of this object to set your special pointer outside of the glut display function. Inside the glut display function, use the “pass the pointer back” interface to get your special pointer.

And this object is stored where? As a global, perchance? :wink: I don’t think this solves the OP’s problem, it just obfuscates it a bit.

I was thinking more along the lines of

class PointlessFudge
{
public:
static Thing* noGlobalPointerHereHonest;
};

which isn’t technically a global, it just might as well be. As I said, it may just be a case of sidestepping the wording of a restriction. Maybe just stick the pointer inside a namespace; again, that’s not technically “global”.

Hi MikeC
Thanks for replying.
My graduate mentor requires us not to use any global in any sense,he declares any gloable variable should be able to be instead of local variable. But it seems what he said may not be true here if I want use GLUT.lib

Anyway, my programm gets this type pointer from a controller, then it calls once initiate function, and call glut functions to render it. Currently I am using a global pointer (only this one)points to a top data structure to get a “copy” of whatever the controller has passed to me. My programm is abstract in the sense that any information,such as the object’s properties, the windows properties, and position, lighting, anything you can name,and dynamically render different objects…
everything works fine now, then suddenly, the mentor says there should not be a global variable.

[This message has been edited by Jane Liu (edited 03-26-2003).]

[This message has been edited by Jane Liu (edited 03-26-2003).]

Yes, that might be a solution, but I am coding in C instead of Java, any other idea?

quote:

I was thinking more along the lines of

code:

class PointlessFudge{public: static Thing* noGlobalPointerHereHonest;};


[This message has been edited by Jane Liu (edited 03-26-2003).]

Originally posted by Jane Liu:
Yes, that might be a solution, but I am coding in C instead of Java, any other idea?

Actually that was C++, not Java. You mean you have to write in pure C? Sheesh.

Have to say your graduate mentor sounds like a bit of an idiot. Feel free to tell him I said so, though I doubt it’ll help your marks.

About the only not-quite-global fudge I can think of for pure C is function-scope static variables:

void* pointerStore(void* newValue)
{
static void* p = 0;
if (newValue != 0) p = newValue;
return p;
}

// In setup
storePointer(thePointerValue);

// In your displayFunc
void* thePointer = pointerStore(0);

This is drifting way off-topic for OpenGL; feel free to continue by mail if you want.

Thanks, MikeC,

I will try it out tonight and let you know the result.

By the way, my mentor does not like static variables either.

void* pointerStore(void* newValue)
{
static void* p = 0;
if (newValue != 0) p = newValue;
return p;
}

// In setup
storePointer(thePointerValue);

// In your displayFunc
void* thePointer = pointerStore(0);

This is drifting way off-topic for OpenGL; feel free to continue by mail if you want.[/b]<HR></BLOCKQUOTE>

[This message has been edited by Jane Liu (edited 03-26-2003).]

Hi MikeC,
You really have solved my problem, thanks!!! How you did it? Without looking at my code and figured out a solution! I believe you really are a super expert on this.
I feel good too, since I can follow your idea and succssfully changed all those up to seven layers of pointers. Everything is working now, and there is no global pointers at all!
What do you do? I guess your job is to answer people’s trick questions. If you do not mind, I will keep your email address and referrence you when I hit this kind of hard problem.
Thanks again
Jane

Originally posted by MikeC:


This is drifting way off-topic for OpenGL; feel free to continue by mail if you want.

Jane,

That pointerStore() function uses a single global just as much as any other solution that’s proposed. A function-level static is still global in lifetime. If you think there’s any difference, then please go back and carefully read the semantics of function-level statics. There is no difference (except for scoping) between a regular global, and a function-level static.

If your mentor doesn’t like a global, but the GLUT API does not allow you to pass in a handle or baton that gets forwarded to your display function, then the GLUT API is incompatible with your mentor. If using GLUT is important to you, then the mentor should realize that a trade-off has to be made, and allow the global. If the rule is “no globals, period” then the GLUT API is simply not suitable to your needs.

Realizing when you can’t get there from here, given the circumstances of the situation, is an important part of good software engineering. Coming up with an appropriate compromise or work-around is an important skill.

Hi jwatte;

I think you have made a clear conclusion.
And before this discution I had shown my mentor that GLUT does not allow passing argument. But he simply believes that “anything can be done”.
So now this solution compromises the situation that “technically” there is no a global.

And the project has been pointed to use GLUT by my mentor in the beginning, so either I nor him has time to change to something else at this point.

Hopefully, through this discution, both I and him can see that this is the situation we have to live by.

Thanks for your suggestion.

Originally posted by jwatte:
[b]Jane,

That pointerStore() function uses a single global just as much as any other solution that’s proposed. A function-level static is still global in lifetime. If you think there’s any difference, then please go back and carefully read the semantics of function-level statics. There is no difference (except for scoping) between a regular global, and a function-level static.

If your mentor doesn’t like a global, but the GLUT API does not allow you to pass in a handle or baton that gets forwarded to your display function, then the GLUT API is incompatible with your mentor. If using GLUT is important to you, then the mentor should realize that a trade-off has to be made, and allow the global. If the rule is “no globals, period” then the GLUT API is simply not suitable to your needs.

Realizing when you can’t get there from here, given the circumstances of the situation, is an important part of good software engineering. Coming up with an appropriate compromise or work-around is an important skill.[/b]

jwatte - you’re right of course, the problem pretty much requires global semantics. But as I said before, her mentor sounds like an idiot, who’s read a list of “C commandments” and doesn’t have the experience to know when to break them.

But hey, maybe (s)he’s too much of an idiot to notice that someone’s following the letter rather than the spirit of the law. Let’s face it, sad as it may be, knowing how to work around clueless management can also be a vital career skill

Originally posted by MikeC:
But as I said before, her mentor sounds like an idiot, who’s read a list of “C commandments” and doesn’t have the experience to know when to break them.

Too right… No global and no static - perhaps everything should be written to text files and read back?

> knowing how to work around clueless management can also be a vital career skill

grin :slight_smile:

> write to text and read back

Who needs globals when there’s the Windows Registry?

Hey Jane what is this project of yours about ? it´s an engine or something

GLUT is endeed a very good tool for OGL portability and stuff …and as you´re working under linux i think you actually did make a good choice

Globals rules ! they´re fast
and actually with good control they don´t mess around with your code as academic minds think
My first C teacher has some thoughts on globals like your mentor …and believe sometimes they´re really useful

Yes, Something like that,it’s a tool kit.

[QUOTE]Originally posted by raverbach:
[b]Hey Jane what is this project of yours about ? it´s an engine or something