# How should I choose between Vec3 and Vec4?

I find myself torn between using Vec3's vs Vec4's for representing position, directions, etc in my non-gpu code. I see the obvious benefit of using Vec3s for stuff like data in buffers, to more tightly pack mesh data, reducing memory used.

The primary motivation for switching from Vec3 to Vec4 is to reduce the tedius conversions back and forth. For example the math library I am using (GLM) only lets mat4's multiply with vec4's.

I realise it dosn't make sense to have a fourth component in say a direction vector, and to make the distinction between say a position and a direction vector becomes more important.

It's not a big issue, but nonetheless I am curious is there any significant pros or cons between using vec3's or vec4's? What makes the most sense to other users?

Use the smallest type that makes sense.

3-components vectors are smaller, but may involve "overhead" when converted to 4-component vectors for some operations. 4-component vectors, on the other hand, are larger and thus have some space "overhead."

In most cases "overhead" in either case is negligible. However, the additional storage taken up by a 4-component vector is not negotiable. It's always there; the additional conversion required of a 3-component vector can be deferred or amortized in a lot of cases, and doesn't happen constantly (or if it does, then you've found a scenario where you should be using the larger type). Plus, it is far more likely to be optimized into a complete non-issue.

So you should evaluate the context in which you are going to use the types, and pick the type that will achieve the most optimal storage/runtime overhead for the majority of the use cases, and which also represents the data in the most accurate fashion for the use cases involved.

If you don't need four components, don't store four components. Wait until that is a demonstrated problem before worrying about it further.

• If nothing else, is there a "standard" way of doing it? What is most generally used? – KaiserJohaan Feb 25 '15 at 22:21
• In my experience (since there's no ISO standard for stuff like this), the typical approach is exactly what Josh Petrie describes: use 3-vectors wherever you can, and only switch to 4-component when necessary, or where it improves a significant demonstrated bottleneck. – DMGregory Feb 25 '15 at 22:25
• Yes. The "standard" way is to think about the actual problems you have to address and choose the most optimal solution for those scenarios, rather than to "always use X." Most of good software/game development is spent staring and thinking. – user1430 Feb 25 '15 at 22:53
• Remember that the cross product is a common operation and is inapplicable to 4-component vectors. The only time I would use a 4-component vector in the client code is either as an alpha-blending color, internally when transforming by a 4x4 matrix, or for texture coordinates when it is important to fix affine texture mapping. Of course there is the other camp that would say to implement everything using 4-component vectors (__m128s) anyways for the SSE capabilities. – jmegaffin Feb 26 '15 at 8:23