Handmade Hero»Forums»Game
Gaurav Gautam
98 posts
Day95/96: Why is the v4 struct done so differently than v2 to get the xyz,rgb?

To get the v3 from v4 he made the struct like this:

struct v4
{
  struct 
  {
    union
    {
      v3 rgb;
      struct 
      {
        real32 r, g, b;
      };
    };
    real32 a;
  }
}

But why can't we do it like the v3 and v2.

struct v4
{
  union {
    struct {
      real32 x, y, z, w;
    };
    struct {
      real32 r, g, b, a;
    };
    struct {
      v3 rgb;
      real32 ignored0_;
    };
    real32 E[4];
  };
}

I tried this and nothing seems to be obviously broken. Not sure why he is making it like this in case of v4.

Simon Anciaux
1337 posts
Day95/96: Why is the v4 struct done so differently than v2 to get the xyz,rgb?

You can also skip the top level struct and removed the ignored members:

union v4
{
    struct {
      real32 x, y, z, w;
    };
    struct {
      real32 r, g, b, a;
    };
    v3 rgb;
    real32 E[4];
};
Gaurav Gautam
98 posts
Day95/96: Why is the v4 struct done so differently than v2 to get the xyz,rgb?
Replying to mrmixer (#26964)

i see, so there is no need to make the whole memory reachable in each thing that is unionized