好的,所以我試圖用C++和OpenGL來點擊並拖動以圍繞對象旋轉。我擁有它的方式是使用以原點爲中心的gluLookAt,並通過使用球體的參數方程來獲得眼睛的座標(eyex = 2 * cos(theta)* sin(phi); eyey = 2 * sin( theta)* sin(phi); eyez = 2 * cos(phi);)。這主要起作用,因爲我可以水平點擊和旋轉,但是當我嘗試垂直旋轉時,它會形成緊密的圓圈而不是垂直旋轉。我試圖通過使用相機的位置和沿着x-z平面成90度角的矢量來獲取向上矢量,並取其中的叉積。 我的代碼如下:使用OpenGL和gluLookAt圍繞球體旋轉
double dotProduct(double v1[], double v2[]) {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
void mouseDown(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
xpos = x;
ypos = y;
}
}
void mouseMovement(int x, int y) {
diffx = x - xpos;
diffy = y - ypos;
xpos = x;
ypos = y;
}
void camera (void) {
theta += 2*PI * (-diffy/glutGet(GLUT_SCREEN_HEIGHT));
phi += PI * (-diffx/glutGet(GLUT_WINDOW_WIDTH));
eyex = 2* cos(theta) * sin(phi);
eyey = 2* sin(theta) * sin(phi);
eyez = 2* cos(phi);
double rightv[3], rightt[3], eyes[3];
rightv[0] = 2* cos(theta + 2/PI) * sin(phi);
rightv[1] = 0;
rightv[2] = 2* cos(phi);
rightt[0] = rightv[0];
rightt[1] = rightv[1];
rightt[2] = rightv[2];
rightv[0] = rightv[0]/sqrt(dotProduct(rightt, rightt));
rightv[1] = rightv[1]/sqrt(dotProduct(rightt, rightt));
rightv[2] = rightv[2]/sqrt(dotProduct(rightt, rightt));
eyes[0] = eyex;
eyes[1] = eyey;
eyes[2] = eyez;
upx = (eyey/sqrt(dotProduct(eyes,eyes)))*rightv[2] + (eyez/sqrt(dotProduct(eyes,eyes)))*rightv[1];
upy = (eyez/sqrt(dotProduct(eyes,eyes)))*rightv[0] + (eyex/sqrt(dotProduct(eyes,eyes)))*rightv[2];
upz = (eyex/sqrt(dotProduct(eyes,eyes)))*rightv[1] + (eyey/sqrt(dotProduct(eyes,eyes)))*rightv[0];
diffx = 0;
diffy = 0;
}
我有點基礎的東西掉this,但它不工作,所以我想我的方式來代替。