Soon_to_be_code_master
Soon_to_be_code_master

Reputation: 323

Need some clarification with the concept of vectors in direct3D11

I thought at first that vectors were just arrays that can store multiple values of the same type. But i think direct3d uses a different terminology when it comes to "vectors"

Lets say for example, we create a vector by using the function XMVectorSet()

XMVECTOR myvector;

myvector = XMVectorSet(0.0f, 0.0f, -0.5f, 0.0f);

What exactly did i store inside myvector? did i just store an array of floating point values?

Upvotes: 0

Views: 103

Answers (3)

Ross Ridge
Ross Ridge

Reputation: 39621

In general vectors in Direct3D are an ordered collection of 2 to 4 elements of the same floating-point or integer type. Conceptually they're similar to an array, but more like a structure. The elements are usually referred to by names like x, y, z and w rather than numbers. Depending on the context you may be able to use either C++ structure or an C++ array to represent a Direct3D vector.

However the XMVECTOR type specifically is an ordered collection of 4 elements that simultaneously contains both 32-bit floating-point and 32-bit unsigned integer types. Each element has the value of a floating-point number and an unsigned integer that share the same machine representation. So using your example, the variable myvector has simultaneously holds both the floating-point vector (0.0, 0.0, -0.5, 0.0f) and the unsigned integer vector (0, 0, 0xbf000000, 0).

(If we use the usual XYZW interpretation of the floating-point value of myvector then it would represent a vector of length 0.5 pointing in the direction of the negative Z axis. If we were to use an unusual RGBA interpretation of the unsigned integer value of myvector then it would represent a 100% transparent blue colour.)

Which value gets used depends on the function that the XMVECTOR object is used with. So for example the XMVectorAdd function treats its arguments as two floating point vectors, while XMVectorAndInt treats is argument as two unsigned integer vectors. Most operations that can be preformed with XMVECTOR objects use the floating-point values. The unsigned integer operands are usually used manipulate bits in the machine representation of the floating-points values.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

C++'s "vectors" are indeed array-like storage containers.

You're right in that Direct3D is using a different meaning of the term "vectors": their more global mathematical meaning. These vectors are quantities that have direction and size.

Further reading:

Upvotes: 0

Jonathan Potter
Jonathan Potter

Reputation: 37192

XMVECTOR has an unspecified internal layout:

In the DirectXMath Library, to fully support portability and optimization, XMVECTOR is, by design, an opaque type. The actual implementation of XMVECTOR is platform dependent.

So it might be an array with four elements, or it might be a structure with .x, .y, .z and .w members. Or it might be something completely different.

Upvotes: 0

Related Questions