Advertisement

Mesh Rendering for Vertex Array, Display List and VBO

Started by July 13, 2005 01:44 PM
4 comments, last by Steve132 19 years, 2 months ago
I'd like to unify these 3 vertex storage methods under a single MESH class. Ideally I could assign either a Vertex Array, a Display List or a VBO to one object and it would make no difference to my Scene Graph Traversal and Display routines. Here's what I have so far, but I can't decide whether I should make the MESH store a pointer to the appropriate rendering method, it doesn't seem right.. What do you make of this?
class MESH {
      void	*VertexData;						// Vertex Data Reference
      GLuint	Texture;			                        // Texture Reference
protected:
      void (*RenderMethod) (MESH);
};
What would you suggest I do for something like this?
What I would do, is have a virtual base class called Mesh_Storage_Method that would have virtual functions able to do the things that all of them could do, like load up the data into the mesh, prep the data for immediate rendering, display the data, unprep the data, and unload the data. Then, you would make three classes that derive from this class, one for Display_List, one for Vertex_Array, and one for VBO. In each of these child classes, you implement the base functions for actual code. Finally, your mesh class has a pointer to a Mesh_Storage_Method that would be initialized to one of the three derived classes at mesh initialization time. Polymorphism will make the pointer automatically use the correct type.

ex:
class Mesh_Storage_Method{public:virtual int load_verts(verts) =0;virtual int render() =0;virtual int destroy_verts()=0;};class Vertex_Array: public Mesh_Storage_Method{public:int load_verts(verts A){//implementation}int render(){//implementation}int destroy_vers(verts A){//implementation}};class Vertex_Buffer_Object: public Mesh_Storage_Method{public:int load_verts(verts A){//implementation}int render(){//implementation}int destroy_vers(verts A){//implementation}};class Display_List: public Mesh_Storage_Method{public:int load_verts(verts A){//implementation}int render();{//implementation}int destroy_vers(verts A){//implementation}};class MESH{private:Mesh_Storage_Method* MSM;public:int initialize(string selected_method){if(selected_method=="VA")MSM = new Vertex_Array();else if(selected_method=="VBO")MSM = new Vertex_Buffer_Object();elseMSM = new Display_List();MSM->load_verts();MSM->Display();MSM->destroy_verts();}};


Advertisement
Ah, I'll give that a try. Many thanks for clearing that up!
So the "verts" datatype is a regular Vertex Array? Should I use a float array instead like so?


class Mesh_Storage_Method{public:	virtual int load_verts(float[]) =0;	...};class Vertex_Array: public Mesh_Storage_Method{public:	int load_verts(float A[])	{//implementation}	...};
Quote: Original post by Shanjaq
So the "verts" datatype is a regular Vertex Array? Should I use a float array instead like so?


*** Source Snippet Removed ***


It's completly up to you.

Think about the concequences of doing it that way. Think about how when you use the method properly, what the implications could be. then, then try and think of the next possible use, and the next. But, at the same time, try and think of a way to leave it open for extension later on. Trust me it's much harder than just writing the first code that comes to mind. This is the challenge of creating a flexible design [smile] It's what most programmers live for

Personally I don't see too much potential problem. Unless, for example, you wanted to have some ubyte4 data types in there, etc. Not impossible to overcome, just a tad inconvienient. I would have also made it private, and had the constructor of the base class call the method. But thats my opinion, and opinions are genrally something you should take as a learning opportunity not as fact. [smile]

However, I'll give you a hint on this one, one thing that does immediatly pop into my mind when I look at that deisgn, is one of those little catches in C++... That unmanaged arrays suffer from...
In my code example, I had no idea how you are actually getting the vertex data into the mesh structure, so I made up a data type to handle it. A float array might do it (but personally I would be wary of trying to figure out how long an array is) but that type could be anything from a series of triangles to a series of raw vertices to a model filename to load. However you plan on doing it is fine.

This topic is closed to new replies.

Advertisement