天天看点

OpenGL学习:第五课

OpenGL学习:第五课

OpenGL实现旋转,调用glRotated函数或者glRotatef函数实现,该函数的声明如下:

WINGDIAPI void APIENTRY glRotated (GLdouble angle, GLdouble x, GLdouble y, GLdouble z);

WINGDIAPI void APIENTRY glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z);

第一个参数angle是旋转的角度,另外三个参数构成三维空间的一个点,实际上调用glRotated函数或者glRotatef函数,首先需要计算一个向量:从原点(0.0f, 0.0f, 0.0f)到空间点(x, y, z)的向量,然后指定的图形以向量(x-0.0, y-0.0, z-0.0)为轴旋转angle角度。

下面是个例子:

int DrawGLScene(GLvoid)          // Here's Where We Do All The Drawing

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

glLoadIdentity();          // Reset The Current Modelview Matrix

glTranslatef(0.0f,0.0f,-7.0f);       // Move Right 1.5 Units And Into The Screen 7.0

glRotatef(60,1.0f,1.0f,1.0f);      // Rotate The Quad On The X axis ( NEW )

glBegin(GL_QUADS);          // Draw A Quad

   glColor3f(0.0f,1.0f,0.0f);       // Set The Color To Green

   glVertex3f( 1.0f, 1.0f,-1.0f);      // Top Right Of The Quad (Top)

   glVertex3f(-1.0f, 1.0f,-1.0f);      // Top Left Of The Quad (Top)

   glVertex3f(-1.0f, 1.0f, 1.0f);      // Bottom Left Of The Quad (Top)

   glVertex3f( 1.0f, 1.0f, 1.0f);      // Bottom Right Of The Quad (Top)

   glColor3f(1.0f,0.5f,0.0f);       // Set The Color To Orange

   glVertex3f( 1.0f,-1.0f, 1.0f);      // Top Right Of The Quad (Bottom)

   glVertex3f(-1.0f,-1.0f, 1.0f);      // Top Left Of The Quad (Bottom)

   glVertex3f(-1.0f,-1.0f,-1.0f);      // Bottom Left Of The Quad (Bottom)

   glVertex3f( 1.0f,-1.0f,-1.0f);      // Bottom Right Of The Quad (Bottom)

   glColor3f(1.0f,0.0f,0.0f);       // Set The Color To Red

   glVertex3f( 1.0f, 1.0f, 1.0f);      // Top Right Of The Quad (Front)

   glVertex3f(-1.0f, 1.0f, 1.0f);      // Top Left Of The Quad (Front)

   glVertex3f(-1.0f,-1.0f, 1.0f);      // Bottom Left Of The Quad (Front)

   glVertex3f( 1.0f,-1.0f, 1.0f);      // Bottom Right Of The Quad (Front)

   glColor3f(1.0f,1.0f,0.0f);       // Set The Color To Yellow

   glVertex3f( 1.0f,-1.0f,-1.0f);      // Top Right Of The Quad (Back)

   glVertex3f(-1.0f,-1.0f,-1.0f);      // Top Left Of The Quad (Back)

   glVertex3f(-1.0f, 1.0f,-1.0f);      // Bottom Left Of The Quad (Back)

   glVertex3f( 1.0f, 1.0f,-1.0f);      // Bottom Right Of The Quad (Back)

   glColor3f(0.0f,0.0f,1.0f);       // Set The Color To Blue

   glVertex3f(-1.0f, 1.0f, 1.0f);      // Top Right Of The Quad (Left)

   glVertex3f(-1.0f, 1.0f,-1.0f);      // Top Left Of The Quad (Left)

   glVertex3f(-1.0f,-1.0f,-1.0f);      // Bottom Left Of The Quad (Left)

   glVertex3f(-1.0f,-1.0f, 1.0f);      // Bottom Right Of The Quad (Left)

   glColor3f(1.0f,0.0f,1.0f);       // Set The Color To Violet

   glVertex3f( 1.0f, 1.0f,-1.0f);      // Top Right Of The Quad (Right)

   glVertex3f( 1.0f, 1.0f, 1.0f);      // Top Left Of The Quad (Right)

   glVertex3f( 1.0f,-1.0f, 1.0f);      // Bottom Left Of The Quad (Right)

   glVertex3f( 1.0f,-1.0f,-1.0f);      // Bottom Right Of The Quad (Right)

glEnd();            // Done Drawing The Quad

return TRUE;           // Keep Going

}

如果不旋转,只能看到立方体的一个面的,旋转之前如图所示:

OpenGL学习:第五课

上面代码绘制了一个立方体,并沿着(0.0f, 0.0f, 0.0f)到(1.0f, 1.0f 1.0f)的轴线旋转了60度。

旋转后运行如图所示:

OpenGL学习:第五课

继续阅读