Rendered a quad, but the texture is grainy and mirrored

Started by
11 comments, last by Sandra-24 18 years, 7 months ago
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]
Advertisement
"the image is reversed " -> your texture coordinates are right ?
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
try to disable / change some filtering / mipmap modes.
Ethereal
I have no filtering / mipmaping or anything enabled, unless such things are enabled by default, in which case how do I turn them off?

-Sandra
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.
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.
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?
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.
Turring Machines are better than C++ any day ^_~
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();}

This topic is closed to new replies.

Advertisement