Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > OpenGL/C++/Cocoa question

OpenGL/C++/Cocoa question
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Jul 23, 2001, 07:29 PM
 
I'm trying to write a VERY simple game engine for a game I'm writing in OpenGL on OSX. However, I DO NOT want to use global variables in this engine. I hate global variables. However, I see absolutely no way around using them in an OpenGL program. Why you ask? Because you have to assign functions for OpenGL to call at certain points, like when the screen needs to be redrawn, or when you press a key. I want to share data between these functions, but I don't want the data to be global. For example, in this game I'm using an object list (each object has all the information about where it is on the screen, how to draw it, and how to move it). I could just make this list static data in the draw function, but I would rather share the list with the idle function. That way, I can do frame rate timing in the idle function (moving the objects, but not drawing them until a certain amount of time has gone by so that the objects move at the same speed at different framerates). How are you supposed to do this? I tried assigning the member functions of a class, so that I can have the list as private data of the class, but I get an error saying "void was not ignored as it should be". This is a short snipit of what I'm trying to do. <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
...
GLvoid InitGL(GLvoid);
GLvoid Idle(GLvoid);
class MyGLControlClass
{
<font color = green>public</font>:
...
GLvoid draw(GLvoid);
GLvoid init(GLvoid);
...
<font color = green>private</font>:
<font color = green>int</font> a;
};
<font color = green>int</font> main(<font color = green>int</font> argc, <font color = green>char</font>** argv)
{
MyGLControlClass con;

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutGameModeString(<font color = red>"1024x768:<font color = blue>32</font>@<font color = blue>75</font>"</font>);
glutEnterGameMode();

con.init();
InitGL();
glutDisplayFunc(con.draw()); <font color = brown>// NOTE: THIS IS CAUSING THE ERROR &lt;-------------------------------</font>
glutIdleFunc(Idle);<font color = brown>//Idle Function is dont every loop</font>
glutMainLoop();


<font color = green>return</font> <font color = blue>0</font>;
}

GLvoid InitGL(GLvoid)
{

glClearColor(<font color = blue>0.</font>0f, <font color = blue>0.</font>0f, <font color = blue>0.</font>0f, <font color = blue>0.</font>0f); <font color = brown>// This Will Clear The Background Color To Black</font>
<font color = brown>// Colours follow standar Red, Green, Blue, then alpha channel</font>
glClearDepth(<font color = blue>1.0</font>); <font color = brown>// Enables Clearing Of The Depth Buffer</font>
glDepthFunc(GL_LESS); <font color = brown>// The Type Of Depth Test To Do</font>
glEnable(GL_DEPTH_TEST); <font color = brown>// Enables Depth Testing</font>
glShadeModel(GL_SMOOTH); <font color = brown>// Enables Smooth Color Shading</font>

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); <font color = brown>// Reset The Projection Matrix</font>

gluPerspective(<font color = blue>45.</font>0f,(GLfloat)<font color = blue>1024</font>/(GLfloat)<font color = blue>768</font>,<font color = blue>0.</font>1f,<font color = blue>100.</font>0f); <font color = brown>// Calculate The Aspect Ratio Of The Window</font>

glMatrixMode(GL_MODELVIEW);

}

<font color = brown>// Idle ---------------------------------------------------------------------</font>

GLvoid Idle(GLvoid)
{
glutPostRedisplay();<font color = brown>//Makes glut re-render the scene</font>
}


GLvoid MyGLControlClass::init(GLvoid)
{
a = <font color = blue>10</font>;
}

GLvoid MyGLControlClass::draw(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); <font color = brown>// Clear The Screen And The Depth Buffer</font>
glLoadIdentity(); <font color = brown>// Reset The View</font>
<font color = brown>//Lesson <font color = blue>2</font></font>
a++;
<font color = green>if</font> (a &gt; <font color = blue>14</font>)
a = <font color = blue>0</font>;
glTranslatef(<font color = blue>-1.</font>5f,<font color = blue>0.</font>0f,<font color = blue>-6.</font>0f); <font color = brown>// Move Left <font color = blue>1.5</font> Units And Into The Screen <font color = blue>6.0</font></font>

<font color = brown>//Setup the rotation <font color = green>for</font> the triangle</font>
glRotatef(a,<font color = blue>0.</font>0f,<font color = blue>1.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Rotate The Triangle On The Y axis</font>
glRotatef(a,<font color = blue>1.</font>0f,<font color = blue>0.</font>0f,<font color = blue>0.</font>0f);
<font color = brown>//Make a Triangle</font>
glBegin(GL_TRIANGLES); <font color = brown>// Drawing Using Triangles</font>
<font color = brown>//Front Face</font>
glColor3f(<font color = blue>1.</font>0f,<font color = blue>0.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Red</font>
glVertex3f( <font color = blue>0.</font>0f, <font color = blue>1.</font>0f, <font color = blue>0.</font>0f); <font color = brown>// Top Of Triangle (Front)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>1.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Green</font>
glVertex3f(<font color = blue>-1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>1.</font>0f); <font color = brown>// Left Of Triangle (Front)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>0.</font>0f,<font color = blue>1.</font>0f); <font color = brown>// Blue</font>
glVertex3f( <font color = blue>1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>1.</font>0f); <font color = brown>// Right Of Triangle (Front)</font>
<font color = brown>//Right Face</font>
glColor3f(<font color = blue>1.</font>0f,<font color = blue>0.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Red</font>
glVertex3f( <font color = blue>0.</font>0f, <font color = blue>1.</font>0f, <font color = blue>0.</font>0f); <font color = brown>// Top Of Triangle (Right)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>0.</font>0f,<font color = blue>1.</font>0f); <font color = brown>// Blue</font>
glVertex3f( <font color = blue>1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>1.</font>0f); <font color = brown>// Left Of Triangle (Right)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>1.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Green</font>
glVertex3f( <font color = blue>1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>-1.</font>0f); <font color = brown>// Right Of Triangle (Right)</font>
<font color = brown>//Left Face</font>
glColor3f(<font color = blue>1.</font>0f,<font color = blue>0.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Red</font>
glVertex3f( <font color = blue>0.</font>0f, <font color = blue>1.</font>0f, <font color = blue>0.</font>0f); <font color = brown>// Top Of Triangle (Back)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>1.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Green</font>
glVertex3f( <font color = blue>1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>-1.</font>0f); <font color = brown>// Left Of Triangle (Back)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>0.</font>0f,<font color = blue>1.</font>0f); <font color = brown>// Blue</font>
glVertex3f(<font color = blue>-1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>-1.</font>0f); <font color = brown>// Right Of Triangle (Back)</font>
<font color = brown>//Back Face</font>
glColor3f(<font color = blue>1.</font>0f,<font color = blue>0.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Red</font>
glVertex3f( <font color = blue>0.</font>0f, <font color = blue>1.</font>0f, <font color = blue>0.</font>0f); <font color = brown>// Top Of Triangle (Left)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>0.</font>0f,<font color = blue>1.</font>0f); <font color = brown>// Blue</font>
glVertex3f(<font color = blue>-1.</font>0f,<font color = blue>-1.</font>0f,<font color = blue>-1.</font>0f); <font color = brown>// Left Of Triangle (Left)</font>
glColor3f(<font color = blue>0.</font>0f,<font color = blue>1.</font>0f,<font color = blue>0.</font>0f); <font color = brown>// Green</font>
glVertex3f(<font color = blue>-1.</font>0f,<font color = blue>-1.</font>0f, <font color = blue>1.</font>0f); <font color = brown>// Right Of Triangle (Left)</font>
glEnd(); <font color = brown>// Finished Drawing The Triangles</font>
glBegin(GL_QUADS);
glVertex3f(<font color = blue>-1.</font>0f,<font color = blue>-0.</font>5f, <font color = blue>1.</font>0f);
glVertex3f(<font color = blue>1.</font>0f,<font color = blue>-0.</font>5f, <font color = blue>1.</font>0f);
glVertex3f(<font color = blue>1.</font>0f,<font color = blue>-0.</font>5f, <font color = blue>-1.</font>0f);
glVertex3f(<font color = blue>-1.</font>0f,<font color = blue>-0.</font>5f, <font color = blue>-1.</font>0f);
glEnd();

glutSwapBuffers();


}
</font>[/code]
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Boulder, CO, USA
Status: Offline
Reply With Quote
Jul 24, 2001, 01:48 PM
 
Yeah, OpenGL sorta tugs you into using globals...

If you genuinely have need for only a single instance of a class through the entire scope of the program, making it global isn't such a bad thing, IMHO.

If you want to legitimize it somewhat, and you don't need to subclass or make multiple instances of your control class, you can either make the required methods and members static, or use a namespace instead of a class. Namespaces can be nested.

Too bad glutDisplayFunc and IdleFunc don't let you pass some user data to the callback. Then you could set up a static callback which gets a pointer to the instance of the class, giving you access to its innards....

I believe when I was working on a game engine a while back I had a few global objects/threads which interacted... a "world" object, which kept track of the objects in the world; a renderer object, which would walk the world's object tree periodically and ask eack object to render itself; a number of camera objects, which set up the modelview matrix prior to rendering (and the renderer could switch among the cameras); a physics thread, which did collision detection and updated objects' positions ....

But then I started with Apple's setupGL sample code, so I wasn't using GLUT. It's a little more flexible.

Eek, sorry for rambling... remembering the "good 'ol days"

Look into namespaces.
     
Fresh-Faced Recruit
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jul 25, 2001, 07:33 AM
 
The problem isnt that using OpenGL forces you to use globals, but rather glut does. If you want to do serious work using openGl, and want your code to be cleaner (no globals, not relying on callbacks) you need to look at implementing openGL natively.

If you are careful, you can implement the program natively, and still maximize portability. Last summer, my friend and I put together a game using openGL. I was using the mac, and he using his pc. Good abstraction at the various levels ensure that most of the code can still be shared, even without glut.

Hope that helps.

Spencer
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:44 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2