Stretched Texture

My texture displays stretched vertically and I have no idea why. I loaded it using TextureLoader and used Sprite.Draw2D(texure, rectange, size, position, Color.White). I always set the rectangle and the size to the same. Can anyone help I'm new at this stuff. Thanks!

Answer this question

Stretched Texture

  • elsonidoq

    Sorry for butting in, but in my Feb2006 MDX I can't find the new operator for the Texture as described above. I have the same issue, I don't want to convert all my images to a square. Is this left out of the managed version Thanks.

  • VijayG

    Here's a method I developed that is quite effective at turning a Bitmap into a texture (and gets around the square problem):

    protected D3D.Texture BitmapToTexture( D3D.Device graphics, ref Bitmap bmp )
    {
    MemoryStream loMS = new MemoryStream();

    // Create a square offscreen bitmap, the dimensions of which are
    // the size of the greatest dimension of the original bitmap
    Bitmap osBmp = new Bitmap( (int)Math.Max( bmp.Width, bmp.Height ), (int)Math.Max( bmp.Width, bmp.Height ) );
    // Draw the original bitmap into the top-left corner of the new bitmap
    Graphics osg = Graphics.FromImage( osBmp );
    osg.DrawImage( bmp, 0, 0, bmp.Width, bmp.Height );
    // Use the new square bitmap as the texture
    osBmp.Save( loMS, System.Drawing.Imaging.ImageFormat.Bmp );
    loMS.Seek( 0, 0 );
    return D3D.TextureLoader.FromStream( graphics, loMS );
    }

    You'll end up with a texture that has whitespace at the edges, so that it still forms a square, but your original bitmap will keep it's proportions. When drawing your sprite, just leave the whitespace out.



  • Serega

    No but you can't use the easy API overloads since they assume square

    use

    myTexture = new Texture(e.Device, Utility.FindMediaFile("texture.dds"), XSIZE, YSIZE, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.None, 0, false, null);

    And all should be well.

    If your textures are square and a power of 2 you can use the simpler

    myTexture = new Texture(e.Device, Utility.FindMediaFile("texture.dds"));

    Remember that though Sprite will work with x,y coordinates under the covers textures u,v coordinates are always 0.0-1.0. Sprite is just doing the math for you.



  • MShanahan

    If your hardware supports it you have to use the more complex Texture Loader call where you can specify the size too.

  • just*Do*It

    Got it. Thanks!

  • Peter Oresnik

    Make sure that the texture conforms to the power of 2 sizes. This means that the texture should be n ^ 2. Such as the following, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, ....
    You could also see if your hardware supports non-power of 2 texture by checking the device caps.

    I hope this helps.
    Take care.


  • SpoBo

    I don't get it... does this mean that any graphic files I import in my program have to be changed so that they're square shape What if my background's 800x600 What if my character is taller than he is wide

    And if I do have to make all of my external bitmaps squares, then how am I supposed to know where to crop them Some might have a whole whack of blank space to make them a square, and others might have very little...



  • thebobman

    I'm glad you told me that. Otherwise I never would have figured it out. I adjusted the texture size and it displays fine now. THANKS!!!
  • Stretched Texture