我是一个使用带有GLM库的现代OpenGL的初学者,我尝试用相机的运动(WSAD代表前进后退左右)和Q&E(顺时针和逆时针)运动来创建键盘运动。
当我尝试在Y轴上旋转一整圈时,似乎旋转了一半,然后跳回到起点(想想是180度圆弧)。旋转会带来问题,但前进方向会带来问题,它会发生变化,而不是向前直线前进,而是在改变旋转角度时向前或向左或向右偏移。
glm::vec3 eyepos = glm::vec3(-0.6f, -0.4f, 31.1f);
glm::vec3 frontvector = glm::vec3(0.0f, 0.0f, -1.0f);//looking pointing vector
glm::vec3 lookvector = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 flatupvec = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 upvec = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 viewmatrix;
glm::mat4 projectionmatrix;//camera lens, projection matrix, need an aspect ratio function here /3D FOV
GLfloat movespeed = 0.1f;
GLfloat turnspeed = 1.0f;
//********************MOVEMENT_TRANSFORMS***********************************
if (kb.w == true) { eyepos += frontvector * movespeed; } //move up
if (kb.s == true) { eyepos -= frontvector * movespeed; } //move down
if (kb.q == true) { glm::quat q = glm::angleAxis(glm::radians(turnspeed),
glm::vec3(0, 1, 0)); frontvector = (frontvector * q); }//turn left (yaw)
if (kb.e == true) { glm::quat q = glm::angleAxis(glm::radians(-turnspeed),
glm::vec3(0, 1, 0)); frontvector = (frontvector * q); }//turn right (yaw)
if (kb.a == true) { eyepos -= glm::normalize(glm::cross(frontvector, flatupvec)) * movespeed; } //strafe left
if (kb.d == true) { eyepos += glm::normalize(glm::cross(frontvector, flatupvec)) * movespeed; } //strafe right
if (kb.r == true) { eyepos += flatupvec * movespeed; } //move up
if (kb.f == true) { eyepos -= flatupvec * movespeed; } //move down
//********************ROTATIONS_EYEPOINT************************************
if (kb.t == true) { glm::quat q = glm::angleAxis(turnspeed, glm::vec3(1, 0, 0)); lookvector = q * lookvector; } //pitch up
if (kb.g == true) { glm::quat q = glm::angleAxis(-turnspeed, glm::vec3(1, 0, 0)); lookvector = q * lookvector; }//pitch down
if (kb.z == true) { glm::quat q = glm::angleAxis(-turnspeed, glm::vec3(0, 0, 1)); upvec = q * upvec; } //roll left
if (kb.x == true) { glm::quat q = glm::angleAxis(turnspeed, glm::vec3(0, 0, 1)); upvec = q * upvec; } //roll right
if (kb.c == true) { glm::quat q = glm::angleAxis(turnspeed, glm::vec3(0, 1, 0)); lookvector = q * lookvector; } //yaw left
if (kb.v == true) { glm::quat q = glm::angleAxis(-turnspeed, glm::vec3(0, 1, 0)); lookvector = q * lookvector; }//yaw right
viewmatrix = glm::lookAt( eyepos, eyepos + frontvector + lookvector, upvec);
projectionmatrix = glm::perspective(45.0f, 1.8f, 0.0001f, 500.0f);
答案 0 :(得分:1)
问题是您的外观向量。 您的函数通过说您在说(10,0,10)而工作,并且您正在看等于的((10,0,10)+(0,0,-1)+(0,0,-1)) (10,0,8)或直接向前。假设您看起来正确((10,0,10)+(0.9,0,-0.1)+(0,0,-1))等于(10.9,0,8.9),而且一切大致还不错。但是假设您将其几乎变成等于(10.1,0,9.9)的最大值((10,0,10)+(0.1,0,0.9)+(0,0,-1)),则比较为(10.1,0 ,9.9)和(10,0,8)到(10,0,10)仍然是相同的矢量(这可以解释180度圆弧)和(10,0,10)视点以及(10,0,10) lookat会模棱两可,因此您的问题是“ viewmatrix = glm :: lookAt(eyepos,eyepos + frontvector + lookvector,upvec);”调用,并同时使用该程序同时包含frontvector和lookvector,您可能希望删除该调用的frontvector部分,但是要保留一些代码;)