Android openGL ES2旋转翻转角度
我有以下摄像头绘图功能,可以旋转、缩放和平移视图矩阵,但在某些位置我有一个问题,角度会发生变化,例如Android openGL ES2旋转翻转角度,android,opengl-es-2.0,Android,Opengl Es 2.0,我有以下摄像头绘图功能,可以旋转、缩放和平移视图矩阵,但在某些位置我有一个问题,角度会发生变化,例如if(event.getAction()==MotionEvent.ACTION\u DOWN)if(event.getAction()==MotionEvent.ACTION\u UP)上的偏航为120,俯仰为-40偏航变为165,俯仰变为-85,无需旋转 Vector3 nv = Vector3.multiplyScalar(Vector3.normalize(Vector3.cros
if(event.getAction()==MotionEvent.ACTION\u DOWN)
if(event.getAction()==MotionEvent.ACTION\u UP)上的偏航为120,俯仰为-40偏航变为165,俯仰变为-85,无需旋转
Vector3 nv = Vector3.multiplyScalar(Vector3.normalize(Vector3.cross(cameraUp, cameraRight)), zoom);
cameraPosition = Vector3.add(cameraPosition, nv);
Vector3 vY = Vector3.multiplyScalar(cameraUp, -mDeltaY);
cameraPosition = Vector3.add(cameraPosition, vY);
Vector3 vZ = Vector3.multiplyScalar(Vector3.normalize(Vector3.cross(cameraDirection, cameraRight)), mDeltaY);
cameraPosition = Vector3.sub(cameraPosition, vZ);
Vector3 vX = Vector3.multiplyScalar(Vector3.normalize(Vector3.cross(cameraDirection, cameraUp)), -mDeltaX);
cameraPosition = Vector3.sub(cameraPosition, vX);
setLookAtM(viewMatrix, 0, cameraPosition.getX(), cameraPosition.getY(), cameraPosition.getZ(), cameraPosition.getX() + cameraDirection.getX(), cameraPosition.getY() + cameraDirection.getY(),
cameraPosition.getZ() + cameraDirection.getZ(), 0, 1, 0);
rotateM(viewMatrix, 0, pitch, 1, 0, 0);
rotateM(viewMatrix, 0, yaw, 0, 1, 0);
mDeltaX = 0;
mDeltaY = 0;
zoom = 0;
摄像机设置代码为:
public void initCamera(Vector3 position, Vector3 target) {
yaw = 0f;
pitch = 0;
Vector3 upV = new Vector3(0, 0, 1);
this.cameraPosition = position;
this.cameraTarget = target;
this.cameraDirection = Vector3.normalize(Vector3.sub(target, position));
this.cameraRight = Vector3.normalize(Vector3.cross(upV, cameraDirection));
this.cameraUp = Vector3.cross(cameraDirection, cameraRight);
}
这是onTouchEvent
代码
if (event.getPointerCount() == 1) {
float yaw = (x - mPreviousX) / mDensity / 2f;
float pitch = (y - mPreviousY) / mDensity / 2f;
mainRenderer.getCamera().yaw += yaw;
mainRenderer.getCamera().pitch += pitch;
}
if (event.getPointerCount() == 2) {
mCurrDis = getDistance(event);
if (mLastDis == 0)
mLastDis = mCurrDis;
mainRenderer.getCamera().zoom(mLastDis - mCurrDis);
mLastDis = getDistance(event);
}
if (event.getPointerCount() == 3) {
float deltaX = (x - mPreviousX) / mDensity / 2f;
float deltaY = (y - mPreviousY) / mDensity / 2f;
mainRenderer.getCamera().mDeltaX += deltaX;
mainRenderer.getCamera().mDeltaY += deltaY;
}
请尝试以下代码:
void Camera::rotate(GLfloat xoffset, GLfloat yoffset, glm::vec3& c, GLboolean constrainpitch) {
xoffset *= this->touchSensitivity;
yoffset *= this->touchSensitivity;
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);
}
void Camera::rotate(GLfloat xoffset、GLfloat yoffset、glm::vec3&c、GLfloat约束间距){
xoffset*=此->触摸灵敏度;
yoffset*=此->触摸灵敏度;
音高+=音高偏移;
偏航+=xoffset;
如果(俯仰){
如果(节距>=最大节距){
螺距=最大螺距;
yoffset=0;
}
如果(俯仰尝试此代码:
void Camera::rotate(GLfloat xoffset, GLfloat yoffset, glm::vec3& c, GLboolean constrainpitch) {
xoffset *= this->touchSensitivity;
yoffset *= this->touchSensitivity;
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);
}
void Camera::rotate(GLfloat xoffset、GLfloat yoffset、glm::vec3&c、GLfloat约束间距){
xoffset*=此->触摸灵敏度;
yoffset*=此->触摸灵敏度;
音高+=音高偏移;
偏航+=xoffset;
如果(俯仰){
如果(节距>=最大节距){
螺距=最大螺距;
yoffset=0;
}
如果(音高)