• Popular Now

• 11
• 27
• 9
• 20
• 31

Archived

This topic is now archived and is closed to further replies.

Polygon class

This topic is 5762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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]

Share on other sites
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

Share on other sites
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]

Share on other sites
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]

Share on other sites
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.