2022年3月28日 星期一

week06👻

 1.電腦圖學之父

Ivan Sutherland(skethpad)


 https://jsyeh.org/3dcg10/下載window.zip data.zip














    解壓縮兩個資料夾

    執行Transformation.exe

----------------------------------------------------------------------------------------------------

#include <GL/glut.h>

#include <stdio.h>

float x=0, y=0, z=0, oldX, oldY;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef(  (x-150)/150.0  , -(y-150)/150.0  ,  z  ); ///Maya: w

        ///glRotatef( angle,  0, 0, 1);

        ///glScalef( scale, scale, scale );

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

void keyboard( unsigned char key, int mouseX, int mouseY )

{

    printf("現在按下:%c 座標在:%d %d\n", key, mouseX, mouseY);

}

void mouse( int button, int state, int mouseX, int mouseY )

{

    oldX = mouseX; oldY = mouseY;

}

void motion( int mouseX, int mouseY )

{

    x += (mouseX-oldX);

    y += (mouseY-oldY);

    oldX = mouseX; oldY = mouseY;

    display();

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06 keyboard mouse motion");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}

黃昱瑋的筆記week06 ⌨ 🐭(鍵盤滑鼠對圖像的控制,keybord、mouse、motion)

1.電腦圖學之父
Ivan Sutherland(skethpad)

2.開啟Translate/Rotate

    跟上禮拜一樣執行 Transformation.exe

    去https://jsyeh.org/3dcg10/下載window.zip data.zip

    解壓縮兩個資料夾

    執行Transformation.exe

    


3-1.偵測鍵盤和滑鼠座標

用上禮拜的程式碼去改

增加一個keyboard的函式


3-2.新增mouse和motion的函式之後要用




3-3移動拖曳   

增加以下程式碼,利用week5寫的程式碼做成拖曳版的茶壺


3-4滑鼠X座標放大縮小

增加scale變數


3-5用鍵盤輸入切換功能

w是拖曳

r是放大縮小



3-6增加旋轉及改變視窗大小、背景顏色
增加幾行程式碼
 glClearColor(0.5,0.5,0.5,1);是背景顏色,最後一個數字1是透明度
 glTranslatef((x-250)/250.0,-(y-250)/250.0,z);150改成250
 glutInitWindowSize(500,500);改變視窗大小







熱血的小葉老師圖學筆記 Week06

 # Week06


電腦圖學之父 Ivan Sutherland (Sketchpad)



看課本範例


jsyeh.org/3dcg10 下載 windows.zip data.zip

windows.zip => 下載\windows\Transformation.exe

data.zip    => 下載\windows\data\3D模型

執行 Transformation.exe


下方可以右鍵 Swap Translate/Rotate


自轉 vs. 公轉





# step01-3


接下來, 今天的主題是keyboard, 我們利用 GLUT 來寫今天的程式, 能 有keyboard互動, 重點是 glutKeyboardFunc(keyboard) 可以註冊 void keyboard(unsigned char key, int x, int y) 這個函式,再配合 stdio.h 的 printf()印出鍵盤的值



實作時間

Maya: qwer  

w:移動 e:轉動 r:縮放  

0. 安裝 freeglut, lib改一下  

1. File-New-Project, GLUT  

   week06_keyboard  

2. 接下來, 從上週 blog 複製我們的程式來用。  


多了3行程式, 分別是


int main() 裡 glutCreateWindow()之後

`glutKeyboardFunc(keyboard);`


前面要宣告 `int keyboard(unsigned char key, int x, int y)` 的鍵盤函式


## step01-3

接下來, 今天的主題是keyboard, 我們利用 GLUT 來寫今天的程式, 能 有keyboard互動, 重點是 glutKeyboardFunc(keyboard) 可以註冊 void keyboard(unsigned char key, int x, int y) 這個函式,再配合 stdio.h 的 printf()印出鍵盤的值


```C++

///從上週 blog 貼我們的程式

#include <GL/glut.h>

#include <stdio.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard( unsigned char key, int x, int y )

{

    printf("現在按下:%c 座標在:%d %d\n", key, x, y);

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06 keyboard");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);


    glutMainLoop();

}

```


# step02-1

接下來我們稍做修改,除了剛剛的keyboard()函式, 我們再加上 mouse() 及 motion()函式, 希望能完整互動




```C++

#include <GL/glut.h>

#include <stdio.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard( unsigned char key, int x, int y )

{

    printf("現在按下:%c 座標在:%d %d\n", key, x, y);

}

void mouse( int button, int state, int x, int y )

{

}

void motion( int x, int y )

{

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06 keyboard mouse motion");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}

```


## step02-2

上上週有教glTranslatef()移動, 我們把它做出來



```C++

#include <GL/glut.h>

#include <stdio.h>

float x=0, y=0, z=0, oldX, oldY;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef(  (x-150)/150.0  , -(y-150)/150.0  ,  z  ); ///Maya: w

        ///glRotatef( angle,  0, 0, 1);

        ///glScalef( scale, scale, scale );

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

void keyboard( unsigned char key, int mouseX, int mouseY )

{

    printf("現在按下:%c 座標在:%d %d\n", key, mouseX, mouseY);

}

void mouse( int button, int state, int mouseX, int mouseY )

{

    oldX = mouseX; oldY = mouseY;

}

void motion( int mouseX, int mouseY )

{

    x += (mouseX-oldX);

    y += (mouseY-oldY);

    oldX = mouseX; oldY = mouseY;

    display();

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06 keyboard mouse motion");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}

```



## step02-3

接下來我們實作出「縮放」的程式,利用 float scale=1.0 這個變數,如果 if(mousX大於oldX)就讓 scale 放大 1%, 反過來就讓 scale 變小 1%, 我們就可以完成這個縮放的版本



```C++

#include <GL/glut.h>

#include <stdio.h>

float x=0, y=0, z=0, scale=1.0, oldX, oldY;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        //glTranslatef(  (x-150)/150.0  , -(y-150)/150.0  ,  z  ); ///Maya: w

        ///glRotatef( angle,  0, 0, 1);

        glScalef( scale, scale, scale ); ///Maya: r

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

void keyboard( unsigned char key, int mouseX, int mouseY )

{

    printf("現在按下:%c 座標在:%d %d\n", key, mouseX, mouseY);

}

void mouse( int button, int state, int mouseX, int mouseY )

{

    oldX = mouseX; oldY = mouseY;

}

void motion( int mouseX, int mouseY )

{

    if( mouseX>oldX ) scale = scale * 1.01;

    if( mouseX<oldX ) scale = scale * 0.99;

    //x += (mouseX-oldX);

    //y += (mouseY-oldY);

    oldX = mouseX; oldY = mouseY;

    display();

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06 keyboard mouse motion");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}

```


## step03-1


今天的主角是keyboard鍵盤函式, 我們 if(key=='w' 或 key=='W') now=1做移動, if(key=='r' 或 key=='R')now=3 做縮放。接下來在 motion()裡, if(now==1)做移動的計算, if(now==3)做縮放的計算, 這樣就可以又移動、又縮放了 



```C++

#include <GL/glut.h>

#include <stdio.h>

float x=150, y=150, z=0, scale=1.0, angle=0.0, oldX, oldY;

int now=1;///1:移動, 2:旋轉, 3:縮放

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef( (x-150)/150.0 , -(y-150)/150.0 , z ); ///Maya: w

        //glRotatef( angle,  0, 0, 1); ///Maya: e

        glScalef( scale, scale, scale ); ///Maya: r

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

void keyboard( unsigned char key, int mouseX, int mouseY )

{   //printf("現在按下:%c 座標在:%d %d\n", key, mouseX, mouseY);

    if(key=='w' || key=='W') now=1;///移動 (小心不要中文)

    //if(key=='e' || key=='E') now=2;///旋轉

    if(key=='r' || key=='R') now=3;///縮放

}

void mouse( int button, int state, int mouseX, int mouseY )

{

    oldX = mouseX; oldY = mouseY;

}

void motion( int mouseX, int mouseY )

{

    if(now==1){///移動

        x += (mouseX-oldX);

        y += (mouseY-oldY);

    }else if(now==3){///縮放

        if( mouseX>oldX ) scale = scale * 1.01;

        if( mouseX<oldX ) scale = scale * 0.99;

    }

    oldX = mouseX; oldY = mouseY;

    display();

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06 keyboard mouse motion");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}

```


## step03-2


今天最後的任務,是加上旋轉。所以用keyboard的 w,e,r 來切換移動、旋轉、縮放。motion()裡面也有對應的程式, display()也會照著進行 glTranslatef() glRotatef() glScalef() 的程式。我們還多學了 glutInitWindowSize(500,500)放大, 導致 glTranslatef()裡面減一半、除一半的地方有修改。.zip



```C++

#include <GL/glut.h>

#include <stdio.h>

float x=250, y=250, z=0, scale=1.0, angle=0.0, oldX, oldY;

int now=1;///1:移動, 2:旋轉, 3:縮放

void display()

{

    glClearColor(0.5, 0.5, 0.5, 1);///用來 Clear的色彩 R,G,B,A Alpha沒用到

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef( (x-250)/250.0 , -(y-250)/250.0 , z ); ///Maya: w

        glRotatef( angle,  0, 0, 1); ///Maya: e

        glScalef( scale, scale, scale ); ///Maya: r

        glColor3f( 1, 1, 0 );

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

void keyboard( unsigned char key, int mouseX, int mouseY )

{   //printf("現在按下:%c 座標在:%d %d\n", key, mouseX, mouseY);

    if(key=='w' || key=='W') now=1;///移動 (小心不要中文)

    if(key=='e' || key=='E') now=2;///旋轉

    if(key=='r' || key=='R') now=3;///縮放

}

void mouse( int button, int state, int mouseX, int mouseY )

{

    oldX = mouseX; oldY = mouseY;

}

void motion( int mouseX, int mouseY )

{

    if(now==1){///移動

        x += (mouseX-oldX);

        y += (mouseY-oldY);

    }else if(now==2){///旋轉

        angle += (mouseX-oldX);

    }else if(now==3){///縮放

        if( mouseX>oldX ) scale = scale * 1.01;

        if( mouseX<oldX ) scale = scale * 0.99;

    }

    oldX = mouseX; oldY = mouseY;

    display();

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowSize(500, 500);

    glutCreateWindow("week06 keyboard mouse motion");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}

```


Amber's note week06

 <Ivan Sutherland> Ivan Sutherland為電腦圖學之父,在那個電腦主機非常大螢幕非常小而且沒有滑鼠的年代,Ivan Sutherland可以利用電腦發出的激光在電腦上畫圖,還可以將畫完的圖分成4個不同視窗,從top,side,front...等不同視角來看

<範例下載>範例下載win32和data壓縮檔並解壓縮,將data資料夾放進win32裡

<公轉 vs. 自轉>在下方框框裡按右鍵可以swap translate/rotate,再去調整旋轉角度,可以看見不同的旋轉模式


<利用滑鼠跟鍵盤來顯示位置>寫一個keyboard函式,來利用鍵盤記錄滑鼠的位置


<keyboard mouse  motion>新增mouse及motion函式,讓滑鼠可以拖著茶壺移動


<放大縮小>scale函式


<搭配鍵盤和滑鼠做縮放和移動>


<移動、旋轉、縮放>



Zfish week06

一:    

    1.電腦圖學之父:Ivan Sutherland (Sketchpad)


二:

1-1.根據week04第一點,執行Transformation.exe。

1-2.

二:鍵盤事件
1.鍵盤
#include <GL/glut.h>
#include <stdio.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}

void Keyboard(unsigned char key, int x, int y)
{
    printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
int main(int argc, char *argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMainLoop();
}

2.鍵盤+滑鼠
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
    printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    x += (mouseX-oldX);
    y += (mouseY-oldY);
    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

3.滑鼠縮放
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
    printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(mouseX>oldX) scale=scale*1.01;
    if(mouseX<oldX) scale=scale*0.99;
    //x += (mouseX-oldX);
    //y += (mouseY-oldY);
    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


4.鍵盤選擇要移動(w)、旋轉(e)、縮放(r)。
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, scale=1.0, angle=0.0, oldX, oldY;
int now=1;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z); 
    glRotatef(angle, 0, 0, 1);
    glScalef(scale, scale, scale);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)///像MAYA
{
    if(key == 'W' || key == 'w') now=1; ///移動
    if(key == 'E' || key == 'e') now=2; ///旋轉
    if(key == 'R' || key == 'r') now=3; ///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now == 1){ ///移動
        x += (mouseX-oldX);
        y += (mouseY-oldY);
    }else if(now == 3){ ///縮放
        if(mouseX>oldX) scale= scale * 1.01;
        if(mouseX<oldX) scale= scale * 0.99;
    }

    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

5.
#include <GL/glut.h>
#include <stdio.h>
float x=250, y=250, z=0, scale=1.0, angle=0.0, oldX, oldY;
int now=1;
void display()
{
    glClearColor(0.8, 0.8, 0.8, 1);///顏色R,G,B,A A->Alpha值
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z); ///w
    glRotatef(angle, 0, 0, 1);///e
    glScalef(scale, scale, scale);///r
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)///像MAYA
{
    if(key == 'W' || key == 'w') now=1; ///移動
    if(key == 'E' || key == 'e') now=2; ///旋轉
    if(key == 'R' || key == 'r') now=3; ///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now == 1){ ///移動
        x += (mouseX-oldX);
        y += (mouseY-oldY);
    }else if(now==2){///旋轉
        angle+=(mouseX-oldX);
    }else if(now == 3){ ///縮放
        if(mouseX>oldX) scale= scale * 1.01;
        if(mouseX<oldX) scale= scale * 0.99;
    }

    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///視窗大小
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


week06

 1.

https://jsyeh.org/3dcg10/

將data裡的data檔案移進windows裡,打開Transformation.exe

自轉vs公轉:下方按右鍵 Swap translate/rotate



2.(找座標)

下載freeglut檔案,開新專案




#include <GL/glut.h>

#include <stdio.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1, 1, 0);

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard(unsigned char key, int x, int y)

{

    printf("現在按下:%c 座標在: %d %d\n", key, x, y);

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMainLoop();

}

3.(未完成)



#include <GL/glut.h>

#include <stdio.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1, 1, 0);

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard(unsigned char key, int x, int y)

{

    printf("現在按下:%c 座標在: %d %d\n", key, x, y);

}

void mouse(int btn, int state, int x, int y)

{

}

void motion(int x, int y)

{

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week06");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    

    glutMainLoop();

}

4.(拖曳茶壺)


#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((x-150)/150.0, -(y-150)/150.0, z);///Maya:w
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{
    printf("現在按下:%c 座標在: %d %d\n", key, mouseX, mouseY);
}
void mouse(int btn, int state, int mouseX, int mouseY)
{
    oldX= mouseX;
    oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    x+=(mouseX-oldX);
    y+=(mouseY-oldY);
    oldX=mouseX;
    oldY=mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

5.(放大縮小)

#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY, scale=1.0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        //glTranslatef((x-150)/150.0, -(y-150)/150.0, z);///Maya:w
        glScalef(scale, scale, scale);///Maya:r
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{
    printf("現在按下:%c 座標在: %d %d\n", key, mouseX, mouseY);
}
void mouse(int btn, int state, int mouseX, int mouseY)
{
    oldX= mouseX;
    oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(mouseX>oldX) scale=scale*1.01;
    if(mouseX<oldX) scale=scale*0.99;
    oldX=mouseX;
    oldY=mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

6.(e未完成)

#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY, scale=1.0, angle=0.0;
int now=1;///1:移動 2:旋轉 3:縮放
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((x-150)/150.0, -(y-150)/150.0, z);///Maya:w
        glRotatef(angle, 0, 0, 1);
        glScalef(scale, scale, scale);///Maya:r
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{
    //printf("現在按下:%c 座標在: %d %d\n", key, mouseX, mouseY);
    if(key=='w'||key=='W') now=1;///移動
    if(key=='e'||key=='E') now=2;///旋轉
    if(key=='r'||key=='R') now=3;///縮放
}
void mouse(int btn, int state, int mouseX, int mouseY)
{
    oldX= mouseX;
    oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now==1){
        x+=(mouseX-oldX);
        y+=(mouseY-oldY);
    }else if(now==3){
        if(mouseX>oldX) scale=scale*1.01;
        if(mouseX<oldX) scale=scale*0.99;
    }
    oldX=mouseX;
    oldY=mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

7.(移動 旋轉 縮放)


#include <GL/glut.h>
#include <stdio.h>
float x=250, y=250, z=0, oldX, oldY, scale=1.0, angle=0.0;
int now=1;///1:移動 2:旋轉 3:縮放
void display()
{
    glClearColor(0.5, 0.5, 0.5, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((x-250)/250.0, -(y-250)/250.0, z);///Maya:w
        glRotatef(angle, 0, 0, 1);///Maya:e
        glScalef(scale, scale, scale);///Maya:r
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX, int mouseY)
{
    //printf("現在按下:%c 座標在: %d %d\n", key, mouseX, mouseY);
    if(key=='w'||key=='W') now=1;///移動
    if(key=='e'||key=='E') now=2;///旋轉
    if(key=='r'||key=='R') now=3;///縮放
}
void mouse(int btn, int state, int mouseX, int mouseY)
{
    oldX= mouseX;
    oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now==1){///移動
        x+=(mouseX-oldX);
        y+=(mouseY-oldY);
    }else if(now==2){///旋轉
        angle+=(mouseX-oldX);
    }else if(now==3){///縮放
        if(mouseX>oldX) scale=scale*1.01;
        if(mouseX<oldX) scale=scale*0.99;
    }
    oldX=mouseX;
    oldY=mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}