lundi 23 février 2015

Stop 3D objects moving when windows is resized [XNA/WinForms/C#]

I have an XNA windows inside a a windows form, and I am running into a problem when I resize the window.


The behaviour I want is for the rendered cube to not move at all. So if there was a second cube offscreen to the right, and the user resized the window to the right, then the second cube would come into view. So basically resizing the window has no effect on the rendered elements at all.


But when I resize the window, for example drag it right, the cube also moves to the right sad.png And when I drag the window down the cube zooms in slowly.


Here is my draw code for each quad of the cube:



GraphicsDevice dev = Core.Device;

dev.BlendState = BlendState.Opaque;
dev.DepthStencilState = DepthStencilState.Default;

dev.SetVertexBuffer(_vb);
dev.Indices = _ib;

effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 150), Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, dev.Viewport.AspectRatio, 0.1f, 10000.0f);
effect.World = Matrix.Identity;

effect.EnableDefaultLighting();
effect.Texture = Texture;

effect.CurrentTechnique.Passes[0].Apply();
dev.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);


I am using the winforms/xna sample from the xna website. When the window is resized the device is reset and the backbuffer width and height get set to the xna windows width and height.


This means the Viewport.AspectRatio gets updated and the 3d cubes do not get stretched. Now I just need their position to not change.


Does anyone know what else I could try?


Aucun commentaire:

Enregistrer un commentaire