Jump to content

  • Log In with Google      Sign In   
  • Create Account


how to I design my mesh class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 sobeit   Members   -  Reputation: 420

Like
0Likes
Like

Posted 25 June 2013 - 04:18 AM

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;
};


Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 25 June 2013 - 04:40 AM

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


#3 cozzie   Members   -  Reputation: 1580

Like
0Likes
Like

Posted 25 June 2013 - 10:17 AM

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.

#4 sobeit   Members   -  Reputation: 420

Like
0Likes
Like

Posted 25 June 2013 - 01:52 PM


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?



#5 cozzie   Members   -  Reputation: 1580

Like
0Likes
Like

Posted 25 June 2013 - 03:10 PM

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
}

#6 sobeit   Members   -  Reputation: 420

Like
0Likes
Like

Posted 25 June 2013 - 04:00 PM


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?



#7 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 25 June 2013 - 04:15 PM

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


#8 uglybdavis   Members   -  Reputation: 901

Like
1Likes
Like

Posted 25 June 2013 - 10:49 PM

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.


Edited by uglybdavis, 25 June 2013 - 10:56 PM.


#9 uglybdavis   Members   -  Reputation: 901

Like
0Likes
Like

Posted 25 June 2013 - 11:02 PM

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
		}
	}
}


#10 sobeit   Members   -  Reputation: 420

Like
0Likes
Like

Posted 26 June 2013 - 06:57 PM

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.



#11 sobeit   Members   -  Reputation: 420

Like
0Likes
Like

Posted 26 June 2013 - 07:01 PM


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

Thanks man, it really helped me out. 

by the way, do you think I need to make function "GetVectorAt" return "const float * "?



#12 uglybdavis   Members   -  Reputation: 901

Like
0Likes
Like

Posted 27 June 2013 - 04:38 AM

That entirely depends on wether or not the rest of your code base is const correct. Being const correct is a good thing (we're required at work) but for demo / prototype / learning code I never worry about it.

Much more important than const correctness or C++ 11 goodies is for you to really think about what you are trying to make and plan accordingly. Really try to separate your logic and data, it will make maintainability actually feasible ;)

#13 sobeit   Members   -  Reputation: 420

Like
0Likes
Like

Posted 27 June 2013 - 01:33 PM


Really try to separate your logic and data, it will make maintainability actually feasible ;)

Thank you very much. I'll try to apply this rule in my future work as much as possible.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS