2022年3月15日 星期二

尖🦉-圖學筆記 Week04

範例理解glRotatef運作方式:
    1.上jsyeh.org/3dcg10/ 下載data和win32 解壓縮,點開Tranformation.exe。
    2.可以調整glRotatef的值
        (可按照x,y,z旋轉,利用安培右手定則,拇指指向x或y或z,則四指為旋轉方向)。
        glRotatef( 旋轉角度 , X軸 ,Y軸 ,Z軸) 
    Ex:
        如果旋轉X軸glRotatef(0 , 1.00 ,0 ,0),人物會向前旋轉 (像敬禮的動作)。
        如果旋轉Z軸glRotatef(0 , 0 ,0 ,1.00),人物會整個往左轉,像側翻 
        (此時的拇指是指向人物面向的方向)。
        如果是glRotatef(0 , 1.00 ,1.00 ,0),拇指方向會朝右上。
        負的會反過去。

圖片是指Y軸方向旋轉


利用glRotatef旋轉:

1.程式碼(朝Z軸旋轉):
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

     glPushMatrix();///備份舊的矩陣
          glRotatef( 90 ,0 ,0,1);///旋轉Z軸90度
          glColor3f(1,1,0);
          glutSolidTeapot(0.3);
     glPopMatrix();///還原矩陣

    glutSwapBuffers();
}
int main(int argc,char**argv)
{
     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
     glutCreateWindow("第三周 Rotate");

     glutDisplayFunc(display);

     glutMainLoop();
}

茶壺按照Z軸轉向

利用滑鼠mouse motion轉動(滑鼠轉動):
★ x是滑鼠左右移動,y是滑鼠上下移動 
程式碼:
#include <GL/glut.h>
float angle;///角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

     glPushMatrix();///備份舊的矩陣
          glRotatef( angle ,0 ,0,1);///旋轉Z軸
          glColor3f(1,1,0);
          glutSolidTeapot(0.3);
     glPopMatrix();///還原矩陣

    glutSwapBuffers();
}
void motion(int x,int y)
{
    angle = x;
    display();///每滑動滑鼠,就重畫茶壺
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("第三周 Rotate");

    glutMotionFunc(motion); 
    glutDisplayFunc(display);

    glutMainLoop();
}

★ 因為轉動會每次重新點位置轉動,以下是解決方式★ 
利用mouse 點擊紀錄位置修正:
★ 利用原先的位置和後來移動的位置做修正 
#include <GL/glut.h>
float angle,oldx=0;///角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

     glPushMatrix();///備份舊的矩陣
          glRotatef( angle ,0 ,0,1);///旋轉Z軸
          glColor3f(1,1,0);
          glutSolidTeapot(0.3);
     glPopMatrix();///還原矩陣

    glutSwapBuffers();
}
void motion(int x,int y)
{
    angle += (x-oldx); ///angle = 將移動到最後的位置 - 滑鼠按下去的第一個位置 = 移動量。
    oldx=x;///將最後的位置紀錄,就不會有問題。
    display();///每滑動滑鼠,就重畫茶壺
}
void mouse(int buttom, int state,int x,int y)
{
    oldx = x; ///紀錄滑鼠按下去的位置(定錨)
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("第三周 Rotate");

    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}






沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...