From: H. H. <hen...@gm...> - 2008-08-23 21:32:43
|
No no... We had this conversation already in the forums few days ago. The struct approach is superior over typedef GLfloat GLMmat2f[4]; for the following reason: - it allows matrix data to be accessed as either using index or members. You can see the memory layout of the matrix from the ordering of the mXX members. - the following piece of code: typedef float FOO[2]; FOO *test (FOO *out, const FOO *f) { (*out)[0] = (*f)[0]; (*out)[1] = (*f)[1]; return out; } int main () { FOO a, b; test (&a, &b); return 0; } Produces the following warning on GCC 4.2.3: warning: passing argument 2 of 'test' from incompatible pointer type Compare this: typedef struct { union { float f[2]; struct { float x, y; }; }; } FOO; FOO *test (FOO *out, const FOO *f) { out->x = f->x; out->y = f->y; return out; } This code is a lot more clear and usable than the previous one. Also: vec[0] = -vec[0]; You cannot be sure if this vec is an array of vectors or are you accessing the first element of a 2D vector. Implementing math datatypes as structure is a lot more cleaner approach than the array approach. Perhaps we should think if the union there is redundant but typedef GLfloat GLMvec2f[2]; is not a good idea. And I don't think we should hinder our design by the assumed ignorance of the afromentioned newbies. Besides, I have already implemented the library by the struct approach design. See the OpenGL forums for the discussion we underwent there fore more points. On Sun, Aug 24, 2008 at 12:04 AM, Jason McKesson <ko...@gm...> wrote: > Henri Häkkinen wrote: > > Somebody mentioned in the OpenGL forums that we should avoid the use of > anonymous structures. Is this worth the concern? > > For example, compare these: > > typedef struct GLMmat2f_s { > union { > GLfloat m[4]; > struct GLMmat2fdata_s { > GLfloat m00, m01, m10, m11; > }; > }; > } GLMmat2f; > > vs. > > typedef struct { > union { > GLfloat m[4]; > struct { > GLfloat m00, m01, m10, m11; > }; > }; > } GLMmat2f; > > We need to enclose the mXX variables into a struct in order for the union > to take them as a whole. > > -- > Henri 'henux' Häkkinen > > I would suggest that those struct definitions violate the design principle > of *simplicity*. You need to pick one: either a matrix is an array of > floats or it is a struct with member fields. It should not be both. If > you're having to use a *union*, you've already done something wrong. > Unions are confusing and scary to neophyte programmers. > > I'd prefer that a matrix were defined as: > > typedef GLfloat[4] GLMmat2f; > > It's simpler and gets the job done without being confusing to a programmer > as to what is going on. Yes, it will need to have conventions associated > with it, but that's a small price to pay for the obviousness of what the > matrix is doing. > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's > challenge > Build the coolest Linux based applications with Moblin SDK & win great > prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > Glsdk-devel mailing list > Gls...@li... > https://lists.sourceforge.net/lists/listinfo/glsdk-devel > > -- Henri 'henux' Häkkinen |