   D3D Vertex color format vs. OpenGL
  IF we are to write a 3D engine that support both D3D and OpenGL and allow user of the engine specify their own vertex format using engine defined "vertex declaration", how to cope with the different in vertex color between D3D and OpenGL?Fixed function D3D only allow type DWORD (unsigned long) while OpenGL doesn't support that.  On the other hand, if you use D3D shader, you can actually specify several different types, however, component types that separate the colors ( 4 floats or 4 ubyte for each color) the format is a, r, g, b.  In OpenGL it is r, g, b, a.I'm having to do something like this to work around:#ifdefWIN32#defineDF_RE_API_D3D9 //this is turned on or off at compile time#endif#ifdefDF_RE_API_D3D9#defineDF_RE_APIGraphics::RenderEngine::T_D3D9#define DF_COLOR_TYPE(n)unsigned32 n#defineDF_COLOR( r, g, b, a )((a << 24) + (r << 16) + (g << 8) + b)#else#defineDF_RE_APIGraphics::RenderEngine::T_OPENGL#define DF_COLOR_TYPE(n)unsigned8 n##R, n##G, n##B, n##A#defineDF_COLOR( r, g, b, a )r, g, b, a#endifstruct VertexPD{Real32 X, Y, Z;DF_COLOR_TYPE( Diffuse );};VertexPD vertices[]={  0.0f, 0.0f, 0.0f, DF_COLOR( 255, 0, 0, 255 ), 0.0f, 1.0f, 0.0f, DF_COLOR( 0, 255, 0, 255 ), 1.0f, 0.0f, 0.0f, DF_COLOR( 0, 0, 255, 255 )};It goes without saying that you cannot decided to use OpenGL or D3D at run-time.  Does anyone know how to deal with this?Thanks
  Do you mean have a vertex class which would work in both APIs? Sorry, maybe its because its morning, but your actual question wasnt that clear   ;  In away, but the key is to allow user to define their own vertex format.  The problem now is the color.   ;Are you absolutely sure that you'll need to let the user define vertex formats ? Well, for the color, you can use some sort of virtualization but I'd say that your approach isn't going to be easy.How about:- The engine provides the graphics API some sort of geometry objects. The graphics API creates a renderable interface from the mesh which is returned to the engine. This way, every gfx API is able to implement the best corresponding representation of the geometry and the engine itself doesn't need to worry about how the thing is actually drawn etc.- If you need to change the vertex colors for some reason, you can do it with this approach also.Pseudo:RenderableInterface *pObj = GrapchisAPI->CreateRenderableInterface(pGeometry);where pGeometry contains list of vertex positions, per vertex colors, texture coordinates, normals, tangents ... etc....pObj->Draw();I hope that I provided some useful information for your question:   ;  When you want to allow shaders, does it not make sense to allow custom vertex format?For OpenGL you can have the vertex components separated and bind them in the end.  However, for D3D, you'll need to have them packed into the vertex buffer.  This could be very slow if you need to pack it everytime.   ;  I don't understand what you're trying to do. It doesn't make sense unless you're also letting the user create one shader for both OpenGL and Direct3D (say, using Cg).If you do use Cg, I don't know what to say. But if for some reason different shaders are used for OpenGL and D3D, then you can just use swizzling in the shader to get the correct component order. Might hurt performance just a bit, but it's an easy solution.   ;  I understand most of the time in game development you usually define a model format and load the vertex information from a file.  My goal is to develop an engine that is suitable for not only game development but also for say, scientific visualization. Vertex information usually vary a great deal depending on the application here so I would like for the developer to be able to specify their own vertex format and modify them during run time.You can also argue to provide a fixed vertex format that has a lot of the components that a developer might need and let me decide what component they want to use from that vertex.  However, that would waste some memory and transfer time. The situation is that say you want to allow developer direct access to the "vertex buffer" so that they could provide the vertex information say through memmove or memcpy, for the most part if you don't use the color component everything is fine.  However, due to the color component different mentioned with D3D vs OpenGL, you can't really use the memory operations directly.  Instead, you'll have to modify indivisual vertices, pack them and then push the information to vertex buffers or buffer objects.   ;  Hi,If you want to handle both OpenGL and DirectX at runtime, then you should create a layer on top of API-dependant parts.For example :// vertex buffer interfaceclass IVertexBuffer{protected:   IVertexBuffer(void);   // protected constructor to avoid direct instantiation   virtual ~IVertexBuffer(void);public:   // here, put all the methods that you'll need to work on vertex buffer   // for example :   bool    Lock(....);   bool    Unlock(....);   void    SetColor(int vertexIndex, float r, float g, float b, float a);   // ...};// DirectX implementationclass CDXVertexBuffer : public IVertexBuffer{public:   CDXVertexBuffer(void);   virtual ~CDXVertexBuffer(void);public:   bool    Lock(....);   bool    Unlock(....);   void    SetColor(int vertexIndex, float r, float g, float b, float a);   // ...};// and now the vertex buffer that the engine will manipulate :class CVertexBuffer{public:   CVertexBuffer(void);   ~CVertexBuffer(void);   // here, you find the same methods as the ones in IVertexBuffer :   inline bool  Lock(....)    { m_pVertexBuffer-&gt;Lock(...);   }   // ...   // the point of this class is the following method.   // eAPIType is a simple enum for OpenGL or DirectX, and in this method,   // you simply have to delete the m_pVertexBuffer and recreate a new one   // specific to the api given in parameter   bool   SetAPI(eAPIType api);private:   IVertexBuffer * m_pVertexBuffer;    // this can point to a DirectX VB or and OpenGL one}Ok, this example might be a bit "dirty" but it's to give you the idea. Create a virtual class, make a DirectX and an OpenGL implementation, then create a layer which manipulate the interface. So in your engine, you always manipulate a CVertexBuffer without the type of buffer. And you don't need to ^^If you want to switch from OpenGL to DirectX and vice versa, it will be a little more complicated than in my example of course. The the SetAPI(..) method can simply be used at initialization time to create the correct VB, depending on the API in use.I hope it helped ^^   ;  paic,It's not the "vertex buffer" abstraction I'm concern but the vertex data format.  You example, the IVertexBuffer has the SetColor method, this assumes that the vertex has a color.  So in this sense you're predefining the vertex format, which defeats the purpose of allowing user of the engine to specify their own vertex format.   ;  You can use any four int values for R, G, B, and A, and then arrange them however you need. You could just make a struct like this:struct OurColorStruct{int r, g, b, a;}Then you could just convert to whichever API you need. For DirectX you would just useDWORD color = D3DCOLOR_RGBA(R, G, B, A)And for OpenGL just use the valuesglColor4i(R, G, B, A);