S
Steven T. Hatton
I am trying to convert some basic OpenGL code to an OO form. This is the C
version of the program:
http://www.opengl.org/resources/code/basics/redbook/double.c
You can see what my current effort at converting that code to an OO form
looks like in the code listed below. The problem I'm running into is that
the OpenGL functions that take the names of other functions as arguments
don't like the way I'm passing the member functions of my class. I started
out making all member functions non-static, but couldn't figure out a way
to pass them to functions such as glutDisplayFunc(&GlutMain::display);
Is there a way to accomplish this?
#ifndef GLUTMAIN_HH
#define GLUTMAIN_HH
#include <GL/glut.h>
namespace sth {
namespace vmathGL {
class GlutMain {
public:
GlutMain(int argc, char* argv[]);
virtual ~GlutMain();
void run();
static void init();
static void display();
static void reshape(int w, int h);
static void mouse(int button, int state, int x, int y);
static void key(unsigned char k, int x, int y);
static void spinDisplay();
private:
static GLfloat _spin;
};
}
}
#endif
#include "GlutMain.hh"
#include <cstdlib>
namespace sth {
namespace vmathGL {
GlutMain::GlutMain(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250,250);
}
GlutMain::~GlutMain()
{
}
void GlutMain::run()
{
init();
glutDisplayFunc(&GlutMain::display); // compiles but segfaults.
/*
glutReshapeFunc(&reshape);
glutMouseFunc(&mouse);
glutMainLoop();
*/
}
void GlutMain::display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(_spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
void GlutMain::init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void GlutMain::reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GlutMain::mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
void GlutMain::key(unsigned char k, int x, int y)
{
switch (k) {
case 27: /* Escape */
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
void GlutMain::spinDisplay()
{
_spin = _spin + 2.0;
if (_spin > 360.0)
_spin = _spin - 360.0;
glutPostRedisplay();
}
GLfloat GlutMain::_spin = 0.0;
}
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
version of the program:
http://www.opengl.org/resources/code/basics/redbook/double.c
You can see what my current effort at converting that code to an OO form
looks like in the code listed below. The problem I'm running into is that
the OpenGL functions that take the names of other functions as arguments
don't like the way I'm passing the member functions of my class. I started
out making all member functions non-static, but couldn't figure out a way
to pass them to functions such as glutDisplayFunc(&GlutMain::display);
Is there a way to accomplish this?
#ifndef GLUTMAIN_HH
#define GLUTMAIN_HH
#include <GL/glut.h>
namespace sth {
namespace vmathGL {
class GlutMain {
public:
GlutMain(int argc, char* argv[]);
virtual ~GlutMain();
void run();
static void init();
static void display();
static void reshape(int w, int h);
static void mouse(int button, int state, int x, int y);
static void key(unsigned char k, int x, int y);
static void spinDisplay();
private:
static GLfloat _spin;
};
}
}
#endif
#include "GlutMain.hh"
#include <cstdlib>
namespace sth {
namespace vmathGL {
GlutMain::GlutMain(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250,250);
}
GlutMain::~GlutMain()
{
}
void GlutMain::run()
{
init();
glutDisplayFunc(&GlutMain::display); // compiles but segfaults.
/*
glutReshapeFunc(&reshape);
glutMouseFunc(&mouse);
glutMainLoop();
*/
}
void GlutMain::display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(_spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
void GlutMain::init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void GlutMain::reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GlutMain::mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
void GlutMain::key(unsigned char k, int x, int y)
{
switch (k) {
case 27: /* Escape */
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
void GlutMain::spinDisplay()
{
_spin = _spin + 2.0;
if (_spin > 360.0)
_spin = _spin - 360.0;
glutPostRedisplay();
}
GLfloat GlutMain::_spin = 0.0;
}
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell