设备如何知道设备方向发生了变化,然后开始响应呢?在一个使用 opengl 的简单应用程序中,当设备方向发生变化时,渲染引擎中的两个函数协同工作来响应此事件:UpdateAnimation、OnRotate 我的问题是,它们是如何触发的?
谢谢你的帮助,亚辛
/////////////////////////////////示例代码
class RenderingEngine1 : public IRenderingEngine {
public:
RenderingEngine1();
void Initialize(int width, int height);
void Render() const;
void UpdateAnimation(float timeStep);
void OnRotate(DeviceOrientation newOrientation);
private:
vector<Vertex> m_cone;
vector<Vertex> m_disk;
Animation m_animation;
GLuint m_framebuffer;
GLuint m_colorRenderbuffer;
GLuint m_depthRenderbuffer;
};
void RenderingEngine1::OnRotate(DeviceOrientation orientation)
{
vec3 direction;
switch (orientation) {
case DeviceOrientationUnknown:
case DeviceOrientationPortrait:
direction = vec3(0, 1, 0);
break;
case DeviceOrientationPortraitUpsideDown:
direction = vec3(0, -1, 0);
break;
case DeviceOrientationFaceDown:
direction = vec3(0, 0, -1);
break;
case DeviceOrientationFaceUp:
direction = vec3(0, 0, 1);
break;
case DeviceOrientationLandscapeLeft:
direction = vec3(+1, 0, 0);
break;
case DeviceOrientationLandscapeRight:
direction = vec3(-1, 0, 0);
break;
}
m_animation.Elapsed = 0;
m_animation.Start = m_animation.Current = m_animation.End;
m_animation.End = Quaternion::CreateFromVectors(vec3(0, 1, 0), direction);
}
void RenderingEngine1::UpdateAnimation(float timeStep)
{
if (m_animation.Current == m_animation.End)
return;
m_animation.Elapsed += timeStep;
if (m_animation.Elapsed >= AnimationDuration) {
m_animation.Current = m_animation.End;
} else {
float mu = m_animation.Elapsed / AnimationDuration;
m_animation.Current = m_animation.Start.Slerp(mu, m_animation.End);
}
}