简体   繁体   English

Direct3D9无法使垂直Fov相机正常工作

[英]Direct3D9 Can't get vertical fov camera working

I'm trying getting started with Direct 3D. 我正在尝试使用Direct 3D。 But i can't get the fov camera working. 但是我无法使fov相机正常工作。 At least not the vertical movement. 至少不是垂直运动。 I figured out why but i still can't fix it. 我想出了原因,但仍然无法解决。

I'm using C++ (140) and D3DX9 我正在使用C ++(140)和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. 这是我的RenderFrame函数的重要部分。 The reason why it won't work is because of rotY and rotXZ. 之所以不起作用,是因为rotY和rotXZ。 For some reason when i turn the camera about 45° ingame the rotY variable only changes to 3-4. 由于某些原因,当我在游戏中将摄像机旋转约45°时,rotY变量仅更改为3-4。 So why the heck is't the D3DXMatrixRotationY function taking an angle as an parameter but some float? 那么,为什么D3DXMatrixRotationY函数不是以角度作为参数,而是有些浮动呢? The parameter is even named angle... I'm on this problem for about 3 days now. 该参数甚至被命名为angle ...我在这个问题上呆了大约3天。 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. 否则,您可以使用D3DXMatrixRotationYawPitchRoll至少将3个角度组合在一行中。

As for the rotY value, remember that it is expected to be measured in radians, so 3-4 corresponds to 180°. 至于rotY值,请记住它应该以弧度为单位进行测量,因此3-4对应于180°。 Observing 45° instead could be explained by rotXZ contribution. 观察45°可以用rotXZ贡献来解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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