來齁來齁看這邊
先做出一個打光的茶壺
給你作法傳送門
你就會得到這個
搭搭 打光的茶壺✨
接下來要做的是移動轉動的茶壺
給你程式碼
int type=1;
float teapotX=0, teapotY=0; ///todo1
float teapotRotY=0;///轉動的變數
int oldX=0, oldY=0;
void mouse (int button, int state, int x, int y )
{
oldX=x;
oldY=y;
}
void motion(int x, int y)
{
if(type==1)///轉動
{
teapotRotY += x-oldX;
}
else if(type==2)///移動
{
teapotX += x-oldX;
teapotY += y-oldY;
}
oldX=x;
oldY=y;
glutPostRedisplay();///更新畫面
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///todo2 備份矩陣
glTranslatef( teapotX/250.0, -teapotY/250.0, 0);
glRotatef(teapotRotY, 0, -1, 0);///x方向的動 vs. Y軸
glutSolidTeapot( 0.3 );
glPopMatrix();///todo2 備份矩陣
glutSwapBuffers();
teapotRotY++;///3:每次多轉動1度
}
void keyboard(unsigned char key, int x, int y)
{
if( key=='e' || key=='E' ) type=1;
if( key=='w' || key=='W' ) type=2;
printf("now: %c\n", key);
}
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMouseFunc(mouse);///0
glutMotionFunc(motion);///1
glutKeyboardFunc(keyboard);///2
glutIdleFunc(display);///3 Idle閒閒的時候 就重畫




沒有留言:
張貼留言