2020年3月31日 星期二

Week04



轉180度後的樣子
定住X軸,順著四隻手指向右轉
轉180度後的樣子
定住Y軸,順著四隻手指向右轉

定住z軸,順著四隻手指向右轉
轉180度後的樣子




float myAngle=0;///宣告一個變數

glutMotionFunc(motion);///呼叫motion函式
void motion (int x,int y)
{
 
const double a = myAngle;///把角度值塞進去
 myAngle=x;///角度=x值
}


#include <GL/glut.h>
float myAngle=0;///角度
int oldX=0;///舊座標
void display(){
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );///清畫面
    glPushMatrix();///先備份一個沒有動過的
        glRotatef(myAngle,0,0,1);///定Z軸
        glutSolidTeapot(0.3);///茶壺
    glPopMatrix();///還原成沒有動過的
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN) oldX=x;///紀錄滑鼠按下的x
}
void motion(int x,int y){
    myAngle+=(x-oldX);///將原座標+(新x-舊x)
    oldX=x;///更新舊座標
    display();///每次做動作,重畫畫面
}
int main(int a,char**b){
    glutInit(&a,b);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week04");

    glutMouseFunc(mouse);///呼叫mouse函式
    glutMotionFunc(motion);///呼叫motion函式
    glutDisplayFunc(display);///呼叫display函式
    glutMainLoop();

}

沒有留言:

張貼留言