how to I design my mesh class

Started by
11 comments, last by sobeit 10 years, 9 months ago

Hi, I want to create a class to contain mesh information and most importantly I need a way to traverse through triangles from outside. I can't think of a good way to do it. any suggestions? Thanks.

right now I mesh class is like:


struct TriIndex
{
    unsigned int v0, v1, v2;
};

class MeshNode : private GLSLutilities
{
public:
	MeshNode();
	MeshNode(const char *filename);

	~MeshNode();

	float getBoundingSphereRadius() const;
	unsigned int getTrinagleNumber() const;
	
	int draw(GLuint shaderProgram);

private:
	std::vector<TriIndex> m_face;
	std::vector<glm::vec3> m_vertex;
	std::vector<glm::vec3> m_normal;
	std::vector<glm::vec2> m_texCoord;

	//bounding sphere radius
	float m_boundingSphereRadius;
};
Advertisement
You might want to start off by mowing the loading and openly specific code to their own classes. The above mesh would represent the CPU side data. So one class interprets a mesh file (probably from memory - actual loading could be handled by a file loader) and gives you CPU side services and all.

Then you might give the CPU side mesh to your rendered which hands you something to represent the GPU side VBOs and stuff.

Something like that - split it up a bit.

o3o

A few advices you might find useful:

- make a mesh and a mesh instance class directly from start
- the mest instance class would have a (private) member of the mesh, a world matrix, world position, rotation etc.

Also try to think about how you want to handle submeshes.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me


You might want to start off by mowing the loading and openly specific code to their own classes. The above mesh would represent the CPU side data. So one class interprets a mesh file (probably from memory - actual loading could be handled by a file loader) and gives you CPU side services and all.

Thank you, I'll try what you suggested. but right now my problem is I don't know how to access triangle information of a mesh from outside.

I was thinking of creating a function returning triangle index like "std::vector<TriIndex> getTriangleIndex() " and another function to get vetices of triangle like "glm::vec3 getVertex(int index)". But I don't think it's a good practice. My algorithm needs to traverse all triangle vertices positions and normals to procedurally create an ivy on the mesh.


- make a mesh and a mesh instance class directly from start

cozzie, I don't quite understand what you mean by "mesh instance class". Is it a class that has mesh class as member and other helper functions?

You will likely want to have more instances of a mesh in your 3d scene/ world.
For example you have a mesh (3d model) of the tree, you can store that in a mesh class object, with a vertexbuffer, indexbuffer, id3dxmesh, possibly textures and materials.

If you also have a class for an instance of a mesh, with a world position, scale, rotation, worldmatrix etc.,you can create 10 objects of the tree fairly easy, and do anything you want with it. With the big advantage that you only need to have/ load the mesh once.

Pseudo code:

class myMesh
{
bufffers
id3dxmesh
int meshid
Void loadmesh
Etc.
}

class myMeshInstance
{
int meshid;

Vector position
Vector rotation
Matrix mWorld
Etc.

Void render()
Void updateworldmatrix
Etc
}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me


If you also have a class for an instance of a mesh, with a world position, scale, rotation, worldmatrix etc.,you can create 10 objects of the tree fairly easy, and do anything you want with it. With the big advantage that you only need to have/ load the mesh once.

Thank you. I'll apply this in my mesh class. but do you have any idea for how to access the triangle information of a mesh from outside, say like I may need to traverse vertices of every triangle in a mesh to create an ivy climbing on the mesh. how can I do that?

Use C++11 lambdas to implement a foreach method (basically a callback that it calls for each vertex, possibly either extra data)

If you do use this make sure you make the callback type a template parameter - std::function is slow.

o3o

You should look at a 3D format like collada. Maybe milkshape would be easier to start with.

It's a tad harder, but assimp is interesting to work with, definateley has good data layout.

You will save yourself a LOT of toruble by seperating a mesh into mesh data (vertex soup), submesh data (indices into the soup), skeleton data and animation data. Assuming at some point you want to animate these things.


struct MeshData {
    // Positions, normals, uv's basically one big mesh soup
    unsigned int m_nVertexBuffer;
};

struct SubmeshData {
    // Just indices into the mesh soup
    unsigned int m_nIndexBuffer;
    unsigned int m_nMaterialIndex; // Indexes into mesh classes materials
};

struct Mesh { // Tough, i'd call this 'struct Model'
    MeshData mesh;
    std::vector<SubmeshData> submeshes;
    SkeletonData skeleton;
    std::vector<ClipData> animations;
    std::vector<Material> materials; // All the materials submeshes may use
    // Materials are shaders with some way of setting uniform data.
};

Rendering would be something like:


void RenderMesh(Mesh& mesh) {
   mesh.BindVertexBuffer();
   for (int i = 0; i < mesh.submeshes; ++i) {
      Submesh& submesh = mesh.submeshes[i];
      submesh.BindMaterial();
      submesh.DrawIndexBuffer();
      submesh.UnbindMaterial();
   }
   mesh.UnbindVertexBuffer();
}

Keep in mind, there is no logic up there. It's all data! you would also need something to represent the current animation state. Also, vbo's are useful when animating on the gpu, you will need to hold on to vertex data for cpu animation.

Edit: Did not read your post about needing to traverse triangles from outside, sorry.

Here is how i'd change my last post after reading your original question:


struct Vertex {
	float m_pPosition[3];
	float m_pNormal[3];
	float m_pUv[2];
};

struct MeshData {
    std::vector<Vertex> m_vVertices;

	float* GetVectorAt(int i) {
		return m_vVertices[i].m_pPosition;
	}
};

struct SubmeshData {
    std::vector<unsigned int> m_vIndices;
	
	int GetNumIndices() {
		return m_vIndices.size();
	}
};

struct Mesh {
    MeshData mesh;
    std::vector<SubmeshData> submeshes;
    SkeletonData skeleton;
    std::vector<ClipData> animations;
    std::vector<Material> materials;
};

And then the update junk:


void DoSomeWork(Mesh& mesh) {
	for (int i = 0; i < mesh.submeshes.size(); ++i) {
		for (int j = 0; j < mesh.submeshes[i].GetNumIndices(); ++j) {
			float* position = mesh.GetVectorAt(mesh.submeshes[i].m_vIndices[j]);
			
			// Do stuff with position
		}
	}
}

Use C++11 lambdas to implement a foreach method (basically a callback that it calls for each vertex, possibly either extra data)

If you do use this make sure you make the callback type a template parameter - std::function is slow.

I checked lambda out, it's really powerful. Thank you.

This topic is closed to new replies.

Advertisement