2018年3月23日 星期五

Week 04 宋侑恩

Week 04 - part 1 使用Transformation來看移動、旋轉、縮放

從 http://jsyeh.org/3dcg10 下載 data.zip、windows.zip、gult32.dll 三個檔案

將 data.zip 和 windows.zip 解壓縮


將解完壓縮的data資料夾和glut32.dull複製並貼到剛剛解完壓縮的wondows資料夾裡


打開 Transformation.exe

在右上角的方框裡,按右鍵可以更換模型(紅色框限裡是可更換的模型)

下方紅色方框裡可以用滑鼠將數值調整去看移動位置、旋轉的角度和縮放大小
(1) glTranslatef(x,y,z) - 移動物件位置
(2) glRotate(旋轉角度,x,y,z) - 旋轉物件
(3) glScale(x,y,z) - 物件大小縮放

下方紅色方框裡按下右鍵,可以將數據變回系統預設


Week 04 - part 2 旋轉、移動的茶壺

複製上禮拜移動茶壺的程式碼,對程式碼加工,加上旋轉

#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0,angle=0;///angle:rotate旋轉要得angle
int oldX=0,oldY=0,nowRotate=0;///oldX和oldY幫忙記下滑鼠點下的位置
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);///茶壺要對著(0,0,1)軸做旋轉angle角度
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;///左鍵:旋轉
    if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;///右鍵:移動
    oldX=x; oldY=y;///紀錄滑鼠點下的的位置
}
void motion(int x,int y)
{
    if(nowRotate==1) angle += (x-oldX);
    ///滑鼠的座標就是旋轉的角度,再加上新的增加量,來改善先前不好的旋轉
    else
    {
        teapotX += (x-oldX)/150.0;
        ///依照motion時的x來改變teapot的座標,再加上新的增加量,來改善先前不好的旋轉
        teapotY += (oldY-y)/150.0;
        ///依照motion時的y來改變teapot的座標,再加上新的增加量,來改善先前不好的旋轉
    }
    oldX=x; oldY=y;///記錄下好的、新的座標
    glutPostRedisplay();
}
int main (int argc,char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week05 Mouse_Rotate");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

下圖為程式碼執行後的截圖,可看到茶壺做旋轉和移動了位置


沒有留言:

張貼留言