简体   繁体   中英

Direct3D9 Can't get vertical fov camera working

I'm trying getting started with Direct 3D. But i can't get the fov camera working. At least not the vertical movement. I figured out why but i still can't fix it.

I'm using C++ (140) and D3DX9

Code:

D3DXMATRIX viewMat;

static D3DXMATRIX viewRotXMat;
static D3DXMATRIX viewRotYMat;
static D3DXMATRIX viewRotZMat;

static float rotY = 0.0f;
static float rotXZ = 0.0f;

if (GetAsyncKeyState(VK_LEFT)) { rotY += 0.1f; }
if (GetAsyncKeyState(VK_RIGHT)) { rotY -= 0.1f; }

if (GetAsyncKeyState(VK_UP)) { rotXZ += 0.1f; }
if (GetAsyncKeyState(VK_DOWN)) { rotXZ -= 0.1f; }

//POINT pMO = getMouseOffset();
//rotY += pMO.x / 10;
//rotXZ += pMO.y / 10;

D3DXMatrixLookAtLH(&viewMat,
    &D3DXVECTOR3(10.0f, 10.0f, 0.0f), //pos
    &D3DXVECTOR3(0.0f, 10.0f, 0.0f),  //look-at
    &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //weird values


D3DXMatrixRotationY(&viewRotYMat, rotY);
D3DXMatrixRotationX(&viewRotXMat, cos(rotY) * rotXZ);
D3DXMatrixRotationZ(&viewRotZMat, (sin(rotY) * rotXZ) * -1);

d3ddev->SetTransform(D3DTS_VIEW, &(viewMat * viewRotXMat * viewRotYMat * viewRotZMat));


D3DXMatrixPerspectiveFovLH(&projMat,
                           D3DXToRadian(90),
                           (FLOAT)SIZEX / (FLOAT)SIZEY,
                           1.0f,
                           1000.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &projMat);

This is the important part of my RenderFrame function. The reason why it won't work is because of rotY and rotXZ. For some reason when i turn the camera about 45° ingame the rotY variable only changes to 3-4. So why the heck is't the D3DXMatrixRotationY function taking an angle as an parameter but some float? The parameter is even named angle... I'm on this problem for about 3 days now. I hope you can help me out.

Your setup for rotation matrix looks a bit weird:

D3DXMatrixRotationY(&viewRotYMat, rotY);
D3DXMatrixRotationX(&viewRotXMat, cos(rotY) * rotXZ);
D3DXMatrixRotationZ(&viewRotZMat, (sin(rotY) * rotXZ) * -1);

What exactly are you trying to achieve here? Usually you would just do a couple of axis rotations and then combine them, and your code looks like exactly that, but with messed order of matrix combination, which is then compensated with some explicit trigonometry. There's a good chance you could do the same with just

D3DXMatrixRotationY(&viewRotYMat, rotY);
D3DXMatrixRotationX(&viewRotXMat, rotXZ);

Otherwise you could use D3DXMatrixRotationYawPitchRoll to at least combine the 3 angles in one line.

As for the rotY value, remember that it is expected to be measured in radians, so 3-4 corresponds to 180°. Observing 45° instead could be explained by rotXZ contribution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM