Sign in to follow this  
Tano_ITA

[Dx9] Using a class for vertex

Recommended Posts

Tano_ITA    126
Hello, i'm start to write my own little engine in dx9. I've a question about the way to use vertex into my engine. When i use a struct, i've no problems, but when i use a class to store vertex, it don't work.. Somebody told me that i must use struct to store vertex and to fill it on vertex buffer.. But i want to create an astraction for vertices, in this way i can use different vertex struct as "Vertex With Normal" or "with Color" etc etc. Thanks for any suggests and sorry for my bad english..

Share this post


Link to post
Share on other sites
Guthur    101
I can't remember DX that well, but I think vertex component ordering is important which could get messy if you were going to allow arbitrary class hierarchies, especially with it involves multiple inheritance. But anyway from what I read the memory layout of inherited classes is not specified in C++ spec so you are snookered.

You could develop some way to map the various classes to a portion of memory but I think you are trying to over-engineer it anyway, KISS. Better to spend time on features that will actually show results, this particular feature wont gain you anything in my opinion.

Share this post


Link to post
Share on other sites
ET3D    810
Since structs and classes are the same in C++ except for default permissions, you must be using them in a different manner for this to make a difference. "Don't work" isn't a very good description, since it doesn't tell what's failing. You will have to provide more details about what you're doing and what error you're getting to get a meaningful response.

Share this post


Link to post
Share on other sites
Tessellator    1394
Hi,

Do you mean you're doing something like this?

class IVertex
{
public:
virtual ~IVertex() {}
};

class PositionVertex : public IVertex
{
public:
virtual ~PositionVertex() {}
float x, y, z;
};

When you derive classes with virtual members, any instanced object of that type is going to need to store a vtable somewhere in the structure. In the example above, the size of PositionVertex is actually 16 bytes (assuming 4 bytes for the vtable). You can't simply copy that into a DX9 vertex buffer as you have to take the vtable into account. Guthur's correct as well with regard to data ordering if you start using class hierarchies.

Generally speaking, creating an abstract interface for things of this nature is a bad idea. Considering performance, when you call a virtual function the correct method has to be looked up in the vtable first. When you're dealing with something as numerous as vertices, this could add quite a bit of overhead. Considering the usage of the data, when will not know what an individual vertex's type is before processing it? It's fairly unheard of to store different vertex types next to each other for example. Instead you'd obtain the vertex type and the process all the vertices in one go.

You could instead create an abstraction for a whole groups of vertices i.e. PositionVertexBuffer to avoid these issues (unless this is what you're proposing already, in which case ignore the above :))

Cheers,
T

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