Multiple Objects

Situation: I am tyring to create a real basic graphics editor. There are two predefined 3D objects that the user can insert into the editor via a mouse menu. The editor starts out blank. Insert puts new objects centered at the origin. The problem I have is how do I store the objects? (an array of pointers of what data type?) This is stumbling me. I want calls to display() to iterate through an array and generate each object. You don’t know how many you have since it depends on how many are inserted by the user. You start out with none. Thanks in advance for all help and feedback.

I’m new to OpenGL…but I believe what may help you is a display list. The only other thing which I think may help you is if you go on the Nate Robbins Site and Download his modelling tutorials. I’m sorry I can’t be of much more help than this!

You can use display lists… or you can write a container class that contains an array of pointers to a generic class Object3D that defines an abstract method display (and others like create, scale, ecc), then inherit all types of obj (spheres, cubes, and so on). You can easily make the container class array resizable managing only the pointers.

Teofuzz is right ; use a CObject3D class and derive all you other objects from it. Then create your abstract Render class…

Eric

P.S. : you need to use C++ (or perhaps Java ? Don’t know it…) for that…

Where can one find the Cobject3D class? And what about this “render class” how do the two relate to one another? I’m really interested in this because so far, I’ve only seen proceedural © implementations of OpenGL and am beginning to wonder just how easy it is to do ANY sort of object-oriented implemtation of OpenGL. Honestly, I don’t know why someone doesn’t start with OO in their examples since that’s the way to go these days anyway. Also, could someone post a link to the “Nate Robbins” site mentioned earlier? Thanks!

Sorry I was not clear… (that’s the second time I confuse people in two posts… Best performance ever…).

You define a CObject3D class (but that is not something you can download ; you need to create your own !).

And this class will have an abstract function member (not an abstract class !) called Render…

A very basic example is as follows:

-****************************************-

class CObject3D
{

public:

float m_fRed,m_fGreen,m_fBlue;
float m_fX,m_fY,m_fZ;

CObject3D();
virtual void Render(void);
virtual ~CObject3D();

};

CObject3D::CObject3D()
{

m_fX=0;
m_fY=0;
m_fZ=0;
m_fRed=1;
m_fGreen=1;
m_fBlue=1;

}

void CObject3D::Render(void)
{
}

CObject3D::~CObject3D()
{
}

class CPoint3D ublic CObject3D
{

virtual void Render(void);
virtual ~CPoint3D();

};

void CPoint3D::Render(void)
{

glColor3f(m_fRed,m_fGreen,m_fBlue);
glBegin(GL_POINTS);
glVertex3f(m_fX,m_fY,m_fZ);
glEnd();

}

CPoint3D::~CPoint3D()
{
}

class CBox3D ublic CObject3D
{

public:

float m_fXDim,m_fYDim,m_fZDim;

CBox3D();
virtual void Render(void);
~CBox3D();

}

CBox3D::CBox3D()
{

m_fXDim=0;
m_fYDim=0;
m_fZDim=0;

}

void CBox3D::Render(void)
{

glColor3f(m_fRed,m_fGreen,m_fBlue);
glBegin(GL_QUADS);
glVertex3f(m_fX,m_fY,m_fZ);
glVertex3f(m_fX+m_fXDim,m_fY,m_fZ);
glVertex3f(m_fX+m_fXDim,m_fY+m_fYDim,m_fZ);
glVertex3f(m_fX,m_fY+m_fYDim,m_fZ);

glEnd();

}

CBox3D::~CBox3D()
{
}

-******************************************-

Phew, that was long… I can not copy/paste my source that is part of a commercial application so I had to write it by hand…

Well, this is a VERY basic implementation of the idea…

The thing is, with these objects, when you have a pointer to a CObject3D, you can call its Render function without knowing if it is a Box or a Point or whatever… THE PROPER ONE WILL BE CALLED !

That’s the great thing about C++ virtual functions… Be aware though that there is a performance hit when using virtual functions too much…

And in case you wonder why I made the destructors (the ~CObject3D, ~CPoint3D and ~CBox3D) virtual, it is simply because it is safer to do so when you start deriving objects that use virtual functions… If you do not do that, you will end up having memory leaks coz’ your destructor will not be called when they should be… Have a look at a C++ course for that matter.

OK, I hope this helped… Do not hesitate to ask further question about the code…

For OpenGL purists, please don’t tell me that what I use is not the best way of drawing a QUAD : I KNOW IT !

Best regards !

Eric

Of course, I forgot to disable smileys…

So instead of them, you should read “:p”

For instance:

class CBox3D:public CObject3D