Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Using DirectXMath and its XMMATRIX structure in C++ and Direct X 11, how does one multiply that matrix structure by a single float scalar?

I want to implement the operation B = A * f; where A and B are XMMATRIX and f is a float.

I found all sorts of functions to multiply a matrix by another matrix or a vector. I found all sorts of functions to construct matrices.

I could find no scalar multiplication! Why is there no such function? Is there no use case? Did I miss something? How do I implement scalar multiplication?

share|improve this question

3 Answers 3

It appears from documentation that the multiplication operator you use only does matrix-matrix multiplication.

The most straight-forward way to scale by a scalar is to matrix multiply your matrix with XMMatrixScaling(f,f,f), or to scale each of the row vectors making up the XMMATRIX by a scalar.

If I had to hazard a guess, I would reckon that the library is designed around exposing fast vectorized functions for the common expensive operations that are error-prone or hard to implement as an end user. Judging by the current other answer, it seems like there's more convenience functionality in more recent versions of the Windows Kits which includes the operators you seek.

share|improve this answer
    
Thanks for your help, i`ll try it out. But, off topic: What kind of an API is that supposed to be??? Not intuitive at all. Something must be wrong here, I feel like. –  user43129 Mar 8 at 12:50
    
but this will not multiply translation part - fourth row! –  Yola Aug 16 at 8:15

There is a multiplication operator. You can multiply XMMATRIX a by scalar b with a*b.

share|improve this answer
    
Thanks for the hint but this doesnt work for me! My compiler neither accepts A * f nor f * A. –  user43129 Mar 8 at 12:09
    
It's weird, it works for me with no problem. I am using Visual studio 12. –  János Turánszki Mar 8 at 13:12
1  
DirectXMath functions including the operators are in the DirectX namespace. If you do not have using namespace DirectX; in your current scope, the operator overloads will not be found. –  Chuck Walbourn Aug 19 at 23:24

DirectXMath has the overloads needed for this operation:

struct XMMATRIX
{
    XMMATRIX&   operator*= (float S);
    XMMATRIX    operator* (float S) const;
    friend XMMATRIX     XM_CALLCONV     operator* (float S, FXMMATRIX M);
};

Likely the problem you are having is that you do not have the proper namespace scope active to find the operator* overload. Try

using namespace DirectX;
XMMATRIX B = A*f;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.