Sign in to follow this  

GLUT Wrapper

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I decided one day that I should wrap my GLUT functionality into a class. I then found out this is a LOT more confusing than I ever thought it would be. After looking at many things on the internet I have it working just about with the use of lots of static things which obviously is not ideal, however as long as its all simple and reusable and I can reduce copy and pasting to about 3 lines instead of large chunks of code then I am happy(ish).

The problem I have now is that the update function is within the GLUT Wrapper class and I don't really want to change that file for every different project. Is there a way I can pass a pointer to a function into the initialise of the class so it uses an update function specified in the main instead of in the class???? I would also want to do this for my draw and keyboard read functions.

Here's some code (it doesn't look very pretty but this GLUT malarkey seems like a lot more hassle then I first thought.)

GLUTWrapper.h

#pragma once
#include "StandardIncludes.h"
#include "FPSCounter.h"

static float windowHeight = 0;
static float windowWidth = 0;

class GLUTWrapper
{
public:
GLUTWrapper(int argWindowWidth, int argWindowHeight);
~GLUTWrapper(void);

void Initialise(int argc, char **argv);

static void WinReshapeFunc(GLint w, GLint h);
void Cleanup();
static void HandleKeyPress(unsigned char key, int x, int y);

static void Draw();
static void Update(int value);

static void ProcessMousePassiveMotion(int x, int y);
static void ProcessMouse(int button, int state, int x, int y);

static int GetWindowWidth() { return windowWidth; }
static void SetWindowWidth(float value) { windowWidth = value; }
static int GetWindowHeight() { return windowHeight; }
static void SetWindowHeight(float value) { windowHeight = value; }


private:
Vector3f cameraPosition;
Vector3f cameraPositionSet;

Vector3f lookAtPosition;

float elapsedTime;
float prevElapsedTime;

Vector2f mousePosition;
//FPSCounter* fpsCount;

int FPS;
};




GLUTWrapper.cpp


#include "GLUTWrapper.h"
#include <ctime>

GLUTWrapper::GLUTWrapper(int argWindowWidth, int argWindowHeight)
{
gravityActive = true;

elapsedTime = 0;
prevElapsedTime = 0;
const int FPS = 60;

windowWidth = argWindowWidth; windowHeight = argWindowHeight;
//fpsCount = new FPSCount();
}


GLUTWrapper::~GLUTWrapper(void)
{
}

void GLUTWrapper::Initialise(int argc, char **argv)
{
//glutFullScreen();

//glutSetCursor(GLUT_CURSOR_NONE); //hide the cursor
glEnable ( GL_LIGHTING ) ; // Enable the lighting
glFrontFace(GL_CCW);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

/* initialize random seed: */
srand ( time(0) );

glClearColor(1.0,1.0,1.0,0.0);
glutInitDisplayMode(GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH);
glShadeModel(GL_SMOOTH); //uses GL_SMOOTH or GL_FLAT
glEnable(GL_POLYGON_SMOOTH);

// initialise the glut library
glutInit(&argc, argv);

// set up the initial display mode
// need both double buffering and z-buffering
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

// set the initial window position
glutInitWindowPosition(0, 0);
// set the initial window size
glutInitWindowSize(windowWidth, windowHeight);
// create and name the window
glutCreateWindow("MarbleMaze");
// reshape callback for current window
glutReshapeFunc(WinReshapeFunc);
// set display callback for current window
glutDisplayFunc(Draw);
glutKeyboardFunc(HandleKeyPress);
glutMouseFunc(ProcessMouse); //Process mouse clicks
glutPassiveMotionFunc(ProcessMousePassiveMotion); //Process mouse movement

glutTimerFunc(25, Update, 0);

// enter the glut event processing loop
glutMainLoop();

}

void GLUTWrapper::WinReshapeFunc(GLint w, GLint h)
{
windowWidth = (float)w;
windowHeight = (float)h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0, windowWidth, 0, windowHeight, 1, 1000);
gluPerspective(45.0, windowWidth / windowHeight, 1.0, 1000.0);
}

void GLUTWrapper::Cleanup()
{
}
void GLUTWrapper::HandleKeyPress(unsigned char key, int x, int y)
{
switch (key)
{
case 27: //Escape key
//Cleanup();
exit(0);
break;


}
}

void GLUTWrapper::Draw()
{
// clear the back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0, windowWidth / windowHeight, 1.0, 1500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
// lookAtPosition.x, lookAtPosition.y, 0,
// //(windowWidth / (currentLevel->GetMazeWidth() * 0.5f)), (windowHeight / (currentLevel->GetMazeHeight() * 0.5f)), 0.0,
// 0.0, 1.0, 0.0);

// swap the buffers of the current window
glutSwapBuffers();
}

void GLUTWrapper::Update(int value)
{
glutPostRedisplay();
glutTimerFunc(25, Update, 0);
}

void GLUTWrapper::ProcessMousePassiveMotion(int x, int y)
{
}

void GLUTWrapper::ProcessMouse(int button, int state, int x, int y)
{
// if a mouse button is down
if (state == GLUT_DOWN)
{
if (button == GLUT_LEFT_BUTTON) //If the left button is down
{
}
if (button == GLUT_RIGHT_BUTTON) //If the right button is down
{

}
}
}




Many thanks!

Share this post


Link to post
Share on other sites
Good morning.

To answer your question first, yes you can pass a pointer to a function as a parameter of a function. You could then use the function pointer to call the function in the sameway as you would call the function. If you're unfamiliar with pointers to functions then you may want to do some googling on the subject. There are many more resources on the web that explain them much more completely than I could in a minute here.

You could try:
http://www.cprogramming.com/tutorial/function-pointers.html
or possibly
http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557


Having said all that, are you sure that is what you really want to do? C++ supports virtual functions and inheritance which can be used to do nearly the same thing.

And lastly, is there a particular reason that you defined the windowHeight and windowWidth as static global variables?

(EDIT: Spelling)

[Edited by - JasonR on December 27, 2010 9:09:20 AM]

Share this post


Link to post
Share on other sites
Many thanks for the reply JasonR,

1st - thanks for the articles on function pointers, very interesting.

2nd - You are right I think overriding functions is the way like is done in this article:
http://paulsolt.com/2009/07/openglglut-classes-oop-and-problems/

I made the window width and height global because it was giving me link errors when I tried to make them static member variables and I couldn't figure out why at all.

To tell the truth all this hassle has persuaded me to look into other options instead of GLUT as it just seems to be complication after complication so I'm now looking into GLFW as I think it will be worth it in the long run.

Thanks :-)

Share this post


Link to post
Share on other sites

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this