我的lookAt和透视调用VS gluPerspective和glLookat(立方体拉伸)有什么不同

时间:2014-08-11 00:08:33

标签: c++ opengl qt4

旁注:大家好,如果您发现我的问题/答案有帮助,请不要忘记投票。我有点需要......

所以我的矩阵[投影和模型]的实现似乎有些不同(除了我为调试目的而注释掉的东西)。下面是我在绘制立方体时看到的错误的屏幕截图。 请记住,我确保视口和矩阵与窗口大小保持同步,并使用浮点数而不是int计算屏幕比例,所以不要打扰询问,我检查了通常的嫌疑人..... < /强>

Screen Shot

Files (linux build, see readme in ./build)
旁注:在调试时,我已经改变了立方体的距离。要重现屏幕截图,请在workspace.cpp的第76行将mDistance设置为大约90,并将窗口框架拉伸到窗口右下角标注的尺寸。

请记住屏幕截图和调试文本输出是单独的事件,因为我一直在调试此问题并获取新数字。

代码:

#define _AP_MAA 0
#define _AP_MAB 1
#define _AP_MAC 2
#define _AP_MAD 3
#define _AP_MBA 4
#define _AP_MBB 5
#define _AP_MBC 6
#define _AP_MBD 7
#define _AP_MCA 8
#define _AP_MCB 9
#define _AP_MCC 10
#define _AP_MCD 11
#define _AP_MDA 12
#define _AP_MDB 13
#define _AP_MDC 14
#define _AP_MDD 15

设置摄像机视角:

void APCamera::setPerspective(GMFloat_t fov, GMFloat_t aspect, GMFloat_t near, GMFloat_t far)
{
   GMFloat_t difZ = near - far;

   GMFloat_t *data;

   mProjection->clear(); //set to identity matrix
   data = mProjection->getData();

   GMFloat_t v = 1.0f / tan(fov / 2.0f);

   data[_AP_MAA] = v / aspect;
   data[_AP_MBB] = v;
   data[_AP_MCC] = (far + near) / (difZ);
   data[_AP_MCD] = -1.0f;
   data[_AP_MDD] = 0.0f;
   data[_AP_MDC] = (2.0f * far * near)/ (difZ);

   mRatio = aspect;

   mInvProjOutdated = true;
   mIsPerspective = true;
}

设置摄像机方向:

bool APCamera::lookTo(Coordinate &to, Coordinate &from, Coordinate &up)  
 {
      Coordinate f, unitUp, right;  
      GMFloat_t *data;

      CoordinateOp::diff(&to, &from, &f);
      VectorOp::toUnit(&f, &f);
      VectorOp::toUnit(&up, &unitUp);
      VectorOp::cross(&f, &unitUp, &right);

      if((fabs(right.x) < FLOAT_THRESHOLD) && (fabs(right.y) < FLOAT_THRESHOLD) && (fabs(right.z) < FLOAT_THRESHOLD))
     {
         return false;
     }

     mCamPt = from;

     VectorOp::toUnit(&right, &mRight);
     mForward = f;
     VectorOp::cross(&mRight, &mForward, &mUp);

     mModelView->clear();
     data = mModelView->getData();

     data[_AP_MAA] = mRight.x;
     data[_AP_MBA] = mRight.y;
     data[_AP_MCA] = mRight.z;

     data[_AP_MAB] = mUp.x;
     data[_AP_MBB] = mUp.y;
     data[_AP_MCB] = mUp.z;

     data[_AP_MAC] = -mForward.x;
     data[_AP_MBC] = -mForward.y;
     data[_AP_MCC] = -mForward.z;

     //translation part is commented out to narrow bugs down, "camera" is kept at the center (0,0,0)
     //data[_AP_MDA] = (data[_AP_MAA] * -mCamPt.x) + (data[_AP_MBA] * -mCamPt.y) + (data[_AP_MCA] * -mCamPt.z);
     //data[_AP_MDB] = (data[_AP_MAB] * -mCamPt.x) + (data[_AP_MBB] * -mCamPt.y) + (data[_AP_MCB] * -mCamPt.z);
     //data[_AP_MDC] = (data[_AP_MAC] * -mCamPt.x) + (data[_AP_MBC] * -mCamPt.y) + (data[_AP_MCC] * -mCamPt.z);

  mInvViewOutdated = true;
  return true;
}  

调试输出:

LookTo()发件人:&lt; 0,0,0&gt;到:其中-1,0,0&GT;
0.000000 0.000000 -1.000000 0.000000
0.000000 1.000000 0.000000 0.000000
1.000000 -0.000000 -0.000000 0.000000
0.000000 0.000000 0.000000 1.000000

setPerspective()fov:0.785398比率:1.185185附近:0.500000远:100.000000:
2.036993 0.000000 0.000000 0.000000
0.000000 2.414213 0.000000 0.000000
0.000000 0.000000 -1.010050 -1.005025
0.000000 0.000000 -1.000000 0.000000

1 个答案:

答案 0 :(得分:0)

最后,看起来麻烦制造者只是FOV。 所以快速回答是不,我没有做任何与记录的观点不同的事情,看看功能。对于有类似问题的人2.0f * atan(tan(DEFAULT_FOV_RAD/mRatio) * mRatio)为我做了这份工作。