2018年3月23日 星期五

Week04 璿元

Transformation是什麼?

1. 環境設置

 (a) 從 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/ 下載 data.zip, windows.zip 及 glut32.dll


 (b) 將 glut32.dll 複製到 windows 資料夾中



用滑鼠來移動及旋轉

1. 新增一個GLUT專案
2. 程式碼
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0;
//茶壺的座標,rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0;
//紀錄mouse在哪裡按下去,oldX, oldY 幫忙了解你走了多少地方
void display()
{
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        //依照茶壺的座標(teapotX, teapotY)移動
        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) //mouse motion 事件
{
    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("Week04 Mouse");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

沒有留言:

張貼留言