Transforming texture coordinates

markbiddlecom

Newcomer
Joined
Jun 30, 2003
Messages
13
Location
Denver, CO
Hi, all!

This is what should be a relatively easy question (I hope). I've got a texture loaded that contains several frames of an animation I'd like to place on a sprite.

My vertex format is Position and Texture1, and is defined by the following structure:

Visual Basic:
Public Structure Vertex
   x As Single
   y As Single
   z As Single
   tu As Single
   tv As Single
End Structure

What I'd like to be able to do is transform the texture coordinates in real-time to produce the animation effect.

So, my drawing code (very much abbreviated) looks like this (where m_buffer is a VertexBuffer object storing my sprite coordinates, and m_texture is an instance of the Texture class that contains the entire animation):

Visual Basic:
Public Sub draw(ByVal device As Direct3D.Device)
   device.VertexFormat = VertexFormats.Position Or _
                         VertexFormats.Texture1

   device.SetTexture(0, m_texture)
   device.TextureState(0).TextureTransform = _
      TextureTransform.Count2

   ' Transform the world coordinates
   device.Transform.World = getSpriteMatrix()

   ' Transform the texture coordinates
   device.Transform.Texture0 = getTextureMatrix()

   ' Draw.
   device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2)
End Sub

Private Function getTextureMatrix() As Matrix
   Dim retVal As Matrix = Matrix.Identity

   ' Determine the current frame...
   ' <code removed>

   retVal = Matrix.Multiply _
    (retVal, _
     Matrix.Scaling(curFrameWidth, curFrameHeight, 0) _ 
    )   

   retVal = Matrix.Multiply _
    (retVal, _
     Matrix.Translation(curFrameLeft, curFrameTop, 0) _
    )

   Return retVal
End Function

Some more information: My vertex buffer contains four verticies, defined as such:

(0,0)
(1,0)
(0,1)
(1,1)

Note that all z values are zero, and that the tu and tv coordinates are identical to the x and y coordinates.

When I run the program, I notice that the texture coordinates properly scale, but they never seem to translate, and always anchor themselves to the texture origin.

I just now realized that I probably need to set the translation coordinates in the 3rd row instead of the 4th, since texture coordinates are 2D...

<edits code>

Well, that didn't work either: now, every other frame (when I perform the texture translation) I get a blank image, instead of my picture of water. Previously, I was just getting the same frame over and over.

The new code looks like this:

Visual Basic:
Private Function getTextureMatrix() As Matrix
   Dim retVal As Matrix = Matrix.Identity

   ' Determine the current frame...
   ' <code removed>

   retVal = Matrix.Multiply _
    (retVal, _
     Matrix.Scaling(curFrameWidth, curFrameHeight, 0) _ 
    )   

   retVal.M31 = curFrameLeft
   retVal.M32 = curFrameTop

   Return retVal
End Function

OK, so I still need help on how to properly translate texture coordinates.

Thanks in advance!

-Mark
 
Well, all, I seemed to have solved my own problem again :P

The issue had to do with the fact that I was actually multiplying the scaled texture matrix (in the getTextureMatrix function) by a new matrix returned by another function, get2DTranslationMatrix. That function returned a matrix with all zeros except for the M31 and M32 positions. So, naturally, when I multiplied it by the scaled matrix, the scale values went to 0.

Boy do I feel like an idiot :P

Thanks anyway!

-Mark
 
Back
Top