2024年3月11日 星期一

Week04 鍵盤滑鼠 motion 牢底座穿

 #Week04

----

glPushMatrix();///備份矩陣

 glRotated(angle++,0,0,1);///旋轉

 glTranslated(teapotX,teapotY,0);///移動

 glScalef(s,s,s);///縮放s倍

glPopMatrix();///還原矩陣

----


1.下載課本範例

2.data,win32 data丟windows資料夾

3.試slScalef(x,y,z);

#week04_mouse)glScalef

0.安裝freeglut,同時把lib/libfreeglut.a複製成libglut32.a

1.File-New-Project,GLUT專案

week04_mouse_glScalef要設定好GLUT

2. glClearColor(1,1,0.9,1);///R,G,B,A

---

glPushMatrix();///備份矩陣

        glScalef(s,s,s);///縮放s倍

---

void motion(int x,int y)

{

    s=1+(x-150)/150.0;///會變大變小的變數0~1~2

    display();///重畫畫面

}

---

glutMotionFunc(motion);///拖動motion的時候

##試著交換translate/rotate兩行程式
1.試課本範例transformation
2.先把translate,移到右方,再rotate看看
3.下方程式,按右鍵,可swap translate/rotate交換
4.再rotate看看,差在哪裡 ---公轉自轉
5.好像是把一個自轉的人搬到右邊去
6.好像是吃辦桌時,桌上的大圓盤,把東西放上去轉
##理解的技巧
1.把左耳靠著左肩,再看程式碼
2.把旋轉中、長胖的藍色的車子移動右邊去

整個一起轉 glRotatef
放在右邊glTranslatef
高瘦的glScalef
藍色車子glBegin
-------------------------------------
##week04_translate_rotate_scale

 縮很小的綠色茶壺
加glutIdleFunc(display);
float teapotX=0.5,teapotY=0;///放右邊
float angle=0;///旋轉角度
---
        glTranslated(teapotX,teapotY,0);
        glRotated(angle++,0,0,1);
---
glutIdleFunc(display);

-----
glRotated(angle++,0,0,1);
glTranslated(teapotX,teapotY,0);
兩行交換不同選轉 自轉變公轉



---
##keyboard_mouse_motion

glutKeyboardFunc(keyboard);///鍵盤事件的函式
加#include <stdio.h>
void keyboard(unsigned char key,int x, int y)
{
    printf("key:%c x:%d y:%d\n",key,x,y);
}
---
glutMouseFunc(mouse);///滑鼠事件的函式
給參數int N=0;
float teapotX[1000],teapotY[1000];///存座標
-------
for(int i=0;i<N;i++){
        glPushMatrix();
        glTranslatef(teapotX[i],teapotY[i],0);
        glutSolidTeapot(0.3);
        glPopMatrix();
    }
--------
void mouse(int button,int state,int x,int y)
{
    teapotX[N]=(x-150)/150.0;
    teapotY[N]=-(y-150)/150.0;
    N++;
}


沒有留言:

張貼留言