2017年3月21日 星期二

week05-卓佾柔的上課筆記

一、安裝Transformation.exe

1.先到 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/  下載檔案"data.zip"、"windows.zip"、
"glut32.dll"
 
2.將壓縮檔解壓縮後,將data資料夾與glut32.dll放入windows資料夾裡
3.點開Transformation.exe
 
 

二、用座標讓茶壺移動

建立一個GLUT專案
程式碼如下

三、用滑鼠讓茶壺移動

程式碼:
 
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();//備份起來
        glTranslatef(mouseX,mouseY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();//還原回去
    glutSwapBuffers();
}
void motion(int x,int y)
{
    mouseX=(x-150)/150.0;
    mouseY=-(y-150)/150.0;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160241 translate rotate scale");
    glutDisplayFunc(display);
    //glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

四、讓茶壺可以縮放

把glTranslatef(mouseX,mouseY,0);改成
glScalef(mouseX,mouseY,0);
讓茶壺能縮放
程式碼:
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清掉舊的畫面
    glPushMatrix();//備份起來
        //glTranslatef(mouseX,mouseY,0);//移動
        glScalef(mouseX,mouseY,0);//改size縮放
        glutSolidTeapot(0.3);
    glPopMatrix();//還原回去
    glutSwapBuffers();//交換繪圖的memory buffer才能畫出來
}
void motion(int x,int y)
{
    mouseX=(x-150)/150.0;
    mouseY=-(y-150)/150.0;
    glutPostRedisplay();//貼一張便利貼,告訴電腦的GLUT要重畫畫面re display
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160241 translate rotate scale");
    glutDisplayFunc(display);
    //glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

五、讓茶壺能旋轉

多宣告一個變數rotX
加一行glRotatef(rotX,0,0,1);
就能旋轉了
程式碼:
 
#include <GL/glut.h>
float mouseX=0,mouseY=0,rotX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清掉舊的畫面
    glPushMatrix();//備份起來
        //glTranslatef(mouseX,mouseY,0);//移動
        glScalef(mouseX,mouseY,0);//改size縮放
        glRotatef(rotX,0,0,1);//旋轉
        glutSolidTeapot(0.3);
    glPopMatrix();//還原回去
    glutSwapBuffers();//交換繪圖的memory buffer才能畫出來
}
void motion(int x,int y)
{
    mouseX=(x-150)/150.0;
    mouseY=-(y-150)/150.0;
    rotX=x;
    glutPostRedisplay();//貼一張便利貼,告訴電腦的GLUT要重畫畫面re display
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160241 translate rotate scale");
    glutDisplayFunc(display);
    //glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

 六、試Transformation.exe改軸(x,y,z)

 

1 則留言: