Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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):

 

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:

 

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

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...