2019年3月15日 星期五

DannyLo筆記 Week04

 :Rotation(旋轉)  

認識Rotation

1.前置動作
http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載 data win32 glut32.dll
windows.zip解壓縮 把data.zip中的data放入 glut32.dll也放入
windows資料夾中
(windows存放範例檔 data存放模型檔)

2.開啟小葉老師的範例檔
       註:數值部分可以按住滑鼠左鍵上下拖曳更改 右鍵可Reset數值

3.glRotatef(角度,X,Y,Z)
    第一個為旋轉程度 後面X,Y,Z代表一個點 以穿過此點的軸當旋轉軸 用右手定則
大拇指指向點的方向(點的正負會影響方向),以其他的四根指頭旋轉就為旋轉方式

開始寫程式瞜


1.複習week03移動程式
#include<GL/glut.h>
float x=0,y=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清除背景,刪除重複圖片
    glPushMatrix();///備份矩陣
        glTranslatef((x-150)/150.0,-(y-150)/150.0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣

    glutSwapBuffers();
}

void motion(int nowx,int nowy)
{
    x=nowx;y=nowy;
    display();
}

int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}

2.旋轉
#include<GL/glut.h>
float x=0,y=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清除背景,刪除重複圖片
    glPushMatrix();///備份矩陣
        ///glTranslatef((x-150)/150.0,-(y-150)/150.0,0);加移動
        glRotatef(x,0,1,0); ///根據滑鼠移動的x 以y軸為旋轉軸旋轉
        glRotatef(y,1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣

    glutSwapBuffers();
}

void motion(int nowx,int nowy)
{
    x=nowx;y=nowy;
    display();
}

int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}

沒有留言:

張貼留言