# Tex-Coords - Matrix

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

## Recommended Posts

Assuming you have a quad with default texture coords of (0,0) (0,1) (1,1) (1,0) clockwise, is there a way to take the actual texture coordinates and calculate the transform matrix that would be required to get from the default to the actual coords. For example: say the coordinates are (0,0) (0,2) (2,2) (2,0), then obviously the matrix simply scales by 2. It get's more complicated when you begin rotating and such, of course, which is why I'm curious if there's a general purpouse algorithim.

##### Share on other sites
I don't know if it is of much help, but my first thoughts are you are having a system of equations. Given four input vectors and four target vectors, compute a transformation T:
v1' = v1 Tv2' = v2 Tv3' = v3 Tv4' = v4 T

A system to be solved. But I think in general there is no solution for T. Just some thoughts.

Greetz,

Illco

##### Share on other sites
There should be a unique solution for 3 points. Although texture coordinates are 4-component and the texture matrix is a 4x4 matrix I'm assuming all the z-components are zero which effectively reduces you to a 3x3 matrix and 3-component coordinates. To get said unique solution you have to use matrix maths - I'm sure there's plenty of web pages about the inversion algorithm out there. The required maths looks something like this:

Since:

( 2 0 0 ) ( 0 0 1 ) ( 0 0 2 )
( 0 2 0 ) * ( 0 1 1 ) = ( 0 2 2 )
( 0 0 1 ) ( 1 1 1 ) ( 1 1 1 )

(Using the first 3 coordinates from your example)

You have to multiply both sides from the right by the inverse of your source coordinates, giving:

( 2 0 0 ) ( 0 0 2 ) ( 0 0 1 )^-1
( 0 2 0 ) = ( 0 2 2 ) * ( 0 1 1 )
( 0 0 1 ) ( 1 1 1 ) ( 1 1 1 )

Where the ^-1 means inverse. Luckily since your source coordinates are constant you can precalculate the inverse matrix, which gives:

( 0 -1 1 )
( -1 1 0 )
( 1 0 0 )

NB the last row in the matrices used gives the w-component (projective component) of the texture coordinates. This means that the target area doesn't necessarily have to be a rectange - it can be a trapezoid. If you just want to be able to rotate coordinates about the origin you can solve for 2 points but that removes the whole ability to distort the shape and do translations, which is really what homogenous coordinates are all about.

##### Share on other sites
Thanks for the explanation! Very helpful!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 19
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015507
×