Jagged edges and weird shading

I am using the XNA built into WinForms, which is downloaded from the Microsoft website. And they noticed that when the models are drawn, everything looks fine, but as soon as I rotate my camera, the edges on the model begin to look jagged. Here are two pictures of what I'm talking about:

This is more noticeable the closer the camera is to the target. I use this to draw each grid:

foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.View = cam.view; effect.Projection = cam.projection; effect.World = mesh.ParentBone.Transform; effect.EnableDefaultLighting(); } mesh.Draw(); } 

Also sometimes, when I rotate the model, some strange shading appears. Shadows do not even change their "location", but remain elongated the same.


EDIT: So, I searched a little Google and saw that turning on MultiSampling should get rid of the jagged edges. Now, does anyone know how to do this in WinForms?

EDIT 2: About the backbuffer, I don't install it anywhere, so I assume this is the way it should be. This is the constructor of GraphicsDeviceService.cpp:

  GraphicsDeviceService(IntPtr windowHandle, int width, int height) { parameters = new PresentationParameters(); parameters.BackBufferWidth = Math.Max(width, 1); parameters.BackBufferHeight = Math.Max(height, 1); parameters.BackBufferFormat = SurfaceFormat.Color; parameters.DepthStencilFormat = DepthFormat.Depth24; parameters.DeviceWindowHandle = windowHandle; parameters.PresentationInterval = PresentInterval.Immediate; parameters.IsFullScreen = false; graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach, parameters); } 

Also, the reset method sets the backBuffer accordingly.

EDIT 3: I tried to set MultiSampleCount to a larger number, but nothing helped, now here are two pictures of what is happening. First I create an object in some place, and then I move the camera only to the right. And the whole object is stretched and gets these jagged edges, as shown in this image below. This is the camera movement code:

http://img152.imageshack.us/img152/5204/normalu.png

http://img28.imageshack.us/img28/4434/movedright.png

 KeyboardState state = Keyboard.GetState(); Vector3 v; if (state.IsKeyDown(Keys.Up) || state.IsKeyDown(Keys.W)) v = new Vector3(0, 0, 1) * moveSpeed; else if (state.IsKeyDown(Keys.Down) || state.IsKeyDown(Keys.S)) v = new Vector3(0, 0, -1) * moveSpeed; else if (state.IsKeyDown(Keys.Left) || state.IsKeyDown(Keys.A)) v = new Vector3(1, 0, 0) * moveSpeed; else if (state.IsKeyDown(Keys.Right) || state.IsKeyDown(Keys.D)) v = new Vector3(-1, 0, 0) * moveSpeed; else if (state.IsKeyDown(Keys.PageUp)) v = new Vector3(0, -1, 0) * moveSpeed; else if (state.IsKeyDown(Keys.PageDown)) v = new Vector3(0, 1, 0) * moveSpeed; else v = new Vector3(0, 0, 0); view *= Matrix.CreateTranslation(v); 

And the view and projection of the camera:

 view = Matrix.CreateLookAt(cameraPos, Vector3.Zero, Vector3.Up); projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), ratio, 0.5f, 50f); 

cameraPos in this case 0,0,10 .

EDIT: Shading I finnally got a picture of the shading problem. As you can see, each grid has its own shadow, and not just one shadow passing through all the grids.

http://img843.imageshack.us/img843/8263/weirdshadowing.png

This is the top view of the model.

+4
source share
1 answer

XNA anti-aliasing built into WinForms

This question is actually a duplicate of this question .

Download the XNA / WinForms Example here .

In your GraphicsDeviceService class, navigate to the constructor:

 GraphicsDeviceService(IntPtr windowHandle, int width, int height) { parameters = new PresentationParameters(); // Add this line // Increase the count to get higher quality anti-aliasing parameters.MultiSampleCount = 8; // More parameter settings and initialization // ... } 

You can see the difference in the image below:

with and without multisampling comparison


Old answer

Smoothing for a standard XNA application

 graphics.PreferMultiSampling = true; 

provides anti-aliasing for your backbuffer as described in this MSDN article .

The PreferMultiSampling property is a member of the GraphicsDeviceManager .

Quality increase / decrease

MultiSampleCount property of PresentationParameter can be used to change the number of samples. More samples per pixel means less artifacts and longer rendering time.


Further Reading on Multisampling


Please note that this solution will be effective only if the source of the problem is in the graphics pipeline, or rather the rasterizer.

As noted by Nico Schertler , make sure that the image resolution does not change when embedding it in WinForms. If the steps at the edges are not equal to one pixel, this is a strong indicator that the problem arises from the texture view in WinForms.

I cannot solve the shadow problem here, since it is difficult to guess the lighting parameters from the images.

+3
source

Source: https://habr.com/ru/post/1446652/


All Articles