| Altair December 24, 2004, 10:48 AM |
|
You can do this easily by using templates & mutator methods, like:
struct COLORVERTEX
{
D3DVECTOR position;
D3DCOLOR color;
// mutators
inline void setPosition(float x_, float y_, float z_) {position.X=x_; position.Y=y_; position.Z=z_;}
inline void setColor(uint32 color_) {color=color_;}
inline void setUV(float u_, float v_) {}
};
struct TEXVERTEX
{
D3DVECTOR position;
float tu, tv;
// mutators
inline void setPosition(float x_, float y_, float z_) {position.X=x_; position.Y=y_; position.Z=z_;}
inline void setColor(uint32 color_) {}
inline void setUV(float u_, float v_) {tu=u_; tv=v_;}
};
template<typename T_>
void FillVertexBuffer(T_ *verts_, int nVertices_)
{
for (int i=0; i<nVertices; ++i)
{
verts_[i].setPosition(10.0f, 10.0f, 10.0f);
verts_[i].setUV(0, 0);
verts_[i].setColor(0xff121212);
}
}
|
Once optimized, the empty mutator implementations will get automatically eliminated from the code by the compiler.
You can also define a vertex base class which implements all empty mutators and derive vertex classes from that one, so when you add new properties, you only need to add empty mutators to the base class. It's a bit easier to maintain that way.
Cheers, Altair
|