Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ideas on how to implement a flexible Triangle and Mesh class

Started by WillPash Aug 30, 2009 at 7:27 PM 2 replies 1.3k views
Original Post
WillPash
WillPash
I am developing a mesh class. I am in two minds of how to implement the triangle class. I have two different ideas on how I want to develop it. I dont know if I should implement a triangle class with it containing the three vertex points, or a triangle with the three indices values. This is just a rundown on the two approaches I am considering working with (this is C++ pseudocode, not the total final solution, just a single brief idea on how i am implementing). This is a quick run-through as it is late :)

Approach Plan A

class Triangle
{
public:
Triangle(Point _A,Point _B Point _C)
{
	Points[0] = _A;
	Points[1] = _B;
	Points[2] = _C;
}
Point Points[3]; //where the three vertices are stored 
};

Once I have generated a list of triangles from my model loader, I will place the data inside a list container full of triangles(would a vector container make a difference!). I will send that container object to the mesh class object (see below)
class Mesh
{
	public:
	Mesh(list<Triangle> _tri)
	{
		triangles = _tri;
	}
	list<Triangle> triangles;// stores the triangles that make the mesh
};

Approach Plan B

I am creating a class which stores the indices inside the triangle class. This article gave me a few ideas on how to do a indices in a triangle.
class Triangle
{
public:
Triangle(unsigned short _A,unsigned short _B unsigned short _C)
{
	indices[0] = _A;
	indices[1] = _B;
	indices[2] = _C;
}
unsigned short indices[3]; //where the three vertices are stored 
};

The triangle class inside the mesh class will be independent from the points vertex data.(see below)
class Mesh
{
	public:
	Mesh(list<Triangle> _tri, vector<Point> _points)
	{
		triangles = _tri;
		points = _points;
	}
	list<Triangle> triangles; // stores the triangles that make the mesh
	vector<Point> points;
};

I am looking for suggestion on how I should implement the triangle and mesh class. The pseudocode shown is just the simplified idea of what i'm working on. I also am looking on working on which one would be futureproof for functions i am including later for example animation and bone rigging, vertex welding, etc.
Dave 'Kit' Wilson - Reliant Code
AndyFirth
AndyFirth
is this for processing of mesh data or rendering of it?
WillPash
WillPash
Sorry I should have been more clear... It is for rendering. It will be rendering initially in D3D10, I am at the moment concentrating on making sure basic static models can load and render onto the scene. I want to build this for more complex objects. I also want to get the triangles to be capable of sub-division, but I will get to that when I finalise which approach i will be using for the rendering.
Dave 'Kit' Wilson - Reliant Code
tomva
tomva
I don't recommend using list<> templates for triangle meshes. You'll probably want to support very large meshes in general, and a list<> has extra overhead per element that will get in your way.

Since this is for rendering, you're probably loading the list of triangles once, and then rendering them many times. So instead of a list<>, use a vector<> or your own managed array.

I recommend separate point and triangle arrays. You'll have a bunch of triangles that share the same vertices, and keeping separate lists means you'll probably save a lot of space overall. You'll have to store indexes to each vertex with each triangle, but indices are small (2-4 bytes) and points are large (~12 bytes) so it's usually worth it.

Also, you may have to do things like transform vertices per render step. That is also faster to do once. That is, sharing vertices means you'll do less vertex processing per render iteration.

Here's my own triangle mesh management class: http://wavepacket-lib.svn.sourceforge.net/viewvc/wavepacket-lib/lib/trimesh/

It's used for processing, not rendering, but you get the idea.

Topic Locked

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

Sign in to reply to this topic.