#include <gl/glut.h>GLfloat Point1[]={0,0.5,0},
Point2[]={0.5,0.5,0},
Point3[]={0.5,0,0},
Point4[]={0,0,0},
Point5[]={0.5,0,-0.5},
Point6[]={0,0,-0.5},
Point7[]={0,0.5,-0.5},
Point8[]={0.5,0.5,-0.5};//定义八个定点GLfloat Color15[]={1.0,0.0,0.0},
Color48[]={0.0,1.0,0.0},
Color37[]={0.0,0.0,1.0},
Color26[]={1.0,1.0,1.0};#define ColoredVertex(c, v) do{ glColor3fv(c); glVertex3fv(v); }while(0)static GLfloat angle=0.0;//控制旋转角度void display(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);//确定观测点
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,1,0);//前三个参数是观察点的位置,后三个是(x,y,z)和它的连线,确定‘上’方向,中间三个是目标的位置

glMatrixMode(GL_PROJECTION);//确定观察范围
glLoadIdentity();
gluPerspective(60,1,1,60);//第一个是角度,第二个是宽高比例,第三个是near,四个是far glRotatef(angle,0,1,0);//旋转 glBegin(GL_QUADS);
//绘制1234
ColoredVertex(Color15,Point1);
ColoredVertex(Color26,Point2);
ColoredVertex(Color37,Point3);
ColoredVertex(Color48,Point4); //绘制2358
ColoredVertex(Color26,Point2);
ColoredVertex(Color37,Point3);
ColoredVertex(Color15,Point5);
ColoredVertex(Color48,Point8); //绘制5678
ColoredVertex(Color15,Point5);
ColoredVertex(Color26,Point6);
ColoredVertex(Color37,Point7);
ColoredVertex(Color48,Point8); //绘制1467
ColoredVertex(Color15,Point1);
ColoredVertex(Color48,Point4);
ColoredVertex(Color26,Point6);
ColoredVertex(Color37,Point7); //绘制3456
ColoredVertex(Color37,Point3);
ColoredVertex(Color48,Point4);
ColoredVertex(Color15,Point5);
ColoredVertex(Color26,Point6); //绘制1287
ColoredVertex(Color15,Point1);
ColoredVertex(Color26,Point2);
ColoredVertex(Color48,Point8);
ColoredVertex(Color37,Point7); glEnd(); glutSwapBuffers();
}void myidle(void)
{
angle++;
if (angle>360.0)
{
angle=0;
} display();
}int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(0,0);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutIdleFunc(myidle);
glutMainLoop();
return 0;
}
这是我的代码,怎么控制这个旋转速度呢??谢谢各位

解决方案 »

  1.   

        angle++;
    改为 angle += 0.5; 速度会慢下来。楼主可以用其它值试试看效果。
    实际中一般使用时间来控制的。
      

  2.   

    可以sleep() 也可以angle += num;把num修改小一点。
      

  3.   

    下面是我现在用的控制方法,不过实际是延迟帧的显示时间实现控制速度,在速度快时不是很流畅,代码是d3d的FLOAT fAngle;
    D3DXMATRIXA16 matRotVec;UINT iTime = timeGetTime() % (m_nRotSpeed*1000);//在m_nRotSpeed秒内旋转2*Pi弧度-即360度
    fAngle =2 * iTime * ( D3DX_PI ) / (m_nRotSpeed*1000.0f);
    D3DXMatrixRotationAxis(&matRotVec,&m_RotationVector,fAngle);
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matRotVec);
      

  4.   

    可以用Sleep()函数 也可以用opengl的glutTimerFunc()这个回调函数试试,可以设置帧频void myidle(void)
    {
        angle++;
        if (angle>360.0)
        {
            angle=0;
        }    display();
    }
    这个函数是在空闲时候执行的回调函数,所以会一直执行...每个时钟周期都angle++当然会转的很快了 ^_^