OpenGL ES3,C 11,Android,我有一个对象和一个实现第三人称相机的对象,因此我想在触摸屏幕时使对象看起来像相机,所以我有一个对象的转换矩阵和相机的视图矩阵,摄像机围绕对象旋转,我想在触摸屏幕时旋转对象朝相机旋转,因此我尝试了以下内容:
glm::vec3 up(0, 1, 0);
glm::vec3 camFor(glm::cross(camera.getRight(), up));
GLfloat theta = glm::angle(glm::vec2(camFor.x, camFor.z), glm::vec2(getDirection().x, getDirection().z));
LOGI("game theta %f", theta);
matrix = glm::rotate(matrix, theta, glm::vec3(0.0, 1.0, 0.0));
我在对象向量向量和摄像机向前的水平角度(glm::vec3(matrix[2][0], matrix[2][1], matrix[2][2])
)之间的水平角度,第一次摄像机恰好在对象后面,我得到的theta等于Pi,这是正确的,然后我触摸屏幕以发射屏幕事件该物体旋转并按照预期的方式旋转到相机,而Theta将变为零,如果我将相机旋转在物体周围并再次发射事件,则为Theta提供了意外的值。相机的旋转代码为:
void Camera::rotate(GLfloat xoffset, GLfloat yoffset, glm::vec3& c, double& delta, GLboolean constrainpitch) {
xoffset *= (delta * this->rotSpeed);
yoffset *= (delta * this->rotSpeed);
pitch += yoffset;
yaw += xoffset;
if (constrainpitch) {
if (pitch >= maxPitch) {
pitch = maxPitch;
yoffset = 0;
}
if (pitch <= minPitch) {
pitch = minPitch;
yoffset = 0;
}
}
glm::quat Qx(glm::angleAxis(glm::radians(yoffset), glm::vec3(1.0f, 0.0f, 0.0f)));
glm::quat Qy(glm::angleAxis(glm::radians(xoffset), glm::vec3(0.0f, 1.0f, 0.0f)));
glm::mat4 rotX = glm::mat4_cast(Qx);
glm::mat4 rotY = glm::mat4_cast(Qy);
view = glm::translate(view, c);
view = rotX * view;
view = view * rotY;
view = glm::translate(view, -c);
}
解决方案:
错误是获取相机矩阵的正确向量这是正确的
glm::vec3& Camera::getRight() {
right = glm::vec3(view[0][0], view[1][0], view[2][0]);
return right;
}
错误是获取相机矩阵的正确向量这是正确的
glm::vec3& Camera::getRight() {
right = glm::vec3(view[0][0], view[1][0], view[2][0]);
return right;
}