Vertex structures

Started by
3 comments, last by steg 19 years, 6 months ago
Hi all, I have been wondering what is the best way to design a 'shape' class which will render itself using Direct3D. I am confused with the vertex structure to use, as some shapes would require texture co-ordinates, normals while others won't. Example : struct Vertex { float x,y,z; D3DXVECTOR3 Normal; }; // Main parent class class CShape { public: virtual ~CShape(); virtual void Render() = 0; virtual void Init() = 0; protected: float m_XPos; // world position of object float m_YPos; IDirect3DVertexBuffer9* m_pVB; IDirect3DIndexBuffer9* m_pVB; }; // A Box object class CBox : public CShape { public: Vertex* m_pVertex; void Render(); void Init(); }; The CBox object derives from CShape and thus has to implement the two pure virtual functions Render and Init. Now I was thinking in the Init method this would create the vertex buffer, objects world position and in the Render, it would render itself. Ok, all seems well and good (at least I think ?), now I have a pointer to a Vertex structure, now my problem is, should I have multiple Vertex structures, i.e. one which just has x,y,z, another which has texture coordinates, another which has texture coordinates and normals, OR, just one vertex structure with all this information in ? Any help, advice is much appreciated. Kind regards, Steve

If it isn't working, take a bath, have a think and try again...

Advertisement
As for the vertex structure, I'd suggest something like this:
 struct vertex{p as D3DVECTOR; //positionn as D3DVECTOR; //normalt as D3DVECTOR2; //texture coordinates}; 

I'd put everything in there just to keep things simple. It only takes up a couple (<50) bytes to keep everything in there, so why not take it? Unless you're going for insane memory optimization, there shouldn't be a problem.

And in your CShape class, don't forget about a m_Zpos... you might just need it even if you think you don't [grin]

...Next time you post lots of code, use [source] tags so that it all fits in the forum nicely. Thanks!
Other than that, everything looks fine to me!
Use two types of vertex description. One that has tex coords and one that does not. There is a performance hit though.
No bombs, No guns, just an army of game creators...
Yeah i ran into a similar issue. my static meshes use a typical XYZ | UV | NORMAL but my skeletal meshes use a custom vertex declaration for shaders. so in my base I store the data inside of a byte array and use C-style casting to convert to the required vertex type. There is a cost for this cast I guess. I've noticed a small bit of slowdown but can;t say for certain that's the issue 4 sure yet.
Thanks all!

Mushu - I forgot to put the m_ZPos in... typo!

Best Regards,
Steve

If it isn't working, take a bath, have a think and try again...

This topic is closed to new replies.

Advertisement