Jump to content
  • Advertisement
Sign in to follow this  
Holy Fuzz

Problem translating texture coords.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm having a problem translating texture coordinates, and maybe someone can help me figure out what the problem is. For the record, I'm using C# with managed DX, though I'm not sure how much that matters. The following is my texture transformation code that does *NOT* translate texture coordinates. The code works perfectly. The point of transforming texture coordinates is to allow the user to specify coordinates where the unit length is a single pixel, instead of the unit length being the entire texture.
DX.Matrix scaling = (image != null)?(DX.Matrix.Scaling(1f/image.OriginalSize.Width, 1f/image.OriginalSize.Height, 1f)):(DX.Matrix.Identity);
DX.Matrix m = translation;
if(m != curTexTransform)
{
	dev.Transform.Texture0 = m;
	curTexTransform = m;
}



"DX.Matrix" is the standard DirectX matrix struct. "image" is an instance of my Image class, which is simply a wrapper around a D3D Texture object. Image.OriginalSize is the original size of the file from which the texture was loaded. "curTexTransform" is simply my engine's cache of the current texture transform. "dev" is a Direct3D Device object. Again, the above code works fine and properly scales texture coordinates. However, my problem lies in trying to introduce coordinate translation in addition to scaling. Here's the code I'm trying to get working:
DX.Matrix scaling = (image != null)?(DX.Matrix.Scaling(1f/image.OriginalSize.Width, 1f/image.OriginalSize.Height, 1f)):(DX.Matrix.Identity);
DX.Matrix translation = DX.Matrix.Translation(32, 0, 0);
DX.Matrix m = translation * scaling;
if(m != curTexTransform)
{
	dev.Transform.Texture0 = m;
	curTexTransform = m;
}



In terms of the graphical output to the screen, this code behaves identically to the code that only scales coordinates. My goal here is to translate texture coordinates 32 pixels to the right before scaling them (for testing purposes). Note that I tried both "m = translation * scaling" and "m = scaling * translation", as well as commenting out the if statement, such that the two statements in the body of the if are always called. In all cases, the output to the screen is identical to the code that only scales. The texture I'm using to test this was created such that translating the x coordinate by 32 pixels should produce wildly different output. [edit: texture coordinate rotation, while pretty pointless in my case, appears to work fine] I should probably also say that I enable texture coordinate translation like so: "dev.TextureState[0].TextureTransform = TextureTransform.Count2;" [edit: none of the other TextureTransform values work either; Count3 and Count4 have no effect, and Projected screws everything up.] Anyone know why my code isn't properly translating texture coordinates? Thanks! - Fuzz [Edited by - Holy Fuzz on August 1, 2005 10:58:49 AM]

Share this post


Link to post
Share on other sites
Advertisement
All right, problem solved. It appears that DirectX handles 2D transformation differently than 3D transformations for some reason, though I have no idea why. The following code does the trick.


Vector2D scale = (image != null)?(new Vector2D(1f/image.OriginalSize.Width, 1f/image.OriginalSize.Height)):(new Vector2D(1, 1));
DX.Matrix m = DX.Matrix.Scaling(scale.X, scale.Y, 0f);
m.M31 = scale.X * 32; // X translation.
m.M32 = scale.Y * 0; // Y translation.
if(m != curTexTransform)
{
dev.Transform.Texture0 = m;
curTexTransform = m;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!