• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sobeit

how to I design my mesh class

12 posts in this topic

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites


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?

0

Share this post


Link to post
Share on other sites
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
}
0

Share this post


Link to post
Share on other sites


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?

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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 * "?

0

Share this post


Link to post
Share on other sites
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 ;)
0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0