2018年3月19日 星期一

Week 04 ------ 想狩獵

1.將data.rar解壓縮成資料夾,放到windows資料夾裡,注意只能有一個data目錄,點開範例Transformation.exe

2.程式碼
glRotatef(角度, x軸, y軸, z軸);///旋轉
glScalef(x軸, y軸, z軸);///放大縮小

3.整段程式碼
#include <stdio.h>
#include <GL/glut.h>
float angle=0, teapotX=0, teapotY=0;///要轉的角度、移動的座標
int oldX=0, oldY=0, nowRotate=0;///0:移動 1:旋轉
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(teapotX, teapotY, 0);///轉完再移動
    glRotatef(angle,0,0,1);///旋轉
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    ///printf("%.1f\n",angle);///印出目前度數
    ///angle++;///增加一度
}
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;///按下去時把oldX、oldY紀錄
    oldY=y;
}
void motion(int x,int y)
{
    ///當滑鼠在drag motion時
    if(nowRotate==1) angle+=(x-oldX);///改變angle值,加上改變量
    else
    {
        teapotX+=(x-oldX)/150.0;///加上改變量
        teapotY+=(oldY-y)/150.0;
    }
    oldX=x;///做完計算時把oldX、oldY紀錄
    oldY=y;
    glutPostRedisplay();///讓畫面重畫
}
int main(int argc ,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GL_DEPTH);
    glutCreateWindow("05161252_Week04");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    ///glutIdleFunc(display);///Idle重畫
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言