使用TIMER固定旋轉速度
利用滑鼠拖曳發射茶壺
#include <GL/glut.h> ///3D glut#include <math.h>
float angle=0; ///自動轉很帥
float potX=0,potY=0,potVX=0,potVY=0;
int potState=0;
void timer(int t)
{ glutTimerFunc(10, timer, t+1);
angle+=1;
if(potState==0)potY=0.5*sin(angle/180.0*3.14);
if(potState==2){potX+=potVX; potY+=potVY;}
//printf("timer now\n");
glutPostRedisplay();
}
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();///自動轉很帥
glTranslatef(potX, potY,0);
glRotatef(angle, 0,1,0);///自動轉很帥
glutSolidTeapot(0.05);
glPopMatrix();///自動轉很帥
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){potState=1;}
else if(state==GLUT_UP)
{
potState=2;
potVX=-(x-350)/35000.0;
potVY=(y-350)/35000.0;
}
}
void motion(int x, int y)
{
if(potState==1)
{
potX=(x-350)/350.0;
potY=-(y-350)/350.0;
}
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(10, timer, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言