Object Oriented Programming with OpenGL

Well this is my first post. Let’s see…

I want to structure OpenGL with OOP. I’m working on a human-computer interface that will include real-time dials, charts, gauges, blah, blah, blah…

I have no problems working with OOP but I have problems transforming all the OpenGL code into a modular framework that will alow me to have modular blocks of my graphical elements.

A good answer to my question would be a simple (but complete?) void main() and a generic class structure with the names of the OpenGL functions that go there.

Thank you all and I hope to help someone one day.

F.

Ok here is my suggestions on the subject.

Do all gl programing in one file. Then create a .h file that uses the extern keyword to make those functions available to the rest of the program.

For example, put funtions like

int LoadGLTextures();
int InitGL();
void ShowGLGauge();

in one file and then build a .h file that says

extern int LoadGLTextures();
extern int InitGL();
extern void ShowGLGauge();

All you have to do then to give all your classes access to opengl is give them access to the .h file. You might also want to think of setting up a .h file for the rest of your functions to give the GL file access to them.

Hi Frank,
I’ve been using this very simple light class I put together. It allows me to create, destroy and modify GL lighting as needed. This is my “first” attempt at abastracting a single facet of OpenGL.

“parent”, passed thru the overloaded constructor is optional. I could have just a easily passed the current light index in. But its nice to have a pointer to the Almighty creator laying around.

If your planning on wrapping the entire API, email me when you get done :P. Id like to see it. Ive seen a couple attempts at this, but the interfaces werent much of an improvement over GLs. Not that my attempt at abstraction is any better

Good luck, & have fun

// Light.h: interface for the CLight class.

#define M_BASE 0x4000

class CLight : public CObject
{
public:
CLight();
CLight(CProBotDoc* parent);
virtual ~CLight();
void GetPosition(GLdouble* position);
void GetAmbiance(GLfloat* ambiance);
void GetDiffuse(GLfloat* diffuse);
void GetSpecular(GLfloat* specular);
void SetPosition(GLdouble* position);
void SetSpecular(GLfloat* specular);
void SetDiffuse(GLfloat* diffuse);
void SetAmbiance(GLfloat* ambiance);
void ON(BOOL state = TRUE);
void SetDefault();
CProBotDoc* m_parent;

//Inline
public:

CString GetName(){return m_name;}
int GetIndex(){return m_indexID;}
void SetName(CString name){m_name = name;}

protected:
GLfloat m_ambiance[4];
GLfloat m_diffuse[4];
GLfloat m_specular[4];
GLfloat m_position[4];
int m_indexID;
CString m_name;
BOOL m_ON;

private:
void SetIndexID();

};

// Light.cpp: implementation of the CLight class.

#include “Light.h”
#include “ProBotDoc.h”

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=FILE;
#define new DEBUG_NEW
#endif

// Construction/Destruction

CLight::CLight()
{
}

CLight::~CLight()
{

}

CLight::CLight(CProBotDoc *parent)
{
//Set default Lighting
m_parent = parent;
SetIndexID();
SetDefault();
}

void CLight::GetSpecular(GLfloat *specular)
{
int i = 0;
while (i++ < 4)
specular[i] = m_specular[i];
}

void CLight::GetDiffuse(GLfloat *diffuse)
{
int i = 0;
while (i++ < 4)
diffuse[i] = m_diffuse[i];
}

void CLight::GetAmbiance(GLfloat *ambiance)
{
int i = 0;
while (i++ < 4)
ambiance[i] = m_ambiance[i];
}

void CLight::GetPosition(GLdouble *position)
{
int i = 0;
while (i++ < 4)
position[i] = m_position[i];
}

void CLight::SetDefault()
{
m_ON = TRUE;

m_ambiance[0] =	.01; 
m_ambiance[1] =	.01;
m_ambiance[2] =	.01;
m_ambiance[3] =	.01;

m_diffuse[0] = .5;
m_diffuse[1] = .5;
m_diffuse[2] = .5;
m_diffuse[3] = 0;

m_specular[0] = .1;
m_specular[1] =	.1;
m_specular[2] =	.1;
m_specular[3] =	0;

m_position[0] =	0;
m_position[1] =	0;
m_position[2] =	100.;
m_position[3] =	1;

glLightfv(M_BASE + m_indexID, GL_AMBIENT, m_ambiance);
glLightfv(M_BASE + m_indexID, GL_DIFFUSE, m_diffuse);
glLightfv(M_BASE + m_indexID, GL_SPECULAR, m_specular);
glLightfv(M_BASE + m_indexID, GL_POSITION, m_position);

glEnable(M_BASE + m_indexID);

}

void CLight::SetIndexID()
{
CString tmp;
m_indexID = m_parent->GetNumLights() + 1;
tmp.Format(“Light%d”, m_indexID);
m_name = tmp;
}

void CLight::SetAmbiance(GLfloat *ambiance)
{
int i = 0;
while (i++ < 4)
{
m_ambiance[i] = ambiance[i];
}
glLightfv(M_BASE + m_indexID, GL_AMBIENT, m_ambiance);
}

void CLight::SetDiffuse(GLfloat *diffuse)
{
int i = 0;
while (i++ < 4)
{
m_diffuse[i] = diffuse[i];
}
glLightfv(M_BASE + m_indexID, GL_DIFFUSE, m_diffuse);
}

void CLight::SetSpecular(GLfloat *specular)
{
int i = 0;
while (i++ < 4)
{
m_specular[i] = specular[i];
}
glLightfv(M_BASE + m_indexID, GL_SPECULAR, m_specular);
}

void CLight::SetPosition(GLdouble *position)
{
int i = 0;
while (i++ < 4)
{
position[i] = m_position[i];
}
glLightfv(M_BASE + m_indexID, GL_POSITION, m_position);
}

void CLight::ON(BOOL state)
{
if (state)
glEnable(M_BASE + m_indexID);
else
glDisable(M_BASE + m_indexID);
}

Ummmmm, dont pay much attention to that previous post. I was hacking an older(lousy) backup when I cut & pasted it here. Its uhhh missing a few very important things!

Sorry bout that…

I have great news
and very bad news

There is an extension for OpenGL that is FULLY OBJECT ORIENTED called Qt.

It’s free only for X11 platform.

I found the links at my school (École Polytechnique de Montreal).
The company that makes Qt is Trolltech.

I guess I should give up!
Take care and thank you for your time,

F.
http://www.trolltech.com/products/qt/qt.html
http://www.trolltech.com/products/purchase/pricing.html

Never Give Up!

I should pay more attention to the front page of the site!

This is a OpenGL C++ Object Oriented Wrapper!
http://www.ugcs.caltech.edu/~dazuma/glow/

F.

See any graph API like SGI’s Cosmo3D…