# Noob matrix-vector question

This topic is 2897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone explain what the result of this operation is? Given Vector V (x, y, z, w) and a Matrix M:
0.5  0    0    0.5
0  0.5   0    0.5
0   0   0.5   0.5
0   0    0     1

What is VxM? Yeah I've been implementing projective texturing and I'm not entirely sure how this bit works. I tried figuring it out on paper but I just ended up the the same vector V as a the result =/

##### Share on other sites
Perhaps you've forgotten that the i-th component of the result vector is build from the i-th column of the matrix?

##### Share on other sites
First, just for clarification, VxM implies a cross-product of the vector with the matrix, which can't really be done. I'll assume you mean V*M (multiply the vector by the matrix).

This is all off the top of my head, by the way.

Your matrix appears to be a scale of 0.5 along each axis, and a translation of 0.5 along each axis.

The result vector r, assuming row major matrices (column 3 appears to be a translation):

Notation: m[row,col]

r.x = v.x*m[0,0] + v.y*m[0,1] + v.z*m[0,2] + v.w*m[0,3];
r.y = v.x*m[1,0] + v.y*m[1,1] + v.z*m[1,2] + v.w*m[1,3];
r.z = v.x*m[2,0] + v.y*m[2,1] + v.z*m[2,2] + v.w*m[2,3];
r.w = v.x*m[3,0] + v.y*m[3,1] + v.z*m[3,2] + v.w*m[3,3]; // == v.w*1 in this case

##### Share on other sites
Hi there Dom!

Your matrix seesm to be in row major order and it contains two transformations: scaling folllowed by translation. Actually it implements what is called scale and bias.

You have row major matrix, you will also have row major vectors so you will multiply in this order:
V * M
1x4 * 4x4
It's like you said.

after the multiplication V will become (x/2+0.5, y/2+0.5,z/2+0.5,w).

##### Share on other sites
Ooops, sorry when I said VxM I did mean multiplication not cross-product >.>

Thanks for your help, it makes sense now. Just out of interest were you able to figure out exactly what the matrix does without doing the calculations? Like just by looking at it?

##### Share on other sites
Quote:
 Original post by Dom_152Ooops, sorry when I said VxM I did mean multiplication not cross-product >.>Thanks for your help, it makes sense now. Just out of interest were you able to figure out exactly what the matrix does without doing the calculations? Like just by looking at it?

Yes, we can figure that out just by looking at it. You will be able to do that soon, so don't worry.

First of all, on the diagonal there are the scaling factors, they are all 0.5.
In the last column there is the translation. These are always in the same position for every (row major) matrix!

##### Share on other sites
Quote:
 Like just by looking at it?

[WINK]
After majoring in engineering physics with 4+ years of calculus and advanced math, and programming 3D graphics for many years, yeah. Education and experience are wonderful things.

You'll be doing it off the top of your head, too, if you keep asking questions.

EDIT:
Quote:
 the translation .. always in the same position for every matrix!
Well, only for row-major mats. In column-major, the last row is the translation.

##### Share on other sites
I think this topic needs some clarification.

Row/column major order denotes how the 2 dimensional layout of a matrix is linearized for storage in 1 dimensional computer memory. It is totally independent on whether row or column vectors are used. The OP shows a matrix in 2D layout. Of course it is possible (and probable) that the shown matrix is re-layouted from 1D computer memory and that an error was done by chosing the wrong order.

With v*M it is so that row vectors are used (due to the reason Deliverance has written). With the given matrix the result would be
v*M = [ 0.5*x, 0.5*y, 0.5*z, 0.5*x+0.5*y+0.5*z+w ]

If, on the other hand, the matrix was incorrectly re-layouted, it would correctly look like
0.5  0   0   0  0  0.5  0   0 0   0  0.5  00.5 0.5 0.5  1
and then
v*M = [ 0.5*x+0.5, 0.5*y+0.5, 0.5*z+0.5, 1 ]

It may further be that the matrix is correct but column vectors are used instead of row vectors. In that case
v*M
is the wrong part. Instead, it has to be
M*v
for which, using the OP's matrix, again
M*v = [ 0.5*x+0.5, 0.5*y+0.5, 0.5*z+0.5, 1 ]

##### Share on other sites
Quote:
 Original post by DeliveranceIn the last column there is the translation. These are always in the same position for every (row major) matrix!
The column (for column vectors) or the row (for row vectors) that has the same index as the homogeneous (usually named w) co-ordinate in a vector stores the translation. It is one convention to use the last component for that, but you can find enough examples where w is the first component.

Please bear in mind that vector math has some conventions, and all sort of problems simply arise because the conventions in use are not communicated.

##### Share on other sites
Thanks to everyone for clarifying and helping me to understand this stuff =)

I'm making my first proper steps into the realms of 3D graphics programming so hopefully this stuff will become second nature over time!

##### Share on other sites
Quote:
 Original post by DeliveranceIn the last column there is the translation. These are always in the same position for every (row major) matrix!
Quote:
Original post by Buckeye
Quote:
 the translation .. always in the same position for every matrix!
Well, only for row-major mats. In column-major, the last row is the translation.
I think haegarr already touched on this, but just to head off any potential confusion I'll go ahead and point out that whether the translation resides in the fourth row or the fourth column has nothing to do with whether the matrix is row-major or column-major (matrix basis orientation and matrix 'majorness' - i.e. layout in memory - are two separate and mostly unrelated issues).

##### Share on other sites
OK so here's another question, you said that the resulting vector will be:

V(x / 2 + 0.5, y / 2 + 0.5, z / 2 + 0.5, w)

So why is it that when, rather than applying that matrix I do:

output.ProjTexCoords.x = output.ProjTexCoords.x / 2 + 0.5f;
output.ProjTexCoords.y = -output.ProjTexCoords.y / 2 + 0.5f;
output.ProjTexCoords.z = output.ProjTexCoords.z / 2 + 0.5f;

I don't get the same results?

##### Share on other sites
first you negate the y coordinate when you do it manually. And second, you should probably do M * V to be consistent with your matrix convention (assuming your convention is translation in last column)

##### Share on other sites
Well I negated the Y because I was following this:

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Projective_texturing.php

But even when it isn't negated I get the same, weird results. =/

I am using the matrix I was just wondering why doing the same thing without the matrix doesn't yield the same results.

##### Share on other sites
VertexShaderOutput VertexShaderFunction(VertexShaderInput input){    VertexShaderOutput output;    float4 worldPosition = mul(input.Position, World);    float4 viewPosition = mul(worldPosition, View);    output.Position = mul(viewPosition, Projection);	output.TexCoords = input.TexCoords;		output.Position3D = worldPosition;		// Create the projective texturing coordinates	output.ProjTexCoords = mul(output.Position, ProjTexMatrix);	output.ProjTexCoords.y *= -1;		// THIS DOESN'T WORK FOR... SOME REASON	//output.ProjTexCoords = output.Position;	// Convert clip space coordinates into texture space coordinates	//output.ProjTexCoords.x = output.ProjTexCoords.x / 2 + 0.5f;	//output.ProjTexCoords.y = output.ProjTexCoords.y / 2 + 0.5f;	//output.ProjTexCoords.z = output.ProjTexCoords.z / 2 + 0.5f;	    return output;}

Currently I'm multiplying by the matrix but below you an see the commented out lines where I tried to do it manually.

Whilst the matrix multiplication works the second method seems to create a weird tiling effect where there is a different part of the projection in four quadrants on the plane =/