Skip to main content
gamedev.net
🔒 Locked

Sharing vertex buffer between modells

Started by Scheermesje Apr 17, 2004 at 1:07 PM 4 replies 2.6k views
Original Post
Scheermesje
Scheermesje
I''ve started a new project where I now have a system where I can do something like: CMObject Cube; Cube.setup(data); The problem is when I create an array of cubes they will all have their own instance of vertex/index data but I think that''s a waste of memmory. So I thought it would be nice to have something like this: CMesh *Cube = new Cube[10]; And this 10 cubes would share their vertex/index data but would have their own data like if they are visible, position and all the other data an object would have on its own. I thougt I could overload the new operator to achieve this but I noticed that if I overloaded the new operator I coulnd''t do something like this: void *CObject::operator new[](size_t sizeo) { Mesh = new CMesh(); position = new CPosition[size] return 0; } I thought I could do it so that all the objects would have an position of their own(the positon array) and share their mesh data but C++ doesn''t allow it to acces non-static data from a overloaded new function. Is there anything I can do to make this work? Maybe someone has another idea how I could share data between different objects? Thnx for your help
SoulSpectre
SoulSpectre
In my engine i have gone a diferent approach. I use many independent objects to form my graphics objects. A graphics object would consist of many smaller objects like a mesh object, texture object, effects object etc. The graphics objects themselves are really nothing more than simple containers that link these objects by pointer. For example i can have only one mesh object but have many graphics objects link to that one mesh object. When the scene is rendered the mesh object is only written once to the vb/ib buffers per frame (if they are dynamic) no matter how many graphics objects link it. By sharing only one mesh object i can make changes to that mesh it and all the graphics objects that link to it will share the same effect. This is very handy for camera objects and effects objects. All i have to do is alter the camera object (for example move it to a new position in space) and all those objects that use it will be displayed correctly on render, and all it took was one object alteration.

Scheermesje
Scheermesje
How have you linked your mesh object to your different graphics objects?

have you done something like:

Mesh newmesh;
Object newobject;
newobject.setMesh(newmesh);

And repeat that or every object that uses this mesh?
SoulSpectre
SoulSpectre
yes, I also have a clone and copy methods for my objects that allow them to create a clone (uses the same objects) or copies them (creates copies of the objects).I have like:

cObject object;
cMesh Mesh;
cTexture Texture;
cEffect Effect; //and so on
cTransform Transform;

//overloading allows me to use the set function name for different types

object.set(mesh);
object.set(texture);
object.set(transform);

cObject ob2.
ob2.set(mesh) //etc OR
ob2.clone(object); //uses all the stuff from object OR
ob2.copy(object); //create a new copy of all the stuff in object

//or get fancy and
ob2.set(ob1.GetTexture());
ob2.set(ob3.GetEffect());
ob2.set(transform);

psykr
psykr
That''s the way I''m going with my current engine, sharing mesh data with GeometryChunk objects, using a combination of reference counting and smart pointers, which basically handles the objects automatically most of the time. I think you should find a better design for your engine than creating one vertex buffer for each 8-vertex mesh, though. For instance, you could have many different meshes sharing the same vertex buffer, and render them with offsets into the buffer.
Scheermesje
Scheermesje
I read some things about offsets into the buffer but I was wondering.. If I have a big buffer which has for example a few differtend modells and I wanted to render modell X how should I know where it is located into the buffer?

If you have a few modells which all have 1000 vertices then modell 3 should start at 2*1000 to 3*1000 but do you guys just store some variable in each mesh to know where it starts in your VB?

Topic Locked

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

Sign in to reply to this topic.