Skip to main content
gamedev.net
🔒 Locked

Shape hierarchy

Started by Mybowlcut 0 replies 1.1k views
Original Post
Mybowlcut
Mybowlcut
Hey. I'm doing an intro to 3d programming unit at uni and we're using C++ with opengl. I've decided to make up shape classes to speed up the work that I'll be doing. I'm a bit unsure how to structure the shape hierarchy though. I have the base Shape_3d abstract class:
class Shape_3d : public GL_Renderable
{
public:
    Shape_3d();
    Shape_3d(const Point_3d& pos);
    virtual ~Shape_3d() = 0;

    virtual void Render() = 0;

    const Point_3d& Get_Pos() const;
protected:
    Point_3d pos;
};



At this point, I am unsure where to go next. So, I have a few questions: I plan to have some shapes that have a vector of vertices (e.g. cube)... is this a good idea or should the vertices for cubes and stuff be rendered/transformed dynamically? If I do store the vertices in a vector, should I make a base class that saves each class declaring a vector of vertices? If I want to have a set of functions to translate/scale/rotate a shape after it is constructed, should I put them in a namespace or a base class as virtual/pure virtual functions? I want to be mindful of the "model inheritance through "has-a" or "is-implement-in-terms-of" through composition" rule of thumb (as seen in Effective C++). Is it wise/possible to do this in my scenario? Basically I have a whole heap of behaviour I want derived shape classes to be able to have and I want to a nice way to implement it. Cheers.

Mybowlcut
Mybowlcut
Another few questions...

I'm doing the Cube class now, and I'm wondering if having the vertices static like that is the way to go or not?
const short Cube::CUBE_VERTICES[QTY_CUBE_VERTICES] ={    1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, // TOP    1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, // BOTTOM    1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, // FRONT    1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, // BACK    -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, // LEFT    1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1 // RIGHT};Cube::Cube(){}Cube::Cube(const Point_3d& pos, const Size_3d& size, const Colour& colour): Shape_3d(pos, colour), size(size){}void Cube::Render(){    glPushMatrix();    glColor3d(colour.r, colour.g, colour.b);    glTranslated(pos.x, pos.y, pos.z);    glScaled(size.w, size.h, size.d);    glDrawElements(GL_QUADS, QTY_CUBE_VERTICES, GL_UNSIGNED_SHORT, CUBE_VERTICES);    glScaled(-size.w, -size.h, -size.d);    glTranslated(-pos.x, -pos.y, -pos.z);    glPopMatrix();}
Static members are declared as:
class Cube : public Shape_3d{public:    Cube();    Cube(const Point_3d& pos, const Size_3d& size, const Colour& colour);    virtual void Render();protected:    Size_3d size;private:    // Cubes all have the same amount of vertices.    // 3 vertices per corner * 4 corners * 6 sides.    static const unsigned short QTY_CUBE_VERTICES = 3 * 4  * 6;    static const short CUBE_VERTICES[QTY_CUBE_VERTICES];};
I think it looks a lot neater than doing each vertex manually.

The problem with the Cube render function is that it's not displaying... first of all I realised that I can't send signed integrals to glDrawElements... so how else can I draw the vertices of the cube? I was thinking something like this:
void Cube::Render(){    glPushMatrix();    glColor3d(colour.r, colour.g, colour.b);    glTranslated(pos.x, pos.y, pos.z);    glScaled(size.w, size.h, size.d);    glBegin(GL_QUADS);    for(unsigned short i = 3; i < QTY_CUBE_VERTICES; i += 3)    {        glVertex3s(CUBE_VERTICES[i - 3], CUBE_VERTICES[i - 2], CUBE_VERTICES[i - 1]);    }    glEnd();    glScaled(-size.w, -size.h, -size.d);    glTranslated(-pos.x, -pos.y, -pos.z);    glPopMatrix();}


I was also thinking of using displayLists. I thought that a static function (eg.g. Load_Display_List()) could be called on each class that I want to use display lists for rendering, storing the display list id in a static variable. The only down side to this is that a check would have to be performed to see if the id is valid so that the display list can be used.. each time render is called!

A lot of questions but yeah... cheers!

[Edited by - Mybowlcut on March 6, 2009 7:16:29 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.