• Advertisement
Sign in to follow this  

DX11 Camera Rotation in DX11

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

Hi People :>

 

So I recently got started with DX11 and graphics programming in general. I followed tutorials and a book on how to understand and set up the basic stuff. At this point I've managed putting a colored cube in a grey window. I also implemented my own input system (mouse and keyboard) based on window messages.

Now I have come to implementing camera controls, and when I tried to rotate it I came across a problem. I have made it so that when I hold down the right mousebutton I enable rotating, and moving the mouse then rotates the camera based on how far the mouse-cursor has traveled since the last frame (pixels). Moving the mouse in either the horizontal or vertical direction alone creates no problem, but as soon as I do a circle-movement with the mouse, the camera starts spinning. So for a reason I don't know I get kind of a "rolling" of the camera in that situation. Does anyone have an idea why this is happening ?

 

The viewmatrix is being bult like this:

View = XMMatrixLookAtLH( EyePos, Focus, UpDir );

Update function of the camera, is being called every frame.

void Camera::Update()
{
	if( fwd )
		Forward();

	if( back )
		Backwards();

	if( left )
		Left();

	if( right )
		Right();

	if( rotate )
	{
		Rotate();
		View *= RotationX;
		View *= RotationY;
	}

	cb.mView = XMMatrixTranspose( View );
	m_pImmediateContext->UpdateSubresource( pConstantBuffer, 0, NULL, &cb, 0, 0 );
}

function that does the rotating, GetMouseDelta() returns how far the mouse has traveled since the last frame

void Camera::Rotate()
{
	RotationX = XMMatrixRotationAxis( XMVectorSet( 1.0f, 0.0f, 0.0f, 0.0f ), -(float)pInput->GetMouseDelta().y*0.001f );
	RotationY = XMMatrixRotationAxis( XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ), -(float)pInput->GetMouseDelta().x*0.001f );
}
Edited by moodiez

Share this post


Link to post
Share on other sites
Advertisement

Your problem is probably caused by the order of transformations. First of, do you want to yield in a look-around rotation, in an orbiting around the focused object, or else (least probable) a rotation around the global origin? When rotation is applied, should it be done w.r.t. to some local or some global axes (I mean is it really yaw/pitch or do you want to spin around the global up direction regardless of any pitch)?

 

You further have to clarify whether you use row or column vectors. AFAIK the XMMatrixLookAtLH returns a row vector matrix. Then you multiply View with the incremental rotations you do it in an order that seems me odd. Then you're applying a XMMatrixTranspose onto the view matrix before transporting it to the shader script. I'm not familiar with D3D so my question is w.r.t. the transpose: Is this needed for the CBuffer and/or the shader script, or what is the reason for this?

Share this post


Link to post
Share on other sites

Hi haegarr.

To answer your questions, I want to rotate the camera around its local axes, not around any other object or global axes. I want to create a "camera" which I can rotate around itself and later on impement to manipulate its position in world space.

The way I rotate the view at the moment works fine, as long as I only rotate it around ONE axis.

 

The transposing is needed to update the data-structure thats being passed ot the shader (world,view,projection matrices)

 

Thanks in advance for further help :)

Share this post


Link to post
Share on other sites

You must be aware that rotating around the x axis followed by rotating around the y axis is different from rotating around the y axis followed by rotating around the x axis. Hence, if you push the mouse 10 units to the right and then 10 unit upwards, the result will differ from pushing the mouse 10 units upwards and then 10 units to the right. If you move the mouse more or less diagonally and do so with relatively high speed, the result will differ from a movement in the same direction but with significantly lower speed. This is because in the first case you do e.g.

    Ry(45°) * Rx(45°)

while in the latter case you do (assuming 1/3 of speed with 3 samples in time)

    Ry(15°) * Rx(15°) * Ry(15°) * Rx(15°) * Ry(15°) * Rx(15°)

 

Notice that the above shows what is called incremental rotation, especially if the angles get really small. The smaller the angles the more independent the rotation. However, you cannot process with arbitrarily high speed, and you will not enforce the user to move the mouse really slowly.

 
When you look at your current solution you'll see that the axis of rotation is orthogonal to the direction of mouse movement: horizontal movement = upwards directed axis of rotation; vertical movement = sidewards directed axis of rotation. Generalizing this means that however the mouse is moved, the axis of rotation is the perpDot of the movement direction. It is perhaps worth a try (I'm not 100% sure of the success, though).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By turanszkij
      Hi, right now building my engine in visual studio involves a shader compiling step to build hlsl 5.0 shaders. I have a separate project which only includes shader sources and the compiler is the visual studio integrated fxc compiler. I like this method because on any PC that has visual studio installed, I can just download the solution from GitHub and everything just builds without additional dependencies and using the latest version of the compiler. I also like it because the shaders are included in the solution explorer and easy to browse, and double-click to open (opening files can be really a pain in the ass in visual studio run in admin mode). Also it's nice that VS displays the build output/errors in the output window.
      But now I have the HLSL 6 compiler and want to build hlsl 6 shaders as well (and as I understand I can also compile vulkan compatible shaders with it later). Any idea how to do this nicely? I want only a single project containing shader sources, like it is now, but build them for different targets. I guess adding different building projects would be the way to go that reference the shader source project? But how would they differentiate from shader type of the sources (eg. pixel shader, compute shader,etc.)? Now the shader building project contains for each shader the shader type, how can other building projects reference that?
      Anyone with some experience in this?
    • By osiris_dev
      Hello!
      Have a problem with reflection shader for D3D11:
      1>engine_render_d3d11_system.obj : error LNK2001: unresolved external symbol IID_ID3D11ShaderReflection
      I tried to add this:
      #include <D3D11Shader.h>
      #include <D3Dcompiler.h>
      #include <D3DCompiler.inl>
      #pragma comment(lib, "D3DCompiler.lib")
      //#pragma comment(lib, "D3DCompiler_47.lib")
      As MSDN tells me but still no fortune. I think lot of people did that already, what I missing?
      I also find this article http://mattfife.com/?p=470
      where recommend to use SDK headers and libs before Wind SDK, but I am not using DirectX SDK for this project at all, should I?
    • By trojanfoe
      Hi there, this is my first post in what looks to be a very interesting forum.
      I am using DirectXTK to put together my 2D game engine but would like to use the GPU depth buffer in order to avoid sorting back-to-front on the CPU and I think I also want to use GPU instancing, so can I do that with SpriteBatch or am I looking at implementing my own sprite rendering?
      Thanks in advance!
    • By Matt_Aufderheide
      I am trying to draw a screen-aligned quad with arbitrary sizes.
       
      currently I just send 4 vertices to the vertex shader like so:
      pDevCon->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
      pDevCon->Draw(4, 0);
       
      then in the vertex shader I am doing this:
      float4 main(uint vI : SV_VERTEXID) : SV_POSITION
      {
       float2 texcoord = float2(vI & 1, vI >> 1);
      return float4((texcoord.x - 0.5f) * 2, -(texcoord.y - 0.5f) * 2, 0, 1);
      }
      that gets me a screen-sized quad...ok .. what's the correct way to get arbitrary sizes?...I have messed around with various numbers, but I think I don't quite get something in these relationships.
      one thing I tried is: 
       
      float4 quad = float4((texcoord.x - (xpos/screensizex)) * (width/screensizex), -(texcoord.y - (ypos/screensizey)) * (height/screensizey), 0, 1);
       
      .. where xpos and ypos is number of pixels from upper right corner..width and height is the desired size of the quad in pixels
      this gets me somewhat close, but not right.. a bit too small..so I'm missing something ..any ideas?
       
      .
  • Advertisement