Jump to content
  • Advertisement
Sign in to follow this  
melbow

Random Union Question

This topic is 3391 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
Advertisement
Using it like GLSL vectors would probably result in undefined behaviour as far as the standard is concerned.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!