Skip to main content
gamedev.net
🔒 Locked

Vertex format issues

Started by junder Mar 9, 2009 at 12:35 AM 3 replies 1k views
Original Post
junder
junder
I am having a bit of a trouble with setting up a vertex format. Here I what I want to do: 1) Have a base vertex structure of just locations (tVertexBase). Then derive more advanced vertex structures from that, such as one with texture coordinates. (tVertexLTEX) 2) Have a base shape class (CShapeBase), which holds an array of tVertexBase. I have a seperate class (CPlane) which derives from CShapeBase that will then use tVertexBase. The problem is though, if I wanted to have a "tVertexBase *m_pShapeVerticies" in CShapeBase, then later in CPlane have "tVertexBase *m_pShapeVerticies = tVertlexLTEX[size];" it will throw off the real size of the vert structure and it will make my rendering look incorrectly. Does anyone know how to do what I am trying to do? I have tried templating, but that will cause a problem with the initialization ( http://www.gamedev.net/community/forums/topic.asp?topic_id=527126 )
Rewdew
Rewdew
I suppose you could have your base vertex class have a virtual unsigned int get_size( void ) const;, so you know the actual stride/complete size...

Personally I don't really like having complex types for something as simple as a vertex since it's kind of just "raw" data after all, and having a virtual table and such generally doesn't help...
Evil Steve
Evil Steve
Quote:
Original post by Rewdew
Personally I don't really like having complex types for something as simple as a vertex since it's kind of just "raw" data after all, and having a virtual table and such generally doesn't help...
Seconded. Putting anything in your vertex struct that isn't vertex data (And that includes having virtual functions) is a very bad idea, and is going to be very inefficient, since you won't be able to memcpy() an array of vertices around.
I'd strongly recommend changing your code so that the vertex struct is just raw data.

If you absolutely must have extra data in the vertex struct, and you don't mind not supporting DX8 class hardware or below, and you're prepared to take the additional memory and possible performance hit, you can always specify a stride for your vertices (in SetStreamSource()) that does not match the vertex size, which would allow you to skip the "extra" information. But would still cause problems with virtual functions, since the vtable comes at the start of the struct.

In short: Don't do that.
junder
junder
Maybe I explained it wrong, but I have nothing but raw data in my vertex stucture. The problem is say if I do "tVertexBase *verts = new tVertexLTEX[size]". If I do "tVertexBase *verts = tVertexBase[size]" or "tVertexLTEX *verts = new tVertexLTEX]" it will work.
Evil Steve
Evil Steve
Quote:
Original post by junder
Maybe I explained it wrong, but I have nothing but raw data in my vertex stucture. The problem is say if I do "tVertexBase *verts = new tVertexLTEX[size]". If I do "tVertexBase *verts = tVertexBase[size]" or "tVertexLTEX *verts = new tVertexLTEX]" it will work.
operator new[] allocates an array of objects, not a number of bytes of memory. If you need X bytes of memory, you should be using the C malloc() function instead.

As Rewdew said, you'll have to implement a getSize() function or similar to allocate the vertices.
However, I don't think the base class should have an array of tVertexBase if the derived class is going to write other data in there. Instead, I'd have a virtual function to return (as a void*) the vertex array, size and stride ready for a call to SetStreamSource(), and have the derived class have its own vertex array.

Topic Locked

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

Sign in to reply to this topic.