Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SharpDX] Textures are rendered blurry


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 addy914   Members   -  Reputation: 158

Like
0Likes
Like

Posted 18 June 2012 - 10:35 PM

I am sure that this topic has probably been posted a billion times, and I have looked around on Google(for quite a while) trying to find the answer to my problem. I am not the best with DirectX, or even OpenGL for that matter. For some reason, there's so many concepts to 3D programming that it overwhelms and confuses me.
I am using SharpDX(Direct3D9 to be exact) to render textures onto the screen. It's just a simple 2D game, so there's not a lot to it. I will show a few segments of my code and I will also list the things that I have tried and that have failed.

Public Sub InitD3D(ByVal form As Form, ByVal windowed As Boolean)
	    direct3D = New Direct3D()
	    Dim deviceSettings As PresentParameters
	    deviceSettings.BackBufferCount = 1
	    deviceSettings.BackBufferWidth = form.ClientSize.Width
	    deviceSettings.BackBufferHeight = form.ClientSize.Height
	    deviceSettings.DeviceWindowHandle = form.Handle
	    deviceSettings.Windowed = windowed
	    deviceSettings.PresentationInterval = PresentInterval.Immediate
	    deviceSettings.SwapEffect = SwapEffect.Discard
	    device = New Device(direct3D, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, deviceSettings)
	    device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(0.0!, Convert.ToSingle(form.ClientSize.Width), 0.0!, Convert.ToSingle(form.ClientSize.Height), 0.0!, 1.0!))
	    device.SetTransform(TransformState.View, Matrix.Identity)
	    device.SetTransform(TransformState.World, Matrix.Identity)
	    device.SetTransform(TransformState.Texture1, Matrix.Identity)
	    device.Viewport = New Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height)
	    device.SetRenderState(RenderState.Lighting, False)
	    additionalSwapChains = New Dictionary(Of String, SwapChain)()
    End Sub

    Public Sub LoadTexture(ByVal fileName As String, ByRef texture As Texture)
	    Try
		    texture = texture.FromFile(device, fileName, Usage.None, Pool.Managed)
	    Catch ex As Exception
		    Throw New Exception("Failed to load texture from " + fileName)
	    End Try
    End Sub
    Public Sub Draw(ByVal texture As Texture, ByVal dx As Integer, ByVal dy As Integer, ByVal dw As Integer, ByVal dh As Integer, Optional ByVal sx As Integer = 0, Optional ByVal sy As Integer = 0)
	    device.VertexFormat = VertexFormat.Position Or VertexFormat.Texture1
	    device.SetTexture(0, texture)

	    Dim left As Single = Convert.ToSingle(dx) - 0.5!
	    Dim right As Single = Convert.ToSingle(dx + dw) - 0.5!
	    Dim bottom As Single = Convert.ToSingle(dy) - 0.5!
	    Dim top As Single = Convert.ToSingle(dy + dh) - 0.5!
	    Dim vertices(4) As CustomTextureVertex
	    vertices(0).position = New Vector3(left, top, 0.0!)
	    vertices(0).texPosition = New Vector2(0.0!, 0.0!)
	    vertices(1).position = New Vector3(right, top, 0.0!)
	    vertices(1).texPosition = New Vector2(1.0!, 0.0!)
	    vertices(2).position = New Vector3(left, bottom, 0.0!)
	    vertices(2).texPosition = New Vector2(0.0!, 1.0!)
	    vertices(3).position = New Vector3(right, bottom, 0.0!)
	    vertices(3).texPosition = New Vector2(1.0!, 1.0!)
	    device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vertices)
    End Sub

Things I have tried:
Adjusting offset by 0.5f, didn't work
The dw & dh is the actual height, not the Powers of Two width/height
The position & texPosition are perfectly fine
Setting minFilter,magFilter,mipFilter,addressU,addressV to different values and nothing(I also placed it in InitD3D and Draw functions in case it mattered)

The last thing I tried could also be my problem because I did not go through and try every possibility, I have just used settings from other help topics.

Thanks for any help, it's greatly appreciated.

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7970

Like
0Likes
Like

Posted 19 June 2012 - 07:14 AM

It's almost definitely half-pixel offset but your previous attempt at offsetting by 0.5f was somehow incorrect. If you still have it, could you post the code you tried?

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 addy914   Members   -  Reputation: 158

Like
0Likes
Like

Posted 19 June 2012 - 07:40 AM

I left the half pixel offset peice of code in there. In the draw function it is where I define the left,top,bottom,right. I subtract 0.5! from them. I think I have found my main cause, the texture I am using is 128x192, and when I load it, it does load it on a 128x256 texture. Well, I made sure that it rendered 128x192, but I did not pay attention to the texture. It stretches the texture to fit the 128x256 which is causing the problem. I saved the texture to a file and I saw that it was stretched.
What could I do to turn off the stretching when loading the texture?
Also, what's a way to get the true width and height of the texture(non power of two dimensions)

I have got it figured out with a little bit of messing around. Now I just need to find a way to get the true width/height of the texture, but I'll google around for that. For future reference, the solution I made was this for loading my texture:
texture = texture.FromFile(device, fileName, 0, 0, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.Point, Filter.Box, 0)
I just changed the filter method basically.

Edited by addy914, 19 June 2012 - 08:04 AM.


#4 xycsoscyx   GDNet+   -  Reputation: 318

Like
0Likes
Like

Posted 20 June 2012 - 06:46 PM

Standard texture sizes are usually going to be power of two in size (because of hacks to speed up the math if I recall), so it will always end up 128x256. The only real exception to that is render targets, which can be any size (but I believe there's a lot of internal things going on to do that). When you're loading a regular texture, it will get the size and then find the next power of two size greater than that, and then scale accordingly using whatever settings you're specified, there isn't a way to actually disable this. If you want full control over the result, you'll need to either manually stretch the image (to handle scaling artifacts yourself), or make the image 128x256 and fill in the extra area with whatever. If you just fill in the rest, then when rendering you'll need to offset the texture coordinates to account for the difference, so instead of 0-1, it's 0-(192/256).

Edited by xycsoscyx, 20 June 2012 - 06:50 PM.


#5 mhagain   Crossbones+   -  Reputation: 7970

Like
0Likes
Like

Posted 21 June 2012 - 01:13 AM

Well, not really. Most hardware these days is actually very capable of supporting non-power-of-two textures, so those old restrictions are largely dead. However, you need texture loading code that recognises this and loads the texture appropriately - not entirely certain how texture.FromFile is implemented but it should be doing so.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS