   2D Texture Transform
  I remimber seeing a post about this a while ago, but I can't seem to find the one I need. I need to scale and translate texture coordinates so I can select only a portion of a texture. Given the position and dimentions of the portion I need to select, as well as the overall size of the texture, how would I go about creating a matrix to do this?   
  Why would you want to use a matrix for that? AFAIK you would need to use a 3x3 matrix to translate and scale UV-coordinates. You could just set the UV-coordinates you need directly.In order to calculate those you know all you need:The overall size of the texture (let´s call it texSizeX and texSizeY), the position and the dimension of the needed portion, which you could translate into 4 points A, B, C, D with corresponding coordinates Ax, Ay, Bx, By and so on...For example if you want to use the rectangle from the pixel (16,16) to the pixel(64,64) from your texture, point A would be (16,16), B would be (16,64), C = (64,16) and D = (64,64).Now to calculate the corresponding UV-coordinates for those points all you need to do is divide those coordinates by the texture size, that is:Ax = Ax / texSizeX, or Ax = Ax / (texSizeX - 1)Ay = Ay / texSizeY, or Ay = Ay / (texSizeY - 1)subtract 1 from the texture sizes if the pixel coordinates range from 0 to texSize - 1, don´t subtract 1 from it if they range from 1 to texSize. Don´t no which one is the case.To do that with a matrix you would have to calculate the sidelengths of the portion rectangle (let´s call them rectX and rectY), divide those by texSize, which would result in the scale factor you want to apply. Now divide the pixel coordinates of the top-left point by the texture size, which gives the translation you want to apply.Now put those things into a 3x3 matrix (scale factors on the diagonal, translation into the last column / last row), and you´re done:( rectX / texSizeX    0 Ax / texSizeX )(  0 rectY / texSizeY   Ay / texSizeY )(  0   01    )if you multiply the above matrix with vectors (0.0, 0.0, 1.0), (0.0, 1.0, 1.0), (1.0, 0.0, 1.0) and (1.0, 1.0, 1.0), the result of those should be the UV-coordinates of the rectangle, just throw the third coordinate of the results away afterwards.On the other hand you could just build a 2x2 matrix that divides the components of a 2-dimensional vector by texSizeX and texSizeY, which would be the same as I described above. That matrix would look like this:( 1.0 / texSizeX    0  )(   0   1.0 / texSizeY )Now you could multiply the four points A, B, C and D from the first method with that matrix. However doing this scale by a matrix multiplication seems wasteful to me, as you would be 4 multiplications (2 for each vector component) instead of 2 with the first method.Hope that helped.   ;  What I'm trying to do is sprite animation. The reason why I want to use a matrix is so I can get the CPU to do the transformations for me. I might not be using a textured quad all the time, but possibly a poly. Thus creating the texture coords myself might get slow.   ;  Texture transforms can be done by changing the upper 3x3 part of the matrix.scale:sx,0,00,sy,00,0,1translate:1,0,00,1,0tx,ty,1rotate:a,b,0c,d,00,0,1where a = cos(angle), b=sin(angle), c=-sin(angle), d=cos(angle)You can use D3DX to do scale X, and Y, and rotation about Z.  Translations must be manually added to the third row (_31, _32)...or atleast it used to be that way.  It seems a .NET addition is in the regular D3DX SDK help.  D3DXMatrixTransformation2D..You then program the matrix -- pDev->SetTransform(D3DTS_TEXTURE0, &mat;And tell D3D you want a 2D transform -- pDev->SetTextureStageState(D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);Remember to disable the transform flags when you're done.Now that your question is out the way... Drawing sprites one by one, which will be the required way to draw when using texture transforms, is NOT the way to go.  A dynamic VB refilled each frame with the correct data and rendered at once will definately improve performance.  Or use D3DXSprite, as it attempts to draw everything nicely for you.   ;  Using ID3DXSprite would simply allow you to select a source RECT in pixels. This is very useful for stuff like sprite animations.Hope this helps.   ;  Thanks, Namethatnobodyelsetook.As far as rendering goes, I'm already planning on using a dynamic VB. Actually I was going to create a "Blitter" class that lets you draw Fans and Strips to the buffer, and automatically break them down to tris for you, then flush them when you change textures or you're done rendering.The reason I'm not using ID3DXSprite is because it only supports texture blitting. While it does let you rotate, scale, translate, and do any other transformation you want to do to it easily, it still only blits quads. I need to be able to blit convex, or possibly even concave polys to the screen. Reason? My game is going to be physics based, and thus uses convex polys for rigid body. I'm not sure how blending rigid bodys and quad blitting would look.But again, thanks for the help, guys!   ;  Ohhhhh... I see where the comment about blitting one at a time came from. Changing the texture transformation... hmm. I might be doing the texture transformation on the processor or anyways, now that you mentioned that.I can't believe I overlooked that. Thanks!   