Hi everyone,
I'm currently having a bug with my aspect ratio in my gis. I'm trying to figure out where I should fix the problem.
I have a panel in a form showing a directx viewport. The problem is when I resize the form i get some kind of scaling which mean that the aspect ratio is not preserved. I'm just not quite sure how to preserve the aspect ratio and how to do it. Is it with the camera or the viewport ?
this is where I set the camera and the world : ptd is the panel on which the viewport is drawn.
[source lang="csharp"] private void SetWorldAndCam()
{
// Set Constant Buffer
cbPerObjectBufferDesc = new BufferDescription();
cbPerObjectBufferDesc.SizeInBytes = Marshal.SizeOf(typeof(cbPerObject));
cbPerObjectBufferDesc.Usage = ResourceUsage.Default;
cbPerObjectBufferDesc.BindFlags = BindFlags.ConstantBuffer;
cbPerObjectBufferDesc.CpuAccessFlags = CpuAccessFlags.None;
cbPerObjectBufferDesc.OptionFlags = ResourceOptionFlags.None;
cbPerObjectBufferDesc.StructureByteStride = Marshal.SizeOf(typeof(cbPerObject));
// Set Camera
camPos = 5;
camPosition = new Vector3(0.0f, 0.0f, -1.0f);
camTarget = new Vector3(0.0f, 0.0f, 0.0f);
camUp = new Vector3(0.0f, 1.0f, 0.0f);
camView = Matrix.LookAtLH(camPosition, camTarget, camUp);
camProj = Matrix.OrthoLH(ptd.Width, ptd.Width, 0.00001f, 1000.0f);
World = Matrix.Identity;
WVP = World * camView * camProj;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Write(cbPerObj);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData,cbPerObjectBufferDesc);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);
}[/source]
This one is triggered when I resize the form :
[source lang="csharp"] public void ResizeViewport()
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, ptd.Width, ptd.Height, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
depthStencilDesc.Width = ptd.Width;
depthStencilDesc.Height = ptd.Height;
depthStencilBuffer.Dispose();
depthStencilBuffer = new Texture2D(device, depthStencilDesc);
depthStencilView.Dispose();
depthStencilView = new DepthStencilView(device, depthStencilBuffer);
context.OutputMerger.SetTargets(depthStencilView,renderTarget);
viewport = new Viewport(0, 0, ptd.Width, ptd.Height);
context.Rasterizer.SetViewports(viewport);
UpdateScene(0, 0, 0);
}[/source]
This is where i update the scene.
[source lang="csharp"] public void UpdateScene(float x , float y, float z)
{
float currentX;
float currentY;
float currentIH = 0;
float currentIW = 0;
currentX = x * (float)camPos;
currentY = y * (float)camPos;
if (z != 0)
{
if (z > 0 && camPos < 20 )
{
// currentIH = ImageHeight / ((float)ptd.Width / (float)ptd.Height);
// currentIW = ImageWidth / ((float)ptd.Width / (float)ptd.Height);
currentIH = ImageHeight / 2;
currentIW = ImageWidth / 2;
camPos = camPos + 0.5;
}
else
{
if (z < 0 && camPos > 1)
{
// currentIH = ImageHeight * ((float)ptd.Width / (float)ptd.Height);
// currentIW = ImageWidth * ((float)ptd.Width / (float)ptd.Height);
currentIH = ImageHeight * 2;
currentIW = ImageWidth * 2;
camPos = camPos - 0.5;
}
}
Console.Write(camPos + "\r");
ImageHeight = currentIH;
ImageWidth = currentIW;
}
camView = camView * Matrix.Translation(new Vector3(currentX, -currentY, 0.0f));
WVP = World * camView * camProj ;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Write(cbPerObj);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData, cbPerObjectBufferDesc);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);
}[/source]
any help would be appreciated. thanks in advance.
Keeping the aspect ratio when resizing control
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement