/* * Quaternion slerp animation * dizuo */ #include <gtl\quat.hpp> #include <windows.h> #include <GL\gl.h> #include <GL\glu.h> #include <GL\glut.h> #include <cstdio> #include <cstdlib> #include <cmath> float rotVerticesBuf[] = {0,0,0, 1,0,0}; gtl::Quatf begQuat( gtl::Vec3f(1,0,0), gtl::Vec3f(1,1,0) ); // 开始状态 gtl::Quatf endQuat( gtl::Vec3f(1,0,0), gtl::Vec3f(-1,1,0) ); // 结束状态 static float yAngle = 0.0f; int frameIdx = 0; int frameNum = 100; float g_fWidth = 512; // 窗体的宽度 float g_fHeight = 392; // 窗体的高度 void init(void); // 初始化opengl的环境 void reshape(int w,int h); // 该函数当窗体改变以后响应,重新设置视口 void display(void); // 渲染opengl内容 void keyboard(unsigned char, int, int); // 响应鼠标事件 int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (g_fWidth, g_fHeight); glutInitWindowPosition (0, 0); glutCreateWindow("ModelRotate"); init (); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc( keyboard ); glutMainLoop(); return 0; } void init (void) { glClearColor (0.0, 0.0, 0.0, 0.0); glClearDepth(1); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } /* Rotate about x-axis when "x" typed; rotate about y-axis when "y" typed; "i" returns torus to original view */ void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; case 'y': yAngle += 10.0f; break; } glutPostRedisplay(); } void reshape(int w, int h) { g_fWidth = w; g_fHeight = h; glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); //回复原有的设置 glLoadIdentity (); gluPerspective(30, g_fWidth/g_fHeight, 0.001, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); } void display(void) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glColor3f (1.0, 1.0, 1.0); // 清屏幕颜色 glPushMatrix(); float lineVerticesBuf[] = {0,0,0, 1,1,0, 0,0,0, -1,1,0}; glEnableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, lineVerticesBuf); glDrawArrays(GL_LINES, 0, 4); frameIdx = (frameIdx+1) % frameNum; glBegin(GL_LINES); for (int k=0; k<2; k++) { gtl::Quatf rotQuat = gtl::Quatf::slerp(begQuat, endQuat, frameIdx*1.0/frameNum); gtl::Vec3f src(rotVerticesBuf + 3*k); gtl::Vec3f dst; rotQuat.getMatrix().multVecMatrix(src, dst); glVertex3fv(dst.getValue()); } glEnd(); glPopMatrix(); // glutSolidCube(1); glutSwapBuffers(); // 防止刷屏,采用双缓冲机制 // glutPostRedisplay(); // 重复调用自身 }
四元数插值:从向量(1,1,0) 旋转到(-1,1,0),
gtl库见blog:http://blog.csdn.net/dizuo/article/details/2491400