Polygon class

Started by
5 comments, last by Max_Payne 21 years, 10 months ago
I would like opinions about one simple problem Right now, i have my own kind of map files, in which i can define both Quad (Squares, rectangles) and Tri (triangles) polygons. So you all know quads are defined by four points, triangles are defined by one. Now the question is, right now, i only have a CTriangle class, which basically holds a triangle, and it has some member functions like Draw(), GetNormal(),etc... And the quad polygons are split in two tri polygons when the map is loaded. The map is stored as a dynamic array of those CTriangle objects... The big deal is.... Should i convert CTriangle into a CPolygon class.... Which can hold any convex polygon.... So that i could store a quad in one CPolygon instead of 2 CTriangle objects. Of course, using one CPolygon will require me to draw them using GL_POLYGON, which.. isnt as fast as GL_TRIANGLES, i will also need a for loop in the drawing, but on the other hand, i am almost only using quad polygons, so this would greatly reduce the amount of polygons stored in memory..... Lower the memory use for the polygons... It would make less polygons, therefore less collision detection testing .... And only one CPolygon will be drawn instead of two CTriangle What are your opinions? Is it worth it? Will GL_POLYGON slow down the thing so much that its not worth saving 25% speed on collision detection test?
Looking for a serious game project?
www.xgameproject.com

Looking for a serious game project?
www.xgameproject.com
Advertisement
Try looking into inheritance and virtual functions. You get something like this:


    class CPolygon // parent class{    Cvertex *vertex;    virtual void draw();};class CTriangle:CPolygon //derived class{    void draw();};class CQuad:CPolygon //derived class{    void draw();};CPolygon *P  


I'm not really sure about the code but it's something like this. Take a look at the last definition: CPolygon *P. If you say:

P = new CPolygon;

whenever you call P.draw() it will use the CPolygon::draw() func. If you say:

P = new CTriangle;

calling P.draw() will execute CTriangle.draw() instead of the draw() in CPolygon. Same story for the quad class. You should write the draw() 3 times. CPolygon::draw() would have a loop drawing any size polygons using GL_POLYGON. CTriangle::draw() would use very fast GL_TRIANGLE and CQuad::draw() would use GL_TRIANGLE_STRIP (fastest way to do quads AFAIK).

Voila. One class, multiple types, fast drawing functions.




Sander Maréchal
[Lone Wolves Production][Articles][E-mail]

[edited by - smarechal on June 6, 2002 4:22:50 AM]

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I''d be very cagy about using virtual functions on a level as low as per-polygon... Virtual functions carry a run-time performance hit (as it needs to do a run-time check to work out the nature of the class it''s calling).

Using virtual functions in games is an open topic (I use them and love them, others hate them), but they should generally be restricted to higher up in the hierarchy (I subclass the Mesh class, not the Poly class).

Another minor point is that most drivers split the polygon into triangles before rendering it anyways, so you may as well keep splitting it upon load

Allan
------------------------------ BOOMZAPTry our latest game, Jewels of Cleopatra
Dont see much point in using a dynamic polygon class if i have two other classes, quad and triangle...

If i use the polygon class i'd use it as so...

CPolygon *Polygon = new CPolygon(CPoint *PointArray, int numPoints);

Then.... Since the polygon class knows how much points it has, it will internally new a vertex array to store those points. When drawing, it will perform a small for loop...

glBegin(GL_POLYGON);
for (int i = 0; i < numPoints; i++);
glVertex3fv(PointArray);
glEnd();

And as you know, for loops are very fast.

Finally, the reason i need one polygon class is because i'm using a dynamic array, and i cant have two type of objects in it.




Looking for a serious game project?
www.xgameproject.com

[edited by - Max_Payne on June 6, 2002 7:39:40 AM]

Looking for a serious game project?
www.xgameproject.com
Well, I was under the (false) impression that you didn''t know till loadtime if you needed tri''s or quads. In that case, virtuals might come in handy.

What you might want to do is make a CPolygon class for GL_TRIANGLE_STRIP instead of GL_POLYGON. Trianglestrips are faster than polygons, although slower than pure triangles. With tri-strips you can do both quads and tri''s.

If you really want the fastest way, store and render your map as a bunch of tri-strips. You''ll need an algotythm to calculate the strips from the tri/quad data though....

Sander Maréchal
[Lone Wolves Production][Articles][E-mail]

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Ya still dont see what i''m talking about, its either fixed triangles, or a polygon class.

And i wouldnt want any calculations, this class must not be limited to four sided polygons as well.

Looking for a serious game project?
www.xgameproject.com
I''ve worked with my own polygon classes before, and I recommend a single, generic CPolygon class where you can add points one at a time. If you need to know if a polygon is a triangle or not, simply check to see how many points there are in member functions such as IsTriangle() or IsQuad(). This can determine what object type to render. The only reason you would want seperate derived classes in this case is if these derived classes offer extended functionality over the generic type. I can''t think of a lot of extra functionality in quads or tris off the top of my head. Your CPolygon class can even contain a member function to split itself into triangles if you so chose.

Whatever works for you is fine, but I just don''t see the strong need for seperate classes in this case.

This topic is closed to new replies.

Advertisement