Texture transform and color preservation

Started by
3 comments, last by jaredf55 15 years, 7 months ago
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.
Advertisement
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?
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.
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.
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);

This topic is closed to new replies.

Advertisement