theacodes / PhoenixCore

An experimental 2D optimizing rendering system built with OpenGL
Other
25 stars 5 forks source link

Template vertex format handler auto generator. #6

Open stonedcoldsoup opened 11 years ago

stonedcoldsoup commented 11 years ago

My idea is to use a boost mpl to define vertex formats with argument unrolling tricks, generating code objects for vertex access and rendering, so the only runtime decision made is following the function pointer to the generated implementation of submitVertexList in the VertexDef class v-table.

So that way you can define your vertex format like so:

typedef
    VertexDef
    <
        VertexTexCoordsDef<4, GL_FLOAT>,
        VertexPositionDef<2, GL_FLOAT>,
        VertexColorDef<4, GL_UNSIGNED_BYTE>
    >
    MyVertexDef;

Recursive instantiation of an inline member function using a selector class can be used to hard code the submitVertexList function for each VertexDef instantiation in compile time. VertexDef inherits AbstractVertexDef, so the resulting type is an interface for doing vertex format specific things to a list of vertices with that type. You could then supply your own type for vertex data:

#pragma pack(push, 1)
struct MyVertexType
{
    float texcoord[4];
    float pos[2];
    uint8_t color[4];
};
#pragma pack(pop)

Then assign a VertexDef to a GeometryFactory

m_some_geometry_factory.setVertexDef(MyVertexDef());

And voila! That would be interpreted correctly by the above VertexDef. It is also possible that an output vertex type be generated by VertexDef instantiation, like VertexDef::VertexType.

Of course, the issue with generating an actual VertexType class from the VertexDef template in boost is that it would require a lot of manual template specialization to select the type for each attribute in the vertex format, because real variadic lists only exist in C++11, and not boost mpl.

Would you be interested in looking it over and trying it if I wrote this up in a branch called, say, flexible_vertex_formats?