Sign in to follow this  

Rendered a quad, but the texture is grainy and mirrored

This topic is 4490 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

They say a picture is worth a thousand words. The image on the right is drawn with directx. Image Hosted by ImageShack.us I managed to get my textured quad to display, but the image is reversed and very poor quality. How can I fix that? I setup an OrthoLH projection and 4 vertices that are rendered as a traingle strip. The region that I'm drawing to is a 512x512 panel, and I'm using a 512x512 bitmap that I loaded into a texture like: texture = new Texture(device, new Bitmap(Image.FromFile(@"M:\Images\5.jpg")), Usage.Dynamic, Pool.Default); Here's my relevant code for the camera and the vertices. Thanks, -Sandra
[source code=C#]
private void SetupCamera()
{
	device.Transform.Projection = Matrix.OrthoLH(2, 2, 0.0f, 1.0f);
        // **If I comment this line out, the image is no longer reversed**
	device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 1.0f), new Vector3(), new Vector3(0, 1, 0));
}

private void SetupVertices()
{
	vertices = new VertexBuffer(typeof(CustomVertex.PositionTextured),
		4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);

	CustomVertex.PositionTextured [] quad = (CustomVertex.PositionTextured[]) vertices.Lock(0, 0);

	// Bottom Left
	quad[0] = new CustomVertex.PositionTextured(-1, -1, 0.0f, 0,1);
	// Top Left
	quad[1] = new CustomVertex.PositionTextured(-1, 1, 0.0f, 0,0);
	// Bottom Right
	quad[2] = new CustomVertex.PositionTextured(1, -1, 0.0f, 1,1);
	// Top Right
	quad[3] = new CustomVertex.PositionTextured(1, 1, 0.0f, 1,0);

	vertices.Unlock();
}



[Edited by - Sandra-24 on August 29, 2005 5:16:37 PM]

Share this post


Link to post
Share on other sites
I posted the code right there. I assume they are correct. 0,0 being the top left, and 1,1 being the bottom right. If I take out the View = Matrix.LookAtLH() line then the image is no longer reversed. I'm less worried about why it is reversed than about why it is so blurry.

-Sandra

Share this post


Link to post
Share on other sites
The blurring just looks like a resolution problem. And chance you're somehow creating the surface with a different resolution?

As for the flipping of the texture, it's probably a LH vs RH thing. Flip the order of your vertices.

Share this post


Link to post
Share on other sites
I had the same problem recently (never solved it). I was trying to make a skybox and my high res texture (512x512) looked awful like your screen shot. I don't know what the problem was because it was based on some old code I had written that worked perfectly. There's gotta be some filtering or mip mapping happening to cause that. I'll be monitoring this thread for the resolution. :)

Perhaps if you showed the code where you load and render the textures, it might shed some more light.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike Bossy
The blurring just looks like a resolution problem. And chance you're somehow creating the surface with a different resolution?

As for the flipping of the texture, it's probably a LH vs RH thing. Flip the order of your vertices.


Either what he said, or you have a backbuffer smaller than the destination rect in the form.
Texture filtering is disabled by default, but if you want to change it it must be something like device.SetSamplerState.. (I don't know C#).

Would you mind to show us your D3D presentation parameters that you're using to create the device?

Share this post


Link to post
Share on other sites
Ok, two things:

1) To make life easier, when you make the projection matrix use coordinates that correspond to pixels. That way positions of vertices line up with pixel positions on screen.

2) Once you do that, you need to correctly map texels to pixels. That means you need to translate all of the vertices by exactly (-.5, -.5). You can read more about this here. The basic idea is that mapping texels to pixels is a little unintuitive because of the way texels are referenced, and this leads to texels overlapping 4 pixels each and blurring the entire image.

Share this post


Link to post
Share on other sites
Thanks to everyone for all the input I really appreciate the help.

Quote:
Either what he said, or you have a backbuffer smaller than the destination rect in the form.


Bang on. I added:

presentParams.BackBufferHeight = 512;
presentParams.BackBufferWidth = 512;

To the code in Initialize() and it looks just like the image on the left (except reversed.) Thanks a bundle!

Here's what it looks like now:
Free Image Hosting at www.ImageShack.us

Quote:
As for the flipping of the texture, it's probably a LH vs RH thing. Flip the order of your vertices.


Reversing the order of the vertices had no effect at all. Changing the projection to RH and the z palne to -1.0 - 0 had no effect with either vertex order.

Quote:
To make life easier, when you make the projection matrix use coordinates that correspond to pixels. That way positions of vertices line up with pixel positions on screen.


This I would love. But how?

Quote:
Once you do that, you need to correctly map texels to pixels...


I think I understand how to do this, but I would need to solve 1) first.

Quote:
Would you mind to show us your D3D presentation parameters that you're using to create the device?


[source land=C#]
private void Initialize()
{
presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.DeviceWindow = this;
presentParams.BackBufferHeight = 512;
presentParams.BackBufferWidth = 512;
presentParams.PresentationInterval = PresentInterval.One;
presentParams.SwapEffect = SwapEffect.Discard;

// store our default adapter
int adapterOrdinal = Manager.Adapters.Default.Adapter;
// get our device capabilities so we can check
Caps caps = Manager.GetDeviceCaps(adapterOrdinal, DeviceType.Hardware);
CreateFlags createFlags;

if(caps.DeviceCaps.SupportsHardwareTransformAndLight)
createFlags = CreateFlags.HardwareVertexProcessing;
else
createFlags = CreateFlags.SoftwareVertexProcessing;

if(caps.DeviceCaps.SupportsPureDevice)
createFlags |= CreateFlags.PureDevice;

// create our device
device = new Device(adapterOrdinal, DeviceType.Hardware, this.Handle, createFlags, presentParams);

SetupDevice();
SetupCamera();
SetupVertices();
}


Share this post


Link to post
Share on other sites
I usually use the Offcenter versions of the Ortho Matrix functions, and pass in (0, Width, Height, 0) for the sizes that the function takes. This will set it up so that the coordinates are screen coordinates, where the top left is (0, 0), positive Y goes down the screen, and positive X to the right. You can switch around the coordinates, translate them, etc to get different coordinate schemes.

Share this post


Link to post
Share on other sites
My guess at why your texture is reversed is because your camera is positioned behind the quad and looking at it, like this:

+ <-- this is probably where your camera is



+-------+ <-- this is the textured quad



+ <-- this is where the camera should be





After you position the camera, if your polygons don't show up don't freak out, your polygons are just being culled. Just create your vertices in a counter-clockwise order.

[Edit] Repaired the screwed up ascii-art

Also, to make texels match pixels, you have to:
1. Make sure your backbuffer size is exacly the same size as your destination rect in the form
2. device.Transform.Projection = Matrix.OrthoLH( backbuffer_width, backbuffer_height, 0.0f, 1.0f);
3. As intrest86 said you have to subtract 0.5 to your vertices' x and y positions

[Edit2] Also make sure you're still looking at the quad after you change your camera's z position

Share this post


Link to post
Share on other sites
Quote:
I usually use the Offcenter versions of the Ortho Matrix functions, and pass in (0, Width, Height, 0) for the sizes that the function takes. This will set it up so that the coordinates are screen coordinates, where the top left is (0, 0), positive Y goes down the screen, and positive X to the right.


Well I tried that and I played around with the coordinates without too much luck until I commented out the view matrix. Then it was just as you said (no surprise there.) I see an image that is rendered perfectly (no blurriness, not flipped in any direction), with (0,0) in the top left and (Width,Height) in te bottom right. When I moved the coordinates up by a half a pixel and left by half a pixel the texture lines up on the pixel boundaries and the 1 pixel border disappeared. But now how can I reintroduce the view matrix? It would be perfect to implement scrolling If I could simply move the camera around. Here's my current code:

-Sandra

[source lang=c#]
private void SetupCamera()
{
float centerX = Width / 2.0f;
float centerY = Height / 2.0f;

device.Transform.Projection = Matrix.OrthoOffCenterLH(0, Width, Height, 0, 0.0f, 1.0f);
//device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 1.0f), new Vector3(0, 0, 0.0f), new Vector3(0, 1, 0));
}

private void SetupVertices()
{
vertices = new VertexBuffer(typeof(CustomVertex.PositionTextured),
4, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);

CustomVertex.PositionTextured [] quad = (CustomVertex.PositionTextured[]) vertices.Lock(0, 0);

// Top Left
quad[0] = new CustomVertex.PositionTextured(-0.5f, -0.5f, 0.0f, 0,0);
// Top Right
quad[1] = new CustomVertex.PositionTextured(Width, -0.5f, 0.0f, 1,0);
// Bottom Left
quad[2] = new CustomVertex.PositionTextured(-0.5f, Height, 0.0f, 0,1);
// Bottom Right
quad[3] = new CustomVertex.PositionTextured(Width, Height, 0.0f, 1,1);

vertices.Unlock();
}

Share this post


Link to post
Share on other sites

This topic is 4490 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.

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