Jump to content

  • Log In with Google      Sign In   
  • Create Account


OpenGl minus global variables?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 JeroMiya   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 July 2001 - 03:21 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.
  
...
GLvoid InitGL(GLvoid);
GLvoid Idle(GLvoid);
class MyGLControlClass
{
    public:
        ...
        GLvoid draw(GLvoid);
        GLvoid init(GLvoid);
        ...
    private:
        int a;
};
int main(int argc, char** argv)
{
        MyGLControlClass con;

	glutInit(&argc, argv);										
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutGameModeString("1024x768:32@75");
        glutEnterGameMode();
        
        con.init();
	InitGL();
        glutDisplayFunc(con.draw()); // NOTE: THIS IS CAUSING THE ERROR <-------------------------------
	glutIdleFunc(Idle);//Idle Function is dont every loop
	glutMainLoop();
	

	return 0;
}

GLvoid InitGL(GLvoid)
{

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

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();				// Reset The Projection Matrix

	gluPerspective(45.0f,(GLfloat)1024/(GLfloat)768,0.1f,100.0f);	// Calculate The Aspect Ratio Of The Window

	glMatrixMode(GL_MODELVIEW);

}

// Idle ---------------------------------------------------------------------

GLvoid Idle(GLvoid)
{	
	glutPostRedisplay();//Makes glut re-render the scene
}


GLvoid MyGLControlClass::init(GLvoid)
{
    a = 10;
}

GLvoid MyGLControlClass::draw(GLvoid)
{
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
	glLoadIdentity();										// Reset The View
	//Lesson 2
	a++;
        if (a > 14)
            a = 0;
	glTranslatef(-1.5f,0.0f,-6.0f);                 // Move Left 1.5 Units And Into The Screen 6.0
	
	//Setup the rotation for the triangle
	glRotatef(a,0.0f,1.0f,0.0f);                 // Rotate The Triangle On The Y axis
        glRotatef(a,1.0f,0.0f,0.0f);
	//Make a Triangle
	glBegin(GL_TRIANGLES);                          // Drawing Using Triangles
			//Front Face
			glColor3f(1.0f,0.0f,0.0f);                      // Red
            glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top Of Triangle (Front)
            glColor3f(0.0f,1.0f,0.0f);                      // Green
            glVertex3f(-1.0f,-1.0f, 1.0f);                  // Left Of Triangle (Front)
            glColor3f(0.0f,0.0f,1.0f);                      // Blue
          	glVertex3f( 1.0f,-1.0f, 1.0f);                  // Right Of Triangle (Front)
			//Right Face
			glColor3f(1.0f,0.0f,0.0f);                      // Red
            glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top Of Triangle (Right)
            glColor3f(0.0f,0.0f,1.0f);                      // Blue
            glVertex3f( 1.0f,-1.0f, 1.0f);                  // Left Of Triangle (Right)
            glColor3f(0.0f,1.0f,0.0f);                      // Green
            glVertex3f( 1.0f,-1.0f, -1.0f);                 // Right Of Triangle (Right)
            //Left Face
            glColor3f(1.0f,0.0f,0.0f);                      // Red
            glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top Of Triangle (Back)
            glColor3f(0.0f,1.0f,0.0f);                      // Green
            glVertex3f( 1.0f,-1.0f, -1.0f);                 // Left Of Triangle (Back)
            glColor3f(0.0f,0.0f,1.0f);                      // Blue
            glVertex3f(-1.0f,-1.0f, -1.0f);                 // Right Of Triangle (Back)
            //Back Face
            glColor3f(1.0f,0.0f,0.0f);                      // Red
            glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top Of Triangle (Left)
            glColor3f(0.0f,0.0f,1.0f);                      // Blue
            glVertex3f(-1.0f,-1.0f,-1.0f);                  // Left Of Triangle (Left)
            glColor3f(0.0f,1.0f,0.0f);                      // Green
            glVertex3f(-1.0f,-1.0f, 1.0f);                  // Right Of Triangle (Left)
    glEnd();                                        // Finished Drawing The Triangles
    glBegin(GL_QUADS);
            glVertex3f(-1.0f,-0.5f, 1.0f);
            glVertex3f(1.0f,-0.5f, 1.0f);
            glVertex3f(1.0f,-0.5f, -1.0f);
            glVertex3f(-1.0f,-0.5f, -1.0f);
    glEnd();

    glutSwapBuffers();


}
[/CODE]    


Sponsor:

#2 Krippy2k   Members   -  Reputation: 134

Like
Likes
Like

Posted 24 July 2001 - 06:50 AM

Well the key is going to be not to use GLUT.

You can create your own object-oriented multi-threaded scheme which makes use of an absolute abstract class with pure virtual callback functions that you can override.

If you use GLUT you are probably stuck with global callbacks.

Seeya
Krippy



#3 Beer Hunter   Members   -  Reputation: 712

Like
Likes
Like

Posted 26 July 2001 - 08:21 PM

I''ve never really seen the difference between using global variables, and placing all variables and program code in a single class...

Still - if you can bear to make the class instance global, then one thing I think you can do, although it''s not beautiful, is:

  
int inline MyGLControlClass::CallbackThing(int CallbackParam1)
{
// do stuff

}

...

int CallbackThing(int CallbackParam1)
{
return ControlClass.CallbackThing(CallbackParam1);
}


#4 smitty1276   Members   -  Reputation: 560

Like
Likes
Like

Posted 26 July 2001 - 09:29 PM

I don''t think you can pass the address of a member function for a specific instance of a class (you can''t define a member function for a callback). The only way to do it is to use a static member function. That shouldn''t be a problem, though, if you are going to only have one instance of the class to hold objects anyway.

#5 JeroMiya   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 July 2001 - 08:18 AM

Ya, I can''t go without using global variables when using glut. I can, however, use native OpenGL for more flexibility, but I''m not experienced enough to go without glut yet, so the single global instance of a controller class will due for now. Thanks for the input.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS