Sign in to follow this  
jaredf55

Texture transform and color preservation

Recommended Posts

I have a texture that I am trying to shrink to fit the area it is drawn to. This is a 2D texture being drawn to a sprite. When the texture is shrunk significantly, the colors fade. I'm trying to figure out how to prevent this. I've read a few other articles that imply the faded colors could be prevented by changing the SamplerState for MagFilter and MinFilter. So, immediately before the Draw method, I call SetSamplerState for both the MagFilter and MinFilter and set them to Point. But when I call the Draw method of the sprite, the texture still becomes gray instead of staying white. The Texture is grayscale, but really only uses white and black. And black is transparent. I'm doing this in C# and using slimdx, though any directX ideas would be helpful, and I can figure out what SlimDX Calls are necessary. The actual code is as follows: float scaleFactor = (float)destRect.Width / sourceRect.Width; _sprite.Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Point); _sprite.Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Point); _sprite.Transform *= Matrix.AffineTransformation2D(scaleFactor, new Vector2(0, 0), 0, new Vector2(destRect.X, destRect.Y)); _sprite.Draw(overlayDisplayTexture, new Rectangle(0, 0, sourceRect.Width, sourceRect.Height), new Color4(1.0f, 1.0f, 1.0f, 1.0f)); I appreciate your ideas.

Share this post


Link to post
Share on other sites
I don't know about SlimDX, but in C++ / DirectX, you need to call ID3DXSprite::Begin() which will set all device state for sprite rendering, then you can call SetSamplerState, etc, then Draw(). It could be that this is the same here?

Share this post


Link to post
Share on other sites
I appreciate the reply. And should have clarified, Sprite.Draw and End are being called before and after the posted code, plus the Device.BeginScene and EndScene functions are wraped around the sprite calls.

Share this post


Link to post
Share on other sites
Oddly enough, this issue resolved itself. Not sure what happened or why, since the code hasn't changed in a way that I would think would cause this problem to stop.

Share this post


Link to post
Share on other sites
Ok, so I was mistaken in that it was fixed, becuase it really wasn't. The graphics was changed from using DirectX to GDI. After changing it back, i discovered that it hadn't fixed itself. So, after some guess and check, I learned that I needed to change the code from:
_sprite.Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Point);
_sprite.Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Point);

to just:
_sprite.Device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.None);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this