2020年3月31日 星期二

wk4



今天我們學 glTranslate, glRotatef, glScalef,
glRotatef(0.0 , 0.00 , 1.00 , 0.00)
glRotatef(旋轉角度 , X軸 , Y軸 , Z軸)
以X軸(下圖黃色箭頭)為旋轉軸,旋轉方向為下圖綠色箭頭所轉方向
以Y軸(下圖藍色箭頭)為旋轉軸,旋轉方向為下圖橘色箭頭所轉方向
以Z軸(下圖紅色箭頭)為旋轉軸,旋轉方向為下圖淺藍色箭頭所轉方向




利用滑鼠來控制旋轉方向



float angle=0;
///變數的宣告

const double a = angle;
///把角度帶進去

void motion (int x, int y){
    angle=x;
///角度的值是X的值
}

glutMotionFunc(motion);
///mouse motion函式,可以捕捉滑鼠動態







#include <GL/glut.h>
float angle=0;///宣告角度變數
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );///清畫面
    glPushMatrix();///備份轉動前的矩陣
        glRotatef(angle,0,0,1); ///對Z軸做旋轉
        glutSolidTeapot( 0.3 );
    glPopMatrix();///回復矩陣
    glutSwapBuffers();
}
void motion(int x, int y){
    angle = x;///角度
    display();///每次做動作時,就重新畫畫面
}
int main(int a, char**b){
    glutInit(&a,b);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week 4");

    glutMotionFunc(motion);///加motion函式
    glutDisplayFunc(display);
    glutMainLoop();
}


利用滑鼠來轉動,並且以MAYA的方式轉動

#include <GL/glut.h>
float angle=0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
int oldx=0;///宣告變數
void mouse(int button,int state ,int x, int y){
///按下時,記住滑鼠在哪
    if(state==GLUT_DOWN) oldx = x;
}
void motion(int x, int y){
    angle += (x-oldx);///把增加的角度加進angle
    oldx = x;
    display();
}
int main(int a, char**b){
    glutInit(&a,b);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week 4");

    glutMotionFunc(motion);
    glutMouseFunc(mouse);///加滑鼠mouse函式
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言