Sign in to follow this  

Random Union Question

This topic is 3107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm attempting to make a vector and matrix classes as similar to GLSL as possible and have had very little experience with what would be considered proper programming methods so I was curious what people thought of using unions like such:
typedef struct _vec3 {
	union {
		float xyz[3];
		float rgb[3];
		float stp[3];

		struct {
			union {
				float xy[2];
				float rg[2];
				float st[2];

				struct {
					union {
						float x;
						float r;
						float s;
					};
					union {
						float y;
						float g;
						float t;
					};
				};
			};
			union {
				float z;
				float b;
				float p;
			};
		};

		struct {
			union {
				float x;
				float r;
				float s;
			};
			union {
				float yz[2];
				float ga[2];
				float tp[2];

				struct {
					union {
						float y;
						float g;
						float t;
					};
					union {
						float z;
						float b;
						float p;
					};
				};
			};
		};
	};
} vec3;

I know it looks hideous (as are all great sleep deprived ideas), but I'm more interested in compatibility and efficiency. I actually found it interesting that it compiles (using MS VS2008), since some variable names are the same. I tested it with a very simple case and it gave me correct results. I also made a vec2 struct similar to this one and used it instead of the float[2]s in this vec3 and it worked as well. Is it guaranteed that the results will be correct?

Share this post


Link to post
Share on other sites

This topic is 3107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this