1
votes

I'm attempting to create vertex structs in OpenGL by using macros to define the vertex basics and having the macro eventually generate the boilerplate code needed for use with OpenGL (glGetAttribLocation, glEnableVertexAttribArray, glVertexAttribPointer etc.).

My macros are defined as such:

#define DEFINE_GPU_VERTEX_ATTRIBUTE(R, DATA, ELEM) BOOST_PP_CAT(ELEM, DATA);

#define DEFINE_GPU_VERTEX(NAME, ...) struct NAME##_gpu_vertex_t \
{ \
    BOOST_PP_SEQ_FOR_EACH(DEFINE_GPU_VERTEX_ATTRIBUTE, _, __VA_ARGS__) \
}

This is a sample of how I intend to use the macro:

DEFINE_GPU_VERTEX(bsp,
((vec3_t), position),
((vec3_t), normal),
((vec2_t), texcoord));

Where the first argument (bsp) is the name prefix, and all variadic arguments are pairs of types and names of attributes.

I expect it to generate a struct definition like so:

struct bsp_gpu_vertex_t
{
    vec3_t position;
    vec3_t normal;
    vec2_t texcoord;
}

However, it appears to generate an empty struct. What am I doing wrong here?

I'm not familiar with C macros, let alone Boost macros as I've never really had reason to use them or learn what makes them tick until now.

Any help is appreciated!

1
Get the preprocessed form (e.g. using g++ -C -E or by using appropriately your compiler and IDE) and look inside with an editor or pager (e.g. less). Also tell what compiler and operating system you are using, and what is your compilation command. - Basile Starynkevitch
I'm using Visual Studio 2013. - Colin Basnett
BOOST_PP_**SEQ**_FOR_EACH requires a sequence and you are not providing one. There is a macro that converts variadic data to a seq that may help. I believe the "usual" way to do this (at least the one used in fusion adaptations/definitions of structs) is to use directly a sequence. The 2nd and 3rd approaches in this answer could be of help. - llonesmiz

1 Answers

1
votes

Warning: this may not answer your original question. It is not even an answer, but rather a question:

Have you considered generating these struct-s using a high-level language instead of C macros?

As you have already found out, C macros are clumsy and difficult to debug. I would rather write a script (say, in Python, but any scripting language with well developed string processing capabilities would do) that would output a opengl_boilerplate.h file that contains your struct declarations. You would then invoke this script when your software is being built. I don't know how to do that with VS2013, but maybe running it once is enough (depends on your project).