Let’s take a look at a tipical OpenGL code :
glBegin(GL_TRIANGLES);
glColor3f(0.0,0.0,0.1);
glVertex3fv(vertex);
glColor3fv(green);
glVertex3f(0,3,2.2);
… // etc
Well, this might seem obvious to you but show this to any C++ expert
and you’re likely to hear :
" Nah !! you’re encoding the type of the argument in the function name ?
why don’t you let your compiler do this job ? "
Yes, it is right, C++ compilers are able to give the same name for different
functions, taking different number argument with different types …
What I need is a library that would allow me to say:
vertex(x,y,z);
and Automatically understands that:
- I’m giving three arguments
- I count on it to deduce the types of the argument and
do the right thing.
and to say :
GLfloat v []={…};
vertex(v);
and Automatically understands that:
- I’m giving one argument and thus must be an array.
- I count on it to deduce the types of the argument which is GLfloat.
GL function taking arguments that specify the type of others (such as glDrawElements)
could also greatly benifit fomr the C++ auto type deduction …
Also there is another problem …
if you look at some implementation of GL/gl.h, you’ll see something like :
#define GL_TRIANGLES //…
#define GL_POINTS // … etc
Again if you show this the above C++ expert be prepared to hear:
" What ?!! you’re #defining constants ? what std::sort of game you’re playing ?"
(If you’re not a C++ fan ignore the ‘std::’ above )
#defining constants has well known problems, for example :
glBegin(GL_LINE);
glVertex …
// …
glEnd();
This won’t do the right thing because the writer really meant GL_LINES,
and your compiler can’t help you.
in ‘the C++ way of doing things’ we would have :
enum DrawMode { GL_LINES, GL_POLYGON ,…};
enum PolygonMode{ GL_FILL, GL_LINE};
void glBegin(DrawMode);
void glPolygonMode(PolygonMode);
And the above error would have been detected at compile time …
But wait there is more !! aren’t you tired of writing all those GL and gl prefixes ?
of course they are important to avoid name clashes but if there is nothing else named
Vertex3fv why do I need to type the gl ? C++ can help here also with the use of namespaces
…
In fact what would be really halpful is to have a C++ wraper around standard
GL calls that would make good use of the C++ compiler capabilities seen above.
Does such a library exists ? (or must I write one myself)
PS: below is a C++ example code that shows how to write the vertex()(and begin()) function
described earlier :
namespace cgl
{
template<class Coord>
void vertex(Coord, Coord, Coord);
template<class Coord>
void vertex(const Coord*);
template<>
inline void vertex<GLfloat>(GLfloat x, GLfloat y, GLfloat z)
{ glVertex3f(x,y,z);}
template<>
inline void vertex<GLdouble>(GLdouble x, GLdouble y, GLdouble z)
{ glVertex3f(x,y,z);}
// etc for vertex ...
enum DrawMode { POINTS, LINES, LINE_STRIP, ... };
inline void begin(DrawMode m)
{ glBegin(static_cast<GLenum>(m));}
// etc ..
}
Thanks in advence …